def stop(self): """ method to stop the ssh traffic process spawned by the stop method. """ client = self._conf_client if self._stats == 'started': transfer_time = int(self._timeout) logger.info('Time to transfer: {} seconds'.format(transfer_time)) sleeptime = 10 elapsedtime = 0 while self._stats != 'completed' and elapsedtime <= transfer_time: if int(transfer_time / 60) != 0: sleeptime = sleeptime * int(transfer_time / 60) time.sleep(sleeptime) self.get_stats() elapsedtime += sleeptime sleeptime += 10 if self._stats != 'completed': logger.error('Ssh traffic not finished') else: logger.info('Ssh traffic finished') if self._outfile is not None: self.get_stats() try: client.exec_command('sudo rm -f {}'.format(self._outfile)) except AttributeError as e: logger.warning(e) super(Ssh, self).__del__()
def power_off_vm(self, vmname): """ method to power off given VM. Args: :vmname (str) :name of vm that user wants to abstract from vm object list Return: :result (bool) True: If Successfully OFF. False: If Fail to VM Power OFF. Raise: vMNotFound Exception. """ try: vm_obj = self._get_vm_object_by_name(vmname) logger.info("VM {} is \ {}".format(vmname, vm_obj.runtime.powerState)) if (self.is_vm_power_on(vm_obj.name)): power_off_vm_task = vm_obj.PowerOffVM_Task() return self._wait_for_tasks([power_off_vm_task], task_name="Power Off VM") else: logger.warning("VM {} is already powered off, \ Can not perform this operation".format(vmname)) except vmodl.MethodFault as error: logger.error("Exception raise {}".format(error.msg)) return error.msg
def stop(self, *args, **kwargs): """ method to stop the icmp traffic process spawned by the start method. It checks if the ping is complete. It does a post transfer cleanup, and sets the traffic started boolean to false. It calls the del function to de-link references to the instances created by the traffic object, this also forces any open paramiko channels to the remote devices to be closed. """ client = self._conf_client if self._stats == 'started': logger.info('Time to ping: {} seconds'.format(self._timeout)) sleeptime = 1 elapsedtime = 0 while self._stats != 'completed' and elapsedtime <= self._timeout: time.sleep(sleeptime) self.get_stats() elapsedtime += sleeptime if self._stats != 'completed': logger.error('Icmp traffic not finished') else: logger.info('Icmp traffic finished') if self._outfile is not None: self.get_stats() try: client.exec_command('sudo rm -f {}'.format(self._outfile)) except AttributeError as e: logger.warning(e) super(Icmp, self).__del__()
def stop(self, *args, **kwargs): """ method to stop the ftp traffic process spawned by the start method. It checks if the file transfer is complete. It then stops the vsftpd daemon, does a post transfer cleanup, and sets the traffic started boolean to false. It calls the del function to de-link references to the instances created by the traffic object, this also forces any open paramiko channels to the remote devices to be closed. """ client = self._conf_client server = self._conf_server if self._stats == 'started': transfer_time = int(self._file_info[1]) / int(self._rate) logger.info('Time to transfer: {} seconds'.format(transfer_time)) sleeptime = 10 elapsedtime = 0 while self._stats != 'completed' and elapsedtime <= transfer_time: if int(transfer_time / 60) != 0: sleeptime = sleeptime * int(transfer_time / 60) time.sleep(sleeptime) self.get_stats() elapsedtime += sleeptime sleeptime += 10 if self._stats != 'completed': logger.error('Ftp traffic not finished') else: logger.info('Ftp traffic finished') if self._outfile is not None: self.get_stats() try: client.exec_command('sudo rm -f {}'.format(self._outfile)) except AttributeError as e: logger.warning(e) server.service_stop(self._service) self._transfer_cleanup_util(True, True) if self.ctr_port != 21: server.exec_command("sed -i '/listen_port/d' /etc/vsftpd.conf") server.exec_command( "sed -i '/isten=/a\listen_port=21' /etc/vsftpd.conf") output = server.exec_command('grep listen_port /etc/vsftpd.conf') server.service_stop(self._service) server.service_start(self._service) super(Ftp, self).__del__()
def stop_by_force(self, *args, **kwargs): """ method to stop the ftp traffic process spawned by force. It doesn't checks if the file transfer is complete. It will stops the vsftpd daemon by force, does a post transfer cleanup, and sets the traffic started boolean to false. It calls the del function to de-link references to the instances created by the traffic object, this also forces any open paramiko channels to the remote devices to be closed. """ client = self._conf_client server = self._conf_server if self._outfile is not None: try: client.exec_command('sudo rm -f {}'.format(self._outfile)) except AttributeError as e: logger.warning(e) server.service_stop(self._service) self._transfer_cleanup_util(True, True) super(Ftp, self).__del__()
def va_get_by_uniq_id(self, uniq_id=None, delegator=True, add_nocli_user=False): """ get the vm by unique id kwargs: uniq_id (str): unique id for the resource vm delegator (bool): if delegator version of the resource is needed (default is False) """ if add_nocli_user : self._uniq_ids.get(uniq_id).add_nocli_user=add_nocli_user if uniq_id in self._uniq_ids: if delegator: return Delegator(self._uniq_ids.get(uniq_id)) else: return self._uniq_ids.get(uniq_id) else: # TODO: raise UnknownUniqId(uniq_id) # I don't know if raise is needed, # you can just let user know uniq id not exists logger.warning("unknown uniq_id {}".format(str(uniq_id)))
def vmotion(self, vmname, hostname): """ method to apply vmware vmotion on vm. Args: :vmname (str): name of vm that user wants to abstract from vm object list. :hostname (str): name of destination host where vm Return: :result (bool) True: If Successfully completed. False: If vMotion Failed. Raise: :vmodl Exception. """ try: vm_obj = self._get_vm_object_by_name(vmname) host_obj = self._get_host_object_by_name(hostname) vm_resource_pool = None for vms in host_obj.vm: if vms.resourcePool is not None: vm_resource_pool = vms.resourcePool break else: continue if (vm_obj.summary.runtime.powerState != "poweredOn"): logger.warning( "Vmotion is applying on powered off VM:{}".format(vmname)) vm_migration_priority = \ vim.VirtualMachine.MovePriority.defaultPriority logger.info("Vmotion Start on VM: {} and destination" "host is {}".format(vmname, hostname)) vmotion_task = vm_obj.Migrate(host=host_obj, priority=vm_migration_priority, pool=vm_resource_pool) return self._wait_for_tasks([vmotion_task], task_name="Vmotion VM") except vmodl.MethodFault as error: logger.error("Exception raise {}".format(error.msg))
def va_set_debug(self, action='enable', is_reboot=True, *args, **kwargs): """ API to set debug into setup.ini file support debug for dir: 'agent', 'konfd', 'vadb', 'cn', 'chasd', 'io', 're', 'sn' support debug for cp : 'agent', 'konfd', 'vadb' support debug for ep : 'agent', 'konfd', 'vadb', 'io', 'sn' support debug for epi: 'agent', 'konfd', 'vadb', 'turbo' if 'name' is not specified, will be enable/disable all debugs based on device type param : action : enable/disable debug is_reboot : reboot device, True by default : kwargs : dict kwargs = { 'name' : name of debug switch, such as 'agent' } return: :bool - True on success or False on failure: example : va_set_debug(action=disable) va_set_debug(action=1) va_set_debug(name='vadb') """ logger.info("\nIn subroutine: " + sys._getframe().f_code.co_name) if action.lower() == 'enable': debug_value = 1 elif action.lower() == 'disable': debug_value = 0 else: raise ValueError( 'Action "{}" not support, should be "disable/enable"'.format( action)) set_debug_name = list() debug_map = { 'agent': 'enable_agent_debug', 'chasd': 'enable_chasd_debug', 'cn': 'enable_cn_debug', 'io': 'enable_io_debug', 'konfd': 'enable_konfd_debug', 're': 'enable_re_debug', 'sn': 'enable_sn_debug', 'turbo': 'enable_turbo_debug', 'vadb': 'enable_vadb_debug', } if 'name' in kwargs: name = kwargs.get('name') if name not in debug_map: raise ValueError('Not support debug name: {}'.format(name)) if not isinstance(name, list): set_debug_name.append(name) else: set_debug_name = name else: device_type = self._resource.get_nodetype() set_debug_name = ['agent', 'konfd', 'vadb'] if device_type == 'dir': set_debug_name.extend(['cn', 'chasd', 'io', 're', 'sn']) elif device_type == 'ep': set_debug_name.extend(['io', 'sn']) elif device_type == 'cp': pass elif device_type == 'epi': set_debug_name.extend(['turbo']) is_updated = False debug_status_before = self.va_get_debug_status() logger.debug('Start to {} debug'.format(action)) for debug_name in set_debug_name: debug_name = debug_map.get(debug_name) check_debug = int(debug_status_before.get(debug_name, -1)) if check_debug == -1 and debug_value: opt = "sed -i \'$a{}={}\'".format(debug_name, debug_value) elif check_debug != -1 and check_debug != debug_value: opt = "sed -i 's/{}={}/{}={}/g'".format(debug_name, check_debug, \ debug_name, debug_value) else: opt = None if opt is not None: setup_ini_file = '/config-varmour/configuration/varmour_conf/setup.ini' sed_output = self._access.va_shell('{} {}'.format( opt, setup_ini_file)) is_updated = True if is_reboot and is_updated: logger.warning('Device will reboot to active debug') self.va_reboot(timeout=60, persistent=True, reconnect_delay=90) elif not is_reboot: logger.warning('Device need reboot to active debug') failure_debug_name = list() debug_status_after = self.va_get_debug_status() for debug_name in set_debug_name: check_debug = int( debug_status_after.get(debug_map.get(debug_name), -1)) if debug_value: if check_debug != debug_value: failure_debug_name.append(debug_name) elif not debug_value: if check_debug != -1 and \ check_debug != 0: failure_debug_name.append(debug_name) if len(failure_debug_name) != 0: logger.error('Failed to {} debug {}'.format( action, failure_debug_name)) return False logger.info('Succeed to {} debug'.format(action)) return True