def send_and_execute_file(self, commands, blocking): generated_uuid = str(uuid.uuid4()) path_to_sh_file = os.path.join(os.path.dirname(__file__), 'temp', generated_uuid + '.sh') path_to_remote_sh_file = os.path.join(self.project_path, generated_uuid + '.sh') with open(path_to_sh_file, 'w') as shell_file: shell_file.write("#!/bin/sh\n") for line in commands: shell_file.write("%s\n" % line) shell_file.write("rm -rf %s\n" % path_to_remote_sh_file) # Copy Shell file with jobs to execute with ShellHandler(self.host, self.username, self.password, self.port, self.tunnel_host, self.tunnel_username, self.tunnel_password, self.tunnel_port, self.use_tunnel, 10025, self.ssh_key_path) as handler: scp = SCPClient(handler.get_ssh_client().get_transport()) scp.put(path_to_sh_file, remote_path=b'%s' % str.encode(path_to_remote_sh_file)) # Delete local file subprocess.run(['rm', '-rf', path_to_sh_file]) # Make remote file executable self.execute_command('chmod +x %s' % path_to_remote_sh_file) # Execute. We need the variable order_needed as it distighushes between two separate possible execution methods logger.info("Execute command: %s" % path_to_remote_sh_file) if blocking: with ShellHandler(self.host, self.username, self.password, self.port, self.tunnel_host, self.tunnel_username, self.tunnel_password, self.tunnel_port, self.use_tunnel, 10026, self.ssh_key_path) as handler: out = handler.execute_file(path_to_remote_sh_file, True) logger.debug('Output: %s' % ' '.join(out)) return out else: thread = JobSubmissionThread( path_to_remote_sh_file, self.host, self.username, self.password, self.port, self.tunnel_host, self.tunnel_username, self.tunnel_password, self.tunnel_port, self.use_tunnel, self.ssh_key_path) thread.start() return None
def run(self): with ShellHandler(self.host, self.username, self.password, self.port, self.tunnel_host, self.tunnel_username, self.tunnel_password, self.tunnel_port, self.use_tunnel, 10020) as handler: out, err = handler.execute_file(self.remote_file, False) logger.debug(out) logger.debug(err)
def execute_command(self, command, ignore_errors=False): logger.info('Execute command: %s' % command) with ShellHandler(self.host, self.username, self.password, self.port, self.tunnel_host, self.tunnel_username, self.tunnel_password, self.tunnel_port, self.use_tunnel, 10024) as handler: (stdout, stderr) = handler.execute(command) logger.debug('Output: %s' % ' '.join(stdout)) logger.debug('ErrorOut: %s' % ' '.join(stderr)) if stderr and not ignore_errors: raise Exception('Error in executing command %s! Error: %s.' % (command, ','.join(stderr))) return stdout
def __init__(self): self.username = HPC['username'] self.password = HPC['password'] self.host = HPC['host'] self.port = HPC['port'] self.queue = HPC['queue'] self.node_properties = HPC['node_properties'] self.tunnel_username = HPC['ssh_tunnel_username'] self.tunnel_password = HPC['ssh_tunnel_password'] self.tunnel_host = HPC['ssh_tunnel_host'] self.tunnel_port = HPC['ssh_tunnel_port'] self.logger = logging.getLogger(__name__) self.ssh = ShellHandler(self.host, self.username, self.password, self.port, self.tunnel_host, self.tunnel_username, self.tunnel_password, self.tunnel_port)
def copy_project_tar(self): with ShellHandler(self.host, self.username, self.password, self.port, self.tunnel_host, self.tunnel_username, self.tunnel_password, self.tunnel_port, self.use_tunnel, 10023) as handler: scp = SCPClient(handler.get_ssh_client().get_transport()) # Copy plugin scp.put('tmp.tar.gz', remote_path=b'~') # Untar plugin self.execute_command('mkdir %s' % self.project_path) self.execute_command('tar -C %s -xvf ~/tmp.tar.gz' % self.project_path) # Delete tar self.execute_command('rm -f ~/tmp.tar.gz')
def get_error_log(self, job): with ShellHandler(self.host, self.username, self.password, self.port, self.tunnel_host, self.tunnel_username, self.tunnel_password, self.tunnel_port, self.use_tunnel, 10022) as handler: sftp_client = handler.get_ssh_client().open_sftp() plugin_execution_output_path = os.path.join(self.log_path, str(job.plugin_execution.id)) try: remote_file = sftp_client.open(os.path.join(plugin_execution_output_path, str(job.id)+'_err.txt')) except FileNotFoundError: return ['File Not Found'] output = [] try: for line in remote_file: output.append(line.strip()) finally: remote_file.close() return output
def copy_plugin(self, plugin): with ShellHandler(self.host, self.username, self.password, self.port, self.tunnel_host, self.tunnel_username, self.tunnel_password, self.tunnel_port, self.use_tunnel, 10023) as handler: scp = SCPClient(handler.get_ssh_client().get_transport()) # Copy plugin scp.put(plugin.get_full_path_to_archive(), remote_path=b'~') try: self.delete_plugin(plugin) except Exception: pass # Untar plugin self.execute_command('mkdir %s/%s' % (self.plugin_path, str(plugin))) self.execute_command('tar -C %s/%s -xvf %s' % (self.plugin_path, str(plugin), plugin.get_name_of_archive())) # Delete tar self.execute_command('rm -f ~/%s' % (plugin.get_name_of_archive()))