Host

This module contains the immediate wrappers around the remote hosts and their terminals. It’s possible to run commands on a host directly by using these classes. As an end-user of this library however, you will call the methods of SSHHost and LocalHost through HostsContainer, the host proxy of a Node.

Base classes

class deployer.host.base.Host(pty=None, logger=None)

Abstract base class for SSHHost and LocalHost.

Parameters:
  • pty (deployer.pseudo_terminal.Pty) – The pseudo terminal wrapper which handles the stdin/stdout.
  • logger (LoggerInterface) – The logger interface.
class MyHost(SSHHost):
    ...
my_host = MyHost()
my_host.run('pwd', interactive=False)
copy(pty=None)

Create a deep copy of this Host class. (the pty-parameter allows to bind it to anothor pty)

exists(filename, use_sudo=True, **kw)

Returns True when a file named filename exists on this hosts.

get_file(remote_path, local_path, use_sudo=False, sandbox=False)

Download this remote_file.

get_ip_address(interface='eth0')

Return internal IP address of this interface.

get_start_path()

The path in which commands at the server will be executed. by default. (if no cd-statements are used.) Usually, this is the home directory. It should always return an absolute path, starting with ‘/’

getcwd()

Return current working directory as absolute path.

ifconfig()

Return the network information for this host.

Returns:An IfConfig instance.
listdir_stat(path='.')

Return a list of Stat instances for each file in this directory.

open(remote_path, mode='rb', use_sudo=False, sandbox=False)

Open file handler to remote file. Can be used both as:

with host.open('/path/to/somefile', 'wb') as f:
    f.write('some content')

or:

host.open('/path/to/somefile', 'wb').write('some content')
password = ''

Password for connecting to the host. (for sudo)

put_file(local_path, remote_path, use_sudo=False, sandbox=False)

Upload this local_file to the remote location.

run(command, use_sudo=False, sandbox=False, interactive=True, user=None, ignore_exit_status=False, initial_input=None, silent=False)

Execute this shell command on the host.

Parameters:
  • command (basestring) – The shell command.
  • use_sudo (bool) – Run as superuser.
  • sandbox (bool) – Validate syntax instead of really executing. (Wrap the command in bash -n.)
  • interactive (bool) – Start an interactive event loop which allows interaction with the remote command. Otherwise, just return the output.
  • initial_input – When interactive, send this input first to the host.
slug = ''

The slug should be a unique identifier for the host.

start_interactive_shell(command=None, initial_input=None)

Start an interactive bash shell.

sudo(command, use_sudo=False, sandbox=False, interactive=True, user=None, ignore_exit_status=False, initial_input=None, silent=False)

Wrapper around run() which uses sudo.

username = ''

Username for connecting to the Host

class deployer.host.base.HostContext

A push/pop stack which keeps track of the context on which commands at a host are executed.

(This is mainly used internally by the library.)

cd(path, expand=False)

Execute commands in this directory. Nesting of cd-statements is allowed.

with host.cd('directory'):
    host.run('ls')
Parameters:expand (bool) – Expand tildes.
copy()

Create a deep copy.

env(variable, value, escape=True)

Set this environment variable

with host.cd('VAR', 'my-value'):
    host.run('echo $VAR')
prefix(command)

Prefix all commands with given command plus &&.

with host.prefix('workon environment'):
    host.run('./manage.py migrate')
class deployer.host.base.Stat(stat_result, filename)

Base Stat class

is_dir

True when this is a directory.

is_file

True when this is a regular file.

st_gid

Group ID

st_size

File size in bytes.

st_uid

User ID

Localhost

class deployer.host.local.LocalHost(pty=None, logger=None)

LocalHost can be used instead of SSHHost for local execution. It uses pexpect underneat.

listdir_stat(path='.')

Return a list of Stat instances for each file in this directory.

start_interactive_shell(command=None, initial_input=None)

Start an interactive bash shell.

SSH Host

class deployer.host.ssh.SSHHost(*a, **kw)

SSH Host.

For the authentication, it’s required to provide either a password, a key_filename or rsa_key. e.g.

class WebServer(SSHHost):
    slug = 'webserver'
    password = '...'
    address = 'example.com'
    username = 'jonathan'
address = 'example.com'

SSH Address

config_filename = '~/.ssh/config'

SSH config file (optional)

get_start_path()

The path in which commands at the server will be executed. by default. (if no cd-statements are used.) Usually, this is the home directory. It should always return an absolute path, starting with ‘/’

keepalive_interval = 30

SSH keep alive in seconds

key_filename = None

RSA key filename (optional)

listdir_stat(path='.')

Return a list of Stat instances for each file in this directory.

port = 22

SSH Port

rsa_key = None

RSA key. (optional)

rsa_key_password = None

RSA key password. (optional)

start_interactive_shell(command=None, initial_input=None, sandbox=False)

Start /bin/bash and redirect all SSH I/O from stdin and to stdout.

timeout = 10

Connection timeout in seconds.

username = ''

SSH Username