Class: Commands::SSHInstance
- Inherits:
-
Object
- Object
- Commands::SSHInstance
- Defined in:
- lib/commands/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/ssh_instance.rb', line 9 def @options ||= { } end |
#register(opts, global_options) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/commands/ssh_instance.rb', line 21 def register(opts, ) opts. = "Usage: ssh [options]" opts.description = "SSH into a server" opts.on('-i', "--instance instance", "The instance to connect to..") do |v| [:instance] = 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 end |
#required_options ⇒ Object
required options
15 16 17 18 19 |
# File 'lib/commands/ssh_instance.rb', line 15 def @required_options ||= Set.new [ :group ] end |
#run(global_options, amazon) ⇒ Object
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 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/commands/ssh_instance.rb', line 39 def run(, amazon) ec2 = amazon.ec2 group_name = [:group] deploy_group = amazon.find_deploy_group(group_name) group_config = deploy_group.config pick = nil instance_id = [:instance] if instance_id.nil? instances = amazon.find_and_sort_named_instances([:group], [:role]) else instances = amazon.find_and_sort_named_instances() instances.each do |instance| resource_id = instance[:resource_id] if resource_id == instance_id pick = instance break end end if pick.nil? raise "The instance you specified was not a valid ZangZing deployed instance." end end if pick.nil? if instances.length == 0 raise "No instance matched your search criteria." end pick = instances[0] if instances.length > 1 # more than one puts "More than one instance matched, pick from list below the instance you want." i = 1 instances.each do |instance| name = instance[:Name] resource_id = instance[:resource_id] puts "#{i}) #{name} => #{resource_id}" i += 1 end print "Type the one you want to use: " r = Readline.readline() pick_num = r.to_i if pick_num < 1 || pick_num > instances.length raise "Your pick was not in range." end pick = instances[pick_num - 1] end end # ok, we have a pick lets ssh to it ec2_instance = ec2.describe_instances(pick[:resource_id])[0] dns_name = ec2_instance[:dns_name] puts "Running SSH for #{pick[:Name]}" ssh_cmd = "ssh -i ~/.ssh/#{group_config[:amazon_security_key]}.pem ec2-user@#{dns_name}" ZZSharedLib::CL.do_cmd ssh_cmd end |