- self.log.debug(e)
- raise e
- return ssh
-
- def _run_command(self, ssh, cmd, pty=True):
- """Run a command remotely via SSH.
-
- :param object ssh: The SSHClient
- :param str cmd: The command to execute
- :param list cmd: The `shlex.split` command to execute
- :param bool pty: Whether to allocate a pty
-
- :return: tuple: The stdout and stderr of the command execution
- :raises: :class:`CalledProcessError` if the command fails
- """
-
- if isinstance(cmd, str):
- cmd = shlex.split(cmd)
-
- if type(cmd) is not list:
- cmd = [cmd]
-
- cmds = ' '.join(cmd)
- stdin, stdout, stderr = ssh.exec_command(cmds, get_pty=pty)
- retcode = stdout.channel.recv_exit_status()
-
- if retcode > 0:
- output = stderr.read().strip()
- raise CalledProcessError(returncode=retcode, cmd=cmd,
- output=output)
- return (
- stdout.read().decode('utf-8').strip(),
- stderr.read().decode('utf-8').strip()
- )
-
- def _init_ubuntu_user(self):
- """Initialize the ubuntu user.
-
- :return: bool: If the initialization was successful
- :raises: :class:`paramiko.ssh_exception.AuthenticationException`
- if the authentication fails
- """
- ssh = None
- try:
- # Run w/o allocating a pty, so we fail if sudo prompts for a passwd
- ssh = self._get_ssh_client()
- stdout, stderr = self._run_command(ssh, "sudo -n true", pty=False)
- except paramiko.ssh_exception.AuthenticationException:
- raise n2vc.exceptions.AuthenticationFailed(self.user)
- except paramiko.ssh_exception.NoValidConnectionsError:
- raise n2vc.exceptions.NoRouteToHost(self.host)
- finally:
- if ssh:
- ssh.close()