lib/awsssh.rb in awsssh-3.0.0 vs lib/awsssh.rb in awsssh-3.1.0
- old
+ new
@@ -1,14 +1,27 @@
require "awsssh/version"
require "thor"
require "inifile"
require "aws-sdk"
+require "colorize"
module Awsssh
class Awsssh < Thor
+ def initialize(*args)
+ super
+ @text_colors = {
+ :infotext => :cyan,
+ :infotext_sub => :yellow,
+ :status => {
+ :online => :green,
+ :stopped => :light_red
+ }
+ }
+ end
+
desc "list_profiles", "List all your avavible profiles"
def list_profiles()
credentials = open_credantial_file
puts "List of all known AWS Accounts"
puts
@@ -16,34 +29,65 @@
puts " #{section}"
end
end
desc "list_server PROFILE", "List all Server for given profile"
+ method_option :all, :type => :boolean, :aliases => "-a", :default => false, :desc => "Show all Server"
def list_server(profile)
credentials = open_credantial_file
- raise "Profile `#{profile}` not found. Please try `awsssh list_profiles`" if credentials[profile].empty?
+
+ puts "Profile `#{profile}` not found. Try `awsssh list_profiles`" if credentials[profile].empty?
+ exit -1 if credentials[profile].empty?
+
aws_connection(profile, credentials)
- puts "Stacks and instances for profile `#{profile}`"
+ puts "Stacks and instances for profile `#{profile}`".colorize(@text_colors[:infotext])
+ puts "only online server".colorize(@text_colors[:infotext_sub]) unless options[:all]
puts
+ server_number = 0
+ server_online = []
+
@client.describe_stacks.stacks.each do |stack|
puts "##### Stack: #{stack.name}"
@client.describe_instances({stack_id: stack.stack_id}).instances.each do |instance|
- printf " %-20s status: %-11s %s\n" % [instance.hostname, instance.status, public_dns(instance)]
+ if instance.status == "online"
+ server_number+=1
+ server_online[server_number] = public_dns = public_dns(instance)
+ printf("[%02d]" % [server_number])
+ else
+ print " "
+ public_dns = "-"
+ end
+ printf " %-20s status: %-11s %s\n".colorize(@text_colors[:status][instance.status.to_sym]) % [instance.hostname, instance.status, public_dns] if instance.status == "online" or options[:all]
end
puts ""
end
+ while true
+ print "Would you like to connect to any server directly? Please enter Server number (Enter to exit): "
+ server_to_connect = STDIN.gets.chomp
+ if server_to_connect.to_i != 0 and !server_online[server_to_connect.to_i].nil?
+ puts "connecting to #{server_online[server_to_connect.to_i]}"
+ puts
+ puts
+ connect_server server_online[server_to_connect.to_i]
+ elsif server_to_connect.to_i > server_online.length+1
+ puts "No Server sellected"
+ puts
+ else
+ break;
+ end
+ end
end
desc "connect SERVERNAME", "Connect to Hostname"
- option :profile, :desc => "specify a profile - see `awsssh list_profiles`"
+ method_option :profile, :desc => "specify a profile - see `awsssh list_profiles`"
def connect (hostname)
hostname_parts = hostname.split("-")
profile = options[:profile] || hostname_parts[0]
credentials = open_credantial_file
- puts "Profile `#{profile}` not found. Try `awsssh SERVERNAME --profile PROFILE`" if credentials[profile].empty?
+ puts "Profile `#{profile}` not found. Try `awsssh connect SERVERNAME --profile PROFILE`" if credentials[profile].empty?
exit -1 if credentials[profile].empty?
aws_connection(profile, credentials)
puts "looking for hostname `#{hostname}` in profile `#{profile}`. This may take a while..."
puts
@@ -51,17 +95,15 @@
if public_dns.nil?
puts "`#{hostname}` was not found or has no public ip."
puts "Try `awsssh list_server PROFILE`"
puts "checking your local ssh config..."
puts
- exec "ssh #{hostname}"
- exit -1
+ connect_server hostname
else
puts "start ssh connection to `#{hostname}`..."
puts
- exec "ssh #{public_dns}"
- exit 0
+ connect_server public_dns
end
end
desc "version", "Version"
def version
@@ -119,8 +161,13 @@
@client.describe_instances({stack_id: stack.stack_id}).instances.each do |instance|
return public_dns(instance) if instance.hostname == hostname
end
end
return nil
+ end
+
+ def connect_server(hostname)
+ exec "ssh #{hostname}"
+ exit 0
end
end
end