|
#!/usr/bin/env ruby |
|
|
|
require 'rubygems' |
|
require 'fog' |
|
require 'trollop' |
|
|
|
# read config file |
|
config = eval(File.open(File.dirname(__FILE__) + '/config') {|f| f.read }) |
|
|
|
# check argumens |
|
if ARGV.length == 0 |
|
puts "Usage:" |
|
puts "ruby aws.rb launch [--region region --aws-key aws_key --aws-secret aws_secret --security-group security_group --key-pair key_pair --key-path key_path --ami-id ami_id--flavor-id flavor_id ]" |
|
puts "ruby aws.rb terminate [--region region --aws-key aws_key --aws-secret aws_secret] --instance-id instance_id" |
|
puts "ruby aws.rb attach [--region region --aws-key aws_key --aws-secret aws_secret] --instance-id instance_id --volume-id volume_id" |
|
end |
|
|
|
# check provided command |
|
case ARGV[0] |
|
when "launch" |
|
ARGV.shift |
|
opts = Trollop::options do |
|
opt :region, "AWS Region on which to spin up the new instance", :default => config[:region] |
|
opt :aws_key, "AWS Access Key", :default => config[:aws_key] |
|
opt :aws_secret, "AWS Secret Access Key", :default => config[:aws_secret] |
|
opt :security_group, "AWS Security group", :default => config[:security_group] |
|
opt :key_pair, "AWS keypair for the new instance", :default => config[:key_pair] |
|
opt :key_path, "Path to the PEM file for ssh access", :default => config[:key_path] |
|
opt :ami_id, "AMI id which to spin up", :default => config[:ami_id] |
|
opt :flavor_id, "Instance size id", :default => config[:flavor_id] |
|
end |
|
# make sure key file exists |
|
Trollop::die :key_path, "must exist" unless File.exist?(File.expand_path(opts[:key_path])) |
|
|
|
# connect to AWS |
|
fog = Fog::Compute.new({:provider => 'AWS', :region => opts[:region], :aws_access_key_id => opts[:aws_key], :aws_secret_access_key => opts[:aws_secret]}) |
|
puts "Creating new instance..." |
|
# create a new instance |
|
server = fog.servers.create(:key_name=>opts[:key_pair], :image_id=>opts[:ami_id], :flavor_id=>'t1.micro', :groups => opts[:security_group]) |
|
#server = fog.servers.last |
|
# set up ssh access |
|
Fog.credentials = Fog.credentials.merge({ |
|
:private_key_path => opts[:key_path], |
|
}) |
|
# wait for the server to become ready |
|
server.wait_for { print "."; ready? } |
|
sleep 60 |
|
#install software |
|
execute_ssh = server.ssh('sudo apt-get update --fix-missing;sudo apt-get install openjdk-6-jdk ant python perl -y') |
|
puts execute_ssh.first.stdout |
|
|
|
puts "New instance id:", server.id.to_s |
|
|
|
# terminate the instance |
|
when "terminate" |
|
ARGV.shift |
|
opts = Trollop::options do |
|
opt :region, "AWS Region on which to spin up the new instance", :default => config[:region] |
|
opt :aws_key, "AWS Access Key", :default => config[:aws_key] |
|
opt :aws_secret, "AWS Secret Access Key", :default => config[:aws_secret] |
|
opt :instance_id, "Instance id", :type => :string |
|
end |
|
Trollop::die :instance_id, "Must be provided" if opts[:instance_id].nil? |
|
# connect to AWS |
|
fog = Fog::Compute.new({:provider => 'AWS', :region => opts[:region], :aws_access_key_id => opts[:aws_key], :aws_secret_access_key => opts[:aws_secret]}) |
|
|
|
server = fog.servers.get(opts[:instance_id]) |
|
# initiate termination |
|
server.destroy |
|
puts "Instance terminated" |
|
|
|
# attach an EBS volume |
|
when "attach" |
|
ARGV.shift |
|
opts = Trollop::options do |
|
opt :region, "AWS Region on which to spin up the new instance", :default => config[:region] |
|
opt :aws_key, "AWS Access Key", :default => config[:aws_key] |
|
opt :aws_secret, "AWS Secret Access Key", :default => config[:aws_secret] |
|
opt :instance_id, "Instance id", :type => :string |
|
opt :volume_id, "Volume id", :type => :string |
|
opt :device, "Device path", :type => :string |
|
end |
|
Trollop::die :instance_id, "Must be provided" if opts[:instance_id].nil? |
|
Trollop::die :volume_id, "Must be provided" if opts[:volume_id].nil? |
|
Trollop::die :device, "Must be provided" if opts[:device].nil? |
|
# connect to AWS |
|
fog = Fog::Compute.new({:provider => 'AWS', :region => opts[:region], :aws_access_key_id => opts[:aws_key], :aws_secret_access_key => opts[:aws_secret]}) |
|
|
|
puts opts[:instance_id] |
|
puts opts[:volume_id] |
|
|
|
# get server and volume info from AWS |
|
server = fog.servers.get(opts[:instance_id]) |
|
volume = fog.volumes.get(opts[:volume_id]) |
|
|
|
volume.device=opts[:device] |
|
volume.server = server |
|
|
|
puts "Volume attached." |
|
else |
|
puts "No arguments provided" |
|
end |
|
|
|
|