Skip to content

Commit

Permalink
Fixes lserman#4
Browse files Browse the repository at this point in the history
  • Loading branch information
Logan Serman committed Mar 23, 2016
1 parent f11eb7f commit 2adad06
Show file tree
Hide file tree
Showing 13 changed files with 48 additions and 31 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
cache: bundler
rvm:
- 2.0.0
- 2.3.0
sudo: false
deploy:
provider: rubygems
Expand Down
16 changes: 16 additions & 0 deletions bin/rspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/usr/bin/env ruby
#
# This file was generated by Bundler.
#
# The application 'rspec' is installed as part of a gem, and
# this file is here to facilitate running it.
#

require "pathname"
ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
Pathname.new(__FILE__).realpath)

require "rubygems"
require "bundler/setup"

load Gem.bin_path("rspec-core", "rspec")
3 changes: 1 addition & 2 deletions lib/elbas/ami.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ def self.create(&block)
ami.cleanup do
ami.save
ami.tag 'Deployed-with' => 'ELBAS'
ami.tag 'ELBAS-Deploy-group' => ami.autoscale_group_name
yield ami
end
end
Expand All @@ -27,7 +28,6 @@ def destroy(images = [])
end

private

def name
timestamp "#{environment}-AMI"
end
Expand All @@ -37,6 +37,5 @@ def trash
deployed_with_elbas? ami
end
end

end
end
3 changes: 1 addition & 2 deletions lib/elbas/aws/autoscaling.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ def autoscale_group
def autoscale_group_name
fetch(:aws_autoscale_group)
end

end
end
end
end
3 changes: 1 addition & 2 deletions lib/elbas/aws/credentials.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ def credentials
_credentials
end
end

end
end
end
end
3 changes: 1 addition & 2 deletions lib/elbas/aws/ec2.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ module EC2
def ec2
@_ec2 ||= ::AWS::EC2.new(credentials)
end

end
end
end
end
9 changes: 3 additions & 6 deletions lib/elbas/aws_resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ def cleanup(&block)
self
end

protected

private
def base_ec2_instance
@_base_ec2_instance ||= autoscale_group.ec2_instances.filter('instance-state-name', 'running').first
end
Expand All @@ -29,11 +28,9 @@ def timestamp(str)
"#{str}-#{Time.now.to_i}"
end

private

def deployed_with_elbas?(resource)
resource.tags['Deployed-with'] == 'ELBAS'
resource.tags['Deployed-with'] == 'ELBAS' &&
resource.tags['ELBAS-Deploy-group'] == autoscale_group_name
end

end
end
16 changes: 7 additions & 9 deletions lib/elbas/launch_configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def save(ami)
end

def attach_to_autoscale_group!
info "Attaching Launch Configuration to AutoScale Group"
info 'Attaching Launch Configuration to AutoScale Group'
autoscale_group.update(launch_configuration: aws_counterpart)
end

Expand All @@ -29,38 +29,36 @@ def destroy(launch_configurations = [])
end

private

def name
timestamp "ELBAS-#{environment}-LC"
timestamp "ELBAS-#{environment}-#{autoscale_group_name}-LC"
end

def instance_size
fetch(:aws_autoscale_instance_size, 'm1.small')
end

def create_options
_options = {
options = {
security_groups: base_ec2_instance.security_groups.to_a,
detailed_instance_monitoring: fetch(:aws_launch_configuration_detailed_instance_monitoring, true),
associate_public_ip_address: fetch(:aws_launch_configuration_associate_public_ip, true),
associate_public_ip_address: fetch(:aws_launch_configuration_associate_public_ip, true)
}

if user_data = fetch(:aws_launch_configuration_user_data, nil)
_options.merge user_data: user_data
options.merge user_data: user_data
end

_options
options
end

def deployed_with_elbas?(lc)
lc.name =~ /ELBAS-#{environment}/
lc.name.include? "ELBAS-#{environment}-#{autoscale_group_name}-LC"
end

def trash
autoscaling.launch_configurations.to_a.select do |lc|
deployed_with_elbas? lc
end
end

end
end
2 changes: 1 addition & 1 deletion lib/elbas/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Elbas
VERSION = '0.20.0'
VERSION = '0.21.0'
end
2 changes: 1 addition & 1 deletion spec/ami_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
subject { Elbas::AMI.new }

it 'retries the tag 3 times' do
expect(subject).to receive(:aws_counterpart).exactly(3).times { OpenStruct.new(tags: nil) }
expect(subject).to receive(:aws_counterpart).exactly(3).times { fail RuntimeError }
subject.tag 'Test' => true
end
end
Expand Down
8 changes: 6 additions & 2 deletions spec/elbas_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@
it 'tags the new AMI with Deployed-with=ELBAS' do
expect(WebMock).to have_requested(:post, /ec2.(.*).amazonaws.com\/\z/).with(body: /Action=CreateTags&ResourceId.1=ami-4fa54026&Tag.1.Key=Deployed-with&Tag.1.Value=ELBAS/)
end

it 'tags the new AMI with ELBAS-Deploy-group=<autoscale group name>' do
expect(WebMock).to have_requested(:post, /ec2.(.*).amazonaws.com\/\z/).with(body: /Action=CreateTags&ResourceId.1=ami-4fa54026&Tag.1.Key=ELBAS-Deploy-group&Tag.1.Value=production/)
end
end

describe 'Launch configuration creation & cleanup' do
Expand All @@ -48,12 +52,12 @@
end

it 'deletes any LCs with name =~ ELBAS-production' do
expect(WebMock).to have_requested(:post, /autoscaling.(.*).amazonaws.com\/\z/).with(body: /Action=DeleteLaunchConfiguration&LaunchConfigurationName=ELBAS-production-LC-1234567890/)
expect(WebMock).to have_requested(:post, /autoscaling.(.*).amazonaws.com\/\z/).with(body: /Action=DeleteLaunchConfiguration&LaunchConfigurationName=ELBAS-production-production-LC-1234567890/)
end

it 'attaches the LC to the autoscale group' do
launch_configuration.attach_to_autoscale_group!
expect(WebMock).to have_requested(:post, /autoscaling.(.*).amazonaws.com\/\z/).with(body: /Action=UpdateAutoScalingGroup&AutoScalingGroupName=production&LaunchConfigurationName=ELBAS-production-LC-\d{10,}/)
expect(WebMock).to have_requested(:post, /autoscaling.(.*).amazonaws.com\/\z/).with(body: /Action=UpdateAutoScalingGroup&AutoScalingGroupName=production&LaunchConfigurationName=ELBAS-production-production-LC-\d{10,}/)
end
end

Expand Down
4 changes: 2 additions & 2 deletions spec/support/stubs/DescribeLaunchConfigurations.200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<PlacementTenancy>dedicated</PlacementTenancy>
<CreatedTime>2013-01-21T23:04:42.200Z</CreatedTime>
<KernelId/>
<LaunchConfigurationName>ELBAS-production-LC-1234567890</LaunchConfigurationName>
<LaunchConfigurationName>ELBAS-production-production-LC-1234567890</LaunchConfigurationName>
<UserData/>
<InstanceType>m1.small</InstanceType>
<LaunchConfigurationARN>arn:aws:autoscaling:us-east-1:803981987763:launchConfiguration:
Expand All @@ -26,4 +26,4 @@
<ResponseMetadata>
<RequestId>d05a22f8-b690-11e2-bf8e-2113fEXAMPLE</RequestId>
</ResponseMetadata>
</DescribeLaunchConfigurationsResponse>
</DescribeLaunchConfigurationsResponse>
8 changes: 7 additions & 1 deletion spec/support/stubs/DescribeTags.200.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,11 @@
<key>Deployed-with</key>
<value>ELBAS</value>
</item>
<item>
<resourceId>ami-1a2b3c4d</resourceId>
<resourceType>image</resourceType>
<key>ELBAS-Deploy-group</key>
<value>production</value>
</item>
</tagSet>
</DescribeTagsResponse>
</DescribeTagsResponse>

0 comments on commit 2adad06

Please sign in to comment.