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 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 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()))