+ def create_config_drive_iso(self, user_data):
+ tmpdir = tempfile.mkdtemp()
+ iso_path = os.path.join(tmpdir, 'ConfigDrive.iso')
+ latest_dir = os.path.join(tmpdir, 'openstack', 'latest')
+ os.makedirs(latest_dir)
+ with open(os.path.join(latest_dir, 'meta_data.json'), 'w') as meta_file_obj, \
+ open(os.path.join(latest_dir, 'user_data'), 'w') as userdata_file_obj:
+ userdata_file_obj.write(user_data)
+ meta_file_obj.write(json.dumps({"availability_zone": "nova",
+ "launch_index": 0,
+ "name": "ConfigDrive",
+ "uuid": str(uuid.uuid4())}
+ )
+ )
+ genisoimage_cmd = 'genisoimage -J -r -V config-2 -o {iso_path} {source_dir_path}'.format(
+ iso_path=iso_path, source_dir_path=tmpdir)
+ self.logger.info('create_config_drive_iso(): Creating ISO by running command "{}"'.format(genisoimage_cmd))
+ try:
+ FNULL = open(os.devnull, 'w')
+ subprocess.check_call(genisoimage_cmd, shell=True, stdout=FNULL)
+ except subprocess.CalledProcessError as e:
+ shutil.rmtree(tmpdir, ignore_errors=True)
+ error_msg = 'create_config_drive_iso(): Exception while running genisoimage command: {}'.format(e)
+ self.logger.error(error_msg)
+ raise Exception(error_msg)
+ return iso_path
+
+ def upload_iso_to_catalog(self, catalog_id, iso_file_path):
+ if not os.path.isfile(iso_file_path):
+ error_msg = "upload_iso_to_catalog(): Given iso file is not present. Given path: {}".format(iso_file_path)
+ self.logger.error(error_msg)
+ raise Exception(error_msg)
+ iso_file_stat = os.stat(iso_file_path)
+ xml_media_elem = '''<?xml version="1.0" encoding="UTF-8"?>
+ <Media
+ xmlns="http://www.vmware.com/vcloud/v1.5"
+ name="{iso_name}"
+ size="{iso_size}"
+ imageType="iso">
+ <Description>ISO image for config-drive</Description>
+ </Media>'''.format(iso_name=os.path.basename(iso_file_path), iso_size=iso_file_stat.st_size)
+ headers = {'Accept':'application/*+xml;version=' + API_VERSION,
+ 'x-vcloud-authorization': self.client._session.headers['x-vcloud-authorization']}
+ headers['Content-Type'] = 'application/vnd.vmware.vcloud.media+xml'
+ catalog_href = self.url + '/api/catalog/' + catalog_id + '/action/upload'
+ response = self.perform_request(req_type='POST', url=catalog_href, headers=headers, data=xml_media_elem)
+
+ if response.status_code != 201:
+ error_msg = "upload_iso_to_catalog(): Failed to POST an action/upload request to {}".format(catalog_href)
+ self.logger.error(error_msg)
+ raise Exception(error_msg)
+
+ catalogItem = XmlElementTree.fromstring(response.content)
+ entity = [child for child in catalogItem if child.get("type") == "application/vnd.vmware.vcloud.media+xml"][0]
+ entity_href = entity.get('href')
+
+ response = self.perform_request(req_type='GET', url=entity_href, headers=headers)
+ if response.status_code != 200:
+ raise Exception("upload_iso_to_catalog(): Failed to GET entity href {}".format(entity_href))
+
+ match = re.search(r'<Files>\s+?<File.+?href="(.+?)"/>\s+?</File>\s+?</Files>', response.text, re.DOTALL)
+ if match:
+ media_upload_href = match.group(1)
+ else:
+ raise Exception('Could not parse the upload URL for the media file from the last response')
+
+ headers['Content-Type'] = 'application/octet-stream'
+ response = self.perform_request(req_type='PUT',
+ url=media_upload_href,
+ headers=headers,
+ data=open(iso_file_path, 'rb'))
+
+ if response.status_code != 200:
+ raise Exception('PUT request to "{}" failed'.format(media_upload_href))