Module: Launch

Included in:
Ec2InstanceManager
Defined in:
lib/ec2-instance-manager/launch.rb

Instance Method Summary collapse

Instance Method Details

#launchObject



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/ec2-instance-manager/launch.rb', line 2

def launch
  puts
  puts "Which AMI Id do you want to launch?"
  ami_id = gets
  ami_id = ami_id.lstrip.rstrip

  result = launch_ami(ami_id)

  instance_id = result.instancesSet.item[0].instanceId
  puts "=> Starting Instance Id: #{instance_id}"
  puts

  instance_state = nil
  while(instance_state != 'running')
    instance_state, dns_name = get_instance_state(instance_id)
    puts "=> Checking for running state... #{output_running_state(instance_state)}"
    puts "=> Public DNS: #{white(dns_name)}" if instance_state == 'running'
    sleep 10 unless instance_state == 'running'
  end

  puts

  puts "Do you want to associate a public IP? (leave empty if not)"
  public_ip = gets

  if not public_ip.empty? and public_ip.size > 1
    puts "Associating public IP address... #{public_ip}"
    result = ec2.associate_address(:instance_id => instance_id, :public_ip => public_ip.lstrip.rstrip)
    if result["return"] == "true"
      puts "=> Success."
    end
  end

  puts
  puts "Please enter your volume id (leave empty if you don't want to attach a volume):"
  volume_id = gets

  if not volume_id.empty? and volume_id.size > 1
    puts "Attaching volume... #{volume_id}"
    result = ec2.attach_volume(:volume_id => volume_id.lstrip.rstrip, :instance_id => instance_id, :device => '/dev/sdf')
    if result["return"] == "true"
      puts "=> Success."
    end
  end
end

#launch_ami(ami_id, options = {}) ⇒ Object



163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/ec2-instance-manager/launch.rb', line 163

def launch_ami(ami_id, options = {})
  default_options = {
    :instance_type => config[@customer_key]['instance_type'],
  	:key_name => config[@customer_key]['key'],
  	:availability_zone => config[@customer_key]['availability_zone'],
  	:architecture => config[@customer_key]['architecture'],
  	:image_id => ami_id
  }
  
  run_options = default_options.merge(options)
  puts "Launch Options: #{run_options.inspect}"
  ec2.run_instances(run_options)
end

#start_launch_planObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
# File 'lib/ec2-instance-manager/launch.rb', line 63

def start_launch_plan
  ami_ids_to_launch = []; detailed_ami_ids_to_launch = []
  puts
  puts "Your Launch Plan:"
  if config[@customer_key]["launch_plan"] and config[@customer_key]["launch_plan"].any?

    launch_plan_groups = config[@customer_key]["launch_plan"].keys.sort

    if self.options[:group]
      puts 
      puts "Existing groups: #{launch_plan_groups.join(", ")}"
      if launch_plan_groups.include?(self.options[:group])
        puts "Targeted Group: #{self.options[:group]}"
        config[@customer_key]["launch_plan"][self.options[:group]].each do |ami_id|
          if (ami_id[1].is_a?(Fixnum))
            puts "#{ami_id[0]} => #{ami_id[1]} Instances to launch"
            ami_ids_to_launch << [ami_id[0], ami_id[1]]
          else
            puts "#{ami_id[0]} => Detailed launch: #{ami_id[1]}"
            detailed_ami_ids_to_launch << [ami_id[0], ami_id[1]]
          end
        end
      else
        puts white("Targeted Launch plan group '#{self.options[:group]}' not found.")
      end
    else
      launch_plan_groups.each do |launch_plan_group|
        puts
        puts "Group: #{launch_plan_group}"
        if config[@customer_key]["launch_plan"][launch_plan_group] and config[@customer_key]["launch_plan"][launch_plan_group].any?
          config[@customer_key]["launch_plan"][launch_plan_group].keys.each do |ami_id|
            if (config[@customer_key]["launch_plan"][launch_plan_group][ami_id].is_a?(Fixnum))
              puts "#{ami_id} => #{config[@customer_key]["launch_plan"][launch_plan_group][ami_id]} Instances to launch"
              ami_ids_to_launch << [ami_id, config[@customer_key]["launch_plan"][launch_plan_group][ami_id]]
            else
              puts "#{ami_id} => Detailed launch: #{config[@customer_key]["launch_plan"][launch_plan_group][ami_id]}"
              detailed_ami_ids_to_launch << [ami_id, config[@customer_key]["launch_plan"][launch_plan_group][ami_id]]
            end
          end
        else
          puts white("No Ami Id's to launch defined.")
        end
      end
    end
    
    puts
    puts red("Warning: Now launching your plan...")
    puts red("Please press CTRL-C to cancel within the next 5 seconds if this isn't what you want...")
    puts
    sleep 5
    
    ami_ids_to_launch.each do |group_ami_id_pair|
      group_ami_id_pair[1].times {
        puts "Launching #{group_ami_id_pair[0]}..."
        result = launch_ami(group_ami_id_pair[0])
      }
    end
    puts if ami_ids_to_launch.any?
    
    detailed_ami_ids_to_launch.each do |group_ami_id_pair|
      your_instance_name = group_ami_id_pair[0]
      instance_assignments = group_ami_id_pair[1].split(";")
      ami_id = instance_assignments[0]
      raise "Incomplete detailed launchplan definition." if instance_assignments.size < 3
      
      puts "Launching '#{your_instance_name}'..."
      architecture = instance_assignments[1]; instance_type = instance_assignments[2]
      result = launch_ami(ami_id, {:instance_type => instance_type, :architecture => architecture})
      
      if result and result["instancesSet"]["item"] and (instance_id = result["instancesSet"]["item"][0]["instanceId"])
        instance_state = ''
        while(instance_state != 'running') do
          instance_state, dns_name = get_instance_state(instance_id)
          puts "Running state of instance #{instance_id}: #{output_running_state(instance_state)}"
          sleep 5
        end

        if instance_assignments[3] and not instance_assignments[3].empty?
          puts "Associating IP #{instance_assignments[3]}..."
          result = ec2.associate_address(:instance_id => instance_id, :public_ip => instance_assignments[3])
        end
        
        if instance_assignments[4] and not instance_assignments[4].empty?
          volumes = instance_assignments[4].split(",")
          volumes.each do |volume_pair|
            volume = volume_pair.split("@")
            puts "Attaching volume #{volume[0]} at mount point #{volume[1]}..."
            result = ec2.attach_volume(:volume_id => volume[0], :instance_id => instance_id, :device => volume[1])
          end
        end
      end
      puts
    end

    puts white("Launchplan executed!")
  else
    puts white("No launch plan groups defined.")
  end
end

#terminate(wait = true) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/ec2-instance-manager/launch.rb', line 48

def terminate(wait = true)
  instances = get_running_instances_list
  if instances and instances.any?
    puts
    cancel_message(instances)
    sleep 5
    ec2.terminate_instances(:instance_id => instances)
    puts
    puts white("All instances are going to terminate now.")
  else
    puts
    puts white("No running instances.")
  end
end