stemcell 0.2.2 → 0.2.3

Sign up to get free protection for your applications and to get access to all the features.
data/README.md CHANGED
@@ -63,8 +63,11 @@ $ ssh unbutu@$IP 'tail -f /var/log/init*'
63
63
 
64
64
  ### Terminating:
65
65
 
66
- This still needs to be completed. For now, you can kill using the
67
- amazon cli tools or the web ui.
66
+ To terminate, use the necrosis command and pass a space seperated list of instance ids:
67
+
68
+ ```bash
69
+ $ necrosis i-12345678 i-12345679 i-12345670
70
+ ```
68
71
 
69
72
  ## Automation ##
70
73
 
data/bin/necrosis CHANGED
@@ -2,15 +2,12 @@
2
2
 
3
3
  # -*- mode: shell -*-
4
4
 
5
- require_relative '../lib/stemcell'
6
-
5
+ require 'aws-sdk'
7
6
  require 'trollop'
8
7
 
9
8
  options = Trollop::options do
10
- version "stemcell #{Stemcell::VERSION} (c) 2013 Martin Rhoads"
11
- banner <<END_OF_BANNER
12
- welcome to stemcell
13
- END_OF_BANNER
9
+ version "stemcell #{Stemcell::VERSION} (c) Airbnb, Inc."
10
+ banner "Necrosis: the killing script"
14
11
 
15
12
  opt('aws_access_key',
16
13
  "aws access key",
@@ -23,39 +20,61 @@ END_OF_BANNER
23
20
  :type => String,
24
21
  :default => ENV['AWS_SECRET_KEY']
25
22
  )
26
-
27
- opt('region',
28
- 'ec2 region to launch in',
29
- :type => String,
30
- :default => ENV['REGION'] ? ENV['REGION'] : 'us-east-1',
31
- )
32
-
33
23
  end
34
24
 
35
-
36
25
  required_parameters = [
37
26
  'aws_access_key',
38
27
  'aws_secret_key',
39
28
  ]
40
29
 
41
-
42
30
  required_parameters.each do |arg|
43
31
  raise ArgumentError, "--#{arg.gsub('_','-')} needs to be specified on the commandline or set \
44
32
  by the #{arg.upcase.gsub('-','_')} environment variable" if
45
33
  options[arg].nil? or ! options[arg]
46
34
  end
47
35
 
48
-
49
36
  raise ArgumentError, "you did not provide any instance ids to kill" if ARGV.size == 0
50
37
 
38
+ # a hash from instance_id => [ec2 instance objects]
39
+ instances = ARGV.inject({}) { |h, n| h[n] = []; h }
51
40
 
52
- # create stemcell object
53
- stemcell = Stemcell::Stemcell.new({
54
- 'aws_access_key' => options['aws_access_key'],
55
- 'aws_secret_key' => options['aws_secret_key'],
56
- 'region' => options['region'],
57
- })
58
-
41
+ ec2 = AWS::EC2.new(:access_key_id => options['aws_access_key'], :secret_access_key => options['aws_secret_key'])
42
+ ec2.regions.each do |region|
43
+ instances.each do |id, objects|
44
+ instance = region.instances[id]
45
+ objects << [instance, region] if instance.exists?
46
+ end
47
+ end
59
48
 
60
- # launch instance(s)
61
- stemcell.kill(ARGV)
49
+ # kill the instances
50
+ instances.each do |id, objects|
51
+ if objects.count == 0
52
+ puts "No instance #{id} found"
53
+ next
54
+ end
55
+
56
+ if objects.count > 1
57
+ puts "Found multiple instances named #{id}"
58
+ next
59
+ end
60
+
61
+ instance, region = objects[0]
62
+ if instance.api_termination_disabled?
63
+ puts "Cannot terminate instance #{id} -- termination protection enabled"
64
+ next
65
+ end
66
+
67
+ puts "Instance #{id} (#{instance.status} in #{region.name})"
68
+ puts "\tKey name: #{instance.key_name}"
69
+ puts "\tLaunched: #{instance.launch_time}"
70
+ instance.tags.to_h.each do |k, v|
71
+ puts "\t#{k} : #{v}"
72
+ end
73
+
74
+ puts "Terminate? (y/N)? "
75
+ confirm = $stdin.gets
76
+ if confirm && confirm.chomp.downcase == 'y'
77
+ instance.terminate
78
+ puts "Instance #{id} terminated"
79
+ end
80
+ end
data/bin/stemcell CHANGED
@@ -7,10 +7,8 @@ require_relative '../lib/stemcell'
7
7
  require 'trollop'
8
8
 
9
9
  options = Trollop::options do
10
- version "stemcell #{Stemcell::VERSION} (c) 2013 Martin Rhoads"
11
- banner <<END_OF_BANNER
12
- welcome to stemcell
13
- END_OF_BANNER
10
+ version "stemcell #{Stemcell::VERSION} (c) 2013 Airbnb"
11
+ banner "Stemcell: get ready to rock"
14
12
 
15
13
  opt('aws_access_key',
16
14
  "aws access key",
@@ -48,10 +46,10 @@ END_OF_BANNER
48
46
  :default => ENV['SECURITY_GROUPS'] ? ENV['SECURITY_GROUPS'] : 'default',
49
47
  )
50
48
 
51
- opt('availability-zone',
49
+ opt('availability_zone',
52
50
  'zone in which to launch instances',
53
51
  :type => String,
54
- :default => ENV['AVAILABILITY-ZONE'],
52
+ :default => ENV['AVAILABILITY_ZONE'],
55
53
  )
56
54
 
57
55
  opt('tags',
@@ -154,7 +152,8 @@ stemcell = Stemcell::Stemcell.new({
154
152
 
155
153
  # launch instance(s)
156
154
  stemcell.launch({
157
- 'instance_type' => options['instance_type'],
155
+ 'availability_zone' => options['availability_zone'],
156
+ 'instance_type' => options['instance_type'],
158
157
  'image_id' => options['image_id'],
159
158
  'security_groups' => options['security_groups'],
160
159
  'chef_role' => options['chef_role'],
@@ -164,5 +163,6 @@ stemcell.launch({
164
163
  'git_key' => options['git_key'],
165
164
  'git_origin' => options['git_origin'],
166
165
  'key_name' => options['key_name'],
166
+ 'tags' => options['tags'],
167
167
  'count' => options['count'],
168
168
  })
data/lib/stemcell.rb CHANGED
@@ -25,7 +25,6 @@ module Stemcell
25
25
 
26
26
  AWS.config({:access_key_id => @aws_access_key, :secret_access_key => @aws_secret_key})
27
27
  @ec2 = AWS::EC2.new(:ec2_endpoint => @ec2_url)
28
- @ec2_region = @ec2.regions[@region]
29
28
  end
30
29
 
31
30
 
@@ -48,7 +47,7 @@ module Stemcell
48
47
  opts['git_key'] = try_file(opts['git_key'])
49
48
  opts['chef_data_bag_secret'] = try_file(opts['chef_data_bag_secret'])
50
49
 
51
- # generate tags and merge in any that were specefied as in inputs
50
+ # generate tags and merge in any that were specefied as inputs
52
51
  tags = {
53
52
  'Name' => "#{opts['chef_role']}-#{opts['chef_environment']}",
54
53
  'Group' => "#{opts['chef_role']}-#{opts['chef_environment']}",
@@ -57,10 +56,7 @@ module Stemcell
57
56
  }
58
57
  tags.merge!(opts['tags']) if opts['tags']
59
58
 
60
- # generate user data script to boot strap instance based on the
61
- # opts that we were passed.
62
- user_data = render_template(opts)
63
-
59
+ # generate launch options
64
60
  launch_options = {
65
61
  :image_id => opts['image_id'],
66
62
  :security_groups => opts['security_groups'],
@@ -68,9 +64,13 @@ module Stemcell
68
64
  :instance_type => opts['instance_type'],
69
65
  :key_name => opts['key_name'],
70
66
  :count => opts['count'],
71
- :user_data => user_data,
72
67
  }
73
- launch_options.merge({:availability_zone => opts['availability_zone']}) if opts['availability_zone']
68
+
69
+ # specify availability zone (optional)
70
+ launch_options[:availability_zone] = opts['availability_zone'] if opts['availability_zone']
71
+
72
+ # generate user data script to bootstrap instance, include in launch optsions
73
+ launch_options[:user_data] = render_template(opts)
74
74
 
75
75
  # launch instances
76
76
  instances = do_launch(launch_options)
@@ -86,12 +86,15 @@ module Stemcell
86
86
  return instances
87
87
  end
88
88
 
89
- def kill(instance_list=[])
90
- @log.info "killing instances #{instance_list}"
91
- instances = instance_list.map {|id| @ec2.instances[id]}
89
+ def find_instance(id)
90
+ return @ec2.instances[id]
91
+ end
92
+
93
+ def kill(instances)
94
+ return if instances.nil?
92
95
  instances.each do |instance|
96
+ log.warn "Terminating instance #{instance.instance_id}"
93
97
  instance.terminate
94
- @log.info "killed instance #{instance.id}"
95
98
  end
96
99
  end
97
100
 
@@ -113,7 +116,7 @@ module Stemcell
113
116
  while true
114
117
  sleep 5
115
118
  if Time.now - @start_time > @timeout
116
- bail(instances)
119
+ kill(instances)
117
120
  raise TimeoutError, "exceded timeout of #{@timeout}"
118
121
  end
119
122
 
@@ -136,7 +139,7 @@ module Stemcell
136
139
  def do_launch(opts={})
137
140
  @log.debug "about to launch instance(s) with options #{opts}"
138
141
  @log.info "launching instances"
139
- instances = @ec2_region.instances.create(opts)
142
+ instances = @ec2.instances.create(opts)
140
143
  instances = [instances] unless instances.class == Array
141
144
  instances.each do |instance|
142
145
  @log.info "launched instance #{instance.instance_id}"
@@ -162,14 +165,6 @@ module Stemcell
162
165
  return generated_template
163
166
  end
164
167
 
165
- def bail(instances)
166
- return if instances.nil?
167
- instances.each do |instance|
168
- log.warn "Terminating instance #{instance.instance_id}"
169
- instance.delete
170
- end
171
- end
172
-
173
168
  # attempt to accept keys as file paths
174
169
  def try_file(opt="")
175
170
  begin
@@ -1,3 +1,3 @@
1
1
  module Stemcell
2
- VERSION = "0.2.2"
2
+ VERSION = "0.2.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: stemcell
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-03-01 00:00:00.000000000 Z
12
+ date: 2013-03-02 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: trollop