Class: RobotArmy::TaskMaster
Overview
The place where the magic happens
Types (shortcuts for use in this file)
- HostList
-
<Array, String, nil>
Class Method Summary collapse
-
.host(host = nil) ⇒ String, :localhost
Gets or sets a single host that instances of
RobotArmy::TaskMaster
subclasses will use. -
.hosts(hosts = nil) ⇒ Array[String]
Gets or sets the hosts that instances of
RobotArmy::TaskMaster
subclasses will use.
Instance Method Summary collapse
-
#connection(host) ⇒ Object
Gets an open connection for the host this instance is configured to use.
-
#cptemp(path, hosts = self.hosts, options = {}) {|path| ... } ⇒ Array<String>
Copies path to a temporary directory on each host.
-
#dependency(dep, ver = nil) ⇒ Object
Add a gem dependency this TaskMaster checks for on each remote host.
-
#host ⇒ String, :localhost
Gets the first host for this instance of
RobotArmy::TaskMaster
. -
#host=(host) ⇒ Object
Sets a single host for this instance of
RobotArmy::TaskMaster
. -
#hosts ⇒ Array[String]
Gets the hosts for the instance of
RobotArmy::TaskMaster
. -
#hosts=(hosts) ⇒ Object
Sets the hosts for this instance of
RobotArmy::TaskMaster
. -
#initialize(*args) ⇒ TaskMaster
constructor
A new instance of TaskMaster.
-
#remote(hosts = self.hosts, options = {}, &proc) ⇒ Object
Runs a block of Ruby on the machine specified by a host string and returns the return value of the block.
-
#scp(src, dest, hosts = self.hosts) ⇒ Object
Copies src to dest on each host.
-
#sudo(hosts = self.hosts, options = {}, &proc) ⇒ Object
Runs a block of Ruby on the machine specified by a host string as root and returns the return value of the block.
Constructor Details
#initialize(*args) ⇒ TaskMaster
Returns a new instance of TaskMaster.
7 8 9 10 |
# File 'lib/robot-army/task_master.rb', line 7 def initialize(*args) super @dep_loader = DependencyLoader.new end |
Class Method Details
.host(host = nil) ⇒ String, :localhost
Gets or sets a single host that instances of RobotArmy::TaskMaster
subclasses will use.
24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/robot-army/task_master.rb', line 24 def self.host(host=nil) if host @hosts = nil @host = host elsif @hosts raise RobotArmy::HostArityError, "There are #{@hosts.size} hosts, so calling host doesn't make sense" else @host end end |
.hosts(hosts = nil) ⇒ Array[String]
Gets or sets the hosts that instances of RobotArmy::TaskMaster
subclasses will use.
44 45 46 47 48 49 50 51 52 53 |
# File 'lib/robot-army/task_master.rb', line 44 def self.hosts(hosts=nil) if hosts @host = nil @hosts = hosts elsif @host [@host] else @hosts || [] end end |
Instance Method Details
#connection(host) ⇒ Object
Gets an open connection for the host this instance is configured to use.
115 116 117 |
# File 'lib/robot-army/task_master.rb', line 115 def connection(host) RobotArmy::GateKeeper.shared_instance.connect(host) end |
#cptemp(path, hosts = self.hosts, options = {}) {|path| ... } ⇒ Array<String>
Copies path to a temporary directory on each host.
229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 |
# File 'lib/robot-army/task_master.rb', line 229 def cptemp(path, hosts=self.hosts, ={}, &block) hosts, = self.hosts, hosts if hosts.is_a?(Hash) results = remote(hosts, ) do File.join(%x{mktemp -d -t robot-army.XXXX}.chomp, File.basename(path)) end me = ENV['USER'] host_and_path = Array(hosts).zip(Array(results)) # copy them over host_and_path.each do |host, tmp| sudo(host) { FileUtils.chown(me, nil, File.dirname(tmp)) } if [:user] scp path, tmp, host sudo(host) { FileUtils.chown([:user], nil, File.dirname(tmp)) } if [:user] end # call the block on each host results = host_and_path.map do |host, tmp| remote(host, .merge(:args => [tmp]), &block) end if block results.size == 1 ? results.first : results end |
#dependency(dep, ver = nil) ⇒ Object
Add a gem dependency this TaskMaster checks for on each remote host.
260 261 262 |
# File 'lib/robot-army/task_master.rb', line 260 def dependency(dep, ver = nil) @dep_loader.add_dependency dep, ver end |
#host ⇒ String, :localhost
Gets the first host for this instance of RobotArmy::TaskMaster
.
64 65 66 67 68 69 70 71 72 73 |
# File 'lib/robot-army/task_master.rb', line 64 def host if @host @host elsif @hosts raise RobotArmy::HostArityError, "There are #{@hosts.size} hosts, so calling host doesn't make sense" else self.class.host end end |
#host=(host) ⇒ Object
Sets a single host for this instance of RobotArmy::TaskMaster
.
80 81 82 83 |
# File 'lib/robot-army/task_master.rb', line 80 def host=(host) @hosts = nil @host = host end |
#hosts ⇒ Array[String]
Gets the hosts for the instance of RobotArmy::TaskMaster
.
90 91 92 93 94 95 96 97 98 |
# File 'lib/robot-army/task_master.rb', line 90 def hosts if @hosts @hosts elsif @host [@host] else self.class.hosts end end |
#hosts=(hosts) ⇒ Object
Sets the hosts for this instance of RobotArmy::TaskMaster
.
105 106 107 108 |
# File 'lib/robot-army/task_master.rb', line 105 def hosts=(hosts) @host = nil @hosts = hosts end |
#remote(hosts = self.hosts, options = {}, &proc) ⇒ Object
Runs a block of Ruby on the machine specified by a host string and returns the return value of the block. Example:
remote { "foo" } # => "foo"
Local variables accessible from the block are also passed along to the remote process:
foo = "bar"
remote { foo } # => "bar"
Objects which can’t be marshalled, such as IO streams, will be proxied instead:
file = File.open("README.markdown", "r")
remote { file.gets } # => "Robot Army\n"
177 178 179 180 181 |
# File 'lib/robot-army/task_master.rb', line 177 def remote(hosts=self.hosts, ={}, &proc) , hosts = hosts, self.hosts if hosts.is_a?(Hash) results = Array(hosts).map {|host| remote_eval({:host => host}.merge(), &proc) } results.size == 1 ? results.first : results end |
#scp(src, dest, hosts = self.hosts) ⇒ Object
Copies src to dest on each host.
197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/robot-army/task_master.rb', line 197 def scp(src, dest, hosts=self.hosts) Array(hosts).each do |host| output = `scp -q #{src} #{"#{host}:" unless host == :localhost}#{dest} 2>&1` case output when /Permission denied/i raise Errno::EACCES, output.chomp when /No such file or directory/i raise Errno::ENOENT, output.chomp end unless $?.exitstatus == 0 end return nil end |
#sudo(hosts = self.hosts, options = {}, &proc) ⇒ Object
Runs a block of Ruby on the machine specified by a host string as root and returns the return value of the block. Example:
sudo { `shutdown -r now` }
You may also specify a user other than root. In this case sudo
is the same as remote
:
sudo(:user => 'www-data') { `/etc/init.d/apache2 restart` }
143 144 145 146 |
# File 'lib/robot-army/task_master.rb', line 143 def sudo(hosts=self.hosts, ={}, &proc) , hosts = hosts, self.hosts if hosts.is_a?(Hash) remote hosts, {:user => 'root'}.merge(), &proc end |