From 2adad06d3ebf8728a63ab88a4e852d8bd14399ed Mon Sep 17 00:00:00 2001 From: Logan Serman Date: Wed, 23 Mar 2016 16:03:10 -0500 Subject: [PATCH] Fixes #4 --- .travis.yml | 2 +- bin/rspec | 16 ++++++++++++++++ lib/elbas/ami.rb | 3 +-- lib/elbas/aws/autoscaling.rb | 3 +-- lib/elbas/aws/credentials.rb | 3 +-- lib/elbas/aws/ec2.rb | 3 +-- lib/elbas/aws_resource.rb | 9 +++------ lib/elbas/launch_configuration.rb | 16 +++++++--------- lib/elbas/version.rb | 2 +- spec/ami_spec.rb | 2 +- spec/elbas_spec.rb | 8 ++++++-- .../stubs/DescribeLaunchConfigurations.200.xml | 4 ++-- spec/support/stubs/DescribeTags.200.xml | 8 +++++++- 13 files changed, 48 insertions(+), 31 deletions(-) create mode 100755 bin/rspec diff --git a/.travis.yml b/.travis.yml index 2cb1d17..fcc16b4 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,7 +1,7 @@ language: ruby cache: bundler rvm: -- 2.0.0 +- 2.3.0 sudo: false deploy: provider: rubygems diff --git a/bin/rspec b/bin/rspec new file mode 100755 index 0000000..3e07513 --- /dev/null +++ b/bin/rspec @@ -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") diff --git a/lib/elbas/ami.rb b/lib/elbas/ami.rb index 304ecda..953fa3a 100644 --- a/lib/elbas/ami.rb +++ b/lib/elbas/ami.rb @@ -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 @@ -27,7 +28,6 @@ def destroy(images = []) end private - def name timestamp "#{environment}-AMI" end @@ -37,6 +37,5 @@ def trash deployed_with_elbas? ami end end - end end diff --git a/lib/elbas/aws/autoscaling.rb b/lib/elbas/aws/autoscaling.rb index 9409e15..1257381 100644 --- a/lib/elbas/aws/autoscaling.rb +++ b/lib/elbas/aws/autoscaling.rb @@ -16,7 +16,6 @@ def autoscale_group def autoscale_group_name fetch(:aws_autoscale_group) end - end end -end \ No newline at end of file +end diff --git a/lib/elbas/aws/credentials.rb b/lib/elbas/aws/credentials.rb index 472e678..fd92695 100644 --- a/lib/elbas/aws/credentials.rb +++ b/lib/elbas/aws/credentials.rb @@ -15,7 +15,6 @@ def credentials _credentials end end - end end -end \ No newline at end of file +end diff --git a/lib/elbas/aws/ec2.rb b/lib/elbas/aws/ec2.rb index 80e2fa6..7a9ebb9 100644 --- a/lib/elbas/aws/ec2.rb +++ b/lib/elbas/aws/ec2.rb @@ -8,7 +8,6 @@ module EC2 def ec2 @_ec2 ||= ::AWS::EC2.new(credentials) end - end end -end \ No newline at end of file +end diff --git a/lib/elbas/aws_resource.rb b/lib/elbas/aws_resource.rb index 432c998..a3a42f8 100644 --- a/lib/elbas/aws_resource.rb +++ b/lib/elbas/aws_resource.rb @@ -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 @@ -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 diff --git a/lib/elbas/launch_configuration.rb b/lib/elbas/launch_configuration.rb index fb560f4..42f80d6 100644 --- a/lib/elbas/launch_configuration.rb +++ b/lib/elbas/launch_configuration.rb @@ -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 @@ -29,9 +29,8 @@ def destroy(launch_configurations = []) end private - def name - timestamp "ELBAS-#{environment}-LC" + timestamp "ELBAS-#{environment}-#{autoscale_group_name}-LC" end def instance_size @@ -39,21 +38,21 @@ def instance_size 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 @@ -61,6 +60,5 @@ def trash deployed_with_elbas? lc end end - end end diff --git a/lib/elbas/version.rb b/lib/elbas/version.rb index 9c6fe80..fbfee31 100644 --- a/lib/elbas/version.rb +++ b/lib/elbas/version.rb @@ -1,3 +1,3 @@ module Elbas - VERSION = '0.20.0' + VERSION = '0.21.0' end diff --git a/spec/ami_spec.rb b/spec/ami_spec.rb index 0303565..04c1e93 100644 --- a/spec/ami_spec.rb +++ b/spec/ami_spec.rb @@ -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 diff --git a/spec/elbas_spec.rb b/spec/elbas_spec.rb index 81c4577..47d9ce1 100644 --- a/spec/elbas_spec.rb +++ b/spec/elbas_spec.rb @@ -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=' 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 @@ -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 diff --git a/spec/support/stubs/DescribeLaunchConfigurations.200.xml b/spec/support/stubs/DescribeLaunchConfigurations.200.xml index ca7f1fe..0241ef5 100644 --- a/spec/support/stubs/DescribeLaunchConfigurations.200.xml +++ b/spec/support/stubs/DescribeLaunchConfigurations.200.xml @@ -7,7 +7,7 @@ dedicated 2013-01-21T23:04:42.200Z - ELBAS-production-LC-1234567890 + ELBAS-production-production-LC-1234567890 m1.small arn:aws:autoscaling:us-east-1:803981987763:launchConfiguration: @@ -26,4 +26,4 @@ d05a22f8-b690-11e2-bf8e-2113fEXAMPLE - \ No newline at end of file + diff --git a/spec/support/stubs/DescribeTags.200.xml b/spec/support/stubs/DescribeTags.200.xml index b4b7973..0a46f04 100644 --- a/spec/support/stubs/DescribeTags.200.xml +++ b/spec/support/stubs/DescribeTags.200.xml @@ -7,5 +7,11 @@ Deployed-with ELBAS + + ami-1a2b3c4d + image + ELBAS-Deploy-group + production + - \ No newline at end of file +