stemcell 0.11.6 → 0.11.7

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 98616ae32c94b573ff3fc0d8b3e3ee8609249aba
4
- data.tar.gz: 30f0e404801f617927783cd7bbe97bad2faf0112
3
+ metadata.gz: bbe504f40413724bf87e2b12fac6d6e82da0f47c
4
+ data.tar.gz: 3ee37995a832825a2479fd784d7d02d8ef070875
5
5
  SHA512:
6
- metadata.gz: 54802c4b403fd732bc340aa12b9c33ffc2dddad093a7a2b011399cb3ec88f0fb6fb4e6c676c83527149b29b266c2e828275c47bb6e6c2f2eb3559c86d730f4e9
7
- data.tar.gz: c981c1205c1c10df841b1b5929f525482b1b80053ac3b2dcd41f11bcf8859334778a57c6f9179ea9d6dc83994930ed81ad706ffdda0d1253b1da188989f2de6c
6
+ metadata.gz: acd48d19f76a18638048eae5ce84f4537bcb60a8d036510a60d2aa87d220840973e19fb094216e2a192d5f1f6b5e6b43914e9cf44e302dcb9c89a0b8f9785d01
7
+ data.tar.gz: b929a418da0773876d21785fb7d67fc8ea19e95e989111dae3a8ee13649602299a65caef33eb500750fc5544c19c7a717ef0685c3d5013a2e6e8f97c9d6f3081
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ # 0.11.7
2
+ - Convert security group names to ids when launching VPC instances
3
+ - Allow classic link to associate VPC security groups by name
4
+
1
5
  # 0.11.6
2
6
  - Result of role expansion needs to be a mutable object
3
7
 
@@ -106,14 +106,21 @@ module Stemcell
106
106
  :count => opts['count'],
107
107
  }
108
108
 
109
- if opts['security_groups'] && !opts['security_groups'].empty?
110
- launch_options[:security_groups] = opts['security_groups']
111
- end
112
-
113
109
  if opts['security_group_ids'] && !opts['security_group_ids'].empty?
114
110
  launch_options[:security_group_ids] = opts['security_group_ids']
115
111
  end
116
112
 
113
+ if opts['security_groups'] && !opts['security_groups'].empty?
114
+ if @vpc_id
115
+ # convert sg names to sg ids as VPC only accepts ids
116
+ security_group_ids = get_vpc_security_group_ids(@vpc_id, opts['security_groups'])
117
+ launch_options[:security_group_ids] ||= []
118
+ launch_options[:security_group_ids].concat(security_group_ids)
119
+ else
120
+ launch_options[:security_groups] = opts['security_groups']
121
+ end
122
+ end
123
+
117
124
  # specify availability zone (optional)
118
125
  if opts['availability_zone']
119
126
  launch_options[:availability_zone] = opts['availability_zone']
@@ -299,10 +306,35 @@ module Stemcell
299
306
  check_errors(:set_tags, instances.map(&:id), errors)
300
307
  end
301
308
 
309
+ # Resolve security group names to their ids in the given VPC
310
+ def get_vpc_security_group_ids(vpc_id, group_names)
311
+ group_map = {}
312
+ @log.info "resolving security groups #{group_names} in #{vpc_id}"
313
+ vpc = AWS::EC2::VPC.new(vpc_id, :ec2_endpoint => "ec2.#{@region}.amazonaws.com")
314
+ vpc.security_groups.each do |sg|
315
+ next if sg.vpc_id != vpc_id
316
+ group_map[sg.name] = sg.group_id
317
+ end
318
+ group_ids = []
319
+ group_names.each do |sg_name|
320
+ raise "Couldn't find security group #{sg_name} in #{vpc_id}" unless group_map.has_key?(sg_name)
321
+ group_ids << group_map[sg_name]
322
+ end
323
+ group_ids
324
+ end
325
+
302
326
  def set_classic_link(left_to_process, classic_link)
303
327
  return unless classic_link
304
328
  return unless classic_link['vpc_id']
305
- return unless classic_link['security_group_ids'] && !classic_link['security_group_ids'].empty?
329
+
330
+ security_group_ids = classic_link['security_group_ids'] || []
331
+ security_group_names = classic_link['security_groups'] || []
332
+ return if security_group_ids.empty? && security_group_names.empty?
333
+
334
+ if !security_group_names.empty?
335
+ extra_group_ids = get_vpc_security_group_ids(classic_link['vpc_id'], security_group_names)
336
+ security_group_ids = security_group_ids + extra_group_ids
337
+ end
306
338
 
307
339
  @log.info "applying classic link settings on #{left_to_process.count} instance(s)"
308
340
 
@@ -323,7 +355,7 @@ module Stemcell
323
355
  result = ec2.client.attach_classic_link_vpc({
324
356
  :instance_id => instance.id,
325
357
  :vpc_id => classic_link['vpc_id'],
326
- :groups => classic_link['security_group_ids'],
358
+ :groups => security_group_ids,
327
359
  })
328
360
  result.error
329
361
  rescue StandardError => e
@@ -102,6 +102,12 @@ module Stemcell
102
102
  :type => String,
103
103
  :env => 'CLASSIC_LINK_SECURITY_GROUP_IDS',
104
104
  },
105
+ {
106
+ :name => 'classic_link_security_groups',
107
+ :desc => 'comma-separated list of security groups to link into ClassicLink; not used unless classic_link_vpc_id is set',
108
+ :type => String,
109
+ :env => 'CLASSIC_LINK_SECURITY_GROUPS',
110
+ },
105
111
  {
106
112
  :name => 'subnet',
107
113
  :desc => "VPC subnet for which to launch this instance",
@@ -415,7 +421,10 @@ module Stemcell
415
421
  options['classic_link']['vpc_id'] = options['classic_link_vpc_id']
416
422
  end
417
423
  if options['classic_link_security_group_ids']
418
- options['classic_link']['security_group_ids'] = options['classic_link_security_group_ids']
424
+ options['classic_link']['security_group_ids'] = options['classic_link_security_group_ids'].split(',')
425
+ end
426
+ if options['classic_link_security_groups']
427
+ options['classic_link']['security_groups'] = options['classic_link_security_groups'].split(',')
419
428
  end
420
429
 
421
430
  options
@@ -1,3 +1,3 @@
1
1
  module Stemcell
2
- VERSION = "0.11.6"
2
+ VERSION = "0.11.7"
3
3
  end
@@ -14,6 +14,15 @@ class MockInstance
14
14
  end
15
15
  end
16
16
 
17
+ class MockSecurityGroup
18
+ attr_reader :group_id, :name, :vpc_id
19
+ def initialize(id, name, vpc_id)
20
+ @group_id = id
21
+ @name = name
22
+ @vpc_id = vpc_id
23
+ end
24
+ end
25
+
17
26
  class MockException < StandardError
18
27
  end
19
28
 
@@ -27,6 +36,61 @@ describe Stemcell::Launcher do
27
36
  let(:instances) { (1..4).map { |id| MockInstance.new(id) } }
28
37
  let(:instance_ids) { instances.map(&:id) }
29
38
 
39
+ describe '#launch' do
40
+ let(:ec2) { instance_double(AWS::EC2) }
41
+ let(:client) { double(AWS::EC2::Client) }
42
+ let(:response) { instance_double(AWS::Core::Response) }
43
+ let(:launcher) {
44
+ opts = {'region' => 'region', 'vpc_id' => 'vpc-1'}
45
+ launcher = Stemcell::Launcher.new(opts)
46
+ launcher
47
+ }
48
+ let(:launch_options) {
49
+ {
50
+ 'chef_role' => 'role',
51
+ 'chef_environment' => 'environment',
52
+ 'chef_data_bag_secret' => 'data_bag_secret',
53
+ 'git_branch' => 'branch',
54
+ 'git_key' => 'key',
55
+ 'git_origin' => 'origin',
56
+ 'key_name' => 'key',
57
+ 'instance_type' => 'c1.xlarge',
58
+ 'image_id' => 'ami-d9d6a6b0',
59
+ 'availability_zone' => 'us-east-1a',
60
+ 'count' => 2,
61
+ 'security_groups' => ['sg_name1', 'sg_name2'],
62
+ 'wait' => false
63
+ }
64
+ }
65
+
66
+ before do
67
+ allow(launcher).to receive(:try_file).and_return('secret')
68
+ allow(launcher).to receive(:render_template).and_return('template')
69
+ allow(launcher).to receive(:ec2).and_return(ec2)
70
+ allow(ec2).to receive(:client).and_return(client)
71
+ allow(response).to receive(:error).and_return(nil)
72
+ end
73
+
74
+ it 'launches all of the instances' do
75
+ expect(launcher).to receive(:get_vpc_security_group_ids).
76
+ with('vpc-1', ['sg_name1', 'sg_name2']).and_call_original
77
+ expect_any_instance_of(AWS::EC2::VPC).to receive(:security_groups).
78
+ and_return([1,2].map { |i| MockSecurityGroup.new("sg-#{i}", "sg_name#{i}", 'vpc-1')})
79
+ expect(launcher).to receive(:do_launch).with(a_hash_including(
80
+ :image_id => 'ami-d9d6a6b0',
81
+ :instance_type => 'c1.xlarge',
82
+ :key_name => 'key',
83
+ :count => 2,
84
+ :security_group_ids => ['sg-1', 'sg-2'],
85
+ :availability_zone => 'us-east-1a',
86
+ :user_data => 'template'
87
+ )).and_return(instances)
88
+ expect(launcher).to receive(:set_tags).with(kind_of(Array), kind_of(Hash)).and_return(nil)
89
+
90
+ launcher.send(:launch, launch_options)
91
+ end
92
+ end
93
+
30
94
  describe '#set_classic_link' do
31
95
  let(:ec2) { instance_double(AWS::EC2) }
32
96
  let(:client) { double(AWS::EC2::Client) }
@@ -37,14 +101,24 @@ describe Stemcell::Launcher do
37
101
  allow(response).to receive(:error).and_return(nil)
38
102
  end
39
103
 
40
- let(:classic_link) { {'vpc_id' => 'vpc_id', 'security_group_ids' => ['sg1', 'sg2']} }
104
+ let(:classic_link) {
105
+ {
106
+ 'vpc_id' => 'vpc-1',
107
+ 'security_group_ids' => ['sg-1', 'sg-2'],
108
+ 'security_groups' => ['sg_name']
109
+ }
110
+ }
41
111
 
42
112
  it 'invokes classic link on all of the instances' do
113
+ expect(launcher).to receive(:get_vpc_security_group_ids).with('vpc-1', ['sg_name']).
114
+ and_call_original
115
+ expect_any_instance_of(AWS::EC2::VPC).to receive(:security_groups).
116
+ and_return([MockSecurityGroup.new('sg-3', 'sg_name', 'vpc-1')])
43
117
  instances.each do |instance|
44
118
  expect(client).to receive(:attach_classic_link_vpc).ordered.with(a_hash_including(
45
119
  :instance_id => instance.id,
46
120
  :vpc_id => classic_link['vpc_id'],
47
- :groups => classic_link['security_group_ids'],
121
+ :groups => ['sg-1', 'sg-2', 'sg-3'],
48
122
  )).and_return(response)
49
123
  end
50
124
 
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.11.6
4
+ version: 0.11.7
5
5
  platform: ruby
6
6
  authors:
7
7
  - Martin Rhoads
@@ -11,7 +11,7 @@ authors:
11
11
  autorequire:
12
12
  bindir: bin
13
13
  cert_chain: []
14
- date: 2017-10-18 00:00:00.000000000 Z
14
+ date: 2017-11-14 00:00:00.000000000 Z
15
15
  dependencies:
16
16
  - !ruby/object:Gem::Dependency
17
17
  name: aws-sdk-v1
@@ -239,7 +239,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
239
239
  version: '0'
240
240
  requirements: []
241
241
  rubyforge_project:
242
- rubygems_version: 2.6.13
242
+ rubygems_version: 2.5.2
243
243
  signing_key:
244
244
  specification_version: 4
245
245
  summary: no summary