Merge lp:~heber013/qakit/adding-image-metadata-option into lp:qakit

Proposed by Heber Parrucci
Status: Merged
Approved by: Max Brustkern
Approved revision: 230
Merged at revision: 230
Proposed branch: lp:~heber013/qakit/adding-image-metadata-option
Merge into: lp:qakit
Diff against target: 183 lines (+73/-15)
2 files modified
qakit/juju/juju_bootstrap (+21/-0)
qakit/juju/juju_bootstrap.py (+52/-15)
To merge this branch: bzr merge lp:~heber013/qakit/adding-image-metadata-option
Reviewer Review Type Date Requested Status
Max Brustkern (community) Approve
Review via email: mp+321461@code.launchpad.net

Commit message

Adding --image option to bootstrap to allow the option of generating metadata
Also delete files/dirs at the end of execution.

Description of the change

Add the option for generating image metadata in juju script.
Also deleting files/dirs created during the setup

To post a comment you must log in.
Revision history for this message
Max Brustkern (nuclearbob) wrote :

I'd consider using a finally clause in the try block instead of having the delete in the try and the except, but that's just a stylistic thing; should be good to land either way.

review: Approve
230. By Heber Parrucci

Adding finally clause for deleting files/dirs

Revision history for this message
Heber Parrucci (heber013) wrote :

Adding finally clause for deleting files/dirs

Revision history for this message
Max Brustkern (nuclearbob) wrote :

Looks good to me!

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'qakit/juju/juju_bootstrap'
2--- qakit/juju/juju_bootstrap 2017-03-28 17:42:58 +0000
3+++ qakit/juju/juju_bootstrap 2017-03-30 17:48:15 +0000
4@@ -35,10 +35,18 @@
5 cloud_name="$2"
6 shift # past argument
7 ;;
8+ --controller_name)
9+ controller_name="$2"
10+ shift # past argument
11+ ;;
12 --proxy)
13 proxy="$2"
14 shift # past argument
15 ;;
16+ --image)
17+ image="$2"
18+ shift # past argument
19+ ;;
20 --replace)
21 replace=YES
22 ;;
23@@ -75,9 +83,22 @@
24
25 # Bootstrap juju cloud/controller/model
26 parameters="--cloud_name ${cloud_name} --username ${OS_USERNAME} --password ${OS_PASSWORD} --tenant ${OS_TENANT_NAME} --region ${OS_REGION_NAME} --url ${OS_AUTH_URL}"
27+
28 if [ ! -z ${replace} ]; then
29 parameters="$parameters --replace"
30 fi
31+
32+if [ ! -z ${image} ]; then
33+ parameters="$parameters --image $image"
34+fi
35+
36+# If controller name is not passed as argument, it takes the same name as the cloud
37+if [ ! -z ${controller_name} ]; then
38+ parameters="$parameters --controller_name $controller_name"
39+else
40+ parameters="$parameters --controller_name $cloud_name"
41+fi
42+
43 ${DIR}/juju_ve/bin/python3 ${DIR}/juju_bootstrap.py ${parameters}
44 bootstrap_result=$?
45 if [ ${bootstrap_result} != 0 ]; then
46
47=== modified file 'qakit/juju/juju_bootstrap.py'
48--- qakit/juju/juju_bootstrap.py 2017-03-28 17:42:58 +0000
49+++ qakit/juju/juju_bootstrap.py 2017-03-30 17:48:15 +0000
50@@ -22,6 +22,8 @@
51 """Bootstrap a cloud using juju"""
52
53 import argparse
54+import os
55+import shutil
56 import subprocess
57 import tempfile
58
59@@ -36,6 +38,8 @@
60 LOGGER.setLevel(logging.DEBUG)
61 LOGGER.propagate = True
62
63+TO_DELETE = []
64+
65
66 def is_installed(pgk_name):
67 """
68@@ -79,6 +83,7 @@
69 cloud_yaml = tempfile.NamedTemporaryFile()
70 with open(cloud_yaml.name, 'w') as temp:
71 temp.write(yaml.dump(clouds_dict))
72+ TO_DELETE.append(cloud_yaml.name)
73 return cloud_yaml
74
75
76@@ -107,6 +112,7 @@
77 credentials_yaml = tempfile.NamedTemporaryFile()
78 with open(credentials_yaml.name, 'w') as temp:
79 temp.write(yaml.dump(credentials_dict))
80+ TO_DELETE.append(credentials_yaml.name)
81 return credentials_yaml
82
83
84@@ -161,38 +167,67 @@
85 return False
86
87
88-def bootstrap(cloud, username, password, tenant, region, url, replace):
89+def generate_image_metadata(image, region, url):
90+ LOGGER.info('Generating image metadata for: %s', image)
91+ streams = tempfile.mkdtemp()
92+ subprocess.check_call(['juju',
93+ 'metadata',
94+ 'generate-image',
95+ '-i', image,
96+ '-d', streams,
97+ '-r', region,
98+ '-u', url])
99+ LOGGER.info('Image metadata generated successfully')
100+ TO_DELETE.append(streams)
101+ return streams
102+
103+
104+def delete_files(files):
105+ """Delete the given list of files/dirs"""
106+ for _f in files:
107+ if os.path.isfile(_f):
108+ os.remove(_f)
109+ elif os.path.isdir(_f):
110+ shutil.rmtree(_f)
111+
112+
113+def bootstrap(cloud, controller, username, password, tenant, region, url, image, replace):
114 """Bootstrap a cloud using juju
115
116 :param cloud: the cloud name
117+ :param controller: the controller name
118 :param username: the username
119 :param password: the password
120 :param tenant: the tenant name
121 :param region: the region name
122 :param url: the auth url
123+ :param image: id of the image you want to generate metadata for
124 :param replace: whether to replace the cloud and
125 credentials if exists
126 """
127- bootstrapped = is_controller_bootstrapped(cloud)
128+ command = ['juju', 'bootstrap', cloud, controller, '--credential', cloud]
129+ if image:
130+ streams = generate_image_metadata(image, region, url)
131+ command.extend(['--metadata-source', streams])
132+ bootstrapped = is_controller_bootstrapped(controller)
133 if replace and bootstrapped:
134 LOGGER.info('Controller %s already bootstrapped. '
135- 'Destroying it before proceeding...', cloud)
136- subprocess.check_call(['juju', 'destroy-controller', cloud,
137+ 'Destroying it before proceeding...', controller)
138+ subprocess.check_call(['juju', 'destroy-controller', controller,
139 '--destroy-all-models', '-y'])
140 elif not replace and bootstrapped:
141 LOGGER.info('Controller %s already bootstrapped and '
142- 'replace parameter is not set...Exiting', cloud)
143+ 'replace parameter is not set...Exiting', controller)
144 return
145- LOGGER.info('About to bootstrap controller %s...', cloud)
146+ LOGGER.info('About to bootstrap controller %s...', controller)
147 add_cloud(cloud, region, url, replace)
148 add_credentials(cloud, username, password, tenant, replace)
149- subprocess.check_call(['juju',
150- 'bootstrap',
151- cloud,
152- cloud,
153- '--credential',
154- cloud])
155-
156+ try:
157+ subprocess.check_call(command)
158+ except subprocess.CalledProcessError:
159+ raise
160+ finally:
161+ delete_files(TO_DELETE)
162
163 if __name__ == '__main__':
164 PARSER = argparse.ArgumentParser(description=
165@@ -200,14 +235,16 @@
166 PARSER.add_argument('--cloud_name',
167 help='Cloud name that will be also use '
168 'for controller name')
169+ PARSER.add_argument('--controller_name')
170 PARSER.add_argument('--username')
171 PARSER.add_argument('--password')
172 PARSER.add_argument('--tenant')
173 PARSER.add_argument('--region')
174 PARSER.add_argument('--url')
175+ PARSER.add_argument('--image')
176 PARSER.add_argument('--replace', action='store_true')
177 ARGS = PARSER.parse_args()
178 add_ppas(PPAS)
179 install_packages(PACKAGES)
180- bootstrap(ARGS.cloud_name, ARGS.username, ARGS.password, ARGS.tenant,
181- ARGS.region, ARGS.url, ARGS.replace)
182+ bootstrap(ARGS.cloud_name, ARGS.controller_name, ARGS.username, ARGS.password, ARGS.tenant,
183+ ARGS.region, ARGS.url, ARGS.image, ARGS.replace)

Subscribers

People subscribed via source and target branches