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 +5 -2
- data/bin/necrosis +44 -25
- data/bin/stemcell +7 -7
- data/lib/stemcell.rb +17 -22
- data/lib/stemcell/version.rb +1 -1
- metadata +2 -2
data/README.md
CHANGED
@@ -63,8 +63,11 @@ $ ssh unbutu@$IP 'tail -f /var/log/init*'
|
|
63
63
|
|
64
64
|
### Terminating:
|
65
65
|
|
66
|
-
|
67
|
-
|
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
|
-
|
6
|
-
|
5
|
+
require 'aws-sdk'
|
7
6
|
require 'trollop'
|
8
7
|
|
9
8
|
options = Trollop::options do
|
10
|
-
version "stemcell #{Stemcell::VERSION} (c)
|
11
|
-
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
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
#
|
61
|
-
|
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
|
11
|
-
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('
|
49
|
+
opt('availability_zone',
|
52
50
|
'zone in which to launch instances',
|
53
51
|
:type => String,
|
54
|
-
:default => ENV['
|
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
|
-
'
|
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
|
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
|
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
|
-
|
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
|
90
|
-
@
|
91
|
-
|
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
|
-
|
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 = @
|
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
|
data/lib/stemcell/version.rb
CHANGED
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.
|
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-
|
12
|
+
date: 2013-03-02 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: trollop
|