ami_spec 0.2.0 → 0.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/lib/ami_spec.rb CHANGED
@@ -36,6 +36,8 @@ module AmiSpec
36
36
  # The username to SSH to the AMI with.
37
37
  # ssh_retries::
38
38
  # Set the maximum number of ssh retries while waiting for the instance to boot.
39
+ # tags:::
40
+ # Additional tags to add to launched instances in the form of comma separated key=value pairs
39
41
  # debug::
40
42
  # Don't terminate the instances on exit
41
43
  # == Returns:
@@ -96,6 +98,7 @@ module AmiSpec
96
98
  opt :aws_public_ip, "Launch instances with a public IP"
97
99
  opt :ssh_retries, "The number of times we should try sshing to the ec2 instance before giving up. Defaults to 30",
98
100
  type: :int, default: 30
101
+ opt :tags, "Additional tags to add to launched instances in the form of comma separated key=value pairs. i.e. Name=AmiSpec", type: :string, default: ""
99
102
  opt :debug, "Don't terminate instances on exit"
100
103
  opt :wait_for_rc, "Wait for oldschool SystemV scripts to run before conducting tests. Currently only supports Ubuntu with upstart"
101
104
  end
@@ -113,6 +116,14 @@ module AmiSpec
113
116
  fail "You must specify either role and ami or role_ami_file"
114
117
  end
115
118
 
119
+ options[:tags] = parse_tags(options[:tags])
120
+
116
121
  exit run(options)
117
122
  end
123
+
124
+ def self.parse_tags(tags)
125
+ tag_pairs = tags.split(',')
126
+ tag_key_values = tag_pairs.collect{ |pair| pair.split('=')}.flatten
127
+ Hash[*tag_key_values]
128
+ end
118
129
  end
@@ -20,6 +20,7 @@ module AmiSpec
20
20
  @public_ip = options.fetch(:aws_public_ip)
21
21
  @region = options.fetch(:aws_region)
22
22
  @security_group_ids = options.fetch(:aws_security_groups)
23
+ @tags = ec2ify_tags(options.fetch(:tags))
23
24
  end
24
25
 
25
26
  def_delegators :@instance, :instance_id, :tags, :terminate, :private_ip_address, :public_ip_address
@@ -39,6 +40,10 @@ module AmiSpec
39
40
  !@region.nil? ? {region: @region} : {}
40
41
  end
41
42
 
43
+ def ec2ify_tags(tags)
44
+ tags.map { |k,v| {key: k, value: v} }
45
+ end
46
+
42
47
  def instances_options
43
48
  params = {
44
49
  image_id: @ami,
@@ -61,7 +66,7 @@ module AmiSpec
61
66
  end
62
67
 
63
68
  def tag_instance
64
- @instance.create_tags(tags: [{ key: 'AmiSpec', value: @role }])
69
+ @instance.create_tags(tags: [{ key: 'AmiSpec', value: @role }] + @tags)
65
70
  end
66
71
  end
67
72
  end
@@ -12,5 +12,6 @@ module AmiSpec
12
12
  property :aws_public_ip
13
13
  property :aws_region
14
14
  property :aws_security_groups
15
+ property :tags
15
16
  end
16
17
  end
@@ -1,3 +1,3 @@
1
1
  module AmiSpec
2
- VERSION = '0.2.0'
2
+ VERSION = '0.3.0'
3
3
  end
@@ -21,6 +21,12 @@ describe AmiSpec do
21
21
  )
22
22
  end
23
23
 
24
+ describe '#invoke' do
25
+ it 'raises a system exit with no arguments' do
26
+ expect{ described_class.invoke }.to raise_error(SystemExit)
27
+ end
28
+ end
29
+
24
30
  describe '#run' do
25
31
  before do
26
32
  allow(AmiSpec::WaitForSSH).to receive(:wait).and_return(true)
@@ -51,4 +57,18 @@ describe AmiSpec do
51
57
  end
52
58
  end
53
59
  end
60
+
61
+ describe '#parse_tags' do
62
+ it 'parses a single key/value pair' do
63
+ expect(described_class.parse_tags("Name=AmiSpec")).to eq( { "Name"=>"AmiSpec" } )
64
+ end
65
+
66
+ it 'parses multiple key/value pairs' do
67
+ expect(described_class.parse_tags("Name=AmiSpec,Owner=Me")).to eq( { "Name"=>"AmiSpec", "Owner"=>"Me" } )
68
+ end
69
+
70
+ it 'parses an empty string' do
71
+ expect(described_class.parse_tags("")).to eq({})
72
+ end
73
+ end
54
74
  end
@@ -7,6 +7,7 @@ describe AmiSpec::AwsInstance do
7
7
  let(:client_double) { instance_double(Aws::EC2::Client) }
8
8
  let(:new_ec2_double) { instance_double(Aws::EC2::Types::Instance) }
9
9
  let(:ec2_double) { instance_double(Aws::EC2::Instance) }
10
+ let(:tags) { {} }
10
11
  subject(:aws_instance) do
11
12
  described_class.new(
12
13
  role: role,
@@ -16,7 +17,8 @@ describe AmiSpec::AwsInstance do
16
17
  aws_instance_type: 't2.micro',
17
18
  aws_public_ip: false,
18
19
  aws_security_groups: sec_group_id,
19
- aws_region: region
20
+ aws_region: region,
21
+ tags: tags
20
22
  )
21
23
  end
22
24
 
@@ -78,6 +80,17 @@ describe AmiSpec::AwsInstance do
78
80
  end
79
81
  end
80
82
 
83
+ context 'with tags' do
84
+ let(:tags) { {"Name" => "AmiSpec"} }
85
+
86
+ it 'tags the instance' do
87
+ expect(ec2_double).to receive(:create_tags).with(
88
+ {tags: [{ key: 'AmiSpec', value: role}, { key: "Name", value: "AmiSpec"}]}
89
+ )
90
+ start
91
+ end
92
+ end
93
+
81
94
  it 'tags the instance with a role' do
82
95
  expect(ec2_double).to receive(:create_tags).with(
83
96
  hash_including(tags: [{ key: 'AmiSpec', value: role}])
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ami_spec
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2016-05-20 00:00:00.000000000 Z
13
+ date: 2016-09-15 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: aws-sdk