@@ -35,11 +35,16 @@ def install_vm(session, urlvhdbz2, sruuid,
3535 # @todo: pipe instead, so the file never actually touches Dom0
3636 cmd = ['curl' , '-o' , atempfile , urlvhdbz2 ]
3737 util .runlocal (cmd )
38- cmd = ['bzip2' , '-d' , atempfile ]
39- util .runlocal (cmd )
40- vdiref = api_helper .import_disk (session , sruuid , atempfileunpacked , 'vhd' ,
41- 'Disk' )
42- os .remove (atempfileunpacked )
38+ try :
39+ cmd = ['bzip2' , '-d' , atempfile ]
40+ util .runlocal (cmd )
41+ vdiref = api_helper .import_disk (session , sruuid , atempfileunpacked ,
42+ 'vhd' , 'Disk' )
43+ finally :
44+ if os .path .exists (atempfile ):
45+ os .remove (atempfile )
46+ if os .path .exists (atempfileunpacked ):
47+ os .remove (atempfileunpacked )
4348 templateref = session .xenapi .VM .get_by_name_label (templatename )[0 ]
4449 vmref = session .xenapi .VM .clone (templateref , vmname )
4550 vmuuid = session .xenapi .VM .get_record (vmref )['uuid' ]
@@ -113,9 +118,11 @@ def load_cloud_config_template(template_path=None):
113118
114119 log .info ("load_cloud_config_template from %s" % (template_path ))
115120
116- fh = open (template_path )
117- template_data = fh .read ()
118- fh .close ()
121+ filehandle = open (template_path )
122+ try :
123+ template_data = filehandle .read ()
124+ finally :
125+ filehandle .close ()
119126
120127 # Append template location to make it clear where it was loaded from.
121128 template_data = ("%s\n \n # Template loaded from %s"
@@ -130,55 +137,71 @@ def get_config_drive_default(session):
130137 userdata = filterxshinexists (userdata )
131138 return userdata
132139
133-
134- def create_config_drive_iso (session , userdata , vmuuid ):
135- log .info ("create_config_drive_iso for vm %s" % (vmuuid ))
136- tempisodir = tempfile .mkdtemp ()
137- tempisofile = tempfile .mkstemp ()[1 ]
138- openstackfolder = os .path .join (tempisodir , 'openstack' )
139- latestfolder = os .path .join (openstackfolder , 'latest' )
140- os .makedirs (latestfolder )
141- userdatafile = os .path .join (latestfolder , 'user_data' )
142- userdata = customize_userdata (session , userdata , vmuuid )
143- util .write_file (userdatafile , userdata )
144- log .debug ("Userdata: %s" % (userdata ))
145- # Also add the Linux guest agent
146- temptoolsisodir = tempfile .mkdtemp ()
147- # First find the latest tools ISO
140+ def find_latest_tools_iso_path ():
148141 tools_iso_paths = glob .glob (XS_TOOLS_ISO_PATH )
149142 if len (tools_iso_paths ) < 1 :
150143 raise util .XSContainerException ("Can't locate XS tools in %s."
151144 % (XS_TOOLS_ISO_PATH ))
152145 tools_iso_paths .sort (key = distutils .version .LooseVersion )
153146 tools_iso_path = tools_iso_paths [- 1 ]
154- # Then copy it
155- cmd = ['mount' , '-o' , 'loop' ,
156- tools_iso_path , temptoolsisodir ]
157- util .runlocal (cmd )
158- agentpath = os .path .join (tempisodir , 'agent' )
159- os .makedirs (agentpath )
160- agentfiles = ['xe-daemon' , 'xe-linux-distribution' ,
161- 'xe-linux-distribution.service' , 'xe-update-guest-attrs' ,
162- 'xen-vcpu-hotplug.rules' , 'install.sh' ,
163- 'versions.deb' , 'versions.rpm' ]
164- for filename in agentfiles :
165- path = os .path .join (temptoolsisodir , 'Linux' , filename )
166- shutil .copy (path , agentpath )
167- cmd = ['umount' , temptoolsisodir ]
168- util .runlocal (cmd )
169- os .rmdir (temptoolsisodir )
170- # Finally wrap up the iso
171- cmd = ['mkisofs' , '-R' , '-V' , 'config-2' , '-o' , tempisofile , tempisodir ]
172- util .runlocal (cmd )
173- # Tidy
174- os .remove (userdatafile )
175- os .rmdir (latestfolder )
176- os .rmdir (openstackfolder )
177- for filename in agentfiles :
178- path = os .path .join (agentpath , filename )
179- os .remove (path )
180- os .rmdir (agentpath )
181- os .rmdir (tempisodir )
147+ return tools_iso_path
148+
149+ def create_config_drive_iso (session , userdata , vmuuid ):
150+ log .info ("create_config_drive_iso for vm %s" % (vmuuid ))
151+ umountrequired = False
152+ temptoolsisodir = None
153+ userdatafile = None
154+ latestfolder = None
155+ openstackfolder = None
156+ agentfilepaths = []
157+ agentpath = None
158+ tempisodir = None
159+ try :
160+ tempisodir = tempfile .mkdtemp ()
161+ tempisofile = tempfile .mkstemp ()[1 ]
162+ # add the userdata-file
163+ openstackfolder = os .path .join (tempisodir , 'openstack' )
164+ latestfolder = os .path .join (openstackfolder , 'latest' )
165+ os .makedirs (latestfolder )
166+ userdatafile = os .path .join (latestfolder , 'user_data' )
167+ userdata = customize_userdata (session , userdata , vmuuid )
168+ util .write_file (userdatafile , userdata )
169+ log .debug ("Userdata: %s" % (userdata ))
170+ # Also add the Linux guest agent
171+ temptoolsisodir = tempfile .mkdtemp ()
172+ tools_iso_path = find_latest_tools_iso_path ()
173+ cmd = ['mount' , '-o' , 'loop' ,
174+ tools_iso_path , temptoolsisodir ]
175+ util .runlocal (cmd )
176+ umountrequired = True
177+ agentpath = os .path .join (tempisodir , 'agent' )
178+ os .makedirs (agentpath )
179+ agentfiles = ['xe-daemon' , 'xe-linux-distribution' ,
180+ 'xe-linux-distribution.service' , 'xe-update-guest-attrs' ,
181+ 'xen-vcpu-hotplug.rules' , 'install.sh' ,
182+ 'versions.deb' , 'versions.rpm' ]
183+ for filename in agentfiles :
184+ path = os .path .join (temptoolsisodir , 'Linux' , filename )
185+ shutil .copy (path , agentpath )
186+ agentfilepaths .append (os .path .join (agentpath , filename ))
187+ # Finally wrap up the iso
188+ cmd = ['mkisofs' , '-R' , '-V' , 'config-2' , '-o' , tempisofile , tempisodir ]
189+ util .runlocal (cmd )
190+ finally :
191+ # And tidy
192+ if umountrequired :
193+ cmd = ['umount' , temptoolsisodir ]
194+ util .runlocal (cmd )
195+ for path in [temptoolsisodir , userdatafile , latestfolder ,
196+ openstackfolder ]+ agentfilepaths + [agentpath , tempisodir ]:
197+ if path != None :
198+ if os .path .isdir (path ):
199+ os .rmdir (path )
200+ elif os .path .isfile (path ):
201+ os .remove (path )
202+ else :
203+ log .debug ("create_config_drive_iso: Not tidying %s because"
204+ " it could not be found" % (path ))
182205 return tempisofile
183206
184207
@@ -203,12 +226,14 @@ def create_config_drive(session, vmuuid, sruuid, userdata):
203226 vmrecord = session .xenapi .VM .get_record (vmref )
204227 prepare_vm_for_config_drive (session , vmref , vmuuid )
205228 isofile = create_config_drive_iso (session , userdata , vmuuid )
206- configdisk_namelabel = 'Automatic Config Drive'
207- other_config_keys = {OTHER_CONFIG_CONFIG_DRIVE_KEY : 'True' }
208- vdiref = api_helper .import_disk (session , sruuid , isofile , 'raw' ,
209- configdisk_namelabel ,
210- other_config_keys = other_config_keys )
211- os .remove (isofile )
229+ try :
230+ configdisk_namelabel = 'Automatic Config Drive'
231+ other_config_keys = {OTHER_CONFIG_CONFIG_DRIVE_KEY : 'True' }
232+ vdiref = api_helper .import_disk (session , sruuid , isofile , 'raw' ,
233+ configdisk_namelabel ,
234+ other_config_keys = other_config_keys )
235+ finally :
236+ os .remove (isofile )
212237 remove_config_drive (session , vmrecord , configdisk_namelabel )
213238 vbdref = api_helper .create_vbd (session , vmref , vdiref , 'ro' , False )
214239 if vmrecord ['power_state' ] == 'Running' :
@@ -223,14 +248,21 @@ def create_config_drive(session, vmuuid, sruuid, userdata):
223248
224249def get_config_drive_configuration (session , vdiuuid ):
225250 log .info ("get_config_drive_configuration from vdi %s" % (vdiuuid ))
251+ tempdir = None
252+ umountrequired = False
226253 filename = api_helper .export_disk (session , vdiuuid )
227- tempdir = tempfile .mkdtemp ()
228- cmd = ['mount' , '-o' , 'loop' , '-t' , 'iso9660' , filename , tempdir ]
229- util .runlocal (cmd )
230- userdatapath = os .path .join (tempdir , 'openstack' , 'latest' , 'user_data' )
231- content = util .read_file (userdatapath )
232- cmd = ['umount' , tempdir ]
233- util .runlocal (cmd )
234- os .rmdir (tempdir )
235- os .remove (filename )
254+ try :
255+ tempdir = tempfile .mkdtemp ()
256+ cmd = ['mount' , '-o' , 'loop' , '-t' , 'iso9660' , filename , tempdir ]
257+ util .runlocal (cmd )
258+ umountrequired = True
259+ userdatapath = os .path .join (tempdir , 'openstack' , 'latest' , 'user_data' )
260+ content = util .read_file (userdatapath )
261+ finally :
262+ os .remove (filename )
263+ if umountrequired :
264+ cmd = ['umount' , tempdir ]
265+ util .runlocal (cmd )
266+ if tempdir :
267+ os .rmdir (tempdir )
236268 return content
0 commit comments