def prepare_download(self, files): # Checks whether the VM is running try: info = machine_setup(self.target) except subprocess.CalledProcessError: logging.critical("Failed to get the status of the machine -- is " "it running?") sys.exit(1) # Connect with SSH self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(IgnoreMissingKey()) self.ssh.connect(**info)
def prepare_upload(self, files): # Checks whether the VM is running try: ssh_info = get_ssh_parameters(self.target) except subprocess.CalledProcessError: logging.critical("Failed to get the status of the machine -- is " "it running?") sys.exit(1) # Connect with scp self.ssh = paramiko.SSHClient() self.ssh.set_missing_host_key_policy(IgnoreMissingKey()) self.ssh.connect(**ssh_info) self.client_scp = scp.SCPClient(self.ssh.get_transport())
def machine_setup(target): """Prepare the machine and get SSH parameters from ``vagrant ssh``. """ try: out = subprocess.check_output(['vagrant', 'ssh-config'], cwd=target.path, stderr=subprocess.PIPE) except subprocess.CalledProcessError: # Makes sure the VM is running logging.info("Calling 'vagrant up'...") try: retcode = subprocess.check_call(['vagrant', 'up'], cwd=target.path) except OSError: logging.critical("vagrant executable not found") sys.exit(1) else: if retcode != 0: logging.critical("vagrant up failed with code %d", retcode) sys.exit(1) # Try again out = subprocess.check_output(['vagrant', 'ssh-config'], cwd=target.path) vagrant_info = {} for line in out.split(b'\n'): line = line.strip().split(b' ', 1) if len(line) != 2: continue value = line[1].decode('utf-8') if len(value) >= 2 and value[0] == '"' and value[-1] == '"': # Vagrant should really be escaping special characters here, but # it's not -- https://github.com/mitchellh/vagrant/issues/6428 value = value[1:-1] vagrant_info[line[0].decode('utf-8').lower()] = value if 'identityfile' in vagrant_info: key_file = vagrant_info['identityfile'] else: key_file = Path('~/.vagrant.d/insecure_private_key').expand_user() info = dict(hostname=vagrant_info.get('hostname', '127.0.0.1'), port=int(vagrant_info.get('port', 2222)), username=vagrant_info.get('user', 'vagrant'), key_filename=key_file) logging.debug("SSH parameters from Vagrant: %s@%s:%s, key=%s", info['username'], info['hostname'], info['port'], info['key_filename']) unpacked_info = read_dict(target) use_chroot = unpacked_info['use_chroot'] gui = unpacked_info['gui'] if use_chroot: # Mount directories ssh = paramiko.SSHClient() ssh.set_missing_host_key_policy(IgnoreMissingKey()) ssh.connect(**info) chan = ssh.get_transport().open_session() chan.exec_command( '/usr/bin/sudo /bin/sh -c %s' % shell_escape( 'for i in dev proc; do ' 'if ! grep "^/experimentroot/$i$" /proc/mounts; then ' 'mount -o rbind /$i /experimentroot/$i; ' 'fi; ' 'done')) if chan.recv_exit_status() != 0: logging.critical("Couldn't mount directories in chroot") sys.exit(1) if gui: # Mount X11 socket chan = ssh.get_transport().open_session() chan.exec_command( '/usr/bin/sudo /bin/sh -c %s' % shell_escape( 'if [ -d /tmp/.X11-unix ]; then ' '[ -d /experimentroot/tmp/.X11-unix ] || ' 'mkdir /experimentroot/tmp/.X11-unix; ' 'mount -o bind ' '/tmp/.X11-unix /experimentroot/tmp/.X11-unix; ' 'fi; exit 0')) if chan.recv_exit_status() != 0: logging.critical("Couldn't mount X11 sockets in chroot") sys.exit(1) ssh.close() return info