def read_response(self, result_obj): """ Read response and convert it into schema object 1) It does not make any REST/CLI call, just fetches data from an already executed create call 2) It always return schema_object irrespective of command execution failure or data parsing failure (because tools like ./dt give out "" stdout when successfully executed) 3) In case of async call it sets the result object before setting the schema object @param result_obj @return schema obj """ response = result_obj.response if self.execution_type == "async": response.status = response.channel.recv_exit_status() result_obj.set_status_code(response.status) self.set_result(result_obj, "sync") schema_object = self.get_schema_object() pylogger.debug("response status is %s " % response.status) # response.read() actually polls the stdout and reads the data, thus exec_command is used # as sync call, while response.read() is used as the blocking call to read the output payload = response.read() pylogger.debug("response data is %s " % payload) if payload != None: schema_object.set_data(payload, self.accept_type) else: return schema_object return schema_object
def enable_passwordless_access(self): """ Method to make request call """ # Workflow # Check if your public key exists # If yes not need to create, else create it # Copy the public key to remote machine for passwordless acess public_key = self.get_rsa_public_key() if re.search('ssh-rsa', public_key) is None: self.generate_rsa_keys() public_key = self.get_rsa_public_key() # Copy the public key to the remote server stdin, stdout, stderr = self.sshconn.exec_command('echo $HOME') remote_home = stdout.read().strip() sftp = self.sshconn.open_sftp() try: sftp.mkdir(remote_home + '/.ssh') except IOError: pass remote_authorized_keys_file = "%s%s" % (remote_home, '/.ssh/authorized_keys') remote_file_handle = sftp.open(remote_authorized_keys_file, 'a+') if re.search(public_key, remote_file_handle.read()) is None: pylogger.debug("Public key copied to remote machine") remote_file_handle.write(public_key) else: pylogger.debug("Public keys already exists in %s on remote machine" % (remote_authorized_keys_file)) remote_file_handle.close()
def append_test_options(self, py_dict): '''Loop through all the testoptions (i.e. keys of IOWorkload) and create a string of testoptions @param py_dict: @return string containg tool test options ''' pylogger.debug("User sent test options %s:" % py_dict) if 'testduration' in py_dict: testoptions = "runtime=%s " % (py_dict['testduration']) else: testoptions = "%s%s " % ("runtime=", "600") testoptions = " of=/tmp/dt_noprog_workfile-fsync " + \ "limit=1g " + \ "oncerr=abort " + \ "disable=pstats " + \ "enable=fsync " + \ "flags=direct " + \ "oflags=trunc " + \ "errors=1 " + \ "dispose=keep " + \ "pattern=10 " + \ "iotype=sequential " + \ "enable=noprog " + \ "noprogt=15s " + \ "noprogtt=1800s " + \ "alarm=3s " + \ "trigger=cmd\:dt_noprog_script.bash " + \ "log=/tmp/dt-fsync.log " + \ "history=3 " + \ "enable=syslog " + \ testoptions return testoptions
def generate_rsa_keys(self): """ Method to make request call @return http response object """ command = 'echo -e \'y\n\'|ssh-keygen -q -t rsa -N \"\" -f ~/.ssh/id_rsa' (output, stdout, stderr) = utility.run_command_sync(command) pylogger.debug('generate_rsa_keys stdout: %s\n \ stderr:%s\n output:%s' % (stdout, stderr, output))
def remove_outdated_ssh_fingerprint(self, remove_host): """ Method to remove outdated ssh fingerprint for given host @return http response object """ command = '%s %s' % ('ssh-keygen -R', remove_host) (output, stdout, stderr) = utility.run_command_sync(command) pylogger.debug('remove_outdated_ssh_fingerprint stdout: %s\n \ stderr:%s\n output:%s' % (stdout, stderr, output))
def build_tool_command(self): """ Build tool command using path and tool binary @return tool command string """ tool_binary = self.get_tool_binary() pylogger.debug("tool_binary:%s" % tool_binary) tool_path = self.get_tool_path() pylogger.debug("tool_path:%s" % tool_path) return '%s%s' % (tool_path, tool_binary)
def get_rsa_public_key(self): """ Method to make request call @return http response object """ command = 'echo \'\'|ssh-keygen -y' (output, stdout, stderr) = utility.run_command_sync(command) pylogger.debug('get_rsa_public_key stdout: %s\n \ stderr:%s\n output:%s' % (stdout, stderr, output)) return stdout
def get_session_result(self, cli_client): """ get_session_result returns schema object for that client @param cli_client: @return schema object """ pylogger.debug("Getting session Result") schema_obj = cli_client.read_response(self.result_obj) if self.result_obj.status_code != int(0): # command execution failed pylogger.info("command execution failed with reason:%s" % self.result_obj.reason) return self.result_obj return schema_obj
def call_session(self, py_dict): """ Wrapper method for startsession, stopsession, getsession result @param py_dict containing all keywords controller the tool behavior @return result obj or schema object """ pylogger.debug( "Called DiskIO session with operation {0:s}".format(py_dict)) operation = py_dict['operation'] cli_client = self.create_cli_client() if (operation.lower() == "startsession"): ssh_connection = connection.Connection(self.test_disk.controlip, self.test_disk.username, self.test_disk.password, "None", "ssh") pylogger.debug("Created new ssh_connection %s" % ssh_connection) cli_client.set_connection(ssh_connection.anchor) return self.start_session(cli_client) if (operation.lower() == "stopsession"): return self.stop_session(cli_client) if (operation.lower() == "getsessionresult"): return self.get_session_result(cli_client)
def call_session(self, py_dict): """ Wrapper method for startsession, stopsession, getsession result @param py_dict containing all keywords controller the tool behavior @return result obj or schema object """ pylogger.debug("Called DiskIO session with operation {0:s}" . format(py_dict)) operation = py_dict['operation'] cli_client = self.create_cli_client() if (operation.lower() == "startsession"): ssh_connection = connection.Connection(self.test_disk.controlip, self.test_disk.username, self.test_disk.password, "None", "ssh") pylogger.debug("Created new ssh_connection %s" % ssh_connection) cli_client.set_connection(ssh_connection.anchor) return self.start_session(cli_client) if (operation.lower() == "stopsession"): return self.stop_session(cli_client) if (operation.lower() == "getsessionresult"): return self.get_session_result(cli_client)
def start_session(self, cli_client): """ Start session by calling the cli client to execute params @param cli_client: @return result obj """ pylogger.debug("Starting session in %s mode" % self.call_type) # We use create because create is a async call, we can read # the data later, thus create provides flexibility self.result_obj = cli_client.create() if self.call_type == 'sync': if self.result_obj.status_code != int(0): # command execution failed pylogger.info("command execution failed with reason:%s" % self.result_obj.reason) return self.result_obj else: return self.get_session_result(cli_client) else: # Return the result obj to calling layer as there is no schema object obtained # at this point in async mode return self.result_obj
def initialize_session(self, py_dict): """ Initialize session by building tool command, tool options setting it as endpoint for the cli client, setting call type etc @param py_dict containing all testoptions to run the tool with """ pylogger.debug("Initializing session ...") tool_command = self.build_tool_command() # Now get the options pylogger.debug("tool_command: %s" % tool_command) tool_options = self.append_test_options(py_dict) pylogger.debug("tool_options: %s" % tool_options) self.end_point = '%s%s' % (tool_command, tool_options) if 'executiontype' in py_dict and py_dict['executiontype'] == "async": self.call_type = "async"
def __init__(self, py_dict): """ Constructor for BaseDiskIOSession @param py_dict containing testdisk which is a py_dict of node attributes @return obj of BaseDiskIOSession """ self.id = None self.test_disk = None self.schema_class = "" self.call_type = "sync" self.end_point = None pylogger.debug("user sent py_dict %s" % py_dict) if 'testdisk' in py_dict.keys(): self.test_disk = Node(py_dict['testdisk']) else: pylogger.debug("testdisk missing in BaseDiskIOSession constructor") pylogger.debug("testdisk %s" % self.test_disk) self.initialize_session(py_dict)
def append_test_options(self): """ Loop through all the testoptions (i.e. keys of IOWorkload) and create a string of testoptions """ pylogger.debug("Append test options")
def stop_session(self): """ Stop session by calling the cli client to execute kill command """ pylogger.debug("stop_session Not yet Implemented")
from vsm import VSM class ReplicationStatus(vsm_client.VSMClient): def __init__(self, vsm=None): """ Constructor to create IPSet object @param vsm object on which Replication status has to be checked """ super(ReplicationStatus, self).__init__() self.log = logger.setup_logging(self.__class__.__name__) self.schema_class = 'vsm_replication_status_schema.ReplicationStatusSchema' self.set_connection(vsm.get_connection()) conn = self.get_connection() self.set_create_endpoint("universalsync/status") self.id = None self.update_as_post = False if __name__ == '__main__': from vmware.common.global_config import pylogger vsm_obj = VSM("10.110.25.146", "admin", "default", "") replication_status_client = ReplicationStatus(vsm_obj) result_obj = replication_status_client.read() pylogger.debug("LastSyncTime: %s" % result_obj.nsxManagersStatusList[0].lastSuccessfulSyncTime) pylogger.debug("VSM ID: %s" % result_obj.nsxManagersStatusList[0].vsmId) pylogger.debug("SycnState: %s " % result_obj.nsxManagersStatusList[0].syncState)