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|
          puts " ** [#{server.connection_string} :: stderr] #{line}"
          $stderr.flush
        end
      end
    end
  end
end

#fetch(name, default) ⇒ Object



110
111
112
# File 'lib/rigger/task_executor.rb', line 110

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

#get(name) ⇒ Object



102
103
104
# File 'lib/rigger/task_executor.rb', line 102

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

#put(data, path) ⇒ Object



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/rigger/task_executor.rb', line 114

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|
        puts " ** [#{ch[:host]} :: stdout] #{line}"
        $stdout.flush
      end
    end

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

#run_locally(command) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
# File 'lib/rigger/task_executor.rb', line 83

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

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

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

#run_task(task_name) ⇒ Object



79
80
81
# File 'lib/rigger/task_executor.rb', line 79

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

#set(name, value) ⇒ Object



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

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