Module: Utils
- Included in:
- Runner
- Defined in:
- lib/utils.rb
Constant Summary collapse
- INSTANCE_MAPPINGS_FILENAME =
File.("~/.ec2/instance_mappings.yml")
Instance Method Summary collapse
- #console_output(instance) ⇒ Object
- #delete_instance_from_datastore(name) ⇒ Object
- #get_instance(name) ⇒ Object
- #get_instances ⇒ Object
- #get_name_for_instance ⇒ Object
- #load_instances_from_datastore ⇒ Object
- #parse_aws_status_line_into_instance(line, name = nil) ⇒ Object
- #run_instance(options = {}) ⇒ Object
- #scp_up(instance, source, destination) ⇒ Object
- #spindown(name) ⇒ Object
- #spinup(options = {}) ⇒ Object
- #ssh(url, options = {}) ⇒ Object
- #terminate_instances(*instances) ⇒ Object
- #write_instance_to_datastore(instance) ⇒ Object
- #write_instances_to_datastore(instances) ⇒ Object
Instance Method Details
#console_output(instance) ⇒ Object
79 80 81 |
# File 'lib/utils.rb', line 79 def console_output instance exec "ec2-get-console-output #{instance.instance_id}" end |
#delete_instance_from_datastore(name) ⇒ Object
127 128 129 130 131 |
# File 'lib/utils.rb', line 127 def delete_instance_from_datastore(name) instances = load_instances_from_datastore instances.delete(name) write_instances_to_datastore(instances) end |
#get_instance(name) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/utils.rb', line 34 def get_instance(name) instance_id = if name =~ /^i-\w{8}$/ name else load_instances_from_datastore[name] end `ec2-describe-instances #{instance_id}`.split("\n").select{ |l| l =~ /^INSTANCE/ }.inject([]) do |result, line| instance = parse_aws_status_line_into_instance(line, name) if instance.status != 'terminated' instance.name = name result << instance else result end end.compact.first end |
#get_instances ⇒ Object
52 53 54 55 56 57 58 59 60 61 62 63 64 |
# File 'lib/utils.rb', line 52 def get_instances instance_mappings = load_instances_from_datastore `ec2-describe-instances`.split("\n").select{|l| l =~ /^INSTANCE/}.inject([]) do |result, line| instance = parse_aws_status_line_into_instance(line) if instance.status != "terminated" instance.name = instance_mappings.find_name_by_instance_id(instance.instance_id) result << instance else result end end end |
#get_name_for_instance ⇒ Object
25 26 27 |
# File 'lib/utils.rb', line 25 def get_name_for_instance Dictionaries.new[:company_names].random.downcase end |
#load_instances_from_datastore ⇒ Object
114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/utils.rb', line 114 def load_instances_from_datastore data = File.read(INSTANCE_MAPPINGS_FILENAME) instances = YAML.load(data) class << instances def find_name_by_instance_id(instance_id) select { |k,v| v == instance_id }[0] end end instances end |
#parse_aws_status_line_into_instance(line, name = nil) ⇒ Object
29 30 31 32 |
# File 'lib/utils.rb', line 29 def parse_aws_status_line_into_instance line, name=nil columns = line.split("\t") instance = Instance.new line, columns[1], columns[2], columns[5], columns[3], name end |
#run_instance(options = {}) ⇒ Object
11 12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/utils.rb', line 11 def run_instance(={}) instance_size = .delete(:instance_size) image_id = .delete(:ami_id) name = .delete(:name) zone = .delete(:zone) user_data_file = .delete(:user_data_file) = "-t #{instance_size}" += " -f #{user_data_file}" if user_data_file += " -z #{zone}" if zone instance = parse_aws_status_line_into_instance `ec2-run-instances #{image_id} -k #{EC2_SSH_KEY_NAME} #{instance_options}`.split("\n")[-1], name write_instance_to_datastore(instance) instance end |
#scp_up(instance, source, destination) ⇒ Object
75 76 77 |
# File 'lib/utils.rb', line 75 def scp_up instance, source, destination exec "scp -i #{EC2_SSH_KEY_PATH} #{source} root@#{instance.url}:#{destination}" end |
#spindown(name) ⇒ Object
133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 |
# File 'lib/utils.rb', line 133 def spindown name puts "Spinning down #{name}" instance = get_instance(name) raise "Unknown instance name #{name}" unless instance terminate_instances instance while instance puts "waiting...#{instance.line}" sleep 2 instance = get_instance(name) end delete_instance_from_datastore(name) puts "Terminated #{name}" end |
#spinup(options = {}) ⇒ Object
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 |
# File 'lib/utils.rb', line 83 def spinup(={}) puts "Spinning up image" name = [:name] current_instance = instance = run_instance() puts "Instance #{instance.instance_id} spinning up" still_loading = true while still_loading puts "waiting...#{current_instance.line}" sleep 2 current_instance = get_instance(name) still_loading = current_instance.status == "pending" end puts "Instance running!" puts "name: #{current_instance.name}, id: #{current_instance.instance_id}, url: #{current_instance.url}" end |
#ssh(url, options = {}) ⇒ Object
70 71 72 73 |
# File 'lib/utils.rb', line 70 def ssh(url, ={}) puts url exec "ssh -i #{options[:keypair_path]} #{options[:user]}@#{url}" end |
#terminate_instances(*instances) ⇒ Object
66 67 68 |
# File 'lib/utils.rb', line 66 def terminate_instances *instances `ec2-terminate-instances #{instances.map{|i| i.instance_id}.join(' ')}` end |
#write_instance_to_datastore(instance) ⇒ Object
102 103 104 105 106 |
# File 'lib/utils.rb', line 102 def write_instance_to_datastore(instance) instances = load_instances_from_datastore || {} instances.merge!({instance.name => instance.instance_id}) write_instances_to_datastore(instances) end |
#write_instances_to_datastore(instances) ⇒ Object
108 109 110 111 112 |
# File 'lib/utils.rb', line 108 def write_instances_to_datastore(instances) File.open(INSTANCE_MAPPINGS_FILENAME, "w+") do |f| f << instances.to_yaml end end |