ec2-instance-manager 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/Rakefile CHANGED
@@ -10,6 +10,7 @@ begin
10
10
  gem.email = "kiessler@inceedo.com"
11
11
  gem.homepage = "http://github.com/okiess/ec2-instance-manager"
12
12
  gem.authors = ["Oliver Kiessler"]
13
+ gem.add_dependency "amazon-ec2"
13
14
  gem.add_development_dependency "thoughtbot-shoulda"
14
15
  # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
16
  end
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/config.yml.sample CHANGED
@@ -5,6 +5,11 @@ default:
5
5
  key:
6
6
  availability_zone: eu-west-1a
7
7
  instance_type: m1.small
8
+ launch_plan:
9
+ group1:
10
+ "ami_id_xxx1": 1
11
+ group2:
12
+ "ami_id_xxx2": 1
8
13
 
9
14
  customer1:
10
15
  amazon_access_key_id:
@@ -21,4 +26,6 @@ customer2:
21
26
  key:
22
27
  availability_zone: eu-west-1a
23
28
  instance_type: m1.small
24
-
29
+ launch_plan:
30
+ group1:
31
+ ami_id_xxx1: 1
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{ec2-instance-manager}
8
- s.version = "0.2.1"
8
+ s.version = "0.3.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Oliver Kiessler"]
12
- s.date = %q{2009-10-09}
12
+ s.date = %q{2009-10-16}
13
13
  s.default_executable = %q{ec2-instance-manager}
14
14
  s.description = %q{Launches EC2 instances for multiple AWS accounts}
15
15
  s.email = %q{kiessler@inceedo.com}
@@ -31,6 +31,7 @@ Gem::Specification.new do |s|
31
31
  "lib/ec2-instance-manager.rb",
32
32
  "lib/ec2-instance-manager/ec2_instance_manager.rb",
33
33
  "lib/ec2-instance-manager/launch.rb",
34
+ "lib/ec2-instance-manager/output.rb",
34
35
  "lib/ec2-instance-manager/status.rb",
35
36
  "test/ec2-instance-manager_test.rb",
36
37
  "test/test_helper.rb"
@@ -1,10 +1,10 @@
1
1
  require File.dirname(__FILE__) + '/status'
2
2
  require File.dirname(__FILE__) + '/launch'
3
+ require File.dirname(__FILE__) + '/output'
3
4
 
4
5
  class Ec2InstanceManager
5
- include Status
6
- include Launch
7
- VERSION = '0.2'
6
+ include Status, Launch, Output
7
+ VERSION = '0.3'
8
8
 
9
9
  attr_reader :config, :customer_key, :options
10
10
 
@@ -14,7 +14,7 @@ class Ec2InstanceManager
14
14
  options = {}
15
15
 
16
16
  optparse = OptionParser.new do |opts|
17
- opts.banner = "Usage: ec2-instance-manager [options]"
17
+ opts.banner = "Usage: ec2-instance-manager #{VERSION} [options]"
18
18
 
19
19
  options[:status] = false
20
20
  opts.on( '-s', '--status', 'Status only' ) do
@@ -25,7 +25,12 @@ class Ec2InstanceManager
25
25
  opts.on( '-t', '--terminate-all', 'Terminates all instances running under a config key' ) do
26
26
  options[:terminate] = true
27
27
  end
28
-
28
+
29
+ options[:start_launch_plan] = false
30
+ opts.on( '-l', '--start-launch-plan', 'Starts a launch plan under a config key' ) do
31
+ options[:start_launch_plan] = true
32
+ end
33
+
29
34
  options[:config] = nil
30
35
  opts.on( '-c', '--config CONFIG_KEY', 'Sets the config key' ) do |key|
31
36
  options[:config] = key
@@ -44,9 +49,11 @@ class Ec2InstanceManager
44
49
  def config; @config ||= read_config; end
45
50
  def read_config
46
51
  if File.exists?("config.yml")
52
+ puts "Using config in this directory"
47
53
  @config = YAML.load(File.read("config.yml"))
48
54
  else
49
55
  begin
56
+ puts "Using config in your home directory"
50
57
  @config = YAML.load(File.read("#{ENV['HOME']}/.ec2_instance_manager_config.yml"))
51
58
  rescue Errno::ENOENT
52
59
  raise "config.yml expected in current directory or ~/.ec2_instance_manager_config.yml"
@@ -60,7 +67,7 @@ class Ec2InstanceManager
60
67
  end
61
68
 
62
69
  def run
63
- puts "EC2 Instance Manager"
70
+ puts "EC2 Instance Manager #{VERSION}"
64
71
  puts
65
72
  unless options[:config]
66
73
  puts "Which customer config do you want to use? (#{config.keys.join(", ")})"
@@ -71,7 +78,7 @@ class Ec2InstanceManager
71
78
  @customer_key = options[:config]
72
79
  end
73
80
 
74
- puts "Configuration: #{@customer_key}"
81
+ puts "Configuration Key: #{@customer_key}"
75
82
  puts "AMAZON_ACCESS_KEY_ID: #{config[@customer_key]['amazon_access_key_id']}"
76
83
  puts "KEY: #{config[@customer_key]['key']}"
77
84
  puts "ZONE: #{config[@customer_key]['availability_zone']}"
@@ -81,6 +88,8 @@ class Ec2InstanceManager
81
88
 
82
89
  if options[:terminate]
83
90
  terminate
91
+ elsif options[:start_launch_plan]
92
+ start_launch_plan
84
93
  else
85
94
  launch unless options[:status]
86
95
  end
@@ -5,12 +5,7 @@ module Launch
5
5
  ami_id = gets
6
6
  ami_id = ami_id.lstrip.rstrip
7
7
 
8
- result = ec2.run_instances(
9
- :image_id => ami_id,
10
- :instance_type => config[@customer_key]['instance_type'],
11
- :key_name => config[@customer_key]['key'],
12
- :availability_zone => config[@customer_key]['availability_zone']
13
- )
8
+ result = launch_ami(ami_id)
14
9
 
15
10
  instance_id = result.instancesSet.item[0].instanceId
16
11
  puts "=> Starting Instance Id: #{instance_id}"
@@ -54,21 +49,71 @@ module Launch
54
49
  instances = get_running_instances_list
55
50
  if instances and instances.any?
56
51
  puts
57
- puts "Warning: Terminating all instances: #{instances.join(", ")}"
58
- puts "Please cancel within the next 5 seconds..."
52
+ puts red("Warning: Terminating all instances: #{instances.join(", ")}")
53
+ puts red("Please cancel within the next 5 seconds...")
59
54
  sleep 5
60
55
  ec2.terminate_instances(:instance_id => instances)
61
56
  puts
62
57
  puts "All instances are going to terminate now."
58
+ else
59
+ puts
60
+ puts "No running instances."
61
+ end
62
+ end
63
+
64
+ def start_launch_plan
65
+ ami_ids_to_launch = []
66
+ puts
67
+ puts "Your Launch Plan:"
68
+ if config[@customer_key]["launch_plan"] and config[@customer_key]["launch_plan"].any?
69
+ config[@customer_key]["launch_plan"].keys.sort.each do |launch_plan_group|
70
+ puts
71
+ puts "Group: #{launch_plan_group}"
72
+ if config[@customer_key]["launch_plan"][launch_plan_group] and config[@customer_key]["launch_plan"][launch_plan_group].any?
73
+ config[@customer_key]["launch_plan"][launch_plan_group].keys.each do |ami_id|
74
+ puts "#{ami_id} => #{config[@customer_key]["launch_plan"][launch_plan_group][ami_id]} Instances to launch"
75
+ ami_ids_to_launch << [ami_id, config[@customer_key]["launch_plan"][launch_plan_group][ami_id]]
76
+ end
77
+ else
78
+ puts "No Ami Id's to launch defined."
79
+ end
80
+ end
81
+
82
+ puts
83
+ puts red("Warning: Now launching your plan...")
84
+ puts red("Please cancel within the next 5 seconds...")
85
+ puts
86
+ sleep 5
87
+
88
+ ami_ids_to_launch.each do |group_ami_id_pair|
89
+ group_ami_id_pair[1].times {
90
+ puts "Launching #{group_ami_id_pair[0]}..."
91
+ result = launch_ami(group_ami_id_pair[0])
92
+ }
93
+ end
94
+
95
+ puts "All instances are launching now..."
96
+ else
97
+ puts "No launch groups defined."
63
98
  end
64
99
  end
100
+
101
+ def launch_ami(ami_id)
102
+ ec2.run_instances(
103
+ :image_id => ami_id,
104
+ :instance_type => config[@customer_key]['instance_type'],
105
+ :key_name => config[@customer_key]['key'],
106
+ :availability_zone => config[@customer_key]['availability_zone']
107
+ )
108
+ end
65
109
 
110
+ private
66
111
  def get_running_instances_list
67
112
  result = ec2.describe_instances
68
113
  if result and result["reservationSet"]
69
114
  result["reservationSet"]["item"].collect do |item|
70
115
  item["instancesSet"]["item"].first["instanceId"] if item["instancesSet"]["item"].first["instanceState"]["name"] == 'running'
71
- end
116
+ end.compact
72
117
  end
73
118
  end
74
119
  end
@@ -0,0 +1,25 @@
1
+ module Output
2
+ def output_running_state(running_state)
3
+ if running_state == 'running'
4
+ green(running_state)
5
+ elsif running_state == 'terminated'
6
+ red(running_state)
7
+ elsif running_state == 'pending'
8
+ yellow(running_state)
9
+ else
10
+ running_state
11
+ end
12
+ end
13
+
14
+ def green(str)
15
+ "\033[32m#{str}\033[0m"
16
+ end
17
+
18
+ def red(str)
19
+ "\033[31m#{str}\033[0m"
20
+ end
21
+
22
+ def yellow(str)
23
+ "\033[33m#{str}\033[0m"
24
+ end
25
+ end
@@ -23,13 +23,13 @@ module Status
23
23
  ami_id = item["instancesSet"]["item"].first["imageId"]
24
24
  running_state = item["instancesSet"]["item"].first["instanceState"]["name"]
25
25
  dns_name = item["instancesSet"]["item"].first["dnsName"]
26
- puts "Instance Id: #{instance_id} - #{running_state} (AMI Id: #{ami_id}) #{dns_name}"
26
+ puts "Instance Id: #{instance_id} - #{output_running_state(running_state)} (AMI Id: #{ami_id}) #{dns_name}"
27
27
  end
28
28
  else
29
29
  puts "No instances."
30
30
  end
31
31
  end
32
-
32
+
33
33
  def display_addresses
34
34
  result = ec2.describe_addresses
35
35
  if result and result["addressesSet"]
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ec2-instance-manager
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.1
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oliver Kiessler
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-10-09 00:00:00 +02:00
12
+ date: 2009-10-16 00:00:00 +02:00
13
13
  default_executable: ec2-instance-manager
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -44,6 +44,7 @@ files:
44
44
  - lib/ec2-instance-manager.rb
45
45
  - lib/ec2-instance-manager/ec2_instance_manager.rb
46
46
  - lib/ec2-instance-manager/launch.rb
47
+ - lib/ec2-instance-manager/output.rb
47
48
  - lib/ec2-instance-manager/status.rb
48
49
  - test/ec2-instance-manager_test.rb
49
50
  - test/test_helper.rb