Class: Deployer::Executor

Inherits:
Object
  • Object
show all
Defined in:
lib/deployer/executor.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(stage, options = {}) {|_self| ... } ⇒ Executor

Returns a new instance of Executor.

Yields:

  • (_self)

Yield Parameters:



7
8
9
10
11
# File 'lib/deployer/executor.rb', line 7

def initialize(stage, options = {})
  @stage = stage
  @options = options
  yield(self) if block_given?
end

Instance Attribute Details

#stageObject (readonly)

Returns the value of attribute stage.



5
6
7
# File 'lib/deployer/executor.rb', line 5

def stage
  @stage
end

Instance Method Details

#download(source, target, options = {}) ⇒ Object



40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/deployer/executor.rb', line 40

def download(source, target, options = {})
  log_command("Execute: download via scp #{source} from #{target}", options[:comment])
  stage.hosts.each_with_index do |host, index|
    log_host(host, index)
    result = download_from(host, source, target)
    log_result(result)
    result
  end

rescue => e
  handle_exception(e, options)
end

#execute(command, options = {}) ⇒ Object

execute a command on all hosts of a stage commands are expanded with :hostname, :port and :stagename



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/deployer/executor.rb', line 15

def execute(command, options = {})
  log_command("Execute: '#{command.strip}'", options[:comment])
  stage.hosts.each_with_index.map do |host, index|
    expand_options = {hostname: host[:host], stagename: stage.name, port: host[:port]}

    expanded_command = command % expand_options
    if expanded_command != command
      log_command("Expanded command to #{expanded_command}")
    end
    log_host(host, index)
    result = execute_on(host, expanded_command)
    log_result(result)
    result
  end

rescue => e
  handle_exception(e, options)
end

#execute_on(host, command) ⇒ Object



34
35
36
37
38
# File 'lib/deployer/executor.rb', line 34

def execute_on(host, command)
  Net::SSH.start(host[:host], host[:user], port: host[:port]) do |ssh|
    ssh.exec_sc!(command, verbose?)
  end
end

#upload(source, target, options = {}) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/deployer/executor.rb', line 53

def upload(source, target, options = {})
  log_command("Execute: upload via scp #{source} to #{target}", options[:comment])
  stage.hosts.each_with_index do |host, index|
    log_host(host, index)
    result = upload_on(host, source, target)
    log_result(result)
    result
  end

rescue => e
  handle_exception(e, options)
end