def guest_deploy(self, userid, image_name, transportfiles=None, remotehost=None, vdev=None): """ Deploy image and punch config driver to target """ # Get image location (TODO: update this image location) image_file = "/var/lib/zvmsdk/images/" + image_name # Unpack image file to root disk vdev = vdev or CONF.zvm.user_root_vdev cmd = ['/opt/zthin/bin/unpackdiskimage', userid, vdev, image_file] (rc, output) = zvmutils.execute(cmd) if rc != 0: err_msg = ("unpackdiskimage failed with return code: %d." % rc) output_lines = output.split('\n') for line in output_lines: if line.__contains__("ERROR:"): err_msg += ("\\n" + line.strip()) LOG.error(err_msg) raise exception.ZVMGuestDeployFailed(userid=userid, msg=err_msg) # Purge guest reader to clean dirty data rd = ("changevm %s purgerdr" % userid) with zvmutils.expect_request_failed_and_reraise( exception.ZVMGuestDeployFailed, userid=userid): self._request(rd) # Punch transport files if specified if transportfiles: # Copy transport file to local try: tmp_trans_dir = tempfile.mkdtemp() local_trans = tmp_trans_dir + '/cfgdrv' if remotehost: cmd = [ "/usr/bin/scp", "-B", ("%s:%s" % (remotehost, transportfiles)), local_trans ] else: cmd = ["/usr/bin/cp", transportfiles, local_trans] (rc, output) = zvmutils.execute(cmd) if rc != 0: err_msg = ("copy config drive to local failed with" "return code: %d." % rc) LOG.error(err_msg) raise exception.ZVMGuestDeployFailed(userid=userid, msg=err_msg) # Punch config drive to guest userid rd = ("changevm %(uid)s punchfile %(file)s --class X" % { 'uid': userid, 'file': local_trans }) with zvmutils.expect_request_failed_and_reraise( exception.ZVMGuestDeployFailed, userid=userid): self._request(rd) finally: # remove the local temp config drive folder self._pathutils.clean_temp_folder(tmp_trans_dir)
def get_vswitch_list(self): smut_userid = zvmutils.get_smut_userid() rd = ' '.join( ("SMAPI %s API Virtual_Network_Vswitch_Query" % smut_userid, "--operands", "-s \'*\'")) with zvmutils.expect_request_failed_and_reraise( exception.ZVMNetworkError): try: result = self._request(rd) except exception.ZVMClientRequestFailed as err: emsg = err.format_message() if ((err.results['rc'] == 212) and (err.results['rs'] == 40)): LOG.warning("No Virtual switch in the host") return [] else: raise exception.ZVMNetworkError( msg=("Failed to query vswitch list, %s") % emsg) if (not result['response'] or not result['response'][0]): return [] else: data = '\n'.join([ s for s in result['response'] if isinstance(s, const._TSTR_OR_TUNI) ]) output = re.findall('VSWITCH: Name: (.*)', data) return output
def _couple_nic(self, userid, vdev, vswitch_name, active=False): """Couple NIC to vswitch by adding vswitch into user direct.""" requestData = ' '.join( ('SMAPI %s' % userid, "API Virtual_Network_Adapter_Connect_Vswitch_DM", "--operands", "-v %s" % vdev, "-n %s" % vswitch_name)) with zvmutils.expect_request_failed_and_reraise( exception.ZVMNetworkError): self._request(requestData) # the inst must be active, or this call will failed if active: requestData = ' '.join( ('SMAPI %s' % userid, 'API Virtual_Network_Adapter_Connect_Vswitch', "--operands", "-v %s" % vdev, "-n %s" % vswitch_name)) try: self._request(requestData) except (exception.ZVMClientRequestFailed, exception.ZVMClientInternalError) as err1: results1 = err1.results msg1 = err1.format_message() if ((results1 is not None) and (results1['rc'] == 204) and (results1['rs'] == 20)): LOG.warning( "Virtual device %s already connected " "on the active guest system", vdev) else: persist_OK = True requestData = ' '.join( ('SMAPI %s' % userid, 'API Virtual_Network_Adapter_Disconnect_DM', "--operands", '-v %s' % vdev)) try: self._request(requestData) except (exception.ZVMClientRequestFailed, exception.ZVMClientInternalError) as err2: results2 = err2.results msg2 = err2.format_message() if ((results2 is not None) and (results2['rc'] == 212) and (results2['rs'] == 32)): persist_OK = True else: persist_OK = False if persist_OK: msg = ("Failed to couple nic %s to vswitch %s " "on the active guest system, %s") % ( vdev, vswitch_name, msg1) else: msg = ("Failed to couple nic %s to vswitch %s " "on the active guest system, %s, and " "failed to revoke user direct's changes, " "%s") % (vdev, vswitch_name, msg1, msg2) raise exception.ZVMNetworkError(msg) """Update information in switch table.""" self._NetDbOperator.switch_updat_record_with_switch( userid, vdev, vswitch_name)
def _create_nic(self, userid, vdev, nic_id=None, mac_addr=None, active=False): requestData = ' '.join( ('SMAPI %s API Virtual_Network_Adapter_Create_Extended_DM' % userid, "--operands", "-k image_device_number=%s" % vdev, "-k adapter_type=QDIO")) if mac_addr is not None: mac = ''.join(mac_addr.split(':'))[6:] requestData += ' -k mac_id=%s' % mac with zvmutils.expect_request_failed_and_reraise( exception.ZVMNetworkError): self._request(requestData) if active: if mac_addr is not None: LOG.warning("Ignore the mac address %s when " "adding nic on an active system" % mac_addr) requestData = ' '.join( ('SMAPI %s API Virtual_Network_Adapter_Create_Extended' % userid, "--operands", "-k image_device_number=%s" % vdev, "-k adapter_type=QDIO")) try: self._request(requestData) except (exception.ZVMClientRequestFailed, exception.ZVMClientInternalError) as err1: msg1 = err1.format_message() persist_OK = True requestData = ' '.join( ('SMAPI %s API Virtual_Network_Adapter_Delete_DM' % userid, "--operands", '-v %s' % vdev)) try: self._request(requestData) except exception.ZVMClientRequestFailed as err2: results = err2.results msg2 = err2.format_message() if ((results['rc'] == 404) and (results['rs'] == 8)): persist_OK = True else: persist_OK = False if persist_OK: msg = ("Failed to create nic %s for %s on the active " "guest system, %s") % (vdev, userid, msg1) else: msg = ("Failed to create nic %s for %s on the active " "guest system, %s, and failed to revoke user " "direct's changes, %s") % (vdev, userid, msg1, msg2) raise exception.ZVMNetworkError(msg) self._NetDbOperator.switch_add_record_for_nic(userid, vdev, port=nic_id)
def set_vswitch_port_vlan_id(self, vswitch_name, userid, vlan_id): smut_userid = zvmutils.get_smut_userid() rd = ' '.join( ("SMAPI %s API Virtual_Network_Vswitch_Set_Extended" % smut_userid, "--operands", "-k grant_userid=%s" % userid, "-k switch_name=%s" % vswitch_name, "-k user_vlan_id=%s" % vlan_id, "-k persist=YES")) with zvmutils.expect_request_failed_and_reraise( exception.ZVMNetworkError): self._request(rd)
def delete_nic(self, userid, vdev, active=False): rd = ' '.join( ("SMAPI %s API Virtual_Network_Adapter_Delete_DM" % userid, '-v %s' % vdev)) with zvmutils.expect_request_failed_and_reraise( exception.ZVMNetworkError): try: self._request(rd) except exception.ZVMClientRequestFailed as err: results = err.results emsg = err.format_message() if ((results['rc'] == 404) and (results['rs'] == 8)): LOG.warning( "Virtual device %s does not exist in " "the guest's user direct", vdev) else: raise exception.ZVMNetworkError( msg=("Failed to delete nic %s for %s in " "the guest's user direct, %s") % (vdev, userid, emsg)) self._NetDbOperator.switch_delete_record_for_nic(userid, vdev) if active: rd = ' '.join( ("SMAPI %s API Virtual_Network_Adapter_Delete" % userid, '-v %s' % vdev)) with zvmutils.expect_request_failed_and_reraise( exception.ZVMNetworkError): try: self._request(rd) except exception.ZVMClientRequestFailed as err: results = err.results emsg = err.format_message() if ((results['rc'] == 204) and (results['rs'] == 8)): LOG.warning( "Virtual device %s does not exist on " "the active guest system", vdev) else: raise exception.ZVMNetworkError( msg=("Failed to delete nic %s for %s on " "the active guest system, %s") % (vdev, userid, emsg))
def revoke_user_from_vswitch(self, vswitch_name, userid): """Revoke user for vswitch.""" smut_userid = zvmutils.get_smut_userid() requestData = ' '.join( ('SMAPI %s API Virtual_Network_Vswitch_Set_Extended' % smut_userid, "--operands", "-k switch_name=%s" % vswitch_name, "-k revoke_userid=%s" % userid, "-k persist=YES")) with zvmutils.expect_request_failed_and_reraise( exception.ZVMNetworkError): self._request(requestData)
def set_vswitch(self, switch_name, **kwargs): """Set vswitch""" smut_userid = zvmutils.get_smut_userid() rd = ' '.join( ("SMAPI %s API Virtual_Network_Vswitch_Set_Extended" % smut_userid, "--operands", "-k switch_name=%s" % switch_name)) for k, v in kwargs.items(): rd = ' '.join((rd, "-k %(key)s=\'%(value)s\'" % { 'key': k, 'value': v })) with zvmutils.expect_request_failed_and_reraise( exception.ZVMNetworkError): self._request(rd)
def add_vswitch(self, name, rdev=None, controller='*', connection='CONNECT', network_type='IP', router="NONROUTER", vid='UNAWARE', port_type='ACCESS', gvrp='GVRP', queue_mem=8, native_vid=1, persist=True): smut_userid = zvmutils.get_smut_userid() rd = ' '.join(("SMAPI %s API Virtual_Network_Vswitch_Create_Extended" % smut_userid, "--operands", '-k switch_name=%s' % name)) if rdev is not None: rd += " -k real_device_address" +\ "=\'%s\'" % rdev.replace(',', ' ') if controller != '*': rd += " -k controller_name=%s" % controller rd = ' '.join( (rd, "-k connection_value=%s" % connection, "-k queue_memory_limit=%s" % queue_mem, "-k transport_type=%s" % network_type, "-k vlan_id=%s" % vid, "-k persist=%s" % (persist and 'YES' or 'NO'))) # Only if vswitch is vlan awared, port_type, gvrp and native_vid are # allowed to specified if isinstance(vid, int) or vid.upper() != 'UNAWARE': if ((native_vid is not None) and ((native_vid < 1) or (native_vid > 4094))): raise exception.ZVMInvalidInput( msg=("Failed to create vswitch %s: %s") % (name, 'valid native VLAN id should be 1-4094 or None')) rd = ' '.join( (rd, "-k port_type=%s" % port_type, "-k gvrp_value=%s" % gvrp, "-k native_vlanid=%s" % native_vid)) if router is not None: rd += " -k routing_value=%s" % router with zvmutils.expect_request_failed_and_reraise( exception.ZVMNetworkError): self._request(rd)
def delete_vswitch(self, switch_name, persist=True): smut_userid = zvmutils.get_smut_userid() rd = ' '.join( ("SMAPI %s API Virtual_Network_Vswitch_Delete_Extended" % smut_userid, "--operands", "-k switch_name=%s" % switch_name, "-k persist=%s" % (persist and 'YES' or 'NO'))) with zvmutils.expect_request_failed_and_reraise( exception.ZVMNetworkError): try: self._request(rd) except exception.ZVMClientRequestFailed as err: results = err.results emsg = err.format_message() if ((results['rc'] == 212) and (results['rs'] == 40)): LOG.warning("Vswitch %s does not exist", switch_name) return else: raise exception.ZVMNetworkError( msg=("Failed to delete vswitch %s: %s") % (switch_name, emsg))