Class: Runner

Inherits:
Object
  • Object
show all
Includes:
MessageLogger
Defined in:
lib/screwcap/runner.rb

Constant Summary collapse

@@silent =
false

Class Method Summary collapse

Methods included from MessageLogger

included

Class Method Details

.execute!(options) ⇒ Object



5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/screwcap/runner.rb', line 5

def self.execute! options
  @@silent = options[:silent]
  @@verbose = options[:verbose]
  task = options[:task]
  results = []

  if (task.__servers.nil? or task.__servers == [] or task.__servers.compact == []) and task.__built_commands.any? {|c| c[:type] == :remote or c[:type] == :scp }
    raise Screwcap::ConfigurationError, "The task #{task.name} includes remote commands, however no servers were defined for this task."
  end

  if options[:servers] and task.__servers
    servers = options[:servers].select {|s| task.__servers.include? s.__name }
    connections = servers.map {|server| server.connect! }.flatten
  end

  _log "\nExecuting task #{task.name}\n", :color => :blue

  task.__built_commands.each do |command|
    ret = case command[:type]
    when :remote
      if command[:parallel] == false or command[:block]
        connections.each do |connection|
          results << run_remote_command(command, connection[:connection], options)
          if command[:block]
            opts = task.__options.clone.merge(:stderr => command[:stderr], :stdout => command[:stdout], :exit_code => command[:exit_code])
            opts[:servers] = task.__servers
            opts[:name] = "Run results"

            inner_task = Task.new(opts, &command[:block])
            inner_task.__build_commands(options[:tasks])
            results << self.execute!(options.merge(:task => inner_task))
          end
        end
      else
        threads = []
        connections.each do |connection|
          threads << Thread.new(connection) do |conn|
            results << run_remote_command(command, conn[:connection], options)
          end
        end
        threads.each {|t| t.join }
      end
    when :local
      result = {}
      result[:stdout] = `#{command[:command]}`
      result[:exit_code] = $?.to_i
      results << result
      if $?.to_i == 0
        if options[:verbose]
          _log "    O: #{ret}\n", :color => :green
        else
          _log(".", :color => :green)
        end
      else
        _errorlog("    E: (local): #{command[:command]} return exit code: #{$?}\n", :color => :red) if $? != 0
      end
    when :scp
      threads = []
      servers.each do |server|
        threads << Thread.new(server) { |_server| _server.upload! command[:local], command[:remote] }
      end
      threads.each {|t| t.join }
    when :block
      command[:block].call
    end
  end
  _log "Complete\n", :color => :blue
  results
end