Created
January 31, 2013 17:30
-
-
Save theist/4684603 to your computer and use it in GitHub Desktop.
Para Arranca y lista maquinas de amazon. es un gist, no busques el readme, seria mas largo que el codigo
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 'yaml' | |
require 'rubygems' | |
require 'fog' | |
require 'thor' | |
require 'colorize' | |
class Main < Thor | |
$config = YAML.load_file('config.yml') | |
desc "list_keys", "Lists credentials" | |
def list_keys | |
for item in $config.each_key | |
puts item | |
end | |
end | |
desc "list_machines", "List machines in account/region" | |
def list_machines(account) | |
ec2 = Fog::Compute.new({:provider => 'AWS', | |
:aws_access_key_id => $config[account]['aws_access_key_id'], | |
:aws_secret_access_key => $config[account]['aws_secret_access_key']}) | |
servers = ec2.servers; | |
if servers.empty? | |
puts "No hay servidores en #{account}".black | |
return | |
end | |
for server in servers | |
if server.tags.has_key? 'Name' | |
puts "#{server.tags['Name']}".red + " Server: " + "#{server.id}".yellow + " Size: #{server.flavor_id} Zone: #{server.availability_zone} Status: #{server.state}" | |
else | |
puts "Server: " + "#{server.id}".yellow + " Size: #{server.flavor_id} Zone: #{server.availability_zone} Status: #{server.state}" | |
end | |
if server.tags.has_key? 'Notas' | |
puts " #{server.tags['Notas']}".black | |
end | |
end | |
end | |
desc "list_all_machines", "Lists All machines" | |
def list_all_machines | |
for account in $config.each_key | |
puts ">> " + "#{account}".blue | |
list_machines(account) | |
end | |
end | |
desc "start", "Starts a Server" | |
def start(account, server_id) | |
ec2 = Fog::Compute.new({:provider => 'AWS', | |
:aws_access_key_id => $config[account]['aws_access_key_id'], | |
:aws_secret_access_key => $config[account]['aws_secret_access_key']}) | |
server = ec2.servers.get(server_id) | |
if server.empty? | |
puts "No such server" | |
return | |
end | |
case server.state | |
when "terminated" | |
puts "Terminated servers cannot be started" | |
when "running" | |
puts "Server already running" | |
when "stopped" | |
server.start | |
puts "Starting server" | |
else | |
puts "Unknow state #{server.state}" | |
end | |
end | |
desc "stop", "Stops a Server" | |
def stop(account, server_id) | |
ec2 = Fog::Compute.new({:provider => 'AWS', | |
:aws_access_key_id => $config[account]['aws_access_key_id'], | |
:aws_secret_access_key => $config[account]['aws_secret_access_key']}) | |
server = ec2.servers.get(server_id) | |
if server.empty? | |
puts "No such server" | |
return | |
end | |
case server.state | |
when "terminated" | |
puts "Terminated servers cannot be stopped" | |
when "running" | |
puts "Stopping server" | |
server.stop | |
when "stopped" | |
puts "Server already running" | |
else | |
puts "Unknow state #{server.state}" | |
end | |
end | |
end | |
Main.start | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment