Class: Rigger::TaskExecutor

Inherits:
Object
  • Object
show all
Defined in:
lib/rigger/task_executor.rb

Defined Under Namespace

Classes: SFTPTransferWrapper

Instance Method Summary collapse

Constructor Details

#initialize(task, servers, execution_service, config) ⇒ TaskExecutor

Returns a new instance of TaskExecutor.



33
34
35
36
37
38
# File 'lib/rigger/task_executor.rb', line 33

def initialize(task, servers, execution_service, config)
  @task              = task
  @current_servers   = servers
  @execution_service = execution_service
  @config            = config
end

Instance Method Details

#callObject



40
41
42
# File 'lib/rigger/task_executor.rb', line 40

def call
  instance_eval(&@task.block)
end

#capture(command) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rigger/task_executor.rb', line 62

def capture(command)
  "".tap do |captured|
    execute(command, [@current_servers.first]) do |ch|
      ch.on_data do |c, data|
        captured << data
      end

      ch.on_extended_data do |c, type, data|
        data.split("\n").each do |line|
          $stderr.puts " ** [#{@current_servers.first.connection_string} :: stderr] #{line}"
          $stderr.flush
        end
      end
    end
  end
end

#capture_all(command) ⇒ Object



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/rigger/task_executor.rb', line 79

def capture_all(command)
  {:stdout => "", :stderr => ""}.tap do |captured|
    execute(command, [@current_servers.first]) do |ch|
      ch.on_data do |c, data|
        captured[:stdout] << data
      end

      ch.on_extended_data do |c, type, data|
        captured[:stderr] << data
      end
    end
  end
end

#erl_call(cookie, node, code) ⇒ Object



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rigger/task_executor.rb', line 93

def erl_call(cookie, node, code)
  streams = capture_all("echo '#{code}' | erl_call -e -c #{cookie} -n #{node}")

  if !streams[:stderr].empty?
    raise CommandError, "erl_call #{code} failed on #{@current_servers.first.connection_string}: #{streams[:stderr]}"
  end

  if !streams[:stdout].include?('{ok,')
    raise CommandError, "erl_call #{code} failed on #{@current_servers.first.connection_string}: #{streams[:stdout]}"
  end
end

#fetch(name, default) ⇒ Object



138
139
140
# File 'lib/rigger/task_executor.rb', line 138

def fetch(name, default)
  @config.fetch(name, default)
end

#get(name) ⇒ Object



130
131
132
# File 'lib/rigger/task_executor.rb', line 130

def get(name)
  @config.get(name)
end

#put(data, path) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
# File 'lib/rigger/task_executor.rb', line 142

def put(data, path)
  servers  = @current_servers.dup
  channels = servers.map do |s|
    callback = Proc.new do |channel, name, sent, total|
      puts "[#{channel[:host]}] #{name}" if sent == 0
    end

    SFTPTransferWrapper.new(s.connection) do |sftp|
      io = StringIO.new(data.respond_to?(:read) ? data.read : data)
      sftp.upload(io, path, {}) do |status, we|
        if status == :finish
          sftp.close_channel
        end
      end
    end
  end

  puts "  * transerring data to #{path}"

  connections = servers.dup

  failing_servers = []
  errors          = []

  loop do
    connections.delete_if do |server|
      begin
        !server.connection.process(0.1) { |s| s.busy? }
      rescue Net::SFTP::StatusException => e
        failing_servers << server
        errors << e.message
      end
    end
    break if connections.empty?
  end

  if !failing_servers.empty?
    raise CommandError, "Upload failed on #{failing_servers.map { |s| s.connection_string }.inspect} with #{errors.join(", ")}."
  end

  puts "  * finished"
end

#run(command) ⇒ Object



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/rigger/task_executor.rb', line 44

def run(command)
  execute(command, @current_servers) do |ch|
    ch.on_data do |c, data|
      data.split("\n").each do |line|
        $stdout.puts" ** [#{ch[:host]} :: stdout] #{line}"
        $stdout.flush
      end
    end

    ch.on_extended_data do |c, type, data|
      data.split("\n").each do |line|
        $stderr.puts" ** [#{ch[:host]} :: stderr] #{line}"
        $stderr.flush
      end
    end
  end
end

#run_locally(command) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
# File 'lib/rigger/task_executor.rb', line 109

def run_locally(command)
  puts "  * executing `#{command}` locally"
  status = POpen4.popen4(command) do |stdout, stderr, stdin, pid|
    stdout.each_line do |line|
      $stdout.puts " ** [locally :: stdout] #{line}"
      $stdout.flush
    end

    stderr.each_line do |line|
      $stderr.puts " ** [locally :: stderr] #{line}"
      $stderr.flush
    end
  end

  if status && status.exitstatus == 0
    puts "  * command finished"
  else
    raise CommandError, "Local command `#{command}` failed."
  end
end

#run_task(task_name) ⇒ Object



105
106
107
# File 'lib/rigger/task_executor.rb', line 105

def run_task(task_name)
  @execution_service.call(task_name)
end

#set(name, value) ⇒ Object



134
135
136
# File 'lib/rigger/task_executor.rb', line 134

def set(name, value)
  @config.set(name, value)
end