def RESTGetVMProperty(infid=None, vmid=None, prop=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: if prop == 'contmsg': info = InfrastructureManager.GetVMContMsg(infid, vmid, auth) else: info = InfrastructureManager.GetVMProperty(infid, vmid, prop, auth) if info is None: return return_error( 404, "Incorrect property %s for VM ID %s" % (prop, vmid)) else: return format_output(info, field_name=prop) except DeletedInfrastructureException as ex: return return_error(404, "Error Getting VM. property: " + str(ex)) except IncorrectInfrastructureException as ex: return return_error(404, "Error Getting VM. property: " + str(ex)) except UnauthorizedUserException as ex: return return_error(403, "Error Getting VM. property: " + str(ex)) except DeletedVMException as ex: return return_error(404, "Error Getting VM. property: " + str(ex)) except IncorrectVMException as ex: return return_error(404, "Error Getting VM. property: " + str(ex)) except Exception as ex: logger.exception("Error Getting VM property") return return_error(400, "Error Getting VM property: " + str(ex))
def test_inf_addresources_without_credentials(self): """Deploy single virtual machine without credentials to check that it raises the correct exception.""" radl = RADL() radl.add( system( "s0", [Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er")])) radl.add(deploy("s0", 1)) auth0 = self.getAuth([0], [], [("Dummy", 0)]) infId = IM.CreateInfrastructure("", auth0) vms = IM.AddResource(infId, str(radl), auth0) self.assertEqual(vms, [0]) res = IM.GetInfrastructureState(infId, auth0) self.assertEqual(res['state'], VirtualMachine.FAILED) res = IM.GetVMContMsg(infId, 0, auth0) self.assertEqual(res, ( "Error launching the VMs of type s0 to cloud ID cloud0 of type Dummy." " No username for deploy: s0\n")) IM.DestroyInfrastructure(infId, auth0)
def test_get_vm_info(self): """ Test GetVMInfo and GetVMProperty and GetVMContMsg and GetInfrastructureRADL and GetInfrastructureContMsg and GetInfrastructureState. """ radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", 1)) auth0 = self.getAuth([0], [], [("Dummy", 0)]) infId = IM.CreateInfrastructure(str(radl), auth0) radl_info = IM.GetVMInfo(infId, "0", auth0) parsed_radl_info = parse_radl(str(radl_info)) self.assertEqual(parsed_radl_info.systems[0].getValue("state"), "running") radl_info = IM.GetVMInfo(infId, "0", auth0, True) parsed_radl_info = parse_radl_json(radl_info) self.assertEqual(parsed_radl_info.systems[0].getValue("state"), "running") state = IM.GetVMProperty(infId, "0", "state", auth0) self.assertEqual(state, "running") contmsg = IM.GetVMContMsg(infId, "0", auth0) self.assertEqual(contmsg, "") InfrastructureList.infrastructure_list[infId].cont_out = "Header" InfrastructureList.infrastructure_list[infId].vm_list[ 0].cloud_connector = MagicMock() InfrastructureList.infrastructure_list[infId].vm_list[ 0].cloud_connector.error_messages = "TESTMSG" contmsg = IM.GetInfrastructureContMsg(infId, auth0) header_contmsg = IM.GetInfrastructureContMsg(infId, auth0, True) InfrastructureList.infrastructure_list[infId].vm_list[ 0].cloud_connector = None self.assertIn("TESTMSG", contmsg) self.assertNotIn("TESTMSG", header_contmsg) self.assertIn("Header", header_contmsg) state = IM.GetInfrastructureState(infId, auth0) self.assertEqual(state["state"], "running") self.assertEqual(state["vm_states"]["0"], "running") radl_info = IM.GetInfrastructureRADL(infId, auth0) parsed_radl_info = parse_radl(str(radl_info)) self.assertEqual( parsed_radl_info.systems[0].getValue( "disk.0.os.credentials.username"), "user") IM.DestroyInfrastructure(infId, auth0)
def test_get_vm_info(self): """ Test GetVMInfo and GetVMProperty and GetVMContMsg and GetInfrastructureRADL and GetInfrastructureContMsg and GetInfrastructureState. """ radl = RADL() radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) radl.add(deploy("s0", 1)) auth0 = self.getAuth([0], [], [("Dummy", 0)]) infId = IM.CreateInfrastructure(str(radl), auth0) radl_info = IM.GetVMInfo(infId, "0", auth0) parsed_radl_info = parse_radl(str(radl_info)) self.assertEqual(parsed_radl_info.systems[0].getValue("state"), "running") state = IM.GetVMProperty(infId, "0", "state", auth0) self.assertEqual(state, "running") contmsg = IM.GetVMContMsg(infId, "0", auth0) self.assertEqual(contmsg, "") contmsg = IM.GetInfrastructureContMsg(infId, auth0) state = IM.GetInfrastructureState(infId, auth0) self.assertEqual(state["state"], "running") self.assertEqual(state["vm_states"]["0"], "running") radl_info = IM.GetInfrastructureRADL(infId, auth0) parsed_radl_info = parse_radl(str(radl_info)) self.assertEqual( parsed_radl_info.systems[0].getValue( "disk.0.os.credentials.username"), "user") IM.DestroyInfrastructure(infId, auth0)
def test_inf_lifecycle(self): """Test Infrastructure lifecycle""" radl = """" network publica (outbound = 'yes') system front ( cpu.arch='x86_64' and cpu.count>=1 and memory.size>=512m and net_interface.0.connection = 'publica' and net_interface.0.ip = '10.0.0.1' and disk.0.image.url = 'mock0://linux.for.ev.er' and disk.0.os.credentials.username = '******' and disk.0.os.credentials.password = '******' and disk.0.os.name = 'linux' and disk.1.size=1GB and disk.1.device='hdb' and disk.1.fstype='ext4' and disk.1.mount_path='/mnt/disk' and disk.0.applications contains (name = 'ansible.modules.micafer.hadoop') and disk.0.applications contains (name='gmetad') and disk.0.applications contains (name='wget') ) deploy front 1 """ auth0 = self.getAuth([0], [], [("Mock", 0)]) IM._reinit() Config.PLAYBOOK_RETRIES = 1 Config.CONTEXTUALIZATION_DIR = os.path.dirname( os.path.realpath(__file__)) + "/../../contextualization" Config.CONFMAMAGER_CHECK_STATE_INTERVAL = 0.001 cloud0 = self.get_cloud_connector_mock("MyMock") self.register_cloudconnector("Mock", cloud0) infId = IM.CreateInfrastructure(str(radl), auth0) time.sleep(15) state = IM.GetInfrastructureState(infId, auth0) self.assertEqual(state["state"], "unconfigured") InfrastructureList.infrastructure_list[infId].ansible_configured = True IM.Reconfigure(infId, "", auth0) time.sleep(2) state = IM.GetInfrastructureState(infId, auth0) self.assertEqual(state["state"], "running") add_radl = RADL() add_radl.add( system("s0", [ Feature("disk.0.image.url", "=", "mock0://linux.for.ev.er"), Feature("disk.0.os.credentials.username", "=", "user"), Feature("disk.0.os.credentials.password", "=", "pass") ])) add_radl.add(deploy("s0", 1)) vms = IM.AddResource(infId, str(add_radl), auth0) self.assertEqual(vms, [1]) state = IM.GetVMProperty(infId, "1", "state", auth0) self.assertEqual(state, "running") contmsg = IM.GetVMContMsg(infId, "1", auth0) self.assertEqual(contmsg, "") cont = IM.RemoveResource(infId, ['1'], auth0) self.assertEqual(cont, 1) IM.DestroyInfrastructure(infId, auth0)
def RESTGetVMProperty(infid=None, vmid=None, prop=None): try: auth = get_auth_header() except: return return_error(401, "No authentication data provided") try: if prop == 'contmsg': info = InfrastructureManager.GetVMContMsg(infid, vmid, auth) elif prop == 'command': auth = InfrastructureManager.check_auth_data(auth) sel_inf = InfrastructureManager.get_infrastructure(infid, auth) step = 1 if "step" in bottle.request.params.keys(): step = int(bottle.request.params.get("step")) if step == 1: url = get_full_url('/infrastructures/' + str(infid) + '/vms/' + str(vmid) + '/command?step=2') auth = sel_inf.auth.getAuthInfo("InfrastructureManager")[0] imuser = auth['username'] impass = auth['password'] command = ( 'curl -s -H "Authorization: type = InfrastructureManager; ' 'username = %s; password = %s" -H "Accept: text/plain" %s' % (imuser, impass, url)) info = """ res="wait" while [ "$res" == "wait" ] do res=`%s` if [ "$res" != "wait" ] then eval "$res" else sleep 20 fi done""" % command logger.debug("Step 1 command: %s" % info) elif step == 2: sel_vm = None for vm in sel_inf.get_vm_list(): if vm.creation_im_id == int(vmid): sel_vm = vm break if not sel_vm: # this must never happen logger.error("Specified vmid in step2 is incorrect!!") info = None else: ssh = sel_vm.get_ssh_ansible_master(retry=False) ssh_ok = False if ssh: ssh_ok = ssh.test_connectivity(time_out=2) if ssh_ok: # if it is the master do not make the ssh command if sel_inf.vm_master and int( vmid) == sel_inf.vm_master.creation_im_id: logger.debug( "Step 2: Is the master do no make ssh command." ) info = "true" else: # if this vm is connected with the master directly do not make it also if sel_vm.isConnectedWith(sel_inf.vm_master): logger.debug( "Step 2: Is connected with the master do no make ssh command." ) info = "true" else: info = sel_vm.get_ssh_command() else: info = "wait" logger.debug("Step 2 command for vm ID: %s is %s" % (vmid, info)) else: info = None else: info = InfrastructureManager.GetVMProperty(infid, vmid, prop, auth) if info is None: return return_error( 404, "Incorrect property %s for VM ID %s" % (prop, vmid)) else: return format_output(info, field_name=prop) except DeletedInfrastructureException as ex: return return_error(404, "Error Getting VM. property: %s" % ex.args[0]) except IncorrectInfrastructureException as ex: return return_error(404, "Error Getting VM. property: %s" % ex.args[0]) except UnauthorizedUserException as ex: return return_error(403, "Error Getting VM. property: %s" % ex.args[0]) except DeletedVMException as ex: return return_error(404, "Error Getting VM. property: %s" % ex.args[0]) except IncorrectVMException as ex: return return_error(404, "Error Getting VM. property: %s" % ex.args[0]) except Exception as ex: logger.exception("Error Getting VM property") return return_error(400, "Error Getting VM property: %s" % ex.args[0])
def RESTGetVMProperty(infid=None, vmid=None, prop=None): try: auth = get_auth_header() except Exception: return return_error(401, "No authentication data provided") try: if prop == 'contmsg': info = InfrastructureManager.GetVMContMsg(infid, vmid, auth) elif prop == 'command': auth = InfrastructureManager.check_auth_data(auth) sel_inf = InfrastructureManager.get_infrastructure(infid, auth) step = 1 if "step" in bottle.request.params.keys(): step = int(bottle.request.params.get("step")) if step == 1: url = get_full_url('/infrastructures/' + str(infid) + '/vms/' + str(vmid) + '/command?step=2') auth = sel_inf.auth.getAuthInfo("InfrastructureManager")[0] if 'token' in auth: imauth = "token = %s" % auth['token'] else: imauth = "username = %s; password = %s" % ( auth['username'], auth['password']) command = ( 'curl --insecure -s -H "Authorization: type = InfrastructureManager; %s" ' '-H "Accept: text/plain" %s' % (imauth, url)) ps_command = "ps aux | grep -v grep | grep 'ssh -N -R'" info = """ res="wait" while [ "$res" == "wait" ] do res=`%s` if [ "$res" != "wait" ] then echo "$res" > /var/tmp/reverse_ssh.sh chmod a+x /var/tmp/reverse_ssh.sh /var/tmp/reverse_ssh.sh if [ "$res" != "true" ] then echo "*/1 * * * * root %s || /var/tmp/reverse_ssh.sh" > /etc/cron.d/reverse_ssh fi else sleep 20 fi done""" % (command, ps_command) logger.debug("Step 1 command: %s" % info) elif step == 2: sel_vm = None for vm in sel_inf.get_vm_list(): if vm.creation_im_id == int(vmid): sel_vm = vm break if not sel_vm: # it sometimes happen when the VM is in creation state logger.warn("Specified vmid in step2 is incorrect!!") info = "wait" else: ssh = sel_vm.get_ssh_ansible_master(retry=False) ssh_ok = False if ssh: ssh_ok = ssh.test_connectivity(time_out=2) if ssh_ok: # if it is the master do not make the ssh command if sel_inf.vm_master and int( vmid) == sel_inf.vm_master.creation_im_id: logger.debug( "Step 2: Is the master do no make ssh command." ) info = "true" else: # if this vm is connected with the master directly do not make it also if sel_vm.isConnectedWith(sel_inf.vm_master): logger.debug( "Step 2: Is connected with the master do no make ssh command." ) info = "true" else: info = sel_vm.get_ssh_command() else: info = "wait" logger.debug("Step 2 command for vm ID: %s is %s" % (vmid, info)) else: info = None else: info = InfrastructureManager.GetVMProperty(infid, vmid, prop, auth) if info is None: return return_error( 404, "Incorrect property %s for VM ID %s" % (prop, vmid)) else: return format_output(info, field_name=prop) except DeletedInfrastructureException as ex: return return_error( 404, "Error Getting VM. property: %s" % get_ex_error(ex)) except IncorrectInfrastructureException as ex: return return_error( 404, "Error Getting VM. property: %s" % get_ex_error(ex)) except UnauthorizedUserException as ex: return return_error( 403, "Error Getting VM. property: %s" % get_ex_error(ex)) except DeletedVMException as ex: return return_error( 404, "Error Getting VM. property: %s" % get_ex_error(ex)) except IncorrectVMException as ex: return return_error( 404, "Error Getting VM. property: %s" % get_ex_error(ex)) except Exception as ex: logger.exception("Error Getting VM property") return return_error(400, "Error Getting VM property: %s" % get_ex_error(ex))