#! /usr/bin/env ruby require 'rubygems' require 'fog' require 'readline' require 'optparse' REGION = 'us-west-1' SLEEP = 45 KEYS = '/home/mike/.ssh/alexdevkey.pem' USER = 'ubuntu' ACCESS_KEY_FILE = '/Users/rhomobile/.ec2' LOCAL_REPO = '/home/mike/git/rhoconnect.tgz' REMOTE_REPO = "/home/#{USER}/git/rhoconnect" STACKS = [ { :image_id => 'ami-3d491a78', :flavor_id => 'm1.small', :key_name => 'alexdevkey', :groups => 'load-test' }, {} ] # cmd # easily issue system commands in a clear way def cmd(cmd) puts cmd puts `#{cmd}` end #cmd # create_ec2_instance # Generates the Fog object and creates a new ec2 instance. def create_ec2_instance make_fog start_new_instance end #create_ec2_instance # get_access_keys # Retrieves the access key and secret access key from the above specified file. def get_access_keys lines = IO.readlines ACCESS_KEY_FILE @access_key = lines.first.strip @secret_access_key = lines.last.strip end #get_access_keys # make_fog # Generates the Fog object used to create the new ec2 instance. def make_fog get_access_keys @fog = Fog::Compute.new( :provider => 'AWS', :region => REGION, :aws_access_key_id => @access_key, :aws_secret_access_key => @secret_access_key ) end #make_fog # start_new_instance # Creates a new ec2 instance as per the given stack. def start_new_instance puts "Creating new instance..." @server = @fog.servers.create(STACKS[0]) @server.wait_for { print "."; STDOUT.flush; ready? } puts if @server.ready? # wait for all services to start on remote VM puts "Waiting #{SLEEP} seconds for services to start on remote VM..." SLEEP.times do sleep( 1 ) print "." STDOUT.flush end #do puts # save important instance information @host = @server.dns_name @server_id = @server.id else puts "Server timed out." exit end #if end #start_new_instance # establish_ssh_connection # Establishes the ssh connection needed to issue commands to the remote machine. def establish_ssh_connection @ssh = Fog::SSH.new( @host, USER, :keys => KEYS ) end #establish_ssh_connection # establish_scp_connection # Establishes a gateway through which to transfer data def establish_scp_connection @scp = Fog::SCP.new( @host, USER, :keys => KEYS ) end #create_scp_connection # ssh_cmd # More easily issue commands over the ssh connection. def ssh_cmd(cmd) puts cmd result = @ssh.run(cmd) if result[0] != nil print result[0].stdout end #if end #ssh_cmd # def transfer(local_data, remote_data) puts "Attempting to establish SCP connection..." puts "DATA: LOCAL - #{local_data}\t REMOTE - #{remote_data}" @scp.upload( local_data, remote_data, :recursive => true ) do |cd, name, sent, total| print "\r#{name}: #{(sent.to_f * 100 / total.to_f).to_i}%" end #do puts end #establish_scp_connection # transfer_rhoconnect # Transfers over SCP the rhoconnect repo def transfer_rhoconnect ssh_cmd [ "sudo mkdir -p #{REMOTE_REPO}", "sudo chmod 777 #{REMOTE_REPO}" ] transfer LOCAL_REPO, "#{REMOTE_REPO}" ssh_cmd "sudo tar -xzf -C #{REMOTE_REPO} #{REMOTE_REPO}/rhoconnect.tgz" ssh_cmd "ls -la #{REMOTE_REPO}" end #transfer_rhoconnect # destroy_ec2_instance # Terminates the current ec2 instance def destroy_ec2_instance @server.destroy puts "Instance Terminated." end #destroy_ec2_instance create_ec2_instance # Establish connections establish_ssh_connection establish_scp_connection transfer_rhoconnect destroy_ec2_instance