aws-codedeploy-agent 0.0.4 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (55) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +5 -1
  3. data/.travis.yml +10 -0
  4. data/CHANGES.md +5 -0
  5. data/Gemfile +6 -3
  6. data/README.md +35 -0
  7. data/Rakefile +52 -0
  8. data/aws-codedeploy-agent.gemspec +5 -10
  9. data/conf/codedeployagent.yml +1 -0
  10. data/features/AwsCredentials.yml +4 -0
  11. data/features/agent.feature +11 -0
  12. data/features/aws_credentials.rb +35 -0
  13. data/features/step_definitions.rb +233 -0
  14. data/init.d/codedeploy-agent +11 -6
  15. data/lib/instance_agent.rb +1 -1
  16. data/lib/instance_agent/config.rb +3 -1
  17. data/lib/instance_agent/log.rb +18 -0
  18. data/lib/instance_agent/platform/linux_util.rb +27 -5
  19. data/lib/instance_agent/plugins/codedeploy/application_specification/application_specification.rb +2 -1
  20. data/lib/instance_agent/plugins/codedeploy/application_specification/file_info.rb +2 -2
  21. data/lib/instance_agent/plugins/codedeploy/application_specification/script_info.rb +3 -2
  22. data/lib/instance_agent/plugins/codedeploy/codedeploy_control.rb +15 -14
  23. data/lib/instance_agent/plugins/codedeploy/command_executor.rb +34 -6
  24. data/lib/instance_agent/plugins/codedeploy/command_poller.rb +12 -1
  25. data/lib/instance_agent/plugins/codedeploy/deployment_specification.rb +2 -12
  26. data/lib/instance_agent/plugins/codedeploy/hook_executor.rb +8 -4
  27. data/lib/instance_agent/plugins/codedeploy/install_instruction.rb +3 -1
  28. data/lib/instance_metadata.rb +1 -1
  29. data/test/instance_agent/config_test.rb +3 -1
  30. data/test/instance_agent/platform/linux_util_test.rb +35 -0
  31. data/test/instance_agent/plugins/codedeploy/application_specification_test.rb +7 -6
  32. data/test/instance_agent/plugins/codedeploy/command_executor_test.rb +13 -10
  33. data/test/instance_agent/plugins/codedeploy/command_poller_test.rb +28 -6
  34. data/test/instance_agent/plugins/codedeploy/deployment_specification_test.rb +9 -9
  35. data/test/instance_agent/plugins/codedeploy/hook_executor_test.rb +12 -4
  36. data/test/test_helper.rb +6 -1
  37. data/vendor/gems/codedeploy-commands/apis/CodeDeployCommand.api.json +1 -0
  38. data/vendor/gems/codedeploy-commands/lib/aws/codedeploy_commands.rb +2 -0
  39. data/vendor/gems/codedeploy-commands/lib/aws/plugins/deploy_agent_version.rb +31 -0
  40. data/vendor/gems/codedeploy-commands/lib/aws/plugins/deploy_control_endpoint.rb +1 -12
  41. data/vendor/gems/process_manager/lib/process_manager/master.rb +3 -3
  42. data/vendor/specifications/codedeploy-commands-1.0.0.gemspec +5 -5
  43. metadata +52 -149
  44. data/vendor/gems/codedeploy-commands/codedeploy-commands-1.0.0.gemspec +0 -28
  45. data/vendor/gems/process_manager/process_manager-0.0.13.gemspec +0 -42
  46. data/vendor/specifications/aws-sdk-core-2.0.42.gemspec +0 -37
  47. data/vendor/specifications/builder-3.2.2.gemspec +0 -29
  48. data/vendor/specifications/gli-2.5.6.gemspec +0 -51
  49. data/vendor/specifications/jmespath-1.0.1.gemspec +0 -29
  50. data/vendor/specifications/little-plugger-1.1.3.gemspec +0 -32
  51. data/vendor/specifications/logging-1.8.1.gemspec +0 -44
  52. data/vendor/specifications/multi_json-1.7.7.gemspec +0 -30
  53. data/vendor/specifications/multi_json-1.8.4.gemspec +0 -30
  54. data/vendor/specifications/multi_xml-0.5.5.gemspec +0 -30
  55. data/vendor/specifications/simple_pid-0.2.1.gemspec +0 -28
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 7bc7be2f6438c961823db732eb0ade0eab2f9b8e
4
+ data.tar.gz: dcf97e2581b8a2ef5dc35a55eab14aa8b18456d8
5
+ SHA512:
6
+ metadata.gz: 888d1bfb6ced3cb485e4ef47cf339f4a75538af5942a5c0c6e4b332a9cf4ade064b7d00903cb5eb91867f1ab851730c50b41c19be0a62cb791a8ee66dbeea8cf
7
+ data.tar.gz: 9e0fda36d32ac49a7e60f5809a869e94819a30b66bb20c123967dd4efcc8ee2bc191d0caed20107bc92c3562616bf624f022783ba1e144e7ce22edeeffb8cdad
data/.gitignore CHANGED
@@ -1,2 +1,6 @@
1
- Gemfile.lock
1
+ *.swp
2
2
  *.gem
3
+ Gemfile.lock
4
+ deployment/
5
+ coverage/
6
+ .version
@@ -0,0 +1,10 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.0.0
4
+ after_success: coveralls
5
+ notifications:
6
+ email:
7
+ recipients:
8
+ - codedeploy-notifications@amazon.com
9
+ on_failure: always
10
+ on_success: never
data/CHANGES.md CHANGED
@@ -1,6 +1,11 @@
1
+ # 0.1.0
2
+
3
+ * Pulled from upstream/master to support SHA-256. (panthomakos)
4
+
1
5
  # 0.0.3/0.0.4
2
6
 
3
7
  * Pulled from upstream/master to include new CA certificate. (panthomakos)
8
+ * Updated gemspec from upstream/master. (panthomakos)
4
9
 
5
10
  # 0.0.2
6
11
 
data/Gemfile CHANGED
@@ -4,10 +4,13 @@ gemspec
4
4
 
5
5
  group :test do
6
6
  gem 'test-unit'
7
+ gem 'activesupport', :require => 'active_support'
8
+ gem 'coveralls', require: false
9
+ gem 'cucumber'
10
+ gem 'fakefs', :require => 'fakefs/safe'
11
+ gem 'mocha'
12
+ gem 'rspec'
7
13
  gem 'shoulda'
8
14
  gem 'shoulda-matchers'
9
15
  gem 'shoulda-context'
10
- gem 'mocha'
11
- gem 'fakefs', :require => 'fakefs/safe'
12
- gem 'activesupport', :require => 'active_support'
13
16
  end
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # AWS CodeDeploy Agent
2
2
 
3
+ [![Code Climate](https://codeclimate.com/github/aws/aws-codedeploy-agent.png)](https://codeclimate.com/github/aws/aws-codedeploy-agent) [![Build Status](https://travis-ci.org/aws/aws-codedeploy-agent.png?branch=master)](https://travis-ci.org/aws/aws-codedeploy-agent) [![Coverage Status](https://coveralls.io/repos/aws/aws-codedeploy-agent/badge.svg?branch=master&service=github)](https://coveralls.io/r/aws/aws-codedeploy-agent?branch=master)
4
+
3
5
  ## Install and Run
4
6
 
5
7
  $ gem install aws-codedeploy-agent
@@ -14,3 +16,36 @@
14
16
 
15
17
  $ sudo service codedeploy-agent stop
16
18
  $ sudo codedeploy-uninstall
19
+
20
+ ## Build Steps
21
+
22
+ ``` ruby
23
+ git clone https://github.com/aws/aws-codedeploy-agent.git
24
+ gem install bundler
25
+ cd aws-codedeploy-agent
26
+ bundle install
27
+ rake clean && rake
28
+ ```
29
+
30
+ ## Integration Test
31
+
32
+ Please do the build steps mentioned above before running the integration test.
33
+
34
+ The integration test creates the following
35
+ * An IAM role "codedeploy-agent-integ-test-deployment-role" if it doesn't exist
36
+ * An IAM role "codedeploy-agent-integ-test-instance-role" if it doesn't exist
37
+ * A CodeDeploy application
38
+ * A CodeDeploy deployment group
39
+ * An EC2 key pair "codedeploy-agent-integ-test-key" if it doesn't exist
40
+ * An EC2 security group "codedeploy-agent-integ-test-sg" if it doesn't exist
41
+ * An EC2 instance tagged with key "codedeploy-agent-integ-test-instance"
42
+ * A CodeDeploy deployment on that ec2 instance.
43
+
44
+ It terminates the test ec2 instance and deletes the CodeDeploy application at the end of each test run.
45
+ It also terminates any test ec2 instances before starting up the test.
46
+
47
+ Update the features/AwsCredentials.yml file with AWS access key and secret key. The access key should have permission to create the above mentioned resources. You can also change the default region and ami id if you want. To run the integration test execute
48
+
49
+ ```
50
+ rake test-integration
51
+ ```
@@ -0,0 +1,52 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rubygems'
4
+
5
+ # Run all units tests in test/
6
+ desc "Run unit tests in test/"
7
+ Rake::TestTask.new(:test) do |t|
8
+ t.libs << ['test', 'lib', 'test/helpers']
9
+
10
+ test_files = FileList.new("test/**/*_test.rb")
11
+ t.test_files = test_files
12
+ t.verbose = true
13
+ end
14
+ task :default => [:version_tracking, :test]
15
+ task :release => [:version_tracking, :test]
16
+
17
+ begin
18
+ require 'cucumber'
19
+ require 'cucumber/rake/task'
20
+ desc = 'aws codedeploy agent integration tests'
21
+ Cucumber::Rake::Task.new('test-integration-aws-codedeploy-agent', desc) do |t|
22
+ t.cucumber_opts = "features -t ~@Ignore"
23
+ end
24
+ task 'test-integration' => 'test-integration-aws-codedeploy-agent'
25
+ rescue LoadError
26
+ desc 'aws codedeploy agent integration tests'
27
+ task 'test:integration' do
28
+ puts 'skipping aws-codedeploy-agent integration tests, cucumber not loaded'
29
+ end
30
+ end
31
+
32
+ # Version tracking
33
+ require 'fileutils'
34
+ task :version_tracking do
35
+ FileUtils.rm('.version') if File.exist?('.version')
36
+ File.open('.version', 'w+') {|file| file.write("agent_version: #{getAgentTrackingInfo}")}
37
+ FileUtils.chmod(0444, '.version')
38
+ end
39
+
40
+ def getAgentTrackingInfo
41
+ begin
42
+ commit_id = `git rev-parse HEAD`.chop!
43
+ tracking = "COMMIT_#{commit_id}"
44
+ rescue
45
+ tracking = "UNKNOWN_VERSION"
46
+ end
47
+ end
48
+
49
+ # Clean up
50
+ task :clean do
51
+ rm_rf 'deployment'
52
+ end
@@ -5,7 +5,7 @@ $:.push File.expand_path("../vendor/gems/process_manager/lib", __FILE__)
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = 'aws-codedeploy-agent'
8
- s.version = '0.0.4'
8
+ s.version = '0.1.0'
9
9
  s.license = 'Apache-2.0'
10
10
  s.summary = 'AWS CodeDeploy Agent'
11
11
  s.description = 'CodeDeploy Agent is responsible for doing the actual work of deploying software on an individual EC2 instance.'
@@ -21,17 +21,12 @@ Gem::Specification.new do |s|
21
21
  s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
22
22
  s.test_files = s.files.grep(%r{^(test|spec|features)/})
23
23
 
24
- s.add_dependency('archive-tar-minitar', '~> 0.5.2')
25
- s.add_dependency('aws-sdk-core', '~> 2.0.42')
26
- s.add_dependency('builder', '~> 3.2.2')
27
24
  s.add_dependency('gli', '~> 2.5')
28
- s.add_dependency('httpclient')
29
25
  s.add_dependency('json_pure', '~> 1.6')
30
- s.add_dependency('little-plugger', '~> 1.1.3')
31
- s.add_dependency('logging', '~> 1.8')
32
- s.add_dependency('multi_json', '~> 1.8.4')
33
- s.add_dependency('multi_xml', '~> 0.5.5')
34
- s.add_dependency('rake', '~> 0.9')
26
+ s.add_dependency('archive-tar-minitar', '~> 0.5.2')
35
27
  s.add_dependency('rubyzip', '~> 1.1.0')
28
+ s.add_dependency('rake', '~> 0.9')
29
+ s.add_dependency('logging', '~> 1.8')
30
+ s.add_dependency('aws-sdk-core', '~> 2.2.0')
36
31
  s.add_dependency('simple_pid', '~> 0.2.1')
37
32
  end
@@ -6,3 +6,4 @@
6
6
  :root_dir: '/tmp/codedeploy-agent'
7
7
  :verbose: false
8
8
  :wait_between_runs: 1
9
+ :proxy_uri:
@@ -0,0 +1,4 @@
1
+ aws_access_key_id:
2
+ aws_secret_access_key:
3
+ region: us-east-1
4
+ ec2_ami_id: ami-1ecae776
@@ -0,0 +1,11 @@
1
+ # language: en
2
+ @codedeploy-agent
3
+ Feature: Deploy using AWS CodeDeploy Agent
4
+
5
+ Scenario: Doing a sample deployment
6
+ Given I have an application
7
+ And I have a deployment group containing a single EC2 AMI with preinstalled new instance agent
8
+ When I create a deployment for the application and deployment group with the test S3 revision
9
+ Then the overall deployment should eventually be in progress
10
+ And the deployment should contain all the instances I tagged
11
+ And the overall deployment should eventually succeed
@@ -0,0 +1,35 @@
1
+ require 'singleton'
2
+
3
+ class AwsCredentials
4
+ include Singleton
5
+ attr_reader :ec2_ami
6
+ attr_reader :keypair_name
7
+
8
+ def configure
9
+ file_path = File.join(File.expand_path(File.dirname(__FILE__)), './AwsCredentials.yml')
10
+ file_config = nil
11
+ if File.exists?(file_path) && File.readable?(file_path)
12
+ begin
13
+ file_config = YAML.load(File.read(file_path)).inject({}){|temp,(k,v)| temp[k.to_sym] = v; temp}
14
+ rescue Exception => e
15
+ puts("Invalid AwsCredentials file")
16
+ raise "Invalid AwsCredentials file"
17
+ end
18
+ else
19
+ puts 'AwsCredentials.yml file does not exist'
20
+ end
21
+ if file_config
22
+ if file_config[:region]
23
+ ENV['AWS_REGION'] = file_config[:region]
24
+ end
25
+ if file_config[:aws_access_key_id]
26
+ ENV['AWS_ACCESS_KEY_ID'] = file_config[:aws_access_key_id]
27
+ end
28
+ if file_config[:aws_secret_access_key]
29
+ ENV['AWS_SECRET_ACCESS_KEY'] = file_config[:aws_secret_access_key]
30
+ end
31
+ @ec2_ami ||= file_config[:ec2_ami_id]
32
+ @keypair_name ||= file_config[:keypair_name]
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,233 @@
1
+ require 'aws-sdk-core'
2
+ require_relative '../features/aws_credentials'
3
+ require 'securerandom'
4
+ require 'base64'
5
+
6
+ CODEDEPLOY_TEST_PREFIX = "codedeploy-agent-integ-test-"
7
+ EC2_SECURITY_GROUP = "#{CODEDEPLOY_TEST_PREFIX}sg"
8
+ EC2_KEY_PAIR = "#{CODEDEPLOY_TEST_PREFIX}key"
9
+ EC2_TAG_KEY = "#{CODEDEPLOY_TEST_PREFIX}instance"
10
+ DEPLOYMENT_ROLE_NAME = "#{CODEDEPLOY_TEST_PREFIX}deployment-role"
11
+ INSTANCE_ROLE_NAME = "#{CODEDEPLOY_TEST_PREFIX}instance-role"
12
+
13
+ def eventually(options = {}, &block)
14
+ seconds = options[:upto] || 300
15
+ delays = [1]
16
+ while delays.inject(0) { |sum, i| sum + i } < seconds
17
+ delays << [delays.last * 1.2, 60.0].min
18
+ end
19
+ begin
20
+ yield
21
+ rescue StandardError, RSpec::Expectations::ExpectationNotMetError => error
22
+ unless delays.empty?
23
+ sleep(delays.shift)
24
+ retry
25
+ end
26
+ raise error
27
+ end
28
+ end
29
+
30
+ Before("@codedeploy-agent") do
31
+ AwsCredentials.instance.configure
32
+ keypair_name = AwsCredentials.instance.keypair_name.nil?? EC2_KEY_PAIR : AwsCredentials.instance.keypair_name
33
+ print("Using keypair : #{keypair_name}\n")
34
+ @codedeploy_client = Aws::CodeDeploy::Client.new
35
+ @ec2_client = Aws::EC2::Client.new
36
+ @iam_client = Aws::IAM::Client.new
37
+ instance_ids = get_test_ec2_instances.collect {|i| i.instance_id}
38
+ @ec2_client.terminate_instances({:instance_ids => instance_ids}) unless instance_ids.empty?
39
+ end
40
+
41
+ After("@codedeploy-agent") do
42
+ @codedeploy_client.delete_application({:application_name => @application_name}) unless @application_name.nil?
43
+ @ec2_client.terminate_instances({:instance_ids => [@instance_id]}) unless @instance_id.nil?
44
+ end
45
+
46
+ Given(/^I have an application$/) do
47
+ @application_name = "codedeploy-github-testapp-#{SecureRandom.hex(10)}"
48
+ @codedeploy_client.create_application(:application_name => @application_name)
49
+ puts "It is going to take a few minutes to spin up an ec2 instance..."
50
+ end
51
+
52
+ Given(/^I have a deployment group containing a single EC2 AMI with preinstalled new instance agent$/) do
53
+ @deployment_group_name = "codedeploy-github-testdg-#{SecureRandom.hex(10)}"
54
+ create_deployment_role
55
+ create_instance_role
56
+ create_instance_profile
57
+ start_ec2_instance
58
+ create_deployment_group
59
+ end
60
+
61
+ When(/^I create a deployment for the application and deployment group with the test S(\d+) revision$/) do |arg1|
62
+ @deployment_id = @codedeploy_client.create_deployment({:application_name => @application_name,
63
+ :deployment_group_name => @deployment_group_name,
64
+ :revision => { :revision_type => "S3",
65
+ :s3_location => {
66
+ :bucket => "aws-codedeploy-us-east-1",
67
+ :key => "samples/latest/SampleApp_Linux.zip",
68
+ :bundle_type => "zip"
69
+ }
70
+ },
71
+ :deployment_config_name => "CodeDeployDefault.OneAtATime",
72
+ :description => "CodeDeploy agent github test",
73
+ }).deployment_id
74
+ end
75
+
76
+ Then(/^the overall deployment should eventually be in progress$/) do
77
+ assert_deployment_status("InProgress", 60)
78
+ end
79
+
80
+ Then(/^the deployment should contain all the instances I tagged$/) do
81
+ instances = @codedeploy_client.list_deployment_instances(:deployment_id => @deployment_id).instances_list
82
+ expect(instances.size).to eq(1)
83
+ end
84
+
85
+ Then(/^the overall deployment should eventually succeed$/) do
86
+ assert_deployment_status("Succeeded", 300)
87
+ end
88
+
89
+ def create_deployment_group
90
+ @codedeploy_client.create_deployment_group({:application_name => @application_name,
91
+ :deployment_group_name => @deployment_group_name,
92
+ :ec2_tag_filters => [{:key => EC2_TAG_KEY, :type => "KEY_ONLY"}],
93
+ :service_role_arn => @deployment_role})
94
+ end
95
+
96
+ def get_test_ec2_instances
97
+ reservations = @ec2_client.describe_instances({:filters => [{:name => "tag-key", :values =>[EC2_TAG_KEY]}, {:name => "instance-state-name", :values => ["running", "pending"]}]}).reservations
98
+ instances = []
99
+ for reservation in reservations
100
+ instances += reservation.instances
101
+ end
102
+ instances
103
+ end
104
+
105
+ def start_ec2_instance
106
+ create_key_pair
107
+ create_security_group
108
+ start_and_tag_instance
109
+ end
110
+
111
+ def create_key_pair
112
+ keypair_name = AwsCredentials.instance.keypair_name.nil?? EC2_KEY_PAIR : AwsCredentials.instance.keypair_name
113
+ begin
114
+ @ec2_client.create_key_pair({:key_name => keypair_name})
115
+ rescue Aws::EC2::Errors::InvalidKeyPairDuplicate
116
+ #Use the existing key
117
+ end
118
+ eventually(:upto => 60) do
119
+ expect(@ec2_client.describe_key_pairs({:key_names => [keypair_name]}).key_pairs).not_to be_empty
120
+ end
121
+ end
122
+
123
+ def create_security_group
124
+ begin
125
+ @security_group_id ||= @ec2_client.create_security_group({:group_name => EC2_SECURITY_GROUP, :description => "CodeDeploy agent integ test instance security group."}).group_id
126
+ rescue Aws::EC2::Errors::InvalidGroupDuplicate
127
+ #Use the existing security group
128
+ end
129
+ eventually(:upto => 60) do
130
+ security_groups = @ec2_client.describe_security_groups({:group_names => [EC2_SECURITY_GROUP]}).security_groups
131
+ expect(security_groups).not_to be_empty
132
+ @security_group_id ||= security_groups[0].group_id
133
+ end
134
+ end
135
+
136
+ def start_and_tag_instance
137
+ eventually(:upto => 60) do
138
+ keypair_name = AwsCredentials.instance.keypair_name.nil?? EC2_KEY_PAIR : AwsCredentials.instance.keypair_name
139
+ @instance_id = @ec2_client.run_instances({
140
+ :image_id => AwsCredentials.instance.ec2_ami,
141
+ :instance_type => "t2.micro",
142
+ :min_count => 1,
143
+ :max_count => 1,
144
+ :key_name => keypair_name,
145
+ :security_group_ids => [@security_group_id],
146
+ :iam_instance_profile => {:arn => @instance_profile},
147
+ :user_data => get_user_data
148
+ }).instances[0].instance_id
149
+ expect(@instance_id).not_to be_nil
150
+ end
151
+ eventually(:upto => 600) do
152
+ expect(@ec2_client.describe_instance_status(:instance_ids => [@instance_id]).instance_statuses[0].instance_state.name).to eq "running"
153
+ end
154
+ @ec2_client.create_tags({:resources => [@instance_id], :tags => [{:key => EC2_TAG_KEY, :value => ""}]})
155
+ end
156
+
157
+ def get_user_data
158
+ user_data = "#!/bin/bash\n"\
159
+ "yum install -y git gcc gcc-c++ ruby-devel\n"\
160
+ "cd /home/ec2-user\n"\
161
+ "gem install io-console\n"\
162
+ "gem install bundler\n"\
163
+ "git clone https://github.com/aws/aws-codedeploy-agent.git\n"\
164
+ "cd aws-codedeploy-agent\n"\
165
+ "BUNDLE=`whereis bundle | cut -d' ' -f 2`\n"\
166
+ "$BUNDLE install\n"\
167
+ "mkdir -p /etc/codedeploy-agent/conf\n"\
168
+ "cp conf/codedeployagent.yml /etc/codedeploy-agent/conf/\n"\
169
+ "$BUNDLE exec bin/codedeploy-agent start"
170
+ Base64.encode64(user_data)
171
+ end
172
+
173
+ def create_deployment_role
174
+ begin
175
+ @iam_client.create_role({:role_name => DEPLOYMENT_ROLE_NAME,
176
+ :assume_role_policy_document => deployment_role_policy}).role.arn
177
+ @iam_client.attach_role_policy({:role_name => DEPLOYMENT_ROLE_NAME,
178
+ :policy_arn => "arn:aws:iam::aws:policy/service-role/AWSCodeDeployRole"})
179
+ rescue Aws::IAM::Errors::EntityAlreadyExists
180
+ #Using the existing role
181
+ end
182
+ eventually(:upto => 60) do
183
+ deployment_role = @iam_client.get_role({:role_name => DEPLOYMENT_ROLE_NAME}).role
184
+ expect(deployment_role).not_to be_nil
185
+ @deployment_role ||= deployment_role.arn
186
+ end
187
+ end
188
+
189
+ def create_instance_role
190
+ begin
191
+ @iam_client.create_role({:role_name => INSTANCE_ROLE_NAME,
192
+ :assume_role_policy_document => instance_role_policy}).role.arn
193
+ @iam_client.attach_role_policy({:role_name => INSTANCE_ROLE_NAME,
194
+ :policy_arn => "arn:aws:iam::aws:policy/service-role/AmazonEC2RoleforAWSCodeDeploy"})
195
+ rescue Aws::IAM::Errors::EntityAlreadyExists
196
+ #Using the existing role
197
+ end
198
+ eventually do
199
+ instance_role = @iam_client.get_role({:role_name => INSTANCE_ROLE_NAME}).role
200
+ expect(instance_role).not_to be_nil
201
+ @instance_role ||= instance_role.arn
202
+ end
203
+ end
204
+
205
+ def create_instance_profile
206
+ begin
207
+ @instance_profile ||= @iam_client.create_instance_profile({:instance_profile_name => INSTANCE_ROLE_NAME}).instance_profile.arn
208
+ @iam_client.add_role_to_instance_profile({:instance_profile_name => INSTANCE_ROLE_NAME, :role_name => INSTANCE_ROLE_NAME})
209
+ eventually(:upto => 60) do
210
+ profile = @iam_client.get_instance_profile({:instance_profile_name => INSTANCE_ROLE_NAME}).instance_profile
211
+ expect(profile).not_to be_nil
212
+ expect(profile.roles).not_to be_empty
213
+ profile_arn ||= profile.arn
214
+ end
215
+ rescue Aws::IAM::Errors::EntityAlreadyExists
216
+ @instance_profile ||= @iam_client.get_instance_profile({:instance_profile_name => INSTANCE_ROLE_NAME}).instance_profile.arn
217
+ end
218
+ end
219
+
220
+ def deployment_role_policy
221
+ "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"codedeploy.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
222
+ end
223
+
224
+ def instance_role_policy
225
+ "{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Principal\":{\"Service\":[\"ec2.amazonaws.com\"]},\"Action\":[\"sts:AssumeRole\"]}]}"
226
+ end
227
+
228
+ def assert_deployment_status(expected_status, wait_sec)
229
+ eventually(:upto => wait_sec) do
230
+ actual_status = @codedeploy_client.get_deployment(:deployment_id => @deployment_id).deployment_info.status
231
+ expect(actual_status).to eq(expected_status)
232
+ end
233
+ end