-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcreate-gce-user-data
More file actions
executable file
·81 lines (62 loc) · 2.35 KB
/
create-gce-user-data
File metadata and controls
executable file
·81 lines (62 loc) · 2.35 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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#!/usr/bin/env python
"""
Create CoreOS user-data by merging contric/coreos/user-data and gce-user-data
Usage: create-gce-user-data.py <discovery_url>
Arguments:
<discovery_url> This is the CoreOS etcd discovery URL. You should generate
a new URL at https://discovery.etcd.io/new and pass it as the argument.
"""
import sys
import os
import re
import yaml
import collections
def combine_dicts(orig_dict, new_dict):
for key, val in new_dict.iteritems():
if isinstance(val, collections.Mapping):
tmp = combine_dicts(orig_dict.get(key, {}), val)
orig_dict[key] = tmp
elif isinstance(val, list):
orig_dict[key] = (orig_dict[key] + val)
else:
orig_dict[key] = new_dict[key]
return orig_dict
def get_file(name, mode="r", abspath=False):
current_dir = os.path.dirname(__file__)
if abspath:
return file(os.path.abspath(os.path.join(current_dir, name)), mode)
else:
return file(os.path.join(current_dir, name), mode)
def main():
try:
url = sys.argv[1]
except (NameError, IndexError):
print __doc__
return 1
url_pattern = 'http[|s]\:[\/]{2}discovery\.etcd\.io\/[0-9a-f]{32}'
m = re.match(url_pattern, url)
if not m:
print "Discovery URL invalid."
return 1
gce_user_data = get_file("gce-user-data", "w", True)
gce_template = get_file("gce-user-data-template")
coreos_template = get_file("../coreos/user-data.example")
configuration_coreos_template = yaml.safe_load(coreos_template)
configuration_gce_template = yaml.safe_load(gce_template)
configuration = combine_dicts(configuration_coreos_template,
configuration_gce_template)
configuration["coreos"]["etcd"]["discovery"] = url
with gce_user_data as outfile:
try:
outfile.write("#cloud-config\n\n" +
yaml.safe_dump(configuration,
default_flow_style=False,
default_style='|'))
except (IOError, ValueError):
print "There was an issue writing to file " + gce_user_data.name
return 1
else:
print "Wrote file \"%s\" with url \"%s\"" %\
(gce_user_data.name, url)
if __name__ == "__main__":
sys.exit(main())