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
=== modified file 'qakit/juju/juju_bootstrap'
--- qakit/juju/juju_bootstrap 2017-03-28 17:42:58 +0000
+++ qakit/juju/juju_bootstrap 2017-03-30 17:48:15 +0000
@@ -35,10 +35,18 @@
35 cloud_name="$2"35 cloud_name="$2"
36 shift # past argument36 shift # past argument
37 ;;37 ;;
38 --controller_name)
39 controller_name="$2"
40 shift # past argument
41 ;;
38 --proxy)42 --proxy)
39 proxy="$2"43 proxy="$2"
40 shift # past argument44 shift # past argument
41 ;;45 ;;
46 --image)
47 image="$2"
48 shift # past argument
49 ;;
42 --replace)50 --replace)
43 replace=YES51 replace=YES
44 ;;52 ;;
@@ -75,9 +83,22 @@
7583
76# Bootstrap juju cloud/controller/model84# Bootstrap juju cloud/controller/model
77parameters="--cloud_name ${cloud_name} --username ${OS_USERNAME} --password ${OS_PASSWORD} --tenant ${OS_TENANT_NAME} --region ${OS_REGION_NAME} --url ${OS_AUTH_URL}"85parameters="--cloud_name ${cloud_name} --username ${OS_USERNAME} --password ${OS_PASSWORD} --tenant ${OS_TENANT_NAME} --region ${OS_REGION_NAME} --url ${OS_AUTH_URL}"
86
78if [ ! -z ${replace} ]; then87if [ ! -z ${replace} ]; then
79 parameters="$parameters --replace"88 parameters="$parameters --replace"
80fi89fi
90
91if [ ! -z ${image} ]; then
92 parameters="$parameters --image $image"
93fi
94
95# If controller name is not passed as argument, it takes the same name as the cloud
96if [ ! -z ${controller_name} ]; then
97 parameters="$parameters --controller_name $controller_name"
98else
99 parameters="$parameters --controller_name $cloud_name"
100fi
101
81${DIR}/juju_ve/bin/python3 ${DIR}/juju_bootstrap.py ${parameters}102${DIR}/juju_ve/bin/python3 ${DIR}/juju_bootstrap.py ${parameters}
82bootstrap_result=$?103bootstrap_result=$?
83if [ ${bootstrap_result} != 0 ]; then104if [ ${bootstrap_result} != 0 ]; then
84105
=== modified file 'qakit/juju/juju_bootstrap.py'
--- qakit/juju/juju_bootstrap.py 2017-03-28 17:42:58 +0000
+++ qakit/juju/juju_bootstrap.py 2017-03-30 17:48:15 +0000
@@ -22,6 +22,8 @@
22"""Bootstrap a cloud using juju"""22"""Bootstrap a cloud using juju"""
2323
24import argparse24import argparse
25import os
26import shutil
25import subprocess27import subprocess
26import tempfile28import tempfile
2729
@@ -36,6 +38,8 @@
36LOGGER.setLevel(logging.DEBUG)38LOGGER.setLevel(logging.DEBUG)
37LOGGER.propagate = True39LOGGER.propagate = True
3840
41TO_DELETE = []
42
3943
40def is_installed(pgk_name):44def is_installed(pgk_name):
41 """45 """
@@ -79,6 +83,7 @@
79 cloud_yaml = tempfile.NamedTemporaryFile()83 cloud_yaml = tempfile.NamedTemporaryFile()
80 with open(cloud_yaml.name, 'w') as temp:84 with open(cloud_yaml.name, 'w') as temp:
81 temp.write(yaml.dump(clouds_dict))85 temp.write(yaml.dump(clouds_dict))
86 TO_DELETE.append(cloud_yaml.name)
82 return cloud_yaml87 return cloud_yaml
8388
8489
@@ -107,6 +112,7 @@
107 credentials_yaml = tempfile.NamedTemporaryFile()112 credentials_yaml = tempfile.NamedTemporaryFile()
108 with open(credentials_yaml.name, 'w') as temp:113 with open(credentials_yaml.name, 'w') as temp:
109 temp.write(yaml.dump(credentials_dict))114 temp.write(yaml.dump(credentials_dict))
115 TO_DELETE.append(credentials_yaml.name)
110 return credentials_yaml116 return credentials_yaml
111117
112118
@@ -161,38 +167,67 @@
161 return False167 return False
162168
163169
164def bootstrap(cloud, username, password, tenant, region, url, replace):170def generate_image_metadata(image, region, url):
171 LOGGER.info('Generating image metadata for: %s', image)
172 streams = tempfile.mkdtemp()
173 subprocess.check_call(['juju',
174 'metadata',
175 'generate-image',
176 '-i', image,
177 '-d', streams,
178 '-r', region,
179 '-u', url])
180 LOGGER.info('Image metadata generated successfully')
181 TO_DELETE.append(streams)
182 return streams
183
184
185def delete_files(files):
186 """Delete the given list of files/dirs"""
187 for _f in files:
188 if os.path.isfile(_f):
189 os.remove(_f)
190 elif os.path.isdir(_f):
191 shutil.rmtree(_f)
192
193
194def bootstrap(cloud, controller, username, password, tenant, region, url, image, replace):
165 """Bootstrap a cloud using juju195 """Bootstrap a cloud using juju
166196
167 :param cloud: the cloud name197 :param cloud: the cloud name
198 :param controller: the controller name
168 :param username: the username199 :param username: the username
169 :param password: the password200 :param password: the password
170 :param tenant: the tenant name201 :param tenant: the tenant name
171 :param region: the region name202 :param region: the region name
172 :param url: the auth url203 :param url: the auth url
204 :param image: id of the image you want to generate metadata for
173 :param replace: whether to replace the cloud and205 :param replace: whether to replace the cloud and
174 credentials if exists206 credentials if exists
175 """207 """
176 bootstrapped = is_controller_bootstrapped(cloud)208 command = ['juju', 'bootstrap', cloud, controller, '--credential', cloud]
209 if image:
210 streams = generate_image_metadata(image, region, url)
211 command.extend(['--metadata-source', streams])
212 bootstrapped = is_controller_bootstrapped(controller)
177 if replace and bootstrapped:213 if replace and bootstrapped:
178 LOGGER.info('Controller %s already bootstrapped. '214 LOGGER.info('Controller %s already bootstrapped. '
179 'Destroying it before proceeding...', cloud)215 'Destroying it before proceeding...', controller)
180 subprocess.check_call(['juju', 'destroy-controller', cloud,216 subprocess.check_call(['juju', 'destroy-controller', controller,
181 '--destroy-all-models', '-y'])217 '--destroy-all-models', '-y'])
182 elif not replace and bootstrapped:218 elif not replace and bootstrapped:
183 LOGGER.info('Controller %s already bootstrapped and '219 LOGGER.info('Controller %s already bootstrapped and '
184 'replace parameter is not set...Exiting', cloud)220 'replace parameter is not set...Exiting', controller)
185 return221 return
186 LOGGER.info('About to bootstrap controller %s...', cloud)222 LOGGER.info('About to bootstrap controller %s...', controller)
187 add_cloud(cloud, region, url, replace)223 add_cloud(cloud, region, url, replace)
188 add_credentials(cloud, username, password, tenant, replace)224 add_credentials(cloud, username, password, tenant, replace)
189 subprocess.check_call(['juju',225 try:
190 'bootstrap',226 subprocess.check_call(command)
191 cloud,227 except subprocess.CalledProcessError:
192 cloud,228 raise
193 '--credential',229 finally:
194 cloud])230 delete_files(TO_DELETE)
195
196231
197if __name__ == '__main__':232if __name__ == '__main__':
198 PARSER = argparse.ArgumentParser(description=233 PARSER = argparse.ArgumentParser(description=
@@ -200,14 +235,16 @@
200 PARSER.add_argument('--cloud_name',235 PARSER.add_argument('--cloud_name',
201 help='Cloud name that will be also use '236 help='Cloud name that will be also use '
202 'for controller name')237 'for controller name')
238 PARSER.add_argument('--controller_name')
203 PARSER.add_argument('--username')239 PARSER.add_argument('--username')
204 PARSER.add_argument('--password')240 PARSER.add_argument('--password')
205 PARSER.add_argument('--tenant')241 PARSER.add_argument('--tenant')
206 PARSER.add_argument('--region')242 PARSER.add_argument('--region')
207 PARSER.add_argument('--url')243 PARSER.add_argument('--url')
244 PARSER.add_argument('--image')
208 PARSER.add_argument('--replace', action='store_true')245 PARSER.add_argument('--replace', action='store_true')
209 ARGS = PARSER.parse_args()246 ARGS = PARSER.parse_args()
210 add_ppas(PPAS)247 add_ppas(PPAS)
211 install_packages(PACKAGES)248 install_packages(PACKAGES)
212 bootstrap(ARGS.cloud_name, ARGS.username, ARGS.password, ARGS.tenant,249 bootstrap(ARGS.cloud_name, ARGS.controller_name, ARGS.username, ARGS.password, ARGS.tenant,
213 ARGS.region, ARGS.url, ARGS.replace)250 ARGS.region, ARGS.url, ARGS.image, ARGS.replace)

Subscribers

People subscribed via source and target branches