openstax_aws 1.3.0 → 1.4.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b1bb7df27bc9eb147fc4dccf7969fbcfaab379a615adcb2e6a255bc1951f72ca
4
- data.tar.gz: dfa73e4d1aa3b1973678aa4fbcff0588e6990ec59a74b1ffad4758820b08600c
3
+ metadata.gz: 75f65f32cd2e572d1e83f624977186c7aee220c6d51924333fa08af2255543ad
4
+ data.tar.gz: bf3969a29e8fb4ff94b54bbd9685581cdff28f9c160f3ebe0fa26b92f4875575
5
5
  SHA512:
6
- metadata.gz: 27c7c692f885a25aed594990cd52478754a53a87b3f5c473e8815777070c809b8cead54937c9dfbb6f9f0d629c3169916e17b747ca92db1e3a9b300d6e3db0dd
7
- data.tar.gz: 40a6e67cd196f2870d9f8c54873641719c7c799ddf7778665c730558de93eef2758ca940a667110a07aa189f755787940596e7865eeb778ec6b83ec4be109cc2
6
+ metadata.gz: 76287d75f76a51cbeb635e6013f5e896174537fcba6352f5febba64c562c3cb857f82aa2a03f71af9583c5db3e8527bf0298e4e2759db9ac658b7a13c0fa6c14
7
+ data.tar.gz: f86924695c16ce1b3729e9a405e36603761477aa4c7af798ea50bd5faeffcaf18d3caca056db5f8d72127c8bf9c7edfd464d46a7e109f8597d1437ecffb2788f
@@ -4,9 +4,7 @@ on:
4
4
  pull_request:
5
5
  push:
6
6
  branches:
7
- - main
8
- schedule:
9
- - cron: '0 0 * * 0' # weekly
7
+ - master
10
8
 
11
9
  jobs:
12
10
  tests:
@@ -17,7 +15,7 @@ jobs:
17
15
  - uses: actions/checkout@v2
18
16
  - uses: actions/setup-ruby@v1
19
17
  with:
20
- ruby-version: 2.7
18
+ ruby-version: 2.6
21
19
  - uses: actions/cache@v2
22
20
  with:
23
21
  path: vendor/bundle
@@ -30,4 +28,4 @@ jobs:
30
28
  bundle config path vendor/bundle
31
29
  bundle config jobs 2
32
30
  bundle install
33
- bundle exec rake
31
+ bundle exec rspec
@@ -260,6 +260,32 @@ module OpenStax::Aws
260
260
  nil # can be overridden by the DSL
261
261
  end
262
262
 
263
+ def failed_statuses_table
264
+ rows = []
265
+
266
+ stacks.each do |stack|
267
+ stack.status(reload: false).failed_events_since_last_user_event.each do |event|
268
+ rows.push([stack.name, event.status_text, event.status_reason])
269
+ end if stack.status.failed?
270
+ end
271
+
272
+ column_widths = [
273
+ 2 + rows.reduce(0) { |result, rowdata| [result, rowdata[0].length].max },
274
+ 2 + rows.reduce(0) { |result, rowdata| [result, rowdata[1].length].max },
275
+ 0
276
+ ]
277
+
278
+ output = []
279
+
280
+ output.push(["Stack", "Status", "Reason"].each_with_index.map { |header, index| header.ljust(column_widths[index]) }.join(''))
281
+
282
+ rows.each { |rowdata|
283
+ output.push(rowdata.each_with_index.map { |value, index| value.ljust(column_widths[index]) }.join(''))
284
+ }
285
+
286
+ output.join("\n")
287
+ end
288
+
263
289
  protected
264
290
 
265
291
  def parameter_default(parameter_name)
@@ -360,13 +386,24 @@ module OpenStax::Aws
360
386
  get_image_tag(image_id: image_id, key: "sha")
361
387
  end
362
388
 
363
- protected
364
-
365
389
  def secrets_namespace(id: 'default')
366
390
  raise "Override this method in your deployment class and provide a namespace " \
367
391
  "for secrets data in the AWS Parameter Store. The key there will be this namespace " \
368
392
  "prefixed by the environment name and suffixed with the secret name."
369
393
  end
370
394
 
395
+ def log_and_exit_if_failed_status
396
+ begin
397
+ yield
398
+ rescue
399
+ if status.failed?
400
+ logger.fatal("The following errors have occurred: \n#{failed_statuses_table}")
401
+ exit(1)
402
+ else
403
+ raise
404
+ end
405
+ end
406
+ end
407
+
371
408
  end
372
409
  end
@@ -34,7 +34,8 @@ module OpenStax::Aws
34
34
  begin
35
35
  Aws::CloudFront::Waiters::InvalidationCompleted.new(
36
36
  client: client,
37
- before_attempt: ->(*) { wait_message.say_it }
37
+ before_attempt: ->(*) { wait_message.say_it },
38
+ max_attempts: 60
38
39
  ).wait(
39
40
  distribution_id: id,
40
41
  id: invalidation_id
@@ -11,7 +11,7 @@ module OpenStax::Aws
11
11
  def self.file_content_at_sha(org_slash_repo:, sha:, path:, github_token: nil )
12
12
  if github_token.blank?
13
13
  location = "https://raw.githubusercontent.com/#{org_slash_repo}/#{sha}/#{path}"
14
- file = open(location)
14
+ file = URI.open(location)
15
15
  file.read
16
16
  else
17
17
  uri = URI("https://raw.githubusercontent.com/#{org_slash_repo}/#{sha}/#{path}")
@@ -41,54 +41,80 @@ module OpenStax::Aws
41
41
  cmd = "PACKER_LOG=1 #{cmd}" if @verbose
42
42
  cmd = "#{cmd} --debug" if @debug
43
43
 
44
- cmd = "#{cmd} #{@absolute_file_path}"
44
+ cmd
45
45
  end
46
46
 
47
47
  def run
48
48
  @logger.info("**** DRY RUN ****") if @dry_run
49
- @logger.info("Running: #{command}")
49
+ @logger.info("Running: #{command} #{@absolute_file_path}")
50
50
 
51
51
  if !@dry_run
52
52
  @logger.info("Printing stderr for desired verbosity")
53
- ami = ""
54
53
 
55
- Open3.popen2e(command) do |stdin, stdout_err, wait_thr|
56
- begin
57
- previous_interrupt_handler = Signal.trap 'INT' do
58
- # Interrupt Packer
59
- Process.kill 'INT', wait_thr.pid
54
+ tmpdir = nil
60
55
 
61
- # Restore previous interrupt handler so we don't interrupt Packer again
62
- Signal.trap 'INT', previous_interrupt_handler
56
+ begin
57
+ config_path = @absolute_file_path
63
58
 
64
- # Disable other code that restores previous interrupt
65
- previous_interrupt_handler = nil
59
+ # Can't handle modifying HCL2 templates yet
60
+ if config_path.ends_with?('.json')
61
+ config = JSON.parse(File.read(config_path))
62
+ config['post-processors'] ||= []
63
+ manifest_config = (config['post-processors']).find do |processor|
64
+ processor['type'] == 'manifest'
66
65
  end
67
66
 
68
- stdout_err.sync = true
67
+ # Configure a manifest post-processor if not already configured
68
+ if manifest_config.nil?
69
+ tmpdir = Dir.mktmpdir
69
70
 
70
- line = ''
71
+ manifest_config = { 'type' => 'manifest', 'output' => "#{tmpdir}/manifest.json" }
71
72
 
72
- while char = stdout_err.getc do
73
- line << char
74
- STDERR.print char
73
+ config['post-processors'] << manifest_config
75
74
 
76
- next unless char == "\n"
75
+ config_path = "#{tmpdir}/packer.json"
77
76
 
78
- matchami = line.match(/AMI: (ami-[0-9\-a-z]*)/i)
79
- ami = matchami.captures[0] if matchami
80
-
81
- line = ''
77
+ File.write(config_path, JSON.dump(config))
82
78
  end
83
- ensure
84
- # Restore previous interrupt unless we did so already
85
- Signal.trap 'INT', previous_interrupt_handler unless previous_interrupt_handler.nil?
86
79
  end
87
80
 
88
- puts ami
81
+ Open3.popen2e("#{command} #{config_path}") do |stdin, stdout_err, wait_thr|
82
+ begin
83
+ previous_interrupt_handler = Signal.trap 'INT' do
84
+ # Interrupt Packer
85
+ Process.kill 'INT', wait_thr.pid
86
+
87
+ # Restore previous interrupt handler so we don't interrupt Packer again
88
+ Signal.trap 'INT', previous_interrupt_handler
89
+
90
+ # Disable other code that restores previous interrupt
91
+ previous_interrupt_handler = nil
92
+ end
93
+
94
+ stdout_err.sync = true
95
+
96
+ # Send all packer output to STDERR
97
+ while char = stdout_err.getc do
98
+ STDERR.print char
99
+ end
100
+ ensure
101
+ # Restore previous interrupt unless we did so already
102
+ Signal.trap 'INT', previous_interrupt_handler unless previous_interrupt_handler.nil?
103
+ end
104
+
105
+ # Read the AMI ID from the manifest file and output it to STDOUT
106
+ unless manifest_config.nil?
107
+ manifest = File.read(manifest_config['output']) rescue nil
89
108
 
90
- # Return Packer's exit status wrapped in a Process::Status object
91
- wait_thr.value
109
+ puts JSON.parse(manifest)['builds'].last['artifact_id'].split(':', 2).last \
110
+ unless manifest.nil?
111
+ end
112
+
113
+ # Return Packer's exit status wrapped in a Process::Status object
114
+ wait_thr.value
115
+ end
116
+ ensure
117
+ FileUtils.remove_entry(tmpdir) unless tmpdir.nil?
92
118
  end
93
119
  end
94
120
  end
@@ -32,9 +32,12 @@ module OpenStax::Aws
32
32
  dry_run: dry_run)
33
33
  end
34
34
 
35
- def build
35
+ def build(debug: false, env_vars: {})
36
36
  # SAM doesn't have an API or SDK - we have to make calls to its CLI
37
+ env_vars_prefix = env_vars.map { |variable, value| "#{variable}=#{value}" }.join(' ')
37
38
  command = "sam build -t #{absolute_template_path} -b #{build_directory}"
39
+ command += ' --debug' if debug
40
+ command = "#{env_vars_prefix} #{command}" if env_vars_prefix.present?
38
41
  System.call(command, logger: logger, dry_run: dry_run)
39
42
  end
40
43
 
@@ -50,7 +53,8 @@ module OpenStax::Aws
50
53
  " --capabilities CAPABILITY_IAM" \
51
54
  " --s3-bucket #{bucket_name}" \
52
55
  " --s3-prefix #{name}" \
53
- " --stack-name #{name}"
56
+ " --stack-name #{name}" \
57
+ " --region #{region}"
54
58
 
55
59
  if params.any?
56
60
  command += " --parameter-overrides #{self.class.format_hash_as_cli_stack_parameters(params)}"
@@ -100,7 +100,7 @@ module OpenStax::Aws
100
100
  end
101
101
 
102
102
  def is_sam?
103
- body.match(/Transform: AWS::Serverless/).present?
103
+ body.match(/Transform: .?AWS::Serverless/).present?
104
104
  end
105
105
 
106
106
  protected
@@ -1,5 +1,5 @@
1
1
  module OpenStax
2
2
  module Aws
3
- VERSION = "1.3.0"
3
+ VERSION = "1.4.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: openstax_aws
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - JP Slavinsky
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-03-23 00:00:00.000000000 Z
11
+ date: 2022-05-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: aws-sdk-autoscaling
@@ -327,7 +327,7 @@ licenses:
327
327
  - MIT
328
328
  metadata:
329
329
  allowed_push_host: https://rubygems.org
330
- post_install_message:
330
+ post_install_message:
331
331
  rdoc_options: []
332
332
  require_paths:
333
333
  - lib
@@ -342,8 +342,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
342
342
  - !ruby/object:Gem::Version
343
343
  version: '0'
344
344
  requirements: []
345
- rubygems_version: 3.2.7
346
- signing_key:
345
+ rubygems_version: 3.1.4
346
+ signing_key:
347
347
  specification_version: 4
348
348
  summary: openstax IaC
349
349
  test_files: []