def getAMIData(self, path): """ Get the region and the AMI ID from an URL of a VMI Arguments: - path(str): URL of a VMI (some like this: aws://eu-west-1/ami-00685b74) Returns: a tuple (region, ami) with the region and the AMI ID """ region = uriparse(path)[1] ami = uriparse(path)[2][1:] return (region, ami)
def test_50_removeresource_noconfig(self): resp = self.create_request("GET", "/infrastructures/" + self.inf_id) self.assertEqual(resp.status_code, 200, msg="ERROR getting the infrastructure info:" + resp.text) vm_ids = resp.text.split("\n") vm_uri = uriparse(vm_ids[1]) resp = self.create_request("DELETE", vm_uri[2] + "?context=0") self.assertEqual(resp.status_code, 200, msg="ERROR removing resources:" + resp.text) resp = self.create_request("GET", "/infrastructures/" + self.inf_id) self.assertEqual(resp.status_code, 200, msg="ERROR getting the infrastructure info:" + resp.text) vm_ids = resp.text.split("\n") self.assertEqual( len(vm_ids), 1, msg=( "ERROR getting infrastructure info: Incorrect number of VMs(" + str(len(vm_ids)) + "). It must be 1"))
def concreteSystem(self, radl_system, auth_data): image_urls = radl_system.getValue("disk.0.image.url") if not image_urls: return [radl_system.clone()] else: if not isinstance(image_urls, list): image_urls = [image_urls] res = [] for str_url in image_urls: url = uriparse(str_url) protocol = url[0] protocol = url[0] if protocol == "azr": res_system = radl_system.clone() instance_type = self.get_instance_type(res_system) if not instance_type: self.logger.error("Error generating the RADL of the VM, no instance type available for the requirements.") self.logger.debug(res_system) else: res_system.addFeature(Feature("disk.0.image.url", "=", str_url), conflict="other", missing="other") self.update_system_info_from_instance(res_system, instance_type) res_system.addFeature(Feature("provider.type", "=", self.type), conflict="other", missing="other") username = res_system.getValue('disk.0.os.credentials.username') if not username: res_system.setValue('disk.0.os.credentials.username','azureuser') res_system.updateNewCredentialValues() res.append(res_system) return res
def get_response(self, method, url, verify, headers, data): resp = MagicMock() parts = uriparse(url) url = parts[2] params = parts[4] if method not in self.call_count: self.call_count[method] = {} if url not in self.call_count[method]: self.call_count[method][url] = 0 self.call_count[method][url] += 1 if method == "GET": if url == "/order/1": resp.status_code = 200 resp.text = read_file_as_string("files/focci_resource.txt") if url == "/compute/08d50672-76c6-4bcb-9eb4-7a17e611b86c@lsd.manager.naf.lsd.ufcg.edu.br": resp.status_code = 200 resp.text = read_file_as_string("files/focci_instance.txt") elif method == "POST": if url == "/order/": resp.status_code = 201 resp.headers = {'location': 'http/server.com/computeid'} elif method == "DELETE": if url == "/order/1": resp.status_code = 200 if url == "/compute/08d50672-76c6-4bcb-9eb4-7a17e611b86c@lsd.manager.naf.lsd.ufcg.edu.br": resp.status_code = 200 return resp
def test_46_removeresource(self): resp = self.create_request("GET", "/infrastructures/" + self.inf_id) self.assertEqual(resp.status_code, 200, msg="ERROR getting the infrastructure info:" + resp.text) vm_ids = resp.text.split("\n") vm_uri = uriparse(vm_ids[1]) resp = self.create_request("DELETE", vm_uri[2]) self.assertEqual(resp.status_code, 200, msg="ERROR removing resources:" + resp.text) resp = self.create_request("GET", "/infrastructures/" + self.inf_id) self.assertEqual(resp.status_code, 200, msg="ERROR getting the infrastructure info:" + resp.text) vm_ids = resp.text.split("\n") self.assertEqual( len(vm_ids), 1, msg=( "ERROR getting infrastructure info: Incorrect number of VMs(" + str(len(vm_ids)) + "). It must be 1")) all_configured = self.wait_inf_state(VirtualMachine.CONFIGURED, 300) self.assertTrue( all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")
def test_46_removeresource(self): self.server.request('GET', "/infrastructures/" + self.inf_id, headers={'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting the infrastructure info:" + output) vm_ids = output.split("\n") vm_uri = uriparse(vm_ids[1]) self.server.request('DELETE', vm_uri[2], headers={ 'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR removing resources:" + output) self.server.request('GET', "/infrastructures/" + self.inf_id, headers={'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting the infrastructure info:" + output) vm_ids = output.split("\n") self.assertEqual(len(vm_ids), 1, msg=("ERROR getting infrastructure info: Incorrect number of VMs(" + str(len(vm_ids)) + "). It must be 1")) all_configured = self.wait_inf_state(VirtualMachine.CONFIGURED, 300) self.assertTrue( all_configured, msg="ERROR waiting the infrastructure to be configured (timeout).")
def concreteSystem(self, radl_system, auth_data): image_urls = radl_system.getValue("disk.0.image.url") if not image_urls: return [radl_system.clone()] else: if not isinstance(image_urls, list): image_urls = [image_urls] res = [] for str_url in image_urls: url = uriparse(str_url) protocol = url[0] protocol = url[0] if protocol == "aws": # Currently EC2 plugin only uses private_key credentials res_system = radl_system.clone() if res_system.getValue('disk.0.os.credentials.private_key'): res_system.delValue('disk.0.os.credentials.password') res_system.addFeature(Feature("disk.0.image.url", "=", str_url), conflict="other", missing="other") res_system.addFeature(Feature("provider.type", "=", self.type), conflict="other", missing="other") instance_type = self.get_instance_type(res_system) if not instance_type: self.logger.error("Error launching the VM, no instance type available for the requirements.") self.logger.debug(res_system) return [] else: self.update_system_info_from_instance(res_system, instance_type) res.append(res_system) return res
def delete_disks(self, node): """ Delete the disks of a node Arguments: - node(:py:class:`libcloud.compute.base.Node`): node object. """ all_ok = True for disk in node.extra['disks']: try: vol_name = os.path.basename(uriparse(disk['source'])[2]) volume = node.driver.ex_get_volume(vol_name) # First try to detach the volume if volume: success = volume.detach() if not success: self.logger.error("Error detaching the volume: " + vol_name) else: # wait a bit to detach the disk time.sleep(2) success = volume.destroy() if not success: self.logger.error("Error destroying the volume: " + vol_name) except ResourceNotFoundError: self.logger.debug("The volume: " + vol_name + " does not exists. Ignore it.") success = True except: self.logger.exception("Error destroying the volume: " + vol_name + " from the node: " + node.id) success = False if not success: all_ok = False return all_ok
def concreteSystem(self, radl_system, auth_data): image_urls = radl_system.getValue("disk.0.image.url") if not image_urls: return [radl_system.clone()] else: if not isinstance(image_urls, list): image_urls = [image_urls] res = [] for str_url in image_urls: url = uriparse(str_url) protocol = url[0] if protocol == 'docker' and url[1]: res_system = radl_system.clone() res_system.addFeature(Feature("virtual_system_type", "=", "docker"), conflict="other", missing="other") res_system.getFeature("cpu.count").operator = "=" res_system.getFeature("memory.size").operator = "=" res_system.setValue('disk.0.os.credentials.username', 'root') res_system.setValue('disk.0.os.credentials.password', self._root_password) res_system.addFeature(Feature("disk.0.image.url", "=", str_url), conflict="other", missing="other") res_system.addFeature(Feature("provider.type", "=", self.type), conflict="other", missing="other") res_system.addFeature(Feature("provider.host", "=", self.cloud.server), conflict="other", missing="other") res_system.addFeature(Feature("provider.port", "=", self.cloud.port), conflict="other", missing="other") res.append(res_system) return res
def get_azure_vm_create_xml(self, vm, storage_account, radl, num, auth_data): """ Generate the XML to create the VM """ system = radl.systems[0] name = system.getValue("instance_name") if not name: name = system.getValue("disk.0.image.name") if not name: name = "userimage" + str(num) url = uriparse(system.getValue("disk.0.image.url")) label = name + " IM created VM" (hostname, _) = vm.getRequestedName( default_hostname=Config.DEFAULT_VM_NAME, default_domain=Config.DEFAULT_DOMAIN) if not hostname: hostname = "AzureNode" + str(num) SourceImageName = url[1] MediaLink = "https://%s.blob.core.windows.net/vhds/%s.vhd" % ( storage_account, SourceImageName) instance_type = self.get_instance_type(system, auth_data) DataVirtualHardDisks = self.gen_data_disks(system, storage_account) ConfigurationSet = self.gen_configuration_set(hostname, system) InputEndpoints = self.gen_input_endpoints(radl) res = ''' <Deployment xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"> <Name>%s</Name> <DeploymentSlot>Production</DeploymentSlot> <Label>%s</Label> <RoleList> <Role i:type="PersistentVMRole"> <RoleName>%s</RoleName> <OsVersion i:nil="true"/> <RoleType>PersistentVMRole</RoleType> <ConfigurationSets> %s <ConfigurationSet i:type="NetworkConfigurationSet"> <ConfigurationSetType>NetworkConfiguration</ConfigurationSetType> %s </ConfigurationSet> </ConfigurationSets> %s <OSVirtualHardDisk> <MediaLink>%s</MediaLink> <SourceImageName>%s</SourceImageName> </OSVirtualHardDisk> <RoleSize>%s</RoleSize> </Role> </RoleList> </Deployment> ''' % (vm.id, label, self.ROLE_NAME, ConfigurationSet, InputEndpoints, DataVirtualHardDisks, MediaLink, SourceImageName, instance_type.Name) self.logger.debug("Azure VM Create XML: " + res) return res
def concreteSystem(self, radl_system, auth_data): image_urls = radl_system.getValue("disk.0.image.url") if not image_urls: return [radl_system.clone()] else: if not isinstance(image_urls, list): image_urls = [image_urls] res = [] for str_url in image_urls: url = uriparse(str_url) protocol = url[0] if protocol in ['fbw']: res_system = radl_system.clone() res_system.addFeature( Feature("disk.0.image.url", "=", str_url), conflict="other", missing="other") res_system.addFeature( Feature("provider.type", "=", self.type), conflict="other", missing="other") res_system.addFeature(Feature( "provider.host", "=", self.cloud.server), conflict="other", missing="other") res_system.addFeature(Feature( "provider.port", "=", self.cloud.port), conflict="other", missing="other") res_system.delValue('disk.0.os.credentials.username') res_system.setValue('disk.0.os.credentials.username', 'fogbow') res.append(res_system) return res
def get_auth_from_tts(tts_url, one_server, token): """ Get username and password from the TTS service """ tts_uri = uriparse(tts_url) scheme = tts_uri[0] host = tts_uri[1] port = None if host.find(":") != -1: parts = host.split(":") host = parts[0] port = int(parts[1]) ttsc = TTSClient(token, host, port, scheme) success, svc = ttsc.find_service(one_server) if not success: raise Exception("Error getting credentials from TTS: %s" % svc) succes, cred = ttsc.request_credential(svc["id"]) if succes: username = password = None for elem in cred['credential']['entries']: if elem['name'] == 'Username': username = elem['value'] elif elem['name'] == 'Password': password = elem['value'] return username, password else: raise Exception("Error getting credentials from TTS: %s" % cred)
def get_auth_header(self, auth_data): """ Generate the auth header needed to contact with the Kubernetes API server. """ url = uriparse(self.cloud.server) auths = auth_data.getAuthInfo(self.type, url[1]) if not auths: self.logger.error( "No correct auth data has been specified to Kubernetes.") return None else: auth = auths[0] auth_header = None if 'username' in auth and 'password' in auth: passwd = auth['password'] user = auth['username'] auth_header = {'Authorization': 'Basic ' + string.strip(base64.encodestring(user + ':' + passwd))} elif 'token' in auth: token = auth['token'] auth_header = {'Authorization': 'Bearer ' + token} return auth_header
def concreteSystem(self, radl_system, auth_data): image_urls = radl_system.getValue("disk.0.image.url") if not image_urls: return [radl_system.clone()] else: if not isinstance(image_urls, list): image_urls = [image_urls] res = [] for str_url in image_urls: url = uriparse(str_url) protocol = url[0] if protocol == "file": res_system = radl_system.clone() res_system.getFeature("cpu.count").operator = "=" res_system.getFeature("memory.size").operator = "=" res_system.addFeature( Feature("disk.0.image.url", "=", str_url), conflict="other", missing="other") res_system.addFeature( Feature("provider.type", "=", self.type), conflict="other", missing="other") res_system.addFeature(Feature( "provider.host", "=", self.cloud.server), conflict="other", missing="other") res.append(res_system) return res
def test_30_get_vm_info(self): self.server.request('GET', "/infrastructures/" + self.inf_id, headers={'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting the infrastructure info:" + output) vm_ids = output.split("\n") vm_uri = uriparse(vm_ids[0]) self.server.request('GET', vm_uri[2], headers={ 'AUTHORIZATION': self.auth_data, 'Accept': 'application/json' }) resp = self.server.getresponse() ct = resp.getheader('Content-type') output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting VM info:" + output) self.assertEqual( ct, "application/json", msg="ERROR getting VM info: Incorrect Content-type: %s" % ct) res = json.loads(output) radl = res["radl"] parse_radl_json(radl)
def test_50_removeresource_noconfig(self): self.server.request('GET', "/infrastructures/" + self.inf_id + "?context=0", headers={'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting the infrastructure info:" + output) vm_ids = output.split("\n") vm_uri = uriparse(vm_ids[1]) self.server.request('DELETE', vm_uri[2], headers={ 'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR removing resources:" + output) self.server.request('GET', "/infrastructures/" + self.inf_id, headers={'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting the infrastructure info:" + output) vm_ids = output.split("\n") self.assertEqual(len(vm_ids), 1, msg=("ERROR getting infrastructure info: Incorrect number of VMs(" + str(len(vm_ids)) + "). It must be 1"))
def wait_inf_state(self, state, timeout, incorrect_states=None, vm_ids=None): """ Wait for an infrastructure to have a specific state """ if not vm_ids: resp = self.create_request("GET", "/infrastructures/" + self.inf_id) self.assertEqual(resp.status_code, 200, msg="ERROR getting infrastructure info:" + resp.text) vm_ids = resp.text.split("\n") else: pass err_states = [VirtualMachine.FAILED, VirtualMachine.UNCONFIGURED] if incorrect_states: err_states.extend(incorrect_states) wait = 0 all_ok = False while not all_ok and wait < timeout: all_ok = True for vm_id in vm_ids: vm_uri = uriparse(vm_id) resp = self.create_request("GET", vm_uri[2] + "/state") vm_state = resp.text self.assertEqual(resp.status_code, 200, msg="ERROR getting VM info:" + vm_state) if vm_state == VirtualMachine.UNCONFIGURED: resp = self.create_request( "GET", "/infrastructures/" + self.inf_id + "/contmsg") print(resp.text) self.assertFalse( vm_state in err_states, msg=("ERROR waiting for a state. '%s' state was expected " "and '%s' was obtained in the VM %s" % (state, vm_state, vm_uri))) if vm_state in err_states: return False elif vm_state != state: all_ok = False if not all_ok: wait += 5 time.sleep(5) return all_ok
def get_image_id(self, path): """ Get the ID of the image to use from the location of the VMI Arguments: - path(str): URL with the location of the VMI Returns: a str with the ID """ return uriparse(path)[2][1:]
def get_cloud_list(auth_data): """ Get the list of cloud providers from the authentication data """ res = [] for i, auth in enumerate(auth_data.auth_list): if auth['type'] not in ['InfrastructureManager', 'VMRC']: cloud_item = CloudInfo() cloud_item.type = auth['type'] if 'id' in auth.keys() and auth['id']: cloud_item.id = auth['id'] else: # We need an ID, so generate one cloud_item.id = cloud_item.type + str(i) try: if 'host' in auth and auth['host']: if auth['host'].find('://') == -1: uri = uriparse("NONE://" + auth['host']) else: uri = uriparse(auth['host']) if uri[0]: cloud_item.protocol = uri[0] if not uri[1]: raise Exception("Incorrect format of host in auth line: %s" % str(auth)) parts = uri[1].split(":") cloud_item.server = parts[0] if len(parts) > 1: if parts[1].isdigit(): cloud_item.port = int(parts[1]) else: raise Exception("Incorrect value for port '%s'. It must be an integer." % parts[1]) # If there is a path if uri[2]: cloud_item.path = uri[2] except: pass res.append(cloud_item) return res
def wait_inf_state(self, state, timeout, incorrect_states=[], vm_ids=None): """ Wait for an infrastructure to have a specific state """ if not vm_ids: self.server.request('GET', "/infrastructures/" + self.inf_id, headers={'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting infrastructure info:" + output) vm_ids = output.split("\n") else: pass err_states = [VirtualMachine.FAILED, VirtualMachine.OFF, VirtualMachine.UNCONFIGURED] err_states.extend(incorrect_states) wait = 0 all_ok = False while not all_ok and wait < timeout: all_ok = True for vm_id in vm_ids: vm_uri = uriparse(vm_id) self.server.request( 'GET', vm_uri[2] + "/state", headers={'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() vm_state = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting VM info:" + vm_state) if vm_state == VirtualMachine.UNCONFIGURED: self.server.request('GET', "/infrastructures/" + self.inf_id + "/contmsg", headers={'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) print output self.assertFalse(vm_state in err_states, msg=("ERROR waiting for a state. '%s' state was expected " "and '%s' was obtained in the VM %s" % (state, vm_state, vm_uri))) if vm_state in err_states: return False elif vm_state != state: all_ok = False if not all_ok: wait += 5 time.sleep(5) return all_ok
def wait_inf_state(self, state, timeout, incorrect_states=[], vm_ids=None): """ Wait for an infrastructure to have a specific state """ if not vm_ids: self.server.request('GET', "/infrastructures/" + self.inf_id, headers={'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting infrastructure info:" + output) vm_ids = output.split("\n") else: pass err_states = [ VirtualMachine.FAILED, VirtualMachine.OFF, VirtualMachine.UNCONFIGURED ] err_states.extend(incorrect_states) wait = 0 all_ok = False while not all_ok and wait < timeout: all_ok = True for vm_id in vm_ids: vm_uri = uriparse(vm_id) self.server.request('GET', vm_uri[2] + "/state", headers={'AUTHORIZATION': self.auth_data}) resp = self.server.getresponse() vm_state = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting VM info:" + vm_state) self.assertFalse( vm_state in err_states, msg=("ERROR waiting for a state. '%s' state was expected " "and '%s' was obtained in the VM %s" % (state, vm_state, vm_uri))) if vm_state in err_states: return False elif vm_state != state: all_ok = False if not all_ok: wait += 5 time.sleep(5) return all_ok
def concreteSystem(self, radl_system, auth_data): image_urls = radl_system.getValue("disk.0.image.url") if not image_urls: return [radl_system.clone()] else: if not isinstance(image_urls, list): image_urls = [image_urls] res = [] for str_url in image_urls: url = uriparse(str_url) protocol = url[0] src_host = url[1].split(':')[0] # TODO: check the port if (protocol == "one") and self.cloud.server == src_host: # Check the space in image and compare with disks.free_size if radl_system.getValue('disks.free_size'): disk_free = int( radl_system.getFeature('disks.free_size').getValue( 'M')) # The VMRC specified the value in MB disk_size = int(radl_system.getValue("disk.0.size")) if disk_size < disk_free: # if the image do not have enough space, discard it return [] res_system = radl_system.clone() res_system.getFeature("cpu.count").operator = "=" res_system.getFeature("memory.size").operator = "=" res_system.addFeature(Feature("disk.0.image.url", "=", str_url), conflict="other", missing="other") res_system.addFeature(Feature("provider.type", "=", self.type), conflict="other", missing="other") res_system.addFeature(Feature("provider.host", "=", self.cloud.server), conflict="other", missing="other") res_system.addFeature(Feature("provider.port", "=", self.cloud.port), conflict="other", missing="other") res.append(res_system) return res
def test_35_get_vm_property(self): self.server.request('GET', "/infrastructures/" + self.inf_id, headers = {'AUTHORIZATION' : self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting the infrastructure info:" + output) vm_ids = output.split("\n") vm_uri = uriparse(vm_ids[0]) self.server.request('GET', vm_uri[2] + "/state", headers = {'AUTHORIZATION' : self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting VM property:" + output)
def test_32_get_vm_contmsg(self): self.server.request('GET', "/infrastructures/" + self.inf_id, headers = {'AUTHORIZATION' : self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting the infrastructure info:" + output) vm_ids = output.split("\n") vm_uri = uriparse(vm_ids[0]) self.server.request('GET', vm_uri[2] + "/contmsg", headers = {'AUTHORIZATION' : self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting VM contmsg:" + output) self.assertEqual(len(output), 0, msg="Incorrect VM contextualization message: " + output)
def create_token(params): """ Contact the specified keystone server to return the token """ if 'username' in params and 'password' in params and 'auth_url' in params and 'tenant' in params: try: keystone_uri = params['auth_url'] uri = uriparse(keystone_uri) server = uri[1].split(":")[0] port = int(uri[1].split(":")[1]) conn = HTTPSConnection(server, port) conn.putrequest('POST', "/v2.0/tokens") conn.putheader('Accept', 'application/json') conn.putheader('Content-Type', 'application/json') conn.putheader('Connection', 'close') body = ('{"auth":{"passwordCredentials":{"username": "******","password": "******"},"tenantName": "' + params['tenant'] + '"}}') conn.putheader('Content-Length', len(body)) conn.endheaders(body) resp = conn.getresponse() # format: -> "{\"access\": {\"token\": {\"issued_at\": # \"2014-12-29T17:10:49.609894\", \"expires\": # \"2014-12-30T17:10:49Z\", \"id\": # \"c861ab413e844d12a61d09b23dc4fb9c\"}, \"serviceCatalog\": # [], \"user\": {\"username\": # \"/DC=es/DC=irisgrid/O=upv/CN=miguel-caballer\", # \"roles_links\": [], \"id\": # \"475ce4978fb042e49ce0391de9bab49b\", \"roles\": [], # \"name\": \"/DC=es/DC=irisgrid/O=upv/CN=miguel-caballer\"}, # \"metadata\": {\"is_admin\": 0, \"roles\": []}}}" output = json.loads(resp.read()) token_id = output['access']['token']['id'] if conn.cert_file and os.path.isfile(conn.cert_file): os.unlink(conn.cert_file) return token_id except: return None else: raise Exception( "Incorrect auth data, auth_url, username, password and tenant must be specified" )
def connect(self): """ Function to connect to the DB Returns: True if the connection is established correctly of False in case of errors. """ uri = uriparse(self.db_url) protocol = uri[0] if protocol == "mysql": return self._connect_mysql(uri[1], uri[2][1:]) elif protocol == "file" or not protocol: # sqlite is the default one return self._connect_sqlite(uri[2]) return False
def test_30_get_vm_info(self): self.server.request('GET', "/infrastructures/" + self.inf_id, headers = {'AUTHORIZATION' : self.auth_data}) resp = self.server.getresponse() output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting the infrastructure info:" + output) vm_ids = output.split("\n") vm_uri = uriparse(vm_ids[0]) self.server.request('GET', vm_uri[2], headers = {'AUTHORIZATION' : self.auth_data, 'Accept':'application/json'}) resp = self.server.getresponse() ct = resp.getheader('Content-type') output = str(resp.read()) self.assertEqual(resp.status, 200, msg="ERROR getting VM info:" + output) self.assertEqual(ct, "application/json", msg="ERROR getting VM info: Incorrect Content-type: %s" % ct) parse_radl_json(output)
def create_token(params): """ Contact the specified keystone server to return the token """ if 'username' in params and 'password' in params and 'auth_url' in params and 'tenant' in params: try: keystone_uri = params['auth_url'] uri = uriparse(keystone_uri) server = uri[1].split(":")[0] port = int(uri[1].split(":")[1]) conn = httplib.HTTPSConnection(server, port) conn.putrequest('POST', "/v2.0/tokens") conn.putheader('Accept', 'application/json') conn.putheader('Content-Type', 'application/json') conn.putheader('Connection', 'close') body = ('{"auth":{"passwordCredentials":{"username": "******","password": "******"},"tenantName": "' + params['tenant'] + '"}}') conn.putheader('Content-Length', len(body)) conn.endheaders(body) resp = conn.getresponse() # format: -> "{\"access\": {\"token\": {\"issued_at\": # \"2014-12-29T17:10:49.609894\", \"expires\": # \"2014-12-30T17:10:49Z\", \"id\": # \"c861ab413e844d12a61d09b23dc4fb9c\"}, \"serviceCatalog\": # [], \"user\": {\"username\": # \"/DC=es/DC=irisgrid/O=upv/CN=miguel-caballer\", # \"roles_links\": [], \"id\": # \"475ce4978fb042e49ce0391de9bab49b\", \"roles\": [], # \"name\": \"/DC=es/DC=irisgrid/O=upv/CN=miguel-caballer\"}, # \"metadata\": {\"is_admin\": 0, \"roles\": []}}}" output = json.loads(resp.read()) token_id = output['access']['token']['id'] if conn.cert_file and os.path.isfile(conn.cert_file): os.unlink(conn.cert_file) return token_id except: return None else: raise Exception( "Incorrect auth data, auth_url, username, password and tenant must be specified")
def get_driver(self, auth_data): """ Get the driver from the auth data Arguments: - auth(Authentication): parsed authentication tokens. Returns: a :py:class:`libcloud.compute.base.NodeDriver` or None in case of error """ if self.driver: return self.driver else: auth = auth_data.getAuthInfo(LibCloudCloudConnector.type) if auth and 'driver' in auth[0]: cls = get_driver(eval("Provider."+auth[0]['driver'])) MAP = { "username" : "key", "password": "******"} params = {} for key, value in auth[0].iteritems(): if key not in ["type","driver","id"]: params[MAP[key]] = value if auth[0]['driver'] == "OPENSTACK": if 'host' in auth[0]: params["ex_force_auth_url"] = auth[0]['host'] else: self.logger.error("Host data is needed in OpenStack") return None else: if 'host' in auth[0]: uri = uriparse(auth[0]['host']) if uri[1].find(":"): parts = uri[1].split(":") params["host"] = parts[0] params["port"] = int(parts[1]) else: params["host"] = uri[1] driver = cls(**params) self.driver = driver return driver else: self.logger.error("Incorrect auth data") return None
def get_image_data(self, path): """ Get the region and the image name from an URL of a VMI Arguments: - path(str): URL of a VMI (some like this: gce://us-central1/debian-7 or gce://debian-7) Returns: a tuple (region, image_name) with the region and the AMI ID """ uri = uriparse(path) if uri[2]: region = uri[1] image_name = uri[2][1:] else: # If the image do not specify the zone, use the default one region = self.DEFAULT_ZONE image_name = uri[1] return (region, image_name)
def concreteSystem(self, radl_system, auth_data): image_urls = radl_system.getValue("disk.0.image.url") if not image_urls: return [radl_system.clone()] else: if not isinstance(image_urls, list): image_urls = [image_urls] res = [] for str_url in image_urls: url = uriparse(str_url) protocol = url[0] src_host = url[1].split(':')[0] # TODO: check the port if (protocol == "one") and self.cloud.server == src_host: # Check the space in image and compare with disks.free_size if radl_system.getValue('disks.free_size'): disk_free = int(radl_system.getFeature( 'disks.free_size').getValue('M')) # The VMRC specified the value in MB disk_size = int(radl_system.getValue("disk.0.size")) if disk_size < disk_free: # if the image do not have enough space, discard it return [] res_system = radl_system.clone() res_system.getFeature("cpu.count").operator = "=" res_system.getFeature("memory.size").operator = "=" res_system.addFeature( Feature("disk.0.image.url", "=", str_url), conflict="other", missing="other") res_system.addFeature( Feature("provider.type", "=", self.type), conflict="other", missing="other") res_system.addFeature(Feature( "provider.host", "=", self.cloud.server), conflict="other", missing="other") res_system.addFeature(Feature( "provider.port", "=", self.cloud.port), conflict="other", missing="other") res.append(res_system) return res
def concreteSystem(self, radl_system, auth_data): image_urls = radl_system.getValue("disk.0.image.url") if not image_urls: return [radl_system.clone()] else: if not isinstance(image_urls, list): image_urls = [image_urls] res = [] for str_url in image_urls: url = uriparse(str_url) protocol = url[0] if protocol == "gce": driver = self.get_driver(auth_data) res_system = radl_system.clone() res_system.addFeature( Feature("disk.0.image.url", "=", str_url), conflict="other", missing="other") if res_system.getValue('availability_zone'): region = res_system.getValue('availability_zone') else: region, _ = self.get_image_data(str_url) instance_type = self.get_instance_type( driver.list_sizes(region), res_system) if not instance_type: return [] self.update_system_info_from_instance( res_system, instance_type) username = res_system.getValue( 'disk.0.os.credentials.username') if not username: res_system.setValue( 'disk.0.os.credentials.username', 'gceuser') res_system.addFeature( Feature("provider.type", "=", self.type), conflict="other", missing="other") res.append(res_system) return res
def concreteSystem(self, radl_system, auth_data): image_urls = radl_system.getValue("disk.0.image.url") if not image_urls: return [radl_system.clone()] else: if not isinstance(image_urls, list): image_urls = [image_urls] res = [] for str_url in image_urls: url = uriparse(str_url) protocol = url[0] protocol = url[0] driver = self.get_driver(auth_data) PROTOCOL_MAP = { "Amazon EC2": "aws", "OpenNebula": "one", "OpenStack": "ost", "LibVirt": "file"} req_protocol = PROTOCOL_MAP.get(driver.name, None) if req_protocol is None or protocol == req_protocol: res_system = radl_system.clone() instance_type = self.get_instance_type( driver.list_sizes(), res_system) self.update_system_info_from_instance( res_system, instance_type) res_system.addFeature( Feature("disk.0.image.url", "=", str_url), conflict="other", missing="other") res_system.addFeature( Feature("provider.type", "=", self.type), conflict="other", missing="other") if self.cloud.server: res_system.addFeature(Feature( "provider.host", "=", self.cloud.server), conflict="other", missing="other") if self.cloud.port != -1: res_system.addFeature(Feature( "provider.port", "=", self.cloud.port), conflict="other", missing="other") res.append(res_system) return res