Class: Commands::MultiSSHInstance
- Inherits:
-
Object
- Object
- Commands::MultiSSHInstance
- Defined in:
- lib/commands/multi_ssh_instance.rb
Instance Method Summary collapse
-
#options ⇒ Object
holds the options that were passed you can set any initial defaults here.
- #register(opts, global_options) ⇒ Object
-
#required_options ⇒ Object
required options.
- #run(global_options, amazon) ⇒ Object
Instance Method Details
#options ⇒ Object
holds the options that were passed you can set any initial defaults here
9 10 11 12 |
# File 'lib/commands/multi_ssh_instance.rb', line 9 def ||= { } end |
#register(opts, global_options) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/commands/multi_ssh_instance.rb', line 21 def register(opts, ) opts. = "Usage: multi_ssh [options]" opts.description = "Run a remote command on multiple servers via ssh" opts.on('-i', "--instances instance1,instance2,etc", Array, "The instance(s) to connect to.") do |v| [:instances] = v end opts.on('-r', "--role role", MetaOptions.roles, "Role to look for.") do |v| [:role] = v end opts.on('-g', "--group deploy_group", "Required: Group to look for.") do |v| [:group] = v end opts.on('-f', "--file file", "A file that contains the commands to execute.") do |v| [:file] = v end opts.on('-p', "--print path", "The directory into which we output the data as a file per host.") do |v| [:result_path] = v end end |
#required_options ⇒ Object
required options
15 16 17 18 19 |
# File 'lib/commands/multi_ssh_instance.rb', line 15 def ||= Set.new [ :group ] end |
#run(global_options, amazon) ⇒ Object
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 74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/commands/multi_ssh_instance.rb', line 47 def run(, amazon) ec2 = amazon.ec2 file_path = [:file] if file_path.nil? if ARGV.length != 1 raise "Must include the remote command to run. Make sure you quote it so it appears as one argument" end remote_cmd = ARGV[0] else remote_cmd = File.open(file_path, 'r') {|f| f.read } end group_name = [:group] deploy_group = amazon.find_deploy_group(group_name) group_config = deploy_group.config user_instances = [:instances] if user_instances.nil? instances = amazon.find_and_sort_named_instances([:group], [:role]) else instances = [] # build the instances wanted here server_instances = amazon.find_and_sort_named_instances(nil, nil, false) server_instances.each do |server_instance| server_instance_id = server_instance[:resource_id] if user_instances.include?(server_instance_id) instances << server_instance end end end if instances.empty? raise "No instances matched your search criteria." end multi = MultiSSH.new(amazon, group_name, deploy_group) multi.run_instances(instances, remote_cmd) multi.output_tracked_data_to_files("multi_ssh", [:result_path]) end |