Class: Rake::RemoteTask
- Includes:
- Open4
- Defined in:
- lib/rake_remote_task.rb
Overview
Rake::RemoteTask is a subclass of Rake::Task that adds remote_actions that execute in parallel on multiple hosts via ssh.
Defined Under Namespace
Classes: Action
Instance Attribute Summary collapse
-
#options ⇒ Object
Options for execution of this task.
-
#remote_actions ⇒ Object
readonly
An Array of Actions this host will perform during execution.
-
#target_host ⇒ Object
The host this task is running on during execution.
Class Method Summary collapse
-
.all_hosts ⇒ Object
Returns an Array with every host configured.
-
.default_env ⇒ Object
The default environment values.
-
.env ⇒ Object
The vlad environment.
-
.fetch(name, default = nil) ⇒ Object
Fetches environment variable
name
from the environment using defaultdefault
. -
.host(host_name, *roles) ⇒ Object
Add host
host_name
that belongs toroles
. -
.hosts_for(*roles) ⇒ Object
Returns an Array of all hosts in
roles
. -
.mandatory(name, desc) ⇒ Object
:nodoc:.
-
.protect_env(name) ⇒ Object
Ensures exclusive access to
name
. -
.remote_task(name, options = {}, &block) ⇒ Object
Adds a remote task named
name
with optionsoptions
that will executeblock
. -
.reserved_name?(name) ⇒ Boolean
Ensures
name
does not conflict with an existing method. -
.reset ⇒ Object
Resets vlad, restoring all roles, tasks and environment variables to the defaults.
-
.role(role_name, host, args = {}) ⇒ Object
Adds role
role_name
withhost
andargs
for that host. -
.roles ⇒ Object
The configured roles.
-
.set(name, value = nil, &default_block) ⇒ Object
Set environment variable
name
tovalue
ordefault_block
. -
.set_defaults ⇒ Object
Sets all the default values.
-
.set_path(name, subdir) ⇒ Object
:nodoc:.
-
.simple_set(*args) ⇒ Object
:nodoc:.
-
.task ⇒ Object
The Rake::RemoteTask executing in this Thread.
-
.tasks ⇒ Object
The configured Rake::RemoteTasks.
Instance Method Summary collapse
-
#enhance(deps = nil, &block) ⇒ Object
Add remote action
block
to this task with dependenciesdeps
. -
#execute(args = nil) ⇒ Object
Execute this action.
-
#initialize(task_name, app) ⇒ RemoteTask
constructor
Create a new task named
task_name
attached to Rake::Applicationapp
. -
#original_enhance ⇒ Object
Add a local action to this task.
-
#rsync(local, remote) ⇒ Object
Use rsync to send
local
toremote
on target_host. -
#run(command) ⇒ Object
Use ssh to execute
command
on target_host. -
#sudo(command) ⇒ Object
Execute
command
under sudo using run. -
#target_hosts ⇒ Object
The hosts this task will execute on.
Methods inherited from Task
Constructor Details
#initialize(task_name, app) ⇒ RemoteTask
Create a new task named task_name
attached to Rake::Application app
.
143 144 145 146 |
# File 'lib/rake_remote_task.rb', line 143 def initialize(task_name, app) super @remote_actions = [] end |
Instance Attribute Details
#options ⇒ Object
Options for execution of this task.
127 128 129 |
# File 'lib/rake_remote_task.rb', line 127 def @options end |
#remote_actions ⇒ Object (readonly)
An Array of Actions this host will perform during execution. Use enhance to add new actions to a task.
138 139 140 |
# File 'lib/rake_remote_task.rb', line 138 def remote_actions @remote_actions end |
#target_host ⇒ Object
The host this task is running on during execution.
132 133 134 |
# File 'lib/rake_remote_task.rb', line 132 def target_host @target_host end |
Class Method Details
.all_hosts ⇒ Object
Returns an Array with every host configured.
252 253 254 |
# File 'lib/rake_remote_task.rb', line 252 def self.all_hosts hosts_for(roles.keys) end |
.default_env ⇒ Object
The default environment values. Used for resetting (mostly for tests).
260 261 262 |
# File 'lib/rake_remote_task.rb', line 260 def self.default_env @@default_env end |
.env ⇒ Object
The vlad environment.
267 268 269 |
# File 'lib/rake_remote_task.rb', line 267 def self.env @@env end |
.fetch(name, default = nil) ⇒ Object
Fetches environment variable name
from the environment using default default
.
275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
# File 'lib/rake_remote_task.rb', line 275 def self.fetch name, default = nil name = name.to_s if Symbol === name if @@env.has_key? name then protect_env(name) do v = @@env[name] v = @@env[name] = v.call if Proc === v v end elsif default v = @@env[name] = default else raise Vlad::FetchError end end |
.host(host_name, *roles) ⇒ Object
303 304 305 306 307 308 309 |
# File 'lib/rake_remote_task.rb', line 303 def self.host host_name, *roles opts = Hash === roles.last ? roles.pop : {} roles.each do |role_name| role role_name, host_name, opts.dup end end |
.hosts_for(*roles) ⇒ Object
Returns an Array of all hosts in roles
.
314 315 316 317 318 |
# File 'lib/rake_remote_task.rb', line 314 def self.hosts_for *roles roles.flatten.map { |r| self.roles[r].keys }.flatten.uniq.sort end |
.mandatory(name, desc) ⇒ Object
:nodoc:
320 321 322 323 324 325 |
# File 'lib/rake_remote_task.rb', line 320 def self.mandatory name, desc # :nodoc: self.set(name) do raise(Vlad::ConfigurationError, "Please specify the #{desc} via the #{name.inspect} variable") end end |
.protect_env(name) ⇒ Object
Ensures exclusive access to name
.
330 331 332 333 334 |
# File 'lib/rake_remote_task.rb', line 330 def self.protect_env name # :nodoc: @@env_locks[name].synchronize do yield end end |
.remote_task(name, options = {}, &block) ⇒ Object
Adds a remote task named name
with options options
that will execute block
.
340 341 342 343 344 |
# File 'lib/rake_remote_task.rb', line 340 def self.remote_task name, = {}, &block t = Rake::RemoteTask.define_task(name, &block) t. = t end |
.reserved_name?(name) ⇒ Boolean
Ensures name
does not conflict with an existing method.
349 350 351 |
# File 'lib/rake_remote_task.rb', line 349 def self.reserved_name? name # :nodoc: !@@env.has_key?(name.to_s) && self.respond_to?(name) end |
.reset ⇒ Object
Resets vlad, restoring all roles, tasks and environment variables to the defaults.
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 |
# File 'lib/rake_remote_task.rb', line 357 def self.reset @@roles = Hash.new { |h,k| h[k] = {} } @@env = {} @@tasks = {} @@env_locks = Hash.new { |h,k| h[k] = Mutex.new } @@default_env.each do |k,v| case v when Symbol, Fixnum, nil, true, false, 42 then # ummmm... yeah. bite me. @@env[k] = v else @@env[k] = v.dup end end end |
.role(role_name, host, args = {}) ⇒ Object
Adds role role_name
with host
and args
for that host.
376 377 378 379 |
# File 'lib/rake_remote_task.rb', line 376 def self.role role_name, host, args = {} raise ArgumentError, "invalid host" if host.nil? or host.empty? @@roles[role_name][host] = args end |
.roles ⇒ Object
The configured roles.
384 385 386 387 388 |
# File 'lib/rake_remote_task.rb', line 384 def self.roles host domain, :app, :web, :db if @@roles.empty? @@roles end |
.set(name, value = nil, &default_block) ⇒ Object
Set environment variable name
to value
or default_block
.
If default_block
is defined, the block will be executed the first time the variable is fetched, and the value will be used for every subsequent fetch.
397 398 399 400 401 402 403 404 405 406 407 408 409 |
# File 'lib/rake_remote_task.rb', line 397 def self.set name, value = nil, &default_block raise ArgumentError, "cannot provide both a value and a block" if value and default_block raise ArgumentError, "cannot set reserved name: '#{name}'" if Rake::RemoteTask.reserved_name?(name) unless $TESTING Rake::RemoteTask.default_env[name.to_s] = Rake::RemoteTask.env[name.to_s] = value || default_block Object.send :define_method, name do Rake::RemoteTask.fetch name end end |
.set_defaults ⇒ Object
Sets all the default values. Should only be called once. Use reset if you need to restore values.
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/rake_remote_task.rb', line 415 def self.set_defaults @@default_env ||= {} self.reset mandatory :code_repo, "code repo path" mandatory :deploy_to, "deploy path" mandatory :domain, "server domain" simple_set(:deploy_timestamped, true, :deploy_via, :export, :keep_releases, 5, :migrate_args, "", :migrate_target, :latest, :rails_env, "production", :rake_cmd, "rake", :revision, "head", :rsync_cmd, "rsync", :rsync_flags, ['-azP', '--delete'], :ssh_cmd, "ssh", :ssh_flags, nil, :sudo_cmd, "sudo", :sudo_flags, nil) set(:current_release) { File.join(releases_path, releases[-1]) } set(:latest_release) { ?release_path: current_release } set(:previous_release) { File.join(releases_path, releases[-2]) } set(:release_name) { Time.now.utc.strftime("%Y%m%d%H%M%S") } set(:release_path) { File.join(releases_path, release_name) } set(:releases) { task.run("ls -x #{releases_path}").split.sort } set_path :current_path, "current" set_path :releases_path, "releases" set_path :scm_path, "scm" set_path :shared_path, "shared" set(:sudo_password) do state = `stty -g` raise Vlad::Error, "stty(1) not found" unless $?.success? begin system "stty -echo" $stdout.print "sudo password: " $stdout.flush sudo_password = $stdin.gets $stdout.puts ensure system "stty #{state}" end sudo_password end end |
.set_path(name, subdir) ⇒ Object
:nodoc:
468 469 470 |
# File 'lib/rake_remote_task.rb', line 468 def self.set_path(name, subdir) # :nodoc: set(name) { File.join(deploy_to, subdir) } end |
.simple_set(*args) ⇒ Object
:nodoc:
472 473 474 475 476 477 |
# File 'lib/rake_remote_task.rb', line 472 def self.simple_set(*args) # :nodoc: args = Hash[*args] args.each do |k, v| set k, v end end |
.task ⇒ Object
The Rake::RemoteTask executing in this Thread.
482 483 484 |
# File 'lib/rake_remote_task.rb', line 482 def self.task Thread.current[:task] end |
.tasks ⇒ Object
The configured Rake::RemoteTasks.
489 490 491 |
# File 'lib/rake_remote_task.rb', line 489 def self.tasks @@tasks end |
Instance Method Details
#enhance(deps = nil, &block) ⇒ Object
Add remote action block
to this task with dependencies deps
. See Rake::Task#enhance.
157 158 159 160 161 |
# File 'lib/rake_remote_task.rb', line 157 def enhance(deps=nil, &block) original_enhance(deps) # can't use super because block passed regardless. @remote_actions << Action.new(self, block) if block_given? self end |
#execute(args = nil) ⇒ Object
Execute this action. Local actions will be performed first, then remote actions will be performed in parallel on each host configured for this RemoteTask.
168 169 170 171 172 173 174 175 176 |
# File 'lib/rake_remote_task.rb', line 168 def execute(args = nil) raise(Vlad::ConfigurationError, "No target hosts specified for task: #{self.name}") if target_hosts.empty? super args @remote_actions.each { |act| act.execute(target_hosts, args) } end |
#original_enhance ⇒ Object
Add a local action to this task. This calls Rake::Task#enhance.
151 |
# File 'lib/rake_remote_task.rb', line 151 alias_method :original_enhance, :enhance |
#rsync(local, remote) ⇒ Object
Use rsync to send local
to remote
on target_host.
181 182 183 184 185 186 187 188 189 |
# File 'lib/rake_remote_task.rb', line 181 def rsync local, remote cmd = [rsync_cmd, rsync_flags, local, "#{@target_host}:#{remote}"].flatten.compact success = system(*cmd) unless success then raise Vlad::CommandFailedError, "execution failed: #{cmd.join ' '}" end end |
#run(command) ⇒ Object
Use ssh to execute command
on target_host. If command
uses sudo, the sudo password will be prompted for then saved for subsequent sudo commands.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/rake_remote_task.rb', line 195 def run command cmd = [ssh_cmd, ssh_flags, target_host, command].compact result = [] warn cmd.join(' ') if $TRACE pid, inn, out, err = popen4(*cmd) inn.sync = true streams = [out, err] out_stream = { out => $stdout, err => $stderr, } # Handle process termination ourselves status = nil Thread.start do status = Process.waitpid2(pid).last end until streams.empty? do # don't busy loop selected, = select streams, nil, nil, 0.1 next if selected.nil? or selected.empty? selected.each do |stream| if stream.eof? then streams.delete stream if status # we've quit, so no more writing next end data = stream.readpartial(1024) out_stream[stream].write data if stream == err and data =~ /^Password:/ then inn.puts sudo_password data << "\n" $stderr.write "\n" end result << data end end unless status.success? then raise(Vlad::CommandFailedError, "execution failed with status #{status.exitstatus}: #{cmd.join ' '}") end result.join end |
#sudo(command) ⇒ Object
Execute command
under sudo using run.
496 497 498 |
# File 'lib/rake_remote_task.rb', line 496 def sudo command run [sudo_cmd, sudo_flags, command].compact.join(" ") end |
#target_hosts ⇒ Object
The hosts this task will execute on. The hosts are determined from the role this task belongs to.
The target hosts may be overridden by providing a comma-separated list of commands to the HOSTS environment variable:
rake my_task HOSTS=app1.example.com,app2.example.com
509 510 511 512 513 514 515 516 |
# File 'lib/rake_remote_task.rb', line 509 def target_hosts if hosts = ENV["HOSTS"] then hosts.strip.gsub(/\s+/, '').split(",") else roles = [:roles] roles ? Rake::RemoteTask.hosts_for(roles) : Rake::RemoteTask.all_hosts end end |