示例#1
0
 def test_pssh_client_copy_file_failure(self):
     """Test failure scenarios of file copy"""
     test_file_data = 'test'
     local_test_path = 'directory_test'
     remote_test_path = 'directory_test_copied'
     for path in [local_test_path, remote_test_path]:
         mask = int('0700') if sys.version_info <= (2,) else 0o700
         if os.path.isdir(path):
             os.chmod(path, mask)
         for root, dirs, files in os.walk(path):
             os.chmod(root, mask)
             for _path in files + dirs:
                 os.chmod(os.path.join(root, _path), mask)
         try:
             shutil.rmtree(path)
         except OSError:
             pass
     os.mkdir(local_test_path)
     os.mkdir(remote_test_path)
     local_file_path = os.path.join(local_test_path, 'test_file')
     remote_file_path = os.path.join(remote_test_path, 'test_file')
     test_file = open(local_file_path, 'w')
     test_file.write('testing\n')
     test_file.close()
     # Permission errors on writing into dir
     mask = 0111 if sys.version_info <= (2,) else 0o111
     os.chmod(remote_test_path, mask)
     client = ParallelSSHClient([self.host], port=self.listen_port,
                                pkey=self.user_key)
     cmds = client.copy_file(local_test_path, remote_test_path, recurse=True)
     for cmd in cmds:
         try:
             cmd.get()
             raise Exception("Expected IOError exception, got none")
         except IOError:
             pass
     self.assertFalse(os.path.isfile(remote_file_path))
     # Create directory tree failure test
     local_file_path = os.path.join(local_test_path, 'test_file')
     remote_file_path = os.path.join(remote_test_path, 'test_dir', 'test_file')
     cmds = client.copy_file(local_file_path, remote_file_path, recurse=True)
     for cmd in cmds:
         try:
             cmd.get()
             raise Exception("Expected IOError exception on creating remote "
                             "directory, got none")
         except IOError:
             pass
     self.assertFalse(os.path.isfile(remote_file_path))
     mask = int('0600') if sys.version_info <= (2,) else 0o600
     os.chmod(remote_test_path, mask)
     for path in [local_test_path, remote_test_path]:
         shutil.rmtree(path)
 def test_pssh_client_copy_file_failure(self):
     """Test failure scenarios of file copy"""
     test_file_data = 'test'
     local_test_path = 'directory_test'
     remote_test_path = 'directory_test_copied'
     for path in [local_test_path, remote_test_path]:
         mask = int('0700') if sys.version_info <= (2,) else 0o700
         if os.path.isdir(path):
             os.chmod(path, mask)
         for root, dirs, files in os.walk(path):
             os.chmod(root, mask)
             for _path in files + dirs:
                 os.chmod(os.path.join(root, _path), mask)
         try:
             shutil.rmtree(path)
         except OSError:
             pass
     os.mkdir(local_test_path)
     os.mkdir(remote_test_path)
     local_file_path = os.path.join(local_test_path, 'test_file')
     remote_file_path = os.path.join(remote_test_path, 'test_file')
     test_file = open(local_file_path, 'w')
     test_file.write('testing\n')
     test_file.close()
     # Permission errors on writing into dir
     mask = int('0111') if sys.version_info <= (2,) else 0o111
     os.chmod(remote_test_path, mask)
     client = ParallelSSHClient([self.host], port=self.listen_port,
                                pkey=self.user_key)
     cmds = client.copy_file(local_test_path, remote_test_path, recurse=True)
     for cmd in cmds:
         try:
             cmd.get()
             raise Exception("Expected IOError exception, got none")
         except IOError:
             pass
     self.assertFalse(os.path.isfile(remote_file_path))
     # Create directory tree failure test
     local_file_path = os.path.join(local_test_path, 'test_file')
     remote_file_path = os.path.join(remote_test_path, 'test_dir', 'test_file')
     cmds = client.copy_file(local_file_path, remote_file_path, recurse=True)
     for cmd in cmds:
         try:
             cmd.get()
             raise Exception("Expected IOError exception on creating remote "
                             "directory, got none")
         except IOError:
             pass
     self.assertFalse(os.path.isfile(remote_file_path))
     mask = int('0600') if sys.version_info <= (2,) else 0o600
     os.chmod(remote_test_path, mask)
     for path in [local_test_path, remote_test_path]:
         shutil.rmtree(path)
示例#3
0
 def test_pssh_client_directory(self):
     """Tests copying directories with SSH client. Copy all the files from
     local directory to server, then make sure they are all present."""
     test_file_data = 'test'
     local_test_path = 'directory_test'
     remote_test_path = 'directory_test_copied'
     for path in [local_test_path, remote_test_path]:
         try:
             shutil.rmtree(path)
         except OSError:
             pass
     os.mkdir(local_test_path)
     remote_file_paths = []
     for i in range(0, 10):
         local_file_path = os.path.join(local_test_path, 'foo' + str(i))
         remote_file_path = os.path.join(remote_test_path, 'foo' + str(i))
         remote_file_paths.append(remote_file_path)
         test_file = open(local_file_path, 'w')
         test_file.write(test_file_data)
         test_file.close()
     client = ParallelSSHClient([self.host], port=self.listen_port,
                                pkey=self.user_key)
     cmds = client.copy_file(local_test_path, remote_test_path, recurse=True)
     for cmd in cmds:
         cmd.get()
     for path in remote_file_paths:
         self.assertTrue(os.path.isfile(path))
     shutil.rmtree(local_test_path)
     shutil.rmtree(remote_test_path)
示例#4
0
 def test_pssh_client_directory(self):
     """Tests copying multiple directories with SSH client. Copy all the files from
     local directory to server, then make sure they are all present."""
     test_file_data = 'test'
     local_test_path = 'directory_test'
     remote_test_path = 'directory_test_copied'
     for path in [local_test_path, remote_test_path]:
         try:
             shutil.rmtree(path)
         except OSError:
             pass
     os.mkdir(local_test_path)
     remote_file_paths = []
     for i in range(0, 10):
         local_file_path_dir = os.path.join(local_test_path, 'dir_foo' + str(i))
         os.mkdir(local_file_path_dir)
         local_file_path = os.path.join(local_file_path_dir, 'foo' + str(i))
         remote_file_path = os.path.join(remote_test_path, 'dir_foo' + str(i), 'foo' + str(i))
         remote_file_paths.append(remote_file_path)
         test_file = open(local_file_path, 'w')
         test_file.write(test_file_data)
         test_file.close()
     client = ParallelSSHClient([self.host], port=self.listen_port,
                                pkey=self.user_key)
     cmds = client.copy_file(local_test_path, remote_test_path, recurse=True)
     for cmd in cmds:
         cmd.get()
     for path in remote_file_paths:
         self.assertTrue(os.path.isfile(path))
     shutil.rmtree(local_test_path)
     shutil.rmtree(remote_test_path)
示例#5
0
def main(fn, key_file):
    with open(fn) as f:
        hosts = [item['public']
                 for item in json.loads(f.read())]  #+ sys.argv[1:]
    print len(hosts)

    if sys.argv[1:]:
        hosts = [hosts[int(idx)] for idx in sys.argv[1:]]

    print hosts
    if key_file is not None:
        client_key = paramiko.RSAKey.from_private_key_file(key_file)
        client = ParallelSSHClient(hosts, pkey=client_key, user="******")
    else:
        client = ParallelSSHClient(hosts, user="******")

    client.copy_file('setup.sh', 'setup.sh')
    client.copy_file('../.boto', '.boto')
    client.pool.join()
    print "copied"

    output = client.run_command(
        'bash setup.sh rq defects4j darioush password00 yes')
    for host in output:
        print "Host: {0}".format(host)
        PREFIX = "*** "
        filtered_lines = [
            line[len(PREFIX):] for line in output[host]['stdout']
            if line.startswith(PREFIX)
        ]
        print ' '.join(filtered_lines)
        stderr = [line for line in output[host]['stderr']]
        sys.stderr.write('\n'.join(stderr))
        assert client.get_exit_code(output[host]) == 0

    client.pool.join()

    output = client.run_command('df')
    for host in output:
        print "Host: {0}".format(host)
        filtered_lines = [line for line in output[host]['stdout']]
        print ' '.join(filtered_lines)
        stderr = [line for line in output[host]['stderr']]
        sys.stderr.write('\n'.join(stderr))
        assert client.get_exit_code(output[host]) == 0
    client.pool.join()
    print "joined"
示例#6
0
def test_parallel():
    """Perform ls and copy file with ParallelSSHClient on localhost"""
    client = ParallelSSHClient(['localhost'])
    cmds = client.exec_command('ls -ltrh')
    output = [client.get_stdout(cmd, return_buffers=True) for cmd in cmds]
    print output
    cmds = client.copy_file('../test', 'test_dir/test')
    client.pool.join()
示例#7
0
def test_parallel():
    """Perform ls and copy file with ParallelSSHClient on localhost"""
    client = ParallelSSHClient(['localhost'])
    cmds = client.exec_command('ls -ltrh')
    output = [client.get_stdout(cmd, return_buffers=True) for cmd in cmds]
    print output
    cmds = client.copy_file('../test', 'test_dir/test')
    client.pool.join()
 def test_sftp_exceptions(self):
     # Port with no server listening on it on separate ip
     host = '127.0.0.3'
     port = self.make_random_port(host=host)
     client = ParallelSSHClient([self.host], port=port, num_retries=1)
     cmds = client.copy_file("test", "test")
     client.pool.join()
     for cmd in cmds:
         self.assertRaises(ConnectionErrorException, cmd.get)
示例#9
0
def main():
    # cleanup just in case
    kill()

    # create new droplet and wait for it
    droplet = start()['droplet']
    print droplet

    while True:
        droplet = lookup(droplet['id'])

        # status
        s = droplet['status']
        assert (s in ['active', 'new'])

        # addr
        ip = None
        for addr in droplet["networks"]["v4"]:
            if addr["type"] == "public":
                ip = addr["ip_address"]

        print 'STATUS: %s, IP: %s' % (str(s), str(ip))
        if s == 'active' and ip != None:
            break

        time.sleep(3)

    hosts = [ip]
    client = ParallelSSHClient(hosts)

    client.copy_file('./test.sh', '/tmp/test.sh')

    url = 'https://raw.githubusercontent.com/open-lambda/open-lambda/master/testing/digitalocean/test.sh'
    output = client.run_command('wget %s; bash test.sh' % url, sudo=True)
    output = output.values()[0]
    for l in output["stdout"]:
        print l
    for l in output["stderr"]:
        print l

    # make sure we cleanup everything!
    kill()
示例#10
0
def main():
    # cleanup just in case
    kill()

    # create new droplet and wait for it
    droplet = start()['droplet']
    print droplet

    while True:
        droplet = lookup(droplet['id'])

        # status
        s = droplet['status']
        assert(s in ['active', 'new'])

        # addr
        ip = None
        for addr in droplet["networks"]["v4"]:
            if addr["type"] == "public":
                ip = addr["ip_address"]
        
        print 'STATUS: %s, IP: %s' % (str(s), str(ip))
        if s == 'active' and ip != None:
            break

        time.sleep(3)

    hosts = [ip]
    client = ParallelSSHClient(hosts)

    client.copy_file('./test.sh', '/tmp/test.sh')

    url = 'https://raw.githubusercontent.com/open-lambda/open-lambda/master/testing/digitalocean/test.sh'
    output = client.run_command('wget %s; bash test.sh' % url, sudo=True)
    output = output.values()[0]
    for l in output["stdout"]:
        print l
    for l in output["stderr"]:
        print l

    # make sure we cleanup everything!
    kill()
示例#11
0
 def test_sftp_exceptions(self):
     self.server.kill()
     # Make socket with no server listening on it on separate ip
     host = '127.0.0.3'
     _socket = make_socket(host)
     port = _socket.getsockname()[1]
     client = ParallelSSHClient([self.host], port=port, num_retries=1)
     cmds = client.copy_file("test", "test")
     client.pool.join()
     for cmd in cmds:
         self.assertRaises(ConnectionErrorException, cmd.get)
示例#12
0
 def test_sftp_exceptions(self):
     self.server.kill()
     # Make socket with no server listening on it on separate ip
     host = '127.0.0.3'
     _socket = make_socket(host)
     port = _socket.getsockname()[1]
     client = ParallelSSHClient([self.host], port=port, num_retries=1)
     cmds = client.copy_file("test", "test")
     client.pool.join()
     for cmd in cmds:
         self.assertRaises(ConnectionErrorException, cmd.get)
示例#13
0
 def test_pssh_copy_file(self):
     """Test parallel copy file"""
     test_file_data = "test"
     local_filename = "test_file"
     remote_test_dir, remote_filename = "remote_test_dir", "test_file_copy"
     remote_filename = os.path.sep.join([remote_test_dir, remote_filename])
     test_file = open(local_filename, "w")
     test_file.writelines([test_file_data + os.linesep])
     test_file.close()
     client = ParallelSSHClient([self.host], port=self.listen_port, pkey=self.user_key)
     cmds = client.copy_file(local_filename, remote_filename)
     cmds[0].get()
     self.assertTrue(os.path.isdir(remote_test_dir), msg="SFTP create remote directory failed")
     self.assertTrue(os.path.isfile(remote_filename), msg="SFTP copy failed")
     for filepath in [local_filename, remote_filename]:
         os.unlink(filepath)
     del client
示例#14
0
def test_parallel():
    """Perform ls and copy file with ParallelSSHClient on localhost.
    
    Two identical hosts cause the same command to be executed
    twice on the same host in two parallel connections.
    In printed output there will be two identical lines per printed per
    line of `ls -ltrh` output as output is printed by host_logger as it
    becomes available and commands are executed in parallel
    
    Host output key is de-duplicated so that output for the two
    commands run on the same host(s) is not lost
    """
    client = ParallelSSHClient(['localhost', 'localhost'])
    output = client.run_command('ls -ltrh')
    client.join(output)
    pprint(output)
    cmds = client.copy_file('../test', 'test_dir/test')
    client.pool.join()
示例#15
0
def test_parallel():
    """Perform ls and copy file with ParallelSSHClient on localhost.
    
    Two identical hosts cause the same command to be executed
    twice on the same host in two parallel connections.
    In printed output there will be two identical lines per printed per
    line of `ls -ltrh` output as output is printed by host_logger as it
    becomes available and commands are executed in parallel
    
    Host output key is de-duplicated so that output for the two
    commands run on the same host(s) is not lost
    """
    client = ParallelSSHClient(['localhost', 'localhost'])
    output = client.run_command('ls -ltrh')
    client.join(output)
    pprint(output)
    cmds = client.copy_file('../test', 'test_dir/test')
    client.pool.join()
示例#16
0
 def test_pssh_copy_file(self):
     """Test parallel copy file"""
     test_file_data = 'test'
     local_filename = 'test_file'
     remote_test_dir, remote_filename = 'remote_test_dir', 'test_file_copy'
     remote_filename = os.path.sep.join([remote_test_dir, remote_filename])
     test_file = open(local_filename, 'w')
     test_file.writelines([test_file_data + os.linesep])
     test_file.close()
     client = ParallelSSHClient([self.host], port=self.listen_port,
                                pkey=self.user_key)
     cmds = client.copy_file(local_filename, remote_filename)
     cmds[0].get()
     self.assertTrue(os.path.isdir(remote_test_dir),
                     msg="SFTP create remote directory failed")
     self.assertTrue(os.path.isfile(remote_filename),
                     msg="SFTP copy failed")
     for filepath in [local_filename, remote_filename]:
         os.unlink(filepath)
     del client
示例#17
0
 def test_pssh_copy_file(self):
     """Test parallel copy file"""
     test_file_data = 'test'
     local_filename = 'test_file'
     remote_test_dir, remote_filename = 'remote_test_dir', 'test_file_copy'
     remote_filename = os.path.sep.join([remote_test_dir, remote_filename])
     test_file = open(local_filename, 'w')
     test_file.writelines([test_file_data + os.linesep])
     test_file.close()
     server = start_server({ self.fake_cmd : self.fake_resp },
                           self.listen_socket)
     client = ParallelSSHClient(['127.0.0.1'], port=self.listen_port,
                                pkey=self.user_key)
     cmds = client.copy_file(local_filename, remote_filename)
     cmds[0].get()
     self.assertTrue(os.path.isdir(remote_test_dir),
                     msg="SFTP create remote directory failed")
     self.assertTrue(os.path.isfile(remote_filename),
                     msg="SFTP copy failed")
     for filepath in [local_filename, remote_filename]:
         os.unlink(filepath)
     del client
     server.join()
示例#18
0
class SetupMultiVM(object):
    """
    Initiate parallel ssh connection and add basic methods like:
    file display, delete, execute, copy etc..
    """
    def __init__(self, hosts, args):
        self.hosts = hosts
        self.client = ParallelSSHClient(hosts, user=args['VM_LOGIN_USER'])
        self.ROOT_DIR = args['MULTIVM_ROOT_DIR']
        self.AIO_MODE = args['AIO_MODE']
        self.SCRIPT_NAMES = '{%s}' % ','.join(args['SCRIPT_NAMES'])
        self.UTIL_NAMES = '{%s}' % ','.join(args['UTIL_NAMES'])
        self.FILENAMES = ','.join(args['SCRIPT_NAMES'] + args['UTIL_NAMES'])

    def display_files(self, msg=''):
        print("\n%s\n" % msg)
        output = self.client.run_command('ls -lh ' +
                                         os.path.join(self.ROOT_DIR, '{%s}' %
                                                      self.FILENAMES))
        self.client.get_exit_codes(output)
        for host in self.hosts:
            print("host: %s -- exit_code: %s" %
                  (host, output[host]['exit_code']))
            for line in output[host]['stdout']:
                print line
            print

    def make_executable(self):
        print("changing mode +x for all scripts..\n")
        output = self.client.run_command(
            'chmod +x %s' % os.path.join(self.ROOT_DIR, self.SCRIPT_NAMES))
        self.client.get_exit_codes(output)
        for host in self.hosts:
            print("host: %s -- exit_code: %s" %
                  (host, output[host]['exit_code']))
            try:
                for line in output[host]['stdout']:
                    print line
            except:
                pass

    def copy_files(self):
        print("\ncopying files to VMs..\n")
        for filename in self.FILENAMES.split(','):
            self.client.copy_file(
                './%s' % (filename),
                os.path.join(self.ROOT_DIR, '%s' % (filename)))
        # additionally copy the config file to /etc
        self.client.copy_file('./multivm.config', '/etc/multivm.config')
        # join pool and execute copy commands
        self.client.pool.join()

    def delete_files(self):
        print("\ndeleting existing files..\n")
        output = self.client.run_command(
            'rm -f %s' % os.path.join(self.ROOT_DIR, '{%s}' % self.FILENAMES))
        self.client.get_exit_codes(output)
        for host in self.hosts:
            print("host: %s -- exit_code: %s" %
                  (host, output[host]['exit_code']))

    def record_output(self, generator_object, vm_hostname, script_name):
        log_file = '/tmp/%s.%s.%s.log' % (self.AIO_MODE, vm_hostname,
                                          script_name)
        f = open(log_file, 'wb')
        print("Hold on.. Logging VM outputs to your host under: %s" % log_file)
        for line in generator_object:
            f.write(line.encode('utf-8') + "\n")
        f.close()

    def execute_script(self, script_name=None, script_args='', nohup=False):
        script_path = os.path.join(self.ROOT_DIR, script_name)
        print("\nexecuting script: '%s %s'..\n" % (script_path, script_args))
        if nohup:
            CMD = 'nohup  %s %s > /tmp/%s.out 2> /tmp/%s.err < /dev/null &' % \
                            (script_path, script_args, script_name, script_name)
        else:
            CMD = '%s %s' % (script_path, script_args)

        output = self.client.run_command(CMD)
        self.client.get_exit_codes(output)
        for host in self.hosts:
            print("host: %s -- exit_code: %s" %
                  (host, output[host]['exit_code']))
            # for line in output[host]['stdout']: print line
            self.record_output(output[host]['stdout'], host, script_name)
class SetupMultiVM(object):
    """
    Initiate parallel ssh connection and add basic methods like:
    file display, delete, execute, copy etc..
    """

    def __init__(self, hosts, args):
        self.hosts = hosts
        self.client = ParallelSSHClient(hosts, user=args['VM_LOGIN_USER'])
        self.ROOT_DIR = args['MULTIVM_ROOT_DIR']
        self.AIO_MODE = args['AIO_MODE']
        self.SCRIPT_NAMES = '{%s}' % ','.join(args['SCRIPT_NAMES'])
        self.UTIL_NAMES = '{%s}' % ','.join(args['UTIL_NAMES'])
        self.FILENAMES = ','.join(args['SCRIPT_NAMES'] + args['UTIL_NAMES'])

    def display_files(self, msg=''):
        print("\n%s\n" % msg)
        output = self.client.run_command('ls -lh ' + os.path.join(self.ROOT_DIR,
                                                        '{%s}' % self.FILENAMES))
        self.client.get_exit_codes(output)
        for host in self.hosts:
            print("host: %s -- exit_code: %s" %(host, output[host]['exit_code']))
            for line in output[host]['stdout']: print line
            print

    def make_executable(self):
        print("changing mode +x for all scripts..\n")
        output = self.client.run_command('chmod +x %s' %
                                            os.path.join(self.ROOT_DIR,
                                            self.SCRIPT_NAMES))
        self.client.get_exit_codes(output)
        for host in self.hosts:
            print("host: %s -- exit_code: %s" %(host, output[host]['exit_code']))
            try:
                for line in output[host]['stdout']: print line
            except:
                pass

    def copy_files(self):
        print("\ncopying files to VMs..\n")
        for filename in self.FILENAMES.split(','):
            self.client.copy_file('./%s' % (filename),
                            os.path.join(self.ROOT_DIR, '%s' % (filename)))
        # additionally copy the config file to /etc
        self.client.copy_file('./multivm.config', '/etc/multivm.config')
        # join pool and execute copy commands
        self.client.pool.join()

    def delete_files(self):
        print("\ndeleting existing files..\n")
        output = self.client.run_command('rm -f %s' %
                                            os.path.join(self.ROOT_DIR,
                                            '{%s}' % self.FILENAMES))
        self.client.get_exit_codes(output)
        for host in self.hosts:
            print("host: %s -- exit_code: %s" %(host, output[host]['exit_code']))

    def record_output(self, generator_object, vm_hostname, script_name):
        log_file = '/tmp/%s.%s.%s.log' % (self.AIO_MODE, vm_hostname, script_name)
        f = open(log_file, 'wb')
        print("Hold on.. Logging VM outputs to your host under: %s" % log_file)
        for line in generator_object:
            f.write(line.encode('utf-8') + "\n")
        f.close()

    def execute_script(self, script_name=None, script_args='', nohup=False):
        script_path =  os.path.join(self.ROOT_DIR, script_name)
        print("\nexecuting script: '%s %s'..\n" % (script_path, script_args))
        if nohup:
            CMD = 'nohup  %s %s > /tmp/%s.out 2> /tmp/%s.err < /dev/null &' % \
                            (script_path, script_args, script_name, script_name)
        else:
            CMD = '%s %s' % (script_path, script_args)

        output = self.client.run_command(CMD)
        self.client.get_exit_codes(output)
        for host in self.hosts:
            print("host: %s -- exit_code: %s" %(host, output[host]['exit_code']))
            # for line in output[host]['stdout']: print line
            self.record_output(output[host]['stdout'], host, script_name)