Last active
December 15, 2015 15:19
-
-
Save korczis/5280519 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /usr/bin/env ruby | |
require 'rubygems' | |
require 'fog' | |
require 'fileutils' | |
require 'yaml' | |
module Apollo | |
class AwsConsole | |
AWS_CONFIG_FILE = File.expand_path("~/.apollo/config/apollo-aws.yml") | |
AWS_DEFAULT_CONFIG = { | |
:provider => "aws", | |
:aws_access_key_id => "", | |
:aws_secret_access_key => "", | |
:aws_default_instance => "i-a4039bee", | |
:region => "eu-west-1" | |
} | |
attr_accessor :connection | |
attr_accessor :config | |
def initialize | |
self.connection = nil | |
self.config = nil | |
end | |
def config_init(path) | |
self.config = AWS_DEFAULT_CONFIG | |
# print "Your AWS_ACCESS_KEY_ID? > " | |
# self.config[:aws_access_key_id] = STDIN.gets.chomp! | |
# print "Your AWS_SECRET_ACCESS_KEY? > " | |
# self.config[:aws_secret_access_key] = STDIN.gets.chomp! | |
AWS_DEFAULT_CONFIG.each do |k, v| | |
print "Override '#{k}' (default: '#{v}') ? > " | |
val = STDIN.gets.chomp! | |
if val.empty? | |
self.config[k] = v | |
else | |
self.config[k] = val | |
end | |
end | |
config_save(AWS_CONFIG_FILE, self.config) | |
end | |
def config_save(path, config) | |
dir = File.dirname(path) | |
if(Dir.exists?(dir) == false) | |
FileUtils.mkpath(dir) | |
end | |
File.open(path, 'w+') do |f| | |
f.write(config.to_yaml) | |
end | |
return config | |
end | |
def config_load(path) | |
self.config = YAML.load_file(path) | |
end | |
def connect() | |
return self.connection if self.connection | |
aws_provider_keys = [:provider, :aws_access_key_id, :aws_secret_access_key, :region] | |
config = self.config.reject { |key, value| !aws_provider_keys.include?(key) } | |
begin | |
self.connection = Fog::Compute.new(config) | |
rescue Exception => e | |
puts "Unable to connect to AWS, reason: #{e.to_s}" | |
return -1 | |
end | |
return self.connection | |
end | |
def get_instance(instance_id) | |
puts "Getting instance '#{instance_id}'" | |
self.connection.servers.get(instance_id) | |
end | |
def run_cmd(cmd) | |
if cmd == "help" | |
puts "Supported commands - init, info, interactive, list, start, stop" | |
return 0 | |
end | |
if self.config.nil? | |
if(File.exists?(AWS_CONFIG_FILE) == false) | |
self.config = config_init(AWS_CONFIG_FILE) | |
else | |
self.config = config_load(AWS_CONFIG_FILE) | |
end | |
end | |
case cmd | |
when "info" | |
instance = self.config[:aws_default_instance] | |
puts "Inspecting instance '#{instance}'" | |
connect() | |
server = get_instance(instance) | |
puts server.inspect | |
return 0 | |
when "init" | |
config_init(AWS_CONFIG_FILE) | |
return 0 | |
when "list" | |
connect() | |
instance_list = connection.servers.all | |
instance_list.table([:id, :flavor_id, :public_ip_address, :private_ip_address, :image_id ]) | |
return 0 | |
when "start" | |
instance = self.config[:aws_default_instance] | |
puts "Starting instance '#{instance}'" | |
connect() | |
server = get_instance(instance) | |
res = server.start | |
puts " => Success: #{res}" | |
puts " => DNS: #{server.dns_name}" | |
return 0 | |
when "stop" | |
instance = self.config[:aws_default_instance] | |
puts "Stopping instance '#{instance}'" | |
connect() | |
server = get_instance(instance) | |
res = server.stop | |
puts " => Success: #{res}" | |
puts " => DNS: #{server.dns_name}" | |
return 0 | |
end | |
puts "Unknown command '#{cmd}'" | |
return -1 | |
end | |
def run() | |
cmd = ARGV.length > 0 ? ARGV[0] : "help" | |
if cmd == "interactive" | |
print "> " | |
while cmd = STDIN.gets.chomp! | |
break if cmd.downcase == "quit" | |
res = run_cmd(cmd) | |
print "> " | |
end | |
return 0 | |
end | |
return run_cmd(cmd) | |
end | |
end | |
end | |
if __FILE__ == $0 | |
Apollo::AwsConsole::new.run() | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment