forked from simonellistonball/cloudbreak-hcp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcluster.py
81 lines (67 loc) · 2.46 KB
/
cluster.py
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
from jinja2 import Template, Environment, FileSystemLoader, select_autoescape
import yaml
from tempfile import NamedTemporaryFile
import collections
import random
import string
import argparse
import sys
import os
import subprocess
from datetime import datetime
def random_password():
return ''.join(random.choice("_-" + string.ascii_letters + string.digits) for _ in range(16))
def update(orig_dict, new_dict):
for key, val in orig_dict.iteritems():
if isinstance(val, collections.Mapping):
tmp = update(orig_dict.get(key, { }), val)
new_dict[key] = tmp
elif isinstance(val, list):
new_dict[key] = (orig_dict.get(key, []) + val)
else:
if orig_dict[key] == "RANDOM":
new_dict[key] = random_password()
else:
new_dict[key] = orig_dict[key]
return new_dict
parser = argparse.ArgumentParser(description='Create a HCP cluster in Cloudbreak')
parser.add_argument('--name', dest='name', type=str,
help='Name for the cluster')
parser.add_argument('--compact', dest='compact', action='store_true',
help='Use compact version')
args = parser.parse_args()
if args.name == None:
print("Must specify cluster name")
sys.exit(1)
suffix = '-compact' if args.compact else ''
env = Environment(loader=FileSystemLoader('.'))
config = {
'aws': {}
}
with open("config.yaml") as cf:
yamls = cf.read()
cd = dict(yaml.load(yamls))
update(cd, config)
cloud = config.get('cloudProvider')
if (cloud.lower() == 'aws'):
cloud_provider = '-aws'
elif (cloud.lower() == 'ycloud'):
cloud_provider = '-ycloud'
else:
print("Cloud provider 'cloudProvider' supported config values are 'aws' or 'ycloud' ")
sys.exit(1)
with NamedTemporaryFile(delete=False) as tmp:
template = env.get_template("template" + suffix + cloud_provider +".j2")
tmp.write(template.render(config))
template_file = tmp.name
try:
subprocess.check_call(["cb cluster create --cli-input-json {template_file} --name {cluster_name}".format(template_file = tmp.name, cluster_name = args.name)], shell=True)
print(yaml.dump(config, default_flow_style=False))
now = datetime.now().strftime("%Y%m%d%H%M%S")
with open("config-" + suffix + "{}.yaml".format(now), 'w') as fout:
fout.write(yaml.dump(config, default_flow_style=False))
with open("template-" + suffix + "{}.yaml".format(now), 'w') as fout:
fout.write(template.render(config))
except:
print(template.render(config))
os.remove(template_file)