def _exec_scp(self): client = ParallelSSHClient(self.hosts) output = client.copy_file(self.source, self.destination, True) joinall(output, raise_error=True) nice_output = dict() for host in output: nice_output[host] = {'stdout': [], 'stderr': []} return nice_output
class BaseTransporter(object): '''The more important class, this is like the core of the fabric clone: it allows to open a tunnel from the (usually) local computer to a certain numbers of remote endpoints. This class should implement the functions needed to act upon the remote system, as filesystem-like commands (cd, mkdir, rm) >>> transporter = Transporter(hosts=hosts) >>> with transporter.cd('/var/www/'): >>> transporter.mkdir(web_root_dir) ''' def __init__(self, hosts=None): if not hosts: raise Exception('hosts parameter must be defined') self.hosts = hosts self.client = ParallelSSHClient(hosts) self._initial_connection() def _parse_output(self, output): '''This parse for our usage the output of the execution''' def _log_channel_output(self, output, prefix=None): for host in output: for stdout in output[host]['stdout']: logger.info('%s%s' % (prefix, stdout)) for stderr in output[host]['stderr']: logger.warn('%s%s' % (prefix, stderr)) def cd(self, path): # handle absolute or relative paths # make clear where the action is done # TODO: context manager pass def mkdir(self, path): pass def rm(self, path): pass def cp(self, src, dst, **kwargs): # src can be local # dst must be remote logger.debug('cp %s -> %s' % (src, dst)) events = self.client.copy_file(src, dst) for event in events: event.get() def _initial_connection(self): '''The ratio of this method is that we need to connect immediately to the remote endpoints in order to be sure that hosts are accessible. AuthenticationException, UnknownHostException and ConnectionErrorException are possible only at the first attempt (TODO: check, I'm not sure for ConnectionErrorException, if the server lost connection?). ''' try: self._log_channel_output(self.client.run_command('uname -a'), prefix=' --- start connection: ') except (AuthenticationException, UnknownHostException, ConnectionErrorException) as e: logger.exception(e) raise e def _exec_command(self, cmd): return self.client.run_command(cmd) def run(self, cmd): return self._exec_command(cmd)
#!/usr/bin/env python from __future__ import print_function from pssh.pssh_client import ParallelSSHClient from pssh.utils import enable_logger, logger from gevent import joinall enable_logger(logger) hosts = ['9.114.120.103'] client = ParallelSSHClient(hosts, user='******', pkey='xxx') cmds = client.copy_file('/tmp/test.txt', '/tmp/test.txt') joinall(cmds, raise_error=True)
# write ip to node mapping to config file config_parser.add_section('Node IP') config_parser.add_section('IP Node') for node, ip in node_ip_mapping.items(): config_parser.set('IP Node', ip, node) config_parser.set('Node IP', node, ip) with open(NODE_CONFIG, 'wb') as configfile: config_parser.write(configfile) hosts = node_ip_mapping.values() # ssh commands pkey = load_private_key(HOME + '/.ssh/id_rsa_pis') client = ParallelSSHClient(hosts, user='******', pkey=pkey) # put node config client.copy_file(NODE_CONFIG, 'node.cfg') def start_server(ips): server_hosts = ParallelSSHClient(ips, user='******', pkey=pkey) servers_outputs = server_hosts.run_command( 'bash $HOME/automate/tools/scripts/run_system.sh server') ssh_client_output(servers_outputs) def start_client(ips): client_host = ParallelSSHClient(ips, user='******', pkey=pkey) client_outputs = client_host.run_command( 'bash $HOME/automate/tools/scripts/run_system.sh') ssh_client_output(client_outputs)
master_client = ParallelSSHClient([master_public], pkey=priv_key) slave_client = ParallelSSHClient(slaves, pkey=priv_key) # kill any locust processes print("killing any 'locust' processes in cluster...") master_output = master_client.run_command('killall -q locust', sudo=True) slave_output = slave_client.run_command('killall -q locust', sudo=True) master_client.join(master_output) slave_client.join(slave_output) print(" done") # SFTP latest locust file to all locust instances print("copying latest mqtt-locustfile.py to all cluster instances") master_greenlets = master_client.copy_file('mqtt-locustfile.py', 'mqtt-locust/eecs397-locustfile.py') slave_greenlets = slave_client.copy_file('mqtt-locustfile.py', 'mqtt-locust/eecs397-locustfile.py') joinall(master_greenlets, raise_error=True) joinall(slave_greenlets, raise_error=True) print(" done") # Start locust print("starting locust on master and slave instances") # master_cmd = ("cd ~/mqtt-locust;" # " locust --master --host={}").format(target_public) # master_output = master_client.run_command(master_cmd, sudo=True) slave_cmd = ("cd ~/mqtt-locust; locust --slave --master-host={}"