stemcell 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore CHANGED
@@ -17,4 +17,5 @@ test/version_tmp
17
17
  tmp
18
18
  .stemcellrc
19
19
  \#*\#
20
- .\#*
20
+ .\#*
21
+ .*sw?
data/bin/stem CHANGED
@@ -24,76 +24,82 @@ END_OF_BANNER
24
24
  :default => ENV['AWS_SECRET_KEY']
25
25
  )
26
26
 
27
- opt('chef_data_bag_secret',
28
- 'path to chef data bag encryption secret',
27
+ opt('region',
28
+ 'ec2 region to launch in',
29
29
  :type => String,
30
- :default => ENV['CHEF_DATA_BAG_SECRET'],
30
+ :default => ENV['REGION'] ? ENV['REGION'] : 'us-east-1',
31
31
  )
32
32
 
33
- opt('chef_role',
34
- 'chef role of instance to be launched',
33
+ opt('machine_type',
34
+ 'instance type to launch',
35
35
  :type => String,
36
- :default => ENV['CHEF_ROLE'],
36
+ :default => ENV['MACHINE_TYPE'] ? ENV['MACHINE_TYPE'] : 'm1.small',
37
37
  )
38
38
 
39
- opt('git_branch',
40
- 'git branch to run off',
39
+ opt('image',
40
+ 'ami to use for launch',
41
41
  :type => String,
42
- :default => ENV['GIT_BRANCH'],
42
+ :default => ENV['AMI'] ? ENV['AMI'] : 'ami-d726abbe',
43
43
  )
44
44
 
45
- opt('git_key',
46
- 'git key to use',
45
+ opt('security_group',
46
+ 'security group to launch instance with',
47
47
  :type => String,
48
- :default => ENV['GIT_KEY'],
48
+ :default => ENV['SECURITY_GROUP'] ? ENV['SECURITY_GROUP'] : 'default',
49
49
  )
50
50
 
51
- opt('git_origin',
52
- 'git origin to use',
51
+ opt('availability-zone',
52
+ 'zone in which to launch instances',
53
53
  :type => String,
54
- :default => ENV['GIT_ORIGIN'],
54
+ :default => ENV['AVAILABILITY-ZONE'],
55
+ )
56
+
57
+ opt('tags',
58
+ 'tags to apply to image',
59
+ :type => String,
60
+ :default => ENV['TAGS'],
55
61
  )
56
62
 
57
63
  opt('key_name',
58
- 'ssh key to launch instance with',
64
+ 'aws ssh key name for the ubuntu user',
59
65
  :type => String,
60
66
  :default => ENV['KEY_NAME'],
61
67
  )
62
68
 
63
- opt('count',
64
- 'number of instances to launch',
69
+ opt('chef_data_bag_secret',
70
+ 'path to secret file (or the string containing the secret)',
65
71
  :type => String,
66
- :default => ENV['COUNT'],
72
+ :default => ENV['CHEF_DATA_BAG_SECRET'],
67
73
  )
68
74
 
69
- opt('region',
70
- 'ec2 region to launch in',
75
+ opt('chef_role',
76
+ 'chef role of instance to be launched',
71
77
  :type => String,
72
- :default => ENV['REGION'],
78
+ :default => ENV['CHEF_ROLE'],
73
79
  )
74
80
 
75
- opt('image',
76
- 'ami to use for launch',
81
+ opt('git_origin',
82
+ 'git origin to use',
77
83
  :type => String,
78
- :default => ENV['AMI'],
84
+ :default => ENV['GIT_ORIGIN'],
79
85
  )
80
86
 
81
- opt('security_group',
82
- 'security group to launch instance with',
87
+ opt('git_branch',
88
+ 'git branch to run off',
83
89
  :type => String,
84
- :default => ENV['SECURITY_GROUP'],
90
+ :default => ENV['GIT_BRANCH'],
85
91
  )
86
92
 
87
- opt('machine_type',
88
- 'instance type to launch',
93
+ opt('git_key',
94
+ 'path to the git repo deploy key (or the string containing the key)',
89
95
  :type => String,
90
- :default => ENV['MACHINE_TYPE'],
96
+ :default => ENV['GIT_KEY'],
91
97
  )
92
98
 
93
- opt('tags',
94
- 'tags to apply to image',
99
+ opt('count',
100
+ 'number of instances to launch',
95
101
  :type => String,
96
- :default => ENV['TAGS'],
102
+ :default => ENV['COUNT'],
97
103
  )
98
104
 
99
105
  end
@@ -114,6 +120,16 @@ by the #{arg.upcase.gsub('-','_')} environment variable" if
114
120
  options[arg].nil? or ! options[arg]
115
121
  end
116
122
 
123
+ # convert tags from string to ruby hash
124
+ tags = {}
125
+ if options['tags']
126
+ options['tags'].split(',').each do |tag_set|
127
+ key, value = tag_set.split('=')
128
+ tags[key] = value
129
+ end
130
+ end
131
+ options['tags'] = tags
132
+
117
133
  stemcell = Stemcell::Stemcell.new(options)
118
134
  stemcell.launch({'count' => options['count']})
119
135
 
@@ -1,3 +1,3 @@
1
1
  module Stemcell
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
data/lib/stemcell.rb CHANGED
@@ -11,6 +11,11 @@ module Stemcell
11
11
  @log.debug "opts are #{opts.inspect}"
12
12
  ['aws_access_key',
13
13
  'aws_secret_key',
14
+ 'region',
15
+ 'machine_type',
16
+ 'image',
17
+ 'security_group',
18
+
14
19
  'chef_role',
15
20
  'git_branch',
16
21
  'git_key',
@@ -21,10 +26,7 @@ module Stemcell
21
26
  instance_variable_set("@#{req}",opts[req])
22
27
  end
23
28
 
24
- @security_group = opts['security_group'] ? opts['security_group'] : 'default'
25
- @image = opts['image'] ? opts['image'] : 'ami-d726abbe'
26
- @machine_type = opts['machine_type'] ? opts['machine_type'] : 'm1.small'
27
- @region = opts['region'] ? opts['region'] : 'us-east-1'
29
+ @zone = opts.include?('availability_zone') ? opts['availability_zone'] : nil
28
30
  @ec2_url = "ec2.#{@region}.amazonaws.com"
29
31
  @timeout = 120
30
32
  @start_time = Time.new
@@ -35,29 +37,19 @@ module Stemcell
35
37
  'created_by' => ENV['USER'],
36
38
  'stemcell' => VERSION,
37
39
  }
38
-
39
- if opts['tags']
40
- tags = {}
41
- opts['tags'].split(',').each do |tag_set|
42
- key, value = tag_set.split('=')
43
- tags[key] = value
44
- end
45
- @tags.merge!(tags)
46
- end
40
+ @tags.merge!(opts['tags']) if opts['tags']
47
41
 
48
42
  begin
49
43
  @git_key_contents = File.read(@git_key)
50
44
  rescue Object => e
51
- # TODO(mkr): we may want to do something better here
52
- @git_key_contents = @chef_validation_key
53
- # raise "\ncould not open specified key #{@git_key}:\n#{e.inspect}#{e.backtrace}"
45
+ @git_key_contents = @git_key # assume content is passed in
54
46
  end
55
47
 
56
- if opts['chef_data_bag_secret']
48
+ if opts.include?('chef_data_bag_secret')
57
49
  begin
58
50
  @chef_data_bag_secret = File.read(opts['chef_data_bag_secret'])
59
51
  rescue Object => e
60
- raise "\ncould not open specified secret key file #{opts['chef_data_bag_secret']}:\n#{e.inspect}#{e.backtrace}"
52
+ @chef_data_bag_secret = opts['chef_data_bag_secret'] # assume secret is passed in
61
53
  end
62
54
  else
63
55
  @chef_data_bag_secret = ''
@@ -91,21 +83,21 @@ module Stemcell
91
83
  end
92
84
 
93
85
  def wait(instances)
94
- sleep 3
86
+ @log.info "Waiting for #{instances.count} instances (#{instances.inspect}):"
87
+
95
88
  while true
89
+ sleep 5
96
90
  if Time.now - @start_time > @timeout
97
91
  bail(instances)
98
92
  raise TimeoutError, "exceded timeout of #{@timeout}"
99
93
  end
100
- puts "instances is #{instances.inspect}"
94
+
101
95
  if instances.select{|i| i.status != :running }.empty?
102
- @log.info "all instances in running state"
103
- return
96
+ break
104
97
  end
105
- @log.info "instances not ready yet. sleeping..."
106
- sleep 5
107
- return wait(instances)
108
98
  end
99
+
100
+ @log.info "all instances in running state"
109
101
  end
110
102
 
111
103
  def do_launch(opts={})
@@ -116,8 +108,9 @@ module Stemcell
116
108
  :instance_type => @machine_type,
117
109
  :key_name => @key_name,
118
110
  }
119
- options.merge!({:availability_zone => opts['avilibility_zone']}) if opts['availability_zone']
120
- options.merge!({:count => opts['count']}) if opts['count']
111
+ options[:availability_zone] = @zone if @zone
112
+ options[:count] = opts['count'] if opts.include?('count')
113
+
121
114
  instances = @ec2_region.instances.create(options)
122
115
  instances = [instances] unless instances.class == Array
123
116
  instances.each do |instance|
@@ -145,9 +138,9 @@ module Stemcell
145
138
  def bail(instances)
146
139
  return if instances.nil?
147
140
  instances.each do |instance|
141
+ log.warn "Terminating instance #{instance.instance_id}"
148
142
  instance.delete
149
143
  end
150
144
  end
151
-
152
145
  end
153
146
  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.0.5
4
+ version: 0.0.6
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: