host_container¶
Access to hosts from within a Node
class happens through a
HostsContainer
proxy. This container object has also methods for reducing
the amount of hosts on which commands are executed, by filtering according to
conditions.
The hosts
property of a node instance returns such a HostsContainer
object.
class MyNode(Node):
class Hosts:
web_servers = [Host1, Host2]
caching_servers = Host3
def do_something(self):
# self.hosts here, is a HostsContainer instance.
self.hosts.filter('caching_servers').run('echo hello')
Reference¶
-
class
deployer.host_container.
HostContainer
(hosts, pty=None, logger=None, is_sandbox=False, host_contexts=None)¶ Similar to hostsContainer, but wraps only around exactly one host.
-
exists
(*a, **kw)¶ Returns
True
when this file exists on the hosts.
-
get
(*args, **kwargs)¶ Download this remote_file.
-
has_command
(*a, **kw)¶ Test whether this command can be found in the bash shell, by executing a ‘which’
-
open
(*args, **kwargs)¶ 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')
-
put
(*args, **kwargs)¶ Upload this local_file to the remote location.
-
run
(*a, **kw)¶ Execute this shell command on the host.
Parameters: - pty (
deployer.pseudo_terminal.Pty
) – The pseudo terminal wrapper which handles the stdin/stdout. - 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.
- logger (LoggerInterface) – The logger interface.
- initial_input (basestring) – When
interactive
, send this input first to the host. - context (:class:`deployer.host.HostContext;) –
- pty (
-
sudo
(*a, **kw)¶ Run this command using sudo.
-
-
class
deployer.host_container.
HostsContainer
(hosts, pty=None, logger=None, is_sandbox=False, host_contexts=None)¶ Facade to the host instances. if you have a role, name ‘www’ inside the service webserver, you can do:
- webserver.hosts.run(...)
- webserver.hosts.www.run(...)
- webserver.hosts[0].run(...)
- webserver.hosts.www[0].run(...)
- webserver.hosts.filter(‘www’)[0].run(...)
The host container also keeps track of HostStatus. So, if we fork a new thread, and the HostStatus object gets modified in either thread. Clone this HostsContainer first.
-
cd
(*a, **kw)¶ Execute commands in this directory. Nesting of cd-statements is allowed.
with host.cd('~/directory'): host.run('ls')
-
env
(*a, **kw)¶ Set this environment variable
-
exists
(filename, use_sudo=True)¶ Returns
True
when this file exists on the hosts.
-
filter
(*roles)¶ Examples:
hosts.filter('role1', 'role2') hosts.filter('*') # Returns everything hosts.filter( ['role1', 'role2' ]) # TODO: deprecate host.filter('role1', MyHostClass) # This means: take 'role1' from this container, but add an instance of this class
-
classmethod
from_definition
(hosts_class, **kw)¶ Create a
HostContainer
from a Hosts class.
-
get
(*roles)¶ Similar to
filter()
, but returns exactly one host instead of a list.
-
has_command
(command, use_sudo=False)¶ Test whether this command can be found in the bash shell, by executing a ‘which’
-
prefix
(*a, **kw)¶ Prefix all commands with given command plus
&&
.with host.prefix('workon environment'): host.run('./manage.py migrate')
-
run
(*a, **kw)¶ Execute this shell command on the host.
Parameters: - pty (
deployer.pseudo_terminal.Pty
) – The pseudo terminal wrapper which handles the stdin/stdout. - 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.
- logger (LoggerInterface) – The logger interface.
- initial_input (basestring) – When
interactive
, send this input first to the host. - context (:class:`deployer.host.HostContext;) –
- pty (
-
sudo
(*args, **kwargs)¶ Run this command using sudo.