Last active
December 16, 2015 12:19
-
-
Save jeevanullas/5433988 to your computer and use it in GitHub Desktop.
Work in Progress for EC2 functionality test
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
require 'rubygems' | |
require 'yaml' | |
require 'aws-sdk' | |
require 'logger' | |
require 'net/http' | |
require 'net/ssh' | |
AWS.config({ | |
:access_key_id => '<Access Key>', | |
:secret_access_key => '<Secret Key>', | |
:ec2_endpoint => '<CLC IP address>', | |
:ec2_port => 8773, | |
:ec2_service_path => '/services/Eucalyptus', | |
:use_ssl => false, | |
:http_wire_trace => true, | |
}) | |
#Initialize stuff | |
key_pair_name = "awsrubysdk-keypair-#{Time.now.to_i}" | |
security_group_name = "awsrubysdk-group-#{Time.now.to_i}" | |
#Get a connection | |
ec2=AWS::EC2.new | |
#Search for a EMI | |
ec2.images.each do |image| | |
if image.type == "machine".to_sym | |
@emi=AWS::EC2::Image.new(image.id) | |
break | |
end | |
end | |
#Create a keypair | |
key_pair=ec2.key_pairs.create(key_pair_name) | |
#Create a security group and authorize port 22 | |
group = ec2.security_groups.create(security_group_name) | |
group.authorize_ingress(:tcp, 22, "0.0.0.0/0") | |
#launch the instance | |
instance = @emi.run_instance(:key_pair => key_pair, | |
:security_groups => group) | |
sleep 10 while instance.status == :pending | |
puts "Launched instance #{instance.id}, status: #{instance.status}" | |
exit 1 unless instance.status == :running | |
begin | |
Net::SSH.start(instance.ip_address, "ubuntu", | |
:key_data => [key_pair.private_key]) do |ssh| | |
puts "Running 'uname -a' on the instance yields:" | |
puts ssh.exec!("uname -a") | |
end | |
rescue SystemCallError, Timeout::Error => e | |
#port 22 might not be available immediately after the instance finishes launching | |
sleep 60 | |
retry | |
end | |
#Cleanup here | |
[instance, | |
group, | |
key_pair].compact.each(&:delete) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment