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
Env
wrapper around 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.
HostsContainer
(hosts, pty=None, logger=None, is_sandbox=False)¶ Proxy to a group of
Host
instances.For instance, if you have a role, name ‘www’ inside the container, you could do:
host_container.run(...) host_container[0].run(...) host_container.filter('www')[0].run(...)
Typically, you get a
HostsContainer
class by accessing thehosts
property of anEnv
(Node
wrapper.)-
cd
(path, expand=False)¶ Execute commands in this directory. Nesting of cd-statements is allowed.
Call
cd()
on theHostContext
of every host.with host_container.cd('directory'): host_container.run('ls')
-
env
(variable, value, escape=True)¶ Sets an environment variable.
This calls
env()
on theHostContext
of every host.with host_container.cd('VAR', 'my-value'): host_container.run('echo $VAR')
-
exists
(filename, use_sudo=True)¶ Returns an array of boolean values that represent whether this a file with this name exist for each host.
-
filter
(*roles)¶ Returns a new HostsContainer instance, containing only the hosts matching this filter. The hosts are passed by reference, so if you’d call cd() on the returned container, it will also effect the hosts in this object.
Examples:
hosts.filter('role1', 'role2')
-
classmethod
from_definition
(hosts_class, **kw)¶ Create a
HostsContainer
from a Hosts class.
-
get_hosts
()¶ Return a set of
deployer.host.Host
classes that appear in this container. Eachdeployer.host.Host
class will abviously appear only once in the set, even when it appears in several roles.
-
get_hosts_as_dict
()¶ Return a dictionary which maps all the roles to the set of
deployer.host.Host
classes for each role.
-
has_command
(command, use_sudo=False)¶ Test whether this command can be found in the bash shell, by executing a ‘which’
-
prefix
(command)¶ Call
prefix()
on theHostContext
of every host.with host.prefix('workon environment'): host.run('./manage.py migrate')
-
-
class
deployer.host_container.
HostContainer
(hosts, pty=None, logger=None, is_sandbox=False)¶ Similar to
HostsContainer
, but wraps only around exactly oneHost
.-
exists
(filename, use_sudo=True)¶ Returns
True
when this file exists on the hosts.
-
get_file
(*args, **kwargs)¶ Download this remote_file.
-
has_command
(command, use_sudo=False)¶ Test whether this command can be found in the bash shell, by executing a
which
ReturnsTrue
when the command exists.
-
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_file
(*args, **kwargs)¶ Upload this local_file to the remote location.
-