Class: Befog::Commands::Run

Inherits:
Object
  • Object
show all
Includes:
Mixins::Command, Mixins::Configurable, Mixins::Help, Mixins::Safely, Mixins::Scope, Mixins::Selectable
Defined in:
lib/befog/commands/run.rb

Instance Attribute Summary

Attributes included from Mixins::Command

#options

Instance Method Summary collapse

Methods included from Mixins::Help

included

Methods included from Mixins::Selectable

included, #run_for_selected

Methods included from Mixins::Safely

#safely

Methods included from Mixins::Scope

#_bank, #account_key, #account_secret, #bank, #bank?, #bank_name, #banks, #compute, #flavor, #flavor?, #get_server, #image, #image?, #keypair, #keypair?, #price, #provider, #provider?, #provider_name, #providers, #region, #region?, #security_group, #security_group?, #servers, #servers=

Methods included from Mixins::Configurable

#_configuration, #configuration, #configuration_name, #configuration_path, included, #save

Methods included from Mixins::Command

#command, #error, included, #initialize, #log, #process_options, #usage

Instance Method Details

#runObject



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
# File 'lib/befog/commands/run.rb', line 39

def run
  script = if options[:command] and not options[:shell]
    $stdout.puts "Running command '#{options[:command]}' ..."
    options[:command]
  elsif options[:shell] and not options[:command]
    $stdout.puts "Running script '#{options[:shell]}' ..."
    # We add the sleep in case the last command is run in the bg
    File.open options[:shell] { |file| file.read + "\nsleep 1"}
  else
    raise "Must specify one of --shell or --command"
  end
  threads = []
  run_for_selected do |id|
    threads << Thread.new do
      safely do
        # TODO: Add check to see if we have a bad ID in the config
        server = get_server(id)
        if server.state == "running"
          address = server.public_ip_address
          $stdout.puts "... for '#{address}'"
          result = Fog::SSH.new(address, "root").run(script).first
          $stdout.puts "#{address} STDOUT: #{result.stdout}"
          $stderr.puts "#{address} STDERR: #{result.stderr}" unless result.stderr.empty?
        else
          $stdout.puts "Server #{id} isn't running - skipping"
        end
      end
    end
  end
  sleep 1 while threads.any? { |t| t.alive? }
end