Skip to content

Commit 7258cd6

Browse files
committed
Merge pull request #1593 from deis/contrib/gce/flake8
chore(contrib/gce): make GCE script pass flake8
2 parents 0b6cc12 + bcffa7c commit 7258cd6

1 file changed

Lines changed: 48 additions & 30 deletions

File tree

contrib/gce/create-gce-user-data

Lines changed: 48 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,8 @@ Create CoreOS user-data by merging contric/coreos/user-data and gce-user-data
55
Usage: create-gce-user-data.py <discovery_url>
66
77
Arguments:
8-
<discovery_url>
9-
This is the CoreOS etcd discovery URL. You should generate a new URL at
10-
https://discovery.etcd.io/new and pass it as the argument.
8+
<discovery_url> This is the CoreOS etcd discovery URL. You should generate
9+
a new URL at https://discovery.etcd.io/new and pass it as the argument.
1110
"""
1211

1312
import sys
@@ -16,48 +15,67 @@ import re
1615
import yaml
1716
import collections
1817

19-
# TODO: Learn how to use docopt and import it for argument handling and validtion
2018

2119
def combine_dicts(orig_dict, new_dict):
2220
for key, val in new_dict.iteritems():
2321
if isinstance(val, collections.Mapping):
24-
tmp = combine_dicts(orig_dict.get(key, { }), val)
22+
tmp = combine_dicts(orig_dict.get(key, {}), val)
2523
orig_dict[key] = tmp
2624
elif isinstance(val, list):
2725
orig_dict[key] = (orig_dict[key] + val)
2826
else:
2927
orig_dict[key] = new_dict[key]
3028
return orig_dict
3129

32-
try:
33-
url = sys.argv[1]
34-
except (NameError, IndexError):
35-
exit('It doesn\'t appear that you specified a discovery URL as the first parameter.')
3630

31+
def get_file(name, mode="r", abspath=False):
32+
current_dir = os.path.dirname(__file__)
3733

38-
url_pattern = 'https\:[\/]{2}discovery\.etcd\.io\/[0-9a-f]{32}'
34+
if abspath:
35+
return file(os.path.abspath(os.path.join(current_dir, name)), mode)
36+
else:
37+
return file(os.path.join(current_dir, name), mode)
3938

40-
m = re.match(url_pattern, url)
4139

42-
if url and m:
43-
current_dir = os.path.dirname(__file__)
44-
gce_user_data = file(os.path.abspath(os.path.join(current_dir, 'gce-user-data')), 'w')
45-
gce_template = file(os.path.join(current_dir, "gce-user-data-template"), 'r')
46-
coreos_template = file(os.path.join(current_dir, "../coreos/user-data"), 'r')
40+
def main():
41+
try:
42+
url = sys.argv[1]
43+
except (NameError, IndexError):
44+
print __doc__
45+
return 1
4746

48-
configuration_coreos_template = yaml.safe_load(coreos_template)
49-
configuration_gce_template = yaml.safe_load(gce_template)
47+
url_pattern = 'http[|s]\:[\/]{2}discovery\.etcd\.io\/[0-9a-f]{32}'
5048

51-
configuration = combine_dicts(configuration_coreos_template, configuration_gce_template)
52-
53-
configuration['coreos']['etcd']['discovery_url'] = url
49+
m = re.match(url_pattern, url)
5450

55-
with gce_user_data as outfile:
56-
try:
57-
outfile.write("#cloud-config\n\n" + yaml.safe_dump(configuration, default_flow_style=False, default_style='|'))
58-
except (IOError, ValueError):
59-
exit('There was an issue writing to file ' + gce_user_data.name)
60-
else:
61-
exit('Wrote file ' + gce_user_data.name + ' with discovery URL ' + url)
62-
else:
63-
print 'Discovery URL doesn\'t appear to be valid.'
51+
if not m:
52+
print "Discovery URL invalid."
53+
return 1
54+
55+
gce_user_data = get_file("gce_user_data", "w", True)
56+
gce_template = get_file("gce-user-data-template")
57+
coreos_template = get_file("../coreos/user-data")
58+
59+
configuration_coreos_template = yaml.safe_load(coreos_template)
60+
configuration_gce_template = yaml.safe_load(gce_template)
61+
62+
configuration = combine_dicts(configuration_coreos_template,
63+
configuration_gce_template)
64+
65+
configuration["coreos"]["etcd"]["discovery_url"] = url
66+
67+
with gce_user_data as outfile:
68+
try:
69+
outfile.write("#cloud-config\n\n" +
70+
yaml.safe_dump(configuration,
71+
default_flow_style=False,
72+
default_style='|'))
73+
except (IOError, ValueError):
74+
print "There was an issue writing to file " + gce_user_data.name
75+
return 1
76+
else:
77+
print "Wrote file \"%s\" with url \"%s\"" %\
78+
(gce_user_data.name, url)
79+
80+
if __name__ == "__main__":
81+
sys.exit(main())

0 commit comments

Comments
 (0)