cfndk 0.1.1 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. checksums.yaml +5 -5
  2. data/.circleci/config.yml +8 -5
  3. data/.gitignore +1 -0
  4. data/.rspec +2 -0
  5. data/Gemfile +0 -11
  6. data/Gemfile.lock +1069 -587
  7. data/README.md +124 -10
  8. data/cfndk.gemspec +7 -2
  9. data/docker/Dockerfile +8 -0
  10. data/docker/build.sh +3 -0
  11. data/docker/cfndk.sh +14 -0
  12. data/lib/cfndk/change_set_command.rb +14 -8
  13. data/lib/cfndk/command.rb +14 -6
  14. data/lib/cfndk/credential_provider_chain.rb +12 -42
  15. data/lib/cfndk/credential_resolvable.rb +10 -0
  16. data/lib/cfndk/diff.rb +38 -0
  17. data/lib/cfndk/global_config.rb +33 -2
  18. data/lib/cfndk/key_pair.rb +33 -1
  19. data/lib/cfndk/key_pair_command.rb +10 -3
  20. data/lib/cfndk/key_pairs.rb +12 -0
  21. data/lib/cfndk/stack.rb +67 -60
  22. data/lib/cfndk/stack_command.rb +26 -8
  23. data/lib/cfndk/stacks.rb +16 -0
  24. data/lib/cfndk/template_packager.rb +210 -0
  25. data/lib/cfndk/uuid.rb +10 -0
  26. data/lib/cfndk/version.rb +1 -1
  27. data/lib/cfndk.rb +12 -1
  28. data/spec/cfndk_spec.rb +1 -1
  29. data/spec/cfndk_stack_create_spec.rb +365 -5
  30. data/spec/cfndk_stack_destroy_spec.rb +64 -0
  31. data/spec/cfndk_stack_update_spec.rb +86 -0
  32. data/spec/fixtures/big_vpc.yaml +533 -0
  33. data/spec/fixtures/lambda_function/index.js +4 -0
  34. data/spec/fixtures/lambda_function/lambda_function.json +4 -0
  35. data/spec/fixtures/lambda_function/lambda_function.yaml +28 -0
  36. data/spec/fixtures/nested_stack.json +35 -0
  37. data/spec/fixtures/nested_stack.yaml +20 -0
  38. data/spec/fixtures/serverless_function/index.js +4 -0
  39. data/spec/fixtures/serverless_function/serverless_function.json +4 -0
  40. data/spec/fixtures/serverless_function/serverless_function.yaml +21 -0
  41. data/spec/fixtures/stack.json +8 -0
  42. data/spec/fixtures/stack.template.json +39 -0
  43. data/spec/fixtures/stack.yaml +22 -0
  44. data/spec/fixtures/vpc.template.json +40 -0
  45. data/vagrant/Vagrantfile +89 -0
  46. metadata +117 -13
@@ -0,0 +1,210 @@
1
+ using Polyfill(
2
+ String: %w[#delete_prefix]
3
+ )
4
+
5
+ module CFnDK
6
+ class TemplatePackager
7
+ def initialize(template_file, region, package, global_config, s3_client, sts_client)
8
+ @template_file = template_file
9
+ @region = region
10
+ @package = package
11
+ @global_config = global_config
12
+ @s3_client = s3_client
13
+ @sts_client = sts_client
14
+ @template_body = nil
15
+ @is_uploaded = false
16
+ end
17
+
18
+ def large_template?
19
+ template_body.size > 51200
20
+ end
21
+
22
+ def template_body
23
+ package_templte
24
+ end
25
+
26
+ def upload_template_file
27
+ key = [@global_config.s3_template_hash, @template_file].compact.join('/')
28
+ url = "https://s3.amazonaws.com/#{bucket_name}/#{key}"
29
+
30
+ unless @is_uploaded
31
+ create_bucket
32
+ @s3_client.put_object(
33
+ body: template_body,
34
+ bucket: bucket_name,
35
+ key: key
36
+ )
37
+ @is_uploaded = true
38
+ CFnDK.logger.info('Put S3 object: ' + url + ' Size: ' + template_body.size.to_s)
39
+ end
40
+ url
41
+ end
42
+
43
+ def package_templte
44
+ if !@template_body
45
+ if !@package
46
+ @template_body = File.open(@template_file, 'r').read
47
+ return @template_body
48
+ end
49
+ orgTemplate = File.open(@template_file, 'r').read
50
+ CFnDK.logger.debug('Original Template:' + orgTemplate)
51
+ if is_json?(orgTemplate)
52
+ data = JSON.parse(orgTemplate)
53
+ else
54
+ data = YAML.load(orgTemplate.gsub(/!/, '____CFNDK!____'))
55
+ end
56
+
57
+ if data['Resources']
58
+ data['Resources'].each do |k, v|
59
+ next unless v.key?('Type')
60
+ t = v['Type']
61
+ properties = v['Properties'] || {}
62
+ case t
63
+ when 'AWS::CloudFormation::Stack' then
64
+ if properties['TemplateURL'] =~ /^\s*./
65
+ tp = TemplatePackager.new(File.dirname(@template_file) + '/' + properties['TemplateURL'].sub(/^\s*.\//, ''), @region, @package, @global_config, @s3_client, @sts_client)
66
+ v['Properties']['TemplateURL'] = tp.upload_template_file
67
+ end
68
+ when 'AWS::Lambda::Function' then
69
+ if properties['Code'].kind_of?(String)
70
+ result = upload_zip_file(File.dirname(@template_file) + '/' + properties['Code'].sub(/^\s*.\//, ''))
71
+ v['Properties']['Code'] = {
72
+ 'S3Bucket' => result['bucket'],
73
+ 'S3Key' => result['key']
74
+ }
75
+ end
76
+ when 'AWS::Serverless::Function' then
77
+ if properties['CodeUri'].kind_of?(String)
78
+ result = upload_zip_file(File.dirname(@template_file) + '/' + properties['CodeUri'].sub(/^\s*.\//, ''))
79
+ v['Properties']['CodeUri'] = {
80
+ 'Bucket' => result['bucket'],
81
+ 'Key' => result['key']
82
+ }
83
+ end
84
+ when 'AWS::Serverless::Api' then
85
+ if properties['DefinitionUri'].kind_of?(String)
86
+ result = upload_file(File.dirname(@template_file) + '/' + properties['DefinitionUri'].sub(/^\s*.\//, ''))
87
+ v['Properties']['DefinitionUri'] = {
88
+ 'Bucket' => result['bucket'],
89
+ 'Key' => result['key']
90
+ }
91
+ end
92
+ when 'AWS::ApiGateway::RestApi' then
93
+ if properties['BodyS3Location'].kind_of?(String)
94
+ result = upload_file(File.dirname(@template_file) + '/' + properties['BodyS3Location'].sub(/^\s*.\//, ''))
95
+ v['Properties']['BodyS3Location'] = {
96
+ 'Bucket' => result['bucket'],
97
+ 'Key' => result['key']
98
+ }
99
+ end
100
+ end
101
+ ## TODO support resources
102
+ # * AWS::AppSync::GraphQLSchema DefinitionS3Location
103
+ # * AWS::AppSync::Resolver RequestMappingTemplateS3Location
104
+ # * AWS::AppSync::Resolver ResponseMappingTemplateS3Location
105
+ # * AWS::ElasticBeanstalk::ApplicationVersion SourceBundle
106
+ # * AWS::Glue::Job Command ScriptLocation
107
+ # * AWS::Include Location
108
+ end
109
+ end
110
+
111
+ if is_json?(orgTemplate)
112
+ @template_body = JSON.dump(data)
113
+ else
114
+ @template_body = YAML.dump_stream(data).gsub(/____CFNDK!____/, '!')
115
+ end
116
+ CFnDK.logger.info('Template Packager diff: ' + @template_file)
117
+ CFnDK.logger.info(CFnDK.diff(orgTemplate, @template_body).to_s)
118
+ CFnDK.logger.debug('Package Template size: ' + @template_body.size.to_s)
119
+ CFnDK.logger.debug('Package Template:' + @template_body)
120
+ end
121
+ @template_body
122
+ end
123
+
124
+ private
125
+
126
+ def upload_zip_file(path)
127
+ create_bucket
128
+ key = [@global_config.s3_template_hash, path.sub(/^.\//, '') + ".zip"].compact.join('/')
129
+
130
+
131
+ buffer = Zip::OutputStream.write_buffer do |out|
132
+ Dir.glob(path + '/**/*') do |file|
133
+ if (!File.directory?(file))
134
+ out.put_next_entry(file.delete_prefix(path + '/'))
135
+ out.write(File.open(file, 'r').read)
136
+ end
137
+ end
138
+ end
139
+
140
+ @s3_client.put_object(
141
+ body: buffer.string,
142
+ bucket: bucket_name,
143
+ key: key
144
+ )
145
+ url = "https://s3.amazonaws.com/#{bucket_name}/#{key}"
146
+ CFnDK.logger.info('Put S3 object: ' + url)
147
+ {
148
+ 'bucket' => bucket_name,
149
+ 'key' => key
150
+ }
151
+ end
152
+
153
+ def upload_file(path)
154
+ create_bucket
155
+ key = [@global_config.s3_template_hash, path.sub(/^.\//, '')].compact.join('/')
156
+
157
+ @s3_client.put_object(
158
+ body: File.open(path, 'r').read,
159
+ bucket: bucket_name,
160
+ key: key
161
+ )
162
+ url = "https://s3.amazonaws.com/#{bucket_name}/#{key}"
163
+ CFnDK.logger.info('Put S3 object: ' + url)
164
+ {
165
+ 'bucket' => bucket_name,
166
+ 'key' => key
167
+ }
168
+ end
169
+
170
+ def create_bucket
171
+ begin
172
+ @s3_client.head_bucket(bucket: bucket_name)
173
+ rescue Aws::S3::Errors::NotFound, Aws::S3::Errors::Forbidden
174
+ @s3_client.create_bucket(bucket: bucket_name)
175
+ CFnDK.logger.info('Creatt S3 bucket: ' + bucket_name)
176
+ @s3_client.put_bucket_lifecycle_configuration(
177
+ bucket: bucket_name,
178
+ lifecycle_configuration: {
179
+ rules: [
180
+ {
181
+ expiration: {
182
+ days: 1,
183
+ },
184
+ status: 'Enabled',
185
+ id: 'Delete Old Files',
186
+ prefix: '',
187
+ abort_incomplete_multipart_upload: {
188
+ days_after_initiation: 1,
189
+ },
190
+ },
191
+ ],
192
+ }
193
+ )
194
+ end
195
+ end
196
+
197
+ def bucket_name
198
+ resp = @sts_client.get_caller_identity({})
199
+ resp.account + '-' + @region + '-' + @global_config.s3_template_bucket
200
+ end
201
+
202
+ def is_json?(str)
203
+ begin
204
+ !!JSON.parse(str)
205
+ rescue
206
+ false
207
+ end
208
+ end
209
+ end
210
+ end
data/lib/cfndk/uuid.rb ADDED
@@ -0,0 +1,10 @@
1
+ module CFnDK
2
+ class Uuid
3
+ include Singleton
4
+
5
+ attr_reader :uuid
6
+ def initialize()
7
+ @uuid = SecureRandom.uuid
8
+ end
9
+ end
10
+ end
data/lib/cfndk/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module CFnDK
2
- VERSION = '0.1.1'.freeze
2
+ VERSION = '0.1.3'.freeze
3
3
  end
data/lib/cfndk.rb CHANGED
@@ -4,14 +4,21 @@ require 'rainbow/ext/string'
4
4
  require 'camelizable'
5
5
  require 'fileutils'
6
6
  require 'pathname'
7
+ require 'ostruct'
7
8
  require 'erb'
8
9
  require 'yaml'
9
10
  require 'json'
10
- require 'aws-sdk'
11
+ require 'zip'
12
+ require 'aws-sdk-ec2'
13
+ require 'aws-sdk-s3'
14
+ require 'aws-sdk-cloudformation'
11
15
  require 'terminal-table'
12
16
  require 'securerandom'
13
17
  require 'logger'
14
18
  require 'thor'
19
+ require 'diff/lcs'
20
+ require 'diff/lcs/hunk'
21
+ require 'polyfill'
15
22
 
16
23
  if ENV['CFNDK_COVERAGE']
17
24
  require 'simplecov'
@@ -29,11 +36,15 @@ require 'cfndk/global_config'
29
36
  require 'cfndk/logger'
30
37
  require 'cfndk/credential_provider_chain'
31
38
  require 'cfndk/subcommand_help_returnable'
39
+ require 'cfndk/credential_resolvable'
32
40
  require 'cfndk/config_file_loadable'
33
41
  require 'cfndk/key_pair_command'
34
42
  require 'cfndk/stack_command'
35
43
  require 'cfndk/change_set_command'
36
44
  require 'cfndk/command'
45
+ require 'cfndk/template_packager'
46
+ require 'cfndk/diff'
47
+ require 'cfndk/uuid'
37
48
 
38
49
  module CFnDK
39
50
  end
data/spec/cfndk_spec.rb CHANGED
@@ -36,7 +36,7 @@ RSpec.describe 'CFnDK', type: :aruba do
36
36
  it 'displays version' do
37
37
  aggregate_failures do
38
38
  expect(last_command_started).to be_successfully_executed
39
- expect(last_command_started).to have_output(/0.1.1/)
39
+ expect(last_command_started).to have_output(/0.1.3/)
40
40
  end
41
41
  end
42
42
  end