-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.py
More file actions
63 lines (46 loc) · 1.66 KB
/
engine.py
File metadata and controls
63 lines (46 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
import json
import yaml
import os
import base64
from jinja2 import Environment, FileSystemLoader
class TemplateGenerator(object):
def __init__(self):
pass
def template_azure(self, node_count, cloudinit, location, password):
'''
Return an azure deployment for the specified nodes
'''
# take cloud init data abd b64 encode
cloudinit_b64 = base64.b64encode(cloudinit.encode('ascii)')).decode('UTF-8')
env = Environment(loader=FileSystemLoader('templates'))
template = env.get_template('deploytemplate.json.j2')
rendered_template = template.render(
node_count=int(node_count), cloudinit=cloudinit_b64, location=location, password=password
)
return rendered_template
class CloudInit(object):
def __init__(self):
'''Initialize the cloud init class'''
self.yaml = {}
self.yaml['write_files'] = []
self.yaml['runcmd'] = []
def write_file(self, path, content, permissions='0755', owner='root:root', encoding=None):
'''Write fules helper'''
yamlblurb = {
'encoding': encoding,
'content': content,
'owner': owner,
'permissions': permissions,
'content': content,
'path': path
}
# remove null encodings
if not encoding:
del yamlblurb['encoding']
self.yaml['write_files'].append(yamlblurb)
def runcmd(self, cmd):
'''cmd can be a string or a list'''
self.yaml['runcmd'].append(cmd)
def render(self):
'''Render cloud init object as yaml'''
return "#cloud-config\n%s" % yaml.dump(self.yaml)