Class: Owssh
Instance Method Summary collapse
- #get_instances(id) ⇒ Object
- #get_stacks ⇒ Object
- #owssh ⇒ Object
- #print_instances(instances) ⇒ Object
- #print_stacks(stacks) ⇒ Object
Instance Method Details
#get_instances(id) ⇒ Object
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/owssh.rb', line 23 def get_instances(id) my_instances = {} instances_json = JSON.parse(`aws opsworks describe-instances --stack-id #{id}`) instances_json['Instances'].each do |instance| pub_ip = instance['ElasticIp'] || instance['PublicIp'] || "DOWN" priv_ip = instance['PrivateIp'] || "N/A" type = instance['Hostname'].split("-").first my_instances[instance['Hostname'].to_s] = { "PUB_IP" => pub_ip.to_s, "PRIV_IP" => priv_ip.to_s, "TYPE" => type } end my_instances end |
#get_stacks ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 |
# File 'lib/owssh.rb', line 11 def get_stacks my_stacks = {} stacks_json = JSON.parse(`aws opsworks describe-stacks`) stacks_json['Stacks'].each do |stack| if $debug then puts "Stack Name: #{stack['Name'].gsub(' ','_').downcase} Stack ID: #{stack['StackId']}" end stack_name = stack['Name'].gsub(' ','_').downcase my_stacks[stack_name.to_s] = stack['StackId'] end my_stacks end |
#owssh ⇒ Object
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 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 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 |
# File 'lib/owssh.rb', line 69 def owssh # Export environment variables for AWS CLI here $debug = false $ssh_key_file = "~/.ssh/id_rsa_dev" $owssh_config = "~/.owssh_conf" if ARGV.empty? puts "Usage:" puts "owssh list - List all environments" puts "owssh describe - Show details of hosts in all stacks" puts "owssh describe [Stack Name] - Show details of a specific stack" puts "owssh [Stack Name] [Hostname] - SSH to a host in a stack" puts "owssh [Stack Name] [Hostname] [Command]- SSH to a host in a stack and run a command" exit end $stacks = {} $instances = {} $stacks = get_stacks if ARGV[0] == "list" then puts "Getting list of stacks..." print_stacks($stacks) exit elsif ARGV[0] == "describe" && ARGV[1].nil? then $stacks.each do |stack_name, id| puts "Getting data for Stack: #{stack_name}" $instances = get_instances(id) print_instances($instances) end exit elsif ARGV[0] == "describe" && !ARGV[1].nil? then stack_name = ARGV[1] if $stacks.has_key?(ARGV[1].downcase.to_s) then if $debug then puts "Stack ID: #{$stacks[stack_name]}" end puts "Getting data for Stack: #{stack_name}" $instances = get_instances($stacks[stack_name]) print_instances($instances) elsif puts "Unable to find stack named '#{ARGV[1]}'" exit end elsif $stacks.has_key?(ARGV[0].downcase.to_s) then if ARGV[1].nil? then puts "Please enter an instance name. I.E. rails-app3" exit end stack_arg = ARGV[0].downcase instances_json = JSON.parse(`aws opsworks describe-instances --stack-id #{$stacks[stack_arg]}`) instances_json['Instances'].each do |instance| $instances["#{instance["Hostname"]}"] = instance["PublicIp"] end if $instances.has_key?(ARGV[1]) then if ARGV[2].nil? then puts "Opening connection to #{ARGV[1]}..." exec("ssh -i ~/.ssh/id_rsa_dev ubuntu@#{$instances[ARGV[1].to_s]}") else puts "Running comand #{ARGV[2]} on host #{ARGV[1]}..." exec("ssh -i ~/.ssh/id_rsa_dev ubuntu@#{$instances[ARGV[1].to_s]} '#{ARGV[2]}'") end else puts "Instance with name '#{ARGV[1]}' not found" exit end else puts "I don't quite understand what you're asking me to do..." puts " Try running owssh with no arguments for help!" exit end end |
#print_instances(instances) ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/owssh.rb', line 35 def print_instances(instances) table(:border => true) do row do column('Hostname', :width => 20) column('Public IP', :width => 15, :align => 'right') column('Private IP', :width => 15, :align => 'right') column('Type', :width => 12) end instances.each do |instance_name, data| row do column(instance_name) column(data['PUB_IP']) column(data['PRIV_IP']) column(data['TYPE']) end end end end |
#print_stacks(stacks) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/owssh.rb', line 54 def print_stacks(stacks) table(:border => true) do row do column('Stack Name', :width => 20) column('Stack ID', :width => 40, :align => 'right') end stacks.each do |stack_name, id| row do column(stack_name) column(id) end end end end |