ruby-cdk 0.1.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.
Files changed (155) hide show
  1. checksums.yaml +7 -0
  2. data/CHANGELOG.md +87 -0
  3. data/LICENSE +21 -0
  4. data/README.md +303 -0
  5. data/THIRD_PARTY_NOTICES.md +327 -0
  6. data/data/spec/META.json +33 -0
  7. data/data/spec/model.json.gz +0 -0
  8. data/exe/ruby-cdk +9 -0
  9. data/lib/ruby-cdk.rb +4 -0
  10. data/lib/ruby_cdk/assets/builder.rb +122 -0
  11. data/lib/ruby_cdk/assets/hasher.rb +175 -0
  12. data/lib/ruby_cdk/assets/record.rb +49 -0
  13. data/lib/ruby_cdk/assets/uploader.rb +139 -0
  14. data/lib/ruby_cdk/assets/zipper.rb +108 -0
  15. data/lib/ruby_cdk/assets.rb +116 -0
  16. data/lib/ruby_cdk/bootstrap.rb +289 -0
  17. data/lib/ruby_cdk/cfn/resource.rb +93 -0
  18. data/lib/ruby_cdk/cfn.rb +10 -0
  19. data/lib/ruby_cdk/cli/arguments.rb +41 -0
  20. data/lib/ruby_cdk/cli/aws.rb +35 -0
  21. data/lib/ruby_cdk/cli/bootstrap.rb +63 -0
  22. data/lib/ruby_cdk/cli/diff.rb +98 -0
  23. data/lib/ruby_cdk/cli/failure.rb +80 -0
  24. data/lib/ruby_cdk/cli/io.rb +70 -0
  25. data/lib/ruby_cdk/cli/list.rb +46 -0
  26. data/lib/ruby_cdk/cli/new.rb +313 -0
  27. data/lib/ruby_cdk/cli/operation.rb +124 -0
  28. data/lib/ruby_cdk/cli/pairs.rb +32 -0
  29. data/lib/ruby_cdk/cli/report.rb +118 -0
  30. data/lib/ruby_cdk/cli/selection.rb +134 -0
  31. data/lib/ruby_cdk/cli/session.rb +290 -0
  32. data/lib/ruby_cdk/cli/shell.rb +30 -0
  33. data/lib/ruby_cdk/cli/synth.rb +58 -0
  34. data/lib/ruby_cdk/cli/validate.rb +101 -0
  35. data/lib/ruby_cdk/cli.rb +242 -0
  36. data/lib/ruby_cdk/constructs/bucket.rb +191 -0
  37. data/lib/ruby_cdk/constructs/function.rb +163 -0
  38. data/lib/ruby_cdk/constructs/grant.rb +90 -0
  39. data/lib/ruby_cdk/constructs/log_group.rb +115 -0
  40. data/lib/ruby_cdk/constructs/policy_statement.rb +161 -0
  41. data/lib/ruby_cdk/constructs/queue.rb +118 -0
  42. data/lib/ruby_cdk/constructs/role.rb +282 -0
  43. data/lib/ruby_cdk/constructs/table.rb +160 -0
  44. data/lib/ruby_cdk/constructs/topic.rb +88 -0
  45. data/lib/ruby_cdk/constructs.rb +56 -0
  46. data/lib/ruby_cdk/core/app.rb +146 -0
  47. data/lib/ruby_cdk/core/construct.rb +105 -0
  48. data/lib/ruby_cdk/core/elements.rb +107 -0
  49. data/lib/ruby_cdk/core/ids.rb +117 -0
  50. data/lib/ruby_cdk/core/stack.rb +291 -0
  51. data/lib/ruby_cdk/core/stateful.rb +156 -0
  52. data/lib/ruby_cdk/core.rb +15 -0
  53. data/lib/ruby_cdk/deploy/capabilities.rb +44 -0
  54. data/lib/ruby_cdk/deploy/deployer.rb +805 -0
  55. data/lib/ruby_cdk/deploy/errors.rb +72 -0
  56. data/lib/ruby_cdk/deploy/fakes.rb +546 -0
  57. data/lib/ruby_cdk/deploy/output/base.rb +167 -0
  58. data/lib/ruby_cdk/deploy/output/human.rb +37 -0
  59. data/lib/ruby_cdk/deploy/output/json.rb +80 -0
  60. data/lib/ruby_cdk/deploy/output/quiet.rb +20 -0
  61. data/lib/ruby_cdk/deploy/parameters.rb +86 -0
  62. data/lib/ruby_cdk/deploy/poller.rb +72 -0
  63. data/lib/ruby_cdk/deploy/ports.rb +205 -0
  64. data/lib/ruby_cdk/deploy/sdk_adapters.rb +608 -0
  65. data/lib/ruby_cdk/deploy/states.rb +62 -0
  66. data/lib/ruby_cdk/deploy.rb +25 -0
  67. data/lib/ruby_cdk/diff/compare.rb +141 -0
  68. data/lib/ruby_cdk/diff/normalizer.rb +83 -0
  69. data/lib/ruby_cdk/diff/render.rb +242 -0
  70. data/lib/ruby_cdk/diff/security.rb +322 -0
  71. data/lib/ruby_cdk/diff/statement.rb +256 -0
  72. data/lib/ruby_cdk/diff/template.rb +524 -0
  73. data/lib/ruby_cdk/diff.rb +62 -0
  74. data/lib/ruby_cdk/env.rb +42 -0
  75. data/lib/ruby_cdk/errors.rb +38 -0
  76. data/lib/ruby_cdk/fn.rb +409 -0
  77. data/lib/ruby_cdk/lazy.rb +55 -0
  78. data/lib/ruby_cdk/nag/config.rb +303 -0
  79. data/lib/ruby_cdk/nag/engine.rb +152 -0
  80. data/lib/ruby_cdk/nag/report.rb +86 -0
  81. data/lib/ruby_cdk/nag/rule.rb +171 -0
  82. data/lib/ruby_cdk/nag/rules/api_gateway_authorization.rb +91 -0
  83. data/lib/ruby_cdk/nag/rules/dynamodb_point_in_time_recovery.rb +45 -0
  84. data/lib/ruby_cdk/nag/rules/ebs_volume_encrypted.rb +41 -0
  85. data/lib/ruby_cdk/nag/rules/ec2_ingress_http.rb +48 -0
  86. data/lib/ruby_cdk/nag/rules/ec2_ingress_other_ports.rb +46 -0
  87. data/lib/ruby_cdk/nag/rules/iam_policy_admin_access.rb +60 -0
  88. data/lib/ruby_cdk/nag/rules/iam_policy_full_access.rb +55 -0
  89. data/lib/ruby_cdk/nag/rules/iam_user_credentials.rb +61 -0
  90. data/lib/ruby_cdk/nag/rules/lambda_runtime_supported.rb +88 -0
  91. data/lib/ruby_cdk/nag/rules/lambda_secret_environment.rb +61 -0
  92. data/lib/ruby_cdk/nag/rules/log_group_retention.rb +40 -0
  93. data/lib/ruby_cdk/nag/rules/policy_statements.rb +128 -0
  94. data/lib/ruby_cdk/nag/rules/rds_storage_encrypted.rb +65 -0
  95. data/lib/ruby_cdk/nag/rules/resource_policy_wildcard_principal.rb +58 -0
  96. data/lib/ruby_cdk/nag/rules/s3_public_access_block.rb +56 -0
  97. data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +136 -0
  98. data/lib/ruby_cdk/nag/rules/stateful_deletion_policy.rb +46 -0
  99. data/lib/ruby_cdk/nag/rules.rb +51 -0
  100. data/lib/ruby_cdk/nag/values.rb +305 -0
  101. data/lib/ruby_cdk/nag.rb +60 -0
  102. data/lib/ruby_cdk/schema/normalizer.rb +164 -0
  103. data/lib/ruby_cdk/schema/property_schema.rb +22 -0
  104. data/lib/ruby_cdk/schema/schema.rb +195 -0
  105. data/lib/ruby_cdk/schema/sources/compiler.rb +56 -0
  106. data/lib/ruby_cdk/schema/sources/descriptor.rb +21 -0
  107. data/lib/ruby_cdk/schema/sources/download.rb +86 -0
  108. data/lib/ruby_cdk/schema/sources/provider_schemas.rb +47 -0
  109. data/lib/ruby_cdk/schema/sources/provider_type.rb +630 -0
  110. data/lib/ruby_cdk/schema/sources/regions.rb +86 -0
  111. data/lib/ruby_cdk/schema/sources/resource_specification.rb +94 -0
  112. data/lib/ruby_cdk/schema/sources/specification_type.rb +159 -0
  113. data/lib/ruby_cdk/schema/sources/update.rb +178 -0
  114. data/lib/ruby_cdk/schema/sources.rb +20 -0
  115. data/lib/ruby_cdk/schema/type_schema.rb +109 -0
  116. data/lib/ruby_cdk/schema/validator/corpus_check.rb +81 -0
  117. data/lib/ruby_cdk/schema/validator/policies.rb +48 -0
  118. data/lib/ruby_cdk/schema/validator/refs.rb +489 -0
  119. data/lib/ruby_cdk/schema/validator/report.rb +92 -0
  120. data/lib/ruby_cdk/schema/validator/rule.rb +119 -0
  121. data/lib/ruby_cdk/schema/validator/scans.rb +146 -0
  122. data/lib/ruby_cdk/schema/validator/structure.rb +469 -0
  123. data/lib/ruby_cdk/schema/validator/template.rb +203 -0
  124. data/lib/ruby_cdk/schema/validator/template_rules.rb +70 -0
  125. data/lib/ruby_cdk/schema/validator/walk.rb +122 -0
  126. data/lib/ruby_cdk/schema/validator.rb +159 -0
  127. data/lib/ruby_cdk/schema.rb +19 -0
  128. data/lib/ruby_cdk/synth/manifest.rb +128 -0
  129. data/lib/ruby_cdk/synth/resolver.rb +326 -0
  130. data/lib/ruby_cdk/synth/rules.rb +83 -0
  131. data/lib/ruby_cdk/synth/serializer.rb +120 -0
  132. data/lib/ruby_cdk/synth/writer.rb +31 -0
  133. data/lib/ruby_cdk/synth.rb +255 -0
  134. data/lib/ruby_cdk/test/capture.rb +133 -0
  135. data/lib/ruby_cdk/test/match.rb +426 -0
  136. data/lib/ruby_cdk/test/matcher.rb +290 -0
  137. data/lib/ruby_cdk/test/private/absent.rb +34 -0
  138. data/lib/ruby_cdk/test/private/conditions.rb +39 -0
  139. data/lib/ruby_cdk/test/private/cyclic.rb +160 -0
  140. data/lib/ruby_cdk/test/private/error.rb +48 -0
  141. data/lib/ruby_cdk/test/private/js.rb +326 -0
  142. data/lib/ruby_cdk/test/private/mappings.rb +39 -0
  143. data/lib/ruby_cdk/test/private/outputs.rb +39 -0
  144. data/lib/ruby_cdk/test/private/parameters.rb +39 -0
  145. data/lib/ruby_cdk/test/private/resources.rb +116 -0
  146. data/lib/ruby_cdk/test/private/section.rb +98 -0
  147. data/lib/ruby_cdk/test/private/sorting.rb +40 -0
  148. data/lib/ruby_cdk/test/private/sparse_matrix.rb +35 -0
  149. data/lib/ruby_cdk/test/private/type.rb +23 -0
  150. data/lib/ruby_cdk/test/template.rb +364 -0
  151. data/lib/ruby_cdk/test.rb +116 -0
  152. data/lib/ruby_cdk/values.rb +8 -0
  153. data/lib/ruby_cdk/version.rb +5 -0
  154. data/lib/ruby_cdk.rb +47 -0
  155. metadata +256 -0
@@ -0,0 +1,167 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "../ports"
4
+ require_relative "../capabilities"
5
+
6
+ module RubyCdk
7
+ module Deploy
8
+ module Output
9
+ # What the outputs share, in plain text: what happens goes to io (standard
10
+ # error), the outputs to result_io (standard out), and a question is asked
11
+ # only where it can be answered. Human adds colour to this, Quiet leaves
12
+ # out the progress.
13
+ #
14
+ # Nothing here knows a credential. What crosses this interface is an
15
+ # account ID, a region, a profile name, changes, events and outputs; the
16
+ # values of template properties and of parameters are never in them.
17
+ class Base
18
+ include Output
19
+
20
+ RESET = "\e[0m"
21
+ BOLD = "\e[1m"
22
+ RED = "\e[31m"
23
+ GREEN = "\e[32m"
24
+ YELLOW = "\e[33m"
25
+ CYAN = "\e[36m"
26
+
27
+ def initialize(io: $stderr, stdin: $stdin, result_io: $stdout)
28
+ @io = io
29
+ @stdin = stdin
30
+ @result_io = result_io
31
+ end
32
+
33
+ def target(account:, region:, profile:)
34
+ say("#{paint("Target", BOLD)}: account #{account}, region #{region}, profile #{profile || "the default credential chain"}")
35
+ end
36
+
37
+ # The change view. It names the properties that change, never their values.
38
+ def changes(list, capabilities: nil)
39
+ return if list.empty?
40
+
41
+ say(paint("Changes", BOLD))
42
+ list.each do |change|
43
+ action = change.action.to_s
44
+ line = " #{paint(action.ljust(8), color_for(action))}#{change.logical_id} #{change.resource_type}"
45
+ line += " #{paint(change.replacement, CYAN)}" if change.replacement && change.replacement != "False"
46
+ say(line)
47
+ names = changed_properties(change)
48
+ say(" #{names.join(", ")}") if names.any?
49
+ end
50
+ needed = Array(capabilities)
51
+ say(" #{capabilities_line(needed)}") if needed.any?
52
+ end
53
+
54
+ # Nobody there to answer is a no. The engine says what to do about it.
55
+ def ask(question)
56
+ return false unless interactive?
57
+
58
+ say("#{paint("?", BOLD)} #{question} #{paint("[y/N]", YELLOW)} ")
59
+ %w[y yes].include?(read_line.to_s.downcase)
60
+ end
61
+
62
+ def confirm(text)
63
+ return false unless interactive?
64
+
65
+ say("To confirm, type #{text.inspect}: ")
66
+ read_line == text
67
+ end
68
+
69
+ # One line per event: the time, the status in a column, what it is about,
70
+ # and why when it failed.
71
+ def event(event)
72
+ status = event.resource_status.to_s
73
+ what = [ event.resource_type, event.logical_id ].compact.join(" ")
74
+ reason = event.status_reason.to_s if status.end_with?("_FAILED") && event.status_reason.to_s.size > 0
75
+ status = status.ljust(34) unless what.empty? || status.length >= 34
76
+
77
+ say([ " #{moment(event)}", paint(status, color_for(status)), what, reason ].compact.reject(&:empty?).join(" "))
78
+ end
79
+
80
+ def outputs(hash)
81
+ return if hash.nil? || hash.empty?
82
+
83
+ result_io.puts "Outputs:"
84
+ hash.each { |key, value| result_io.puts " #{key} = #{value}" }
85
+ end
86
+
87
+ def failure(detail)
88
+ say(paint(detail.to_s, RED))
89
+ end
90
+
91
+ def retained(list)
92
+ return if list.empty?
93
+
94
+ say(paint("Kept after the stack is deleted (their DeletionPolicy says so):", YELLOW))
95
+ list.each { |resource| say(" #{resource.logical_id} #{resource.resource_type} #{resource.deletion_policy}") }
96
+ end
97
+
98
+ def announce(text)
99
+ say(text)
100
+ end
101
+
102
+ private
103
+
104
+ attr_reader :io, :stdin, :result_io
105
+
106
+ def say(text)
107
+ io.puts text
108
+ end
109
+
110
+ # True when there is somebody to answer: a terminal on standard input.
111
+ def interactive?
112
+ stdin.respond_to?(:tty?) && stdin.tty?
113
+ end
114
+
115
+ # One line from the person, or nil. Interrupting the question is not an answer.
116
+ def read_line
117
+ stdin.gets&.strip
118
+ rescue Interrupt
119
+ nil
120
+ end
121
+
122
+ # [attribute: name] for what a change touches. The detail comes in the
123
+ # shape CloudFormation describes it.
124
+ def changed_properties(change)
125
+ Array(change.detail).filter_map do |entry|
126
+ next unless entry.is_a?(Hash)
127
+
128
+ target = entry[:target] || entry["target"]
129
+ next unless target.is_a?(Hash)
130
+
131
+ name = target[:name] || target["name"]
132
+ next if name.nil?
133
+
134
+ "#{target[:attribute] || target["attribute"] || "Properties"}: #{name}"
135
+ end
136
+ end
137
+
138
+ def capabilities_line(capabilities)
139
+ reason = if capabilities.include?(Capabilities::NAMED_IAM)
140
+ "the template creates IAM resources, some with explicit names"
141
+ else
142
+ "the template creates IAM resources"
143
+ end
144
+ paint("Capabilities needed: #{capabilities.join(", ")}, because #{reason}.", CYAN)
145
+ end
146
+
147
+ def moment(event)
148
+ if event.timestamp.respond_to?(:utc)
149
+ event.timestamp.utc.strftime("%H:%M:%SZ")
150
+ else
151
+ "--:--:--"
152
+ end
153
+ end
154
+
155
+ # Human puts a colour around text; here there is none.
156
+ def paint(text, _code)
157
+ text
158
+ end
159
+
160
+ # The colour of a status or an action, for Human.
161
+ def color_for(_status)
162
+ nil
163
+ end
164
+ end
165
+ end
166
+ end
167
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module RubyCdk
6
+ module Deploy
7
+ module Output
8
+ # For a person: the same text as Base, with a colour on what to look at.
9
+ # Coloured only when the output is a terminal, unless color says otherwise.
10
+ class Human < Base
11
+ def initialize(color: nil, **options)
12
+ super(**options)
13
+ @color = color.nil? ? io.respond_to?(:tty?) && io.tty? : color
14
+ end
15
+
16
+ private
17
+
18
+ def paint(text, code)
19
+ code && @color ? "#{code}#{text}#{RESET}" : text
20
+ end
21
+
22
+ # Red is what went wrong or was rolled back; yellow is what is under
23
+ # way, a rollback included; green is what succeeded. A rollback in
24
+ # progress is not a failure yet, so what is still in progress is asked
25
+ # first: UPDATE_ROLLBACK_COMPLETE_CLEANUP_IN_PROGRESS is under way.
26
+ def color_for(status)
27
+ status = status.to_s
28
+ return YELLOW if status.end_with?("_IN_PROGRESS") || status == "Modify"
29
+ return RED if status.end_with?("_FAILED") || status.include?("ROLLBACK_COMPLETE") || status == "Remove"
30
+ return GREEN if status.end_with?("_COMPLETE") || status == "Add"
31
+
32
+ nil
33
+ end
34
+ end
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+ require_relative "base"
5
+
6
+ module RubyCdk
7
+ module Deploy
8
+ module Output
9
+ # For --json: everything the engine says is one JSON object per line, on
10
+ # io, each with a `type`. Standard out is left whole for the command,
11
+ # which prints the result of the deploy.
12
+ class Json < Base
13
+ def target(account:, region:, profile:)
14
+ emit(type: "target", account: account, region: region, profile: profile)
15
+ end
16
+
17
+ # The change view: what changes and which properties, by name. The
18
+ # values of the properties are left out here as in the text, and for the
19
+ # same reason: a template can hold a value that is not for a log.
20
+ def changes(list, capabilities: nil)
21
+ emit(type: "changes", changes: list.map { |change| summary_of(change) }, capabilities: Array(capabilities))
22
+ end
23
+
24
+ def ask(question)
25
+ emit(type: "ask", question: question)
26
+ return false unless interactive?
27
+
28
+ %w[y yes].include?(read_line.to_s.downcase)
29
+ end
30
+
31
+ def confirm(text)
32
+ emit(type: "confirm", text: text)
33
+ return false unless interactive?
34
+
35
+ read_line == text
36
+ end
37
+
38
+ def event(event)
39
+ emit(type: "event", event: plain(event.to_h))
40
+ end
41
+
42
+ def outputs(hash)
43
+ emit(type: "outputs", outputs: hash)
44
+ end
45
+
46
+ def failure(detail)
47
+ emit(type: "failure", message: detail.to_s)
48
+ end
49
+
50
+ def retained(list)
51
+ emit(type: "retained", resources: list.map { |resource| plain(resource.to_h) })
52
+ end
53
+
54
+ def announce(text)
55
+ emit(type: "announce", message: text)
56
+ end
57
+
58
+ private
59
+
60
+ # A change without its detail: the detail carries the values.
61
+ def summary_of(change)
62
+ hash = change.to_h.reject { |key, _value| key == :detail }
63
+ hash[:properties] = changed_properties(change) if change.respond_to?(:detail)
64
+ plain(hash)
65
+ end
66
+
67
+ # A time is written as CloudFormation writes it, in UTC.
68
+ def plain(hash)
69
+ hash.transform_values do |value|
70
+ value.respond_to?(:utc) ? value.utc.strftime("%Y-%m-%dT%H:%M:%S.%6NZ") : value
71
+ end
72
+ end
73
+
74
+ def emit(hash)
75
+ io.puts JSON.generate(hash)
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "base"
4
+
5
+ module RubyCdk
6
+ module Deploy
7
+ module Output
8
+ # For --quiet: no progress. What a decision needs is still said: the
9
+ # target, the resources a destroy will keep, a question. So are a failure
10
+ # and the outputs.
11
+ class Quiet < Base
12
+ def changes(list, capabilities: nil); end
13
+
14
+ def event(event); end
15
+
16
+ def announce(text); end
17
+ end
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,86 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "errors"
4
+
5
+ module RubyCdk
6
+ module Deploy
7
+ # The parameter values a deploy sends, and whether they differ from the
8
+ # ones deployed. The rule is the CDK's (ParameterValues in its CLI): a value
9
+ # that is given wins, then the template's default; a parameter that has
10
+ # neither is an error; a value for a parameter the template does not declare
11
+ # is passed on, and CloudFormation reports the typo.
12
+ class Parameters
13
+ # CloudFormation resolves these at deploy time, so they can change with no local sign of it.
14
+ SSM_TYPE_PREFIX = "AWS::SSM::Parameter::"
15
+
16
+ # { parameter key => value }: what the stack will use, defaults included.
17
+ attr_reader :values
18
+ # What goes to create_change_set: an Array of
19
+ # { parameter_key:, parameter_value: } and of
20
+ # { parameter_key:, use_previous_value: true }.
21
+ attr_reader :api
22
+
23
+ # template is parsed; overrides is { key => value }; deployed is
24
+ # { key => value } as the stack last had it, when there is a stack.
25
+ def initialize(template, overrides = {}, deployed: nil)
26
+ section = template["Parameters"]
27
+ declared = section.is_a?(Hash) ? section : {}
28
+ given = overrides.to_h.transform_keys(&:to_s)
29
+
30
+ @values = {}
31
+ @api = []
32
+ missing = []
33
+ declared.each do |key, spec|
34
+ if given.key?(key)
35
+ add(key, given[key])
36
+ elsif spec.is_a?(Hash) && !spec["Default"].nil?
37
+ @values[key] = text(spec["Default"])
38
+ elsif deployed && deployed.key?(key)
39
+ # Not given again: keep what the stack has, as the CDK sends it
40
+ # (UsePreviousValue). A NoEcho value comes back masked and is sent
41
+ # back masked, so a deploy with nothing new is recognised as such.
42
+ @values[key] = text(deployed[key])
43
+ @api << { parameter_key: key, use_previous_value: true }
44
+ else
45
+ missing << key
46
+ end
47
+ end
48
+ if missing.any?
49
+ raise ParametersMissing, "The following CloudFormation parameters are missing a value: #{missing.join(", ")}.\n" \
50
+ "Run the command again with --parameters #{missing.map { |key| "#{key}=<value>" }.join(" ")}."
51
+ end
52
+
53
+ given.each { |key, value| add(key, value) unless declared.key?(key) || text(value).empty? }
54
+ @resolved_by_cloud_formation = declared.values.any? do |spec|
55
+ spec.is_a?(Hash) && spec["Type"].to_s.start_with?(SSM_TYPE_PREFIX)
56
+ end
57
+ end
58
+
59
+ # True when a parameter's value is only known after CloudFormation resolved it.
60
+ def resolved_by_cloud_formation?
61
+ @resolved_by_cloud_formation
62
+ end
63
+
64
+ # deployed is { key => value } as the stack was last deployed.
65
+ def changes?(deployed)
66
+ return true if resolved_by_cloud_formation?
67
+
68
+ current = deployed || {}
69
+ current.any? { |key, value| !@values.key?(key) || text(value) != @values[key] } ||
70
+ @values.any? { |key, _| !current.key?(key) }
71
+ end
72
+
73
+ private
74
+
75
+ def add(key, value)
76
+ @values[key] = text(value)
77
+ @api << { parameter_key: key, parameter_value: @values[key] }
78
+ end
79
+
80
+ # A list parameter arrives as an Array; CloudFormation takes it as one comma-separated string.
81
+ def text(value)
82
+ value.is_a?(Array) ? value.map { |item| text(item) }.join(",") : value.to_s
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,72 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "errors"
4
+
5
+ module RubyCdk
6
+ module Deploy
7
+ # The engine's own polling: a first interval, a backoff, and an overall
8
+ # limit. The SDK's waiters do the same with a fixed 30-second delay.
9
+ #
10
+ # The sleep is a callable, so a test never waits. The limit is measured on
11
+ # the time spent sleeping, which makes it exact whatever the clock does.
12
+ class Poller
13
+ # Gives up on a change set that CloudFormation has not described within
14
+ # this many seconds (the SDK's waiter allows an hour).
15
+ CHANGE_SET_LIMIT = 900
16
+
17
+ # A stack operation can run for hours. This is the longest the engine follows one.
18
+ OPERATION_LIMIT = 4 * 60 * 60
19
+
20
+ attr_reader :limit
21
+
22
+ # sleep is called with a number of seconds.
23
+ def initialize(interval:, factor:, max_interval:, limit:, sleep: nil)
24
+ @interval = interval
25
+ @factor = factor
26
+ @max_interval = max_interval
27
+ @limit = limit
28
+ @sleep = sleep || ->(seconds) { Kernel.sleep(seconds) }
29
+ @waited = 0.0
30
+ @delay = interval
31
+ end
32
+
33
+ def self.change_set
34
+ new(interval: 1, factor: 1.5, max_interval: 15, limit: CHANGE_SET_LIMIT)
35
+ end
36
+
37
+ def self.operation
38
+ new(interval: 2, factor: 1.5, max_interval: 10, limit: OPERATION_LIMIT)
39
+ end
40
+
41
+ # For tests, which script every answer: it never really waits, and it still
42
+ # gives up, so a test that forgot the last answer fails instead of hanging.
43
+ def self.instant
44
+ new(interval: 0.001, factor: 1, max_interval: 0.001, limit: 2, sleep: ->(_seconds) { })
45
+ end
46
+
47
+ # Calls the block until it returns something other than nil, pausing in
48
+ # between. Returns what the block returned. Raises WaitTimedOut when the
49
+ # limit passes first.
50
+ def run
51
+ @waited = 0.0
52
+ @delay = @interval
53
+ loop do
54
+ result = yield
55
+ return result unless result.nil?
56
+
57
+ pause
58
+ end
59
+ end
60
+
61
+ def pause
62
+ if @waited + @delay > @limit
63
+ raise WaitTimedOut, "the limit of #{@limit} seconds passed"
64
+ end
65
+
66
+ @sleep.call(@delay)
67
+ @waited += @delay
68
+ @delay = [ @delay * @factor, @max_interval ].min
69
+ end
70
+ end
71
+ end
72
+ end
@@ -0,0 +1,205 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RubyCdk
4
+ module Deploy
5
+ ChangeSet = Struct.new(:id, :status, :status_reason, :execution_status, :changes, keyword_init: true)
6
+ Change = Struct.new(:action, :logical_id, :resource_type, :replacement, :detail, keyword_init: true)
7
+ Event = Struct.new(:resource_status, :logical_id, :resource_type, :status_reason, :timestamp, keyword_init: true)
8
+ StackResource = Struct.new(:logical_id, :status, :physical_id, keyword_init: true)
9
+ Identity = Struct.new(:account, :region, :arn, keyword_init: true)
10
+ StackInfo = Struct.new(:name, :id, :status, :outputs, :termination_protection, keyword_init: true)
11
+ ListedStack = Struct.new(:name, :id, :status, keyword_init: true)
12
+
13
+ # What stack_state answers.
14
+ #
15
+ # :absent no such stack, or DELETE_COMPLETE: create it
16
+ # :review REVIEW_IN_PROGRESS: a change set was never executed; create it again
17
+ # :stable *_COMPLETE and not one of the two below: update it
18
+ # :rollback_complete the creation failed and rolled back: delete it, then create it
19
+ # :rollback_failed a rollback is waiting to be continued: refuse
20
+ # :busy an operation is running: refuse
21
+ STACK_STATES = %i[absent review stable rollback_complete rollback_failed busy].freeze
22
+
23
+ # The state each CloudFormation stack status is in. This is the promise of
24
+ # stack_state, so the table sits with the port: the adapter that answers
25
+ # stack_state and the engine that acts on the answer read the same one.
26
+ # The grouping is the CDK's (stack-status.ts in its CLI).
27
+ module StackStatus
28
+ IN_PROGRESS = "_IN_PROGRESS"
29
+ REVIEW = "REVIEW_IN_PROGRESS"
30
+ DELETED = "DELETE_COMPLETE"
31
+
32
+ STATE = {
33
+ DELETED => :absent,
34
+ REVIEW => :review,
35
+ "CREATE_COMPLETE" => :stable,
36
+ "UPDATE_COMPLETE" => :stable,
37
+ "UPDATE_ROLLBACK_COMPLETE" => :stable,
38
+ "IMPORT_COMPLETE" => :stable,
39
+ "IMPORT_ROLLBACK_COMPLETE" => :stable,
40
+ # The CDK's isCreationFailure: only a delete is possible, then a new creation.
41
+ "ROLLBACK_COMPLETE" => :rollback_complete,
42
+ "ROLLBACK_FAILED" => :rollback_complete,
43
+ # A rollback is waiting to be continued, for an update as for an import:
44
+ # an IMPORT_COMPLETE stack whose update fails rolls back into
45
+ # IMPORT_ROLLBACK_FAILED.
46
+ "UPDATE_ROLLBACK_FAILED" => :rollback_failed,
47
+ "CREATE_FAILED" => :rollback_failed,
48
+ "UPDATE_FAILED" => :rollback_failed,
49
+ "DELETE_FAILED" => :rollback_failed,
50
+ "IMPORT_ROLLBACK_FAILED" => :rollback_failed
51
+ }.freeze
52
+
53
+ # The state of a stack status, or nil for a status this tool does not know.
54
+ # nil is a stack that does not exist. REVIEW_IN_PROGRESS is looked up
55
+ # before the rule for in-progress statuses, because it ends the same way.
56
+ def self.state(status)
57
+ return :absent if status.nil?
58
+ return nil unless status.is_a?(String)
59
+
60
+ STATE.fetch(status) { status.end_with?(IN_PROGRESS) ? :busy : nil }
61
+ end
62
+ end
63
+
64
+ # How the engine talks to a person, or to --json and --quiet.
65
+ module Output
66
+ def target(account:, region:, profile:)
67
+ Kernel.raise NotImplementedError
68
+ end
69
+
70
+ # capabilities are the ones the template needed; the change view says which.
71
+ def changes(list, capabilities: nil)
72
+ Kernel.raise NotImplementedError
73
+ end
74
+
75
+ # Asks a yes-or-no question. Returns true for yes.
76
+ def ask(question)
77
+ Kernel.raise NotImplementedError
78
+ end
79
+
80
+ # Asks the person to type text, exactly. Returns true only when they did.
81
+ def confirm(text)
82
+ Kernel.raise NotImplementedError
83
+ end
84
+
85
+ def event(event)
86
+ Kernel.raise NotImplementedError
87
+ end
88
+
89
+ def outputs(hash)
90
+ Kernel.raise NotImplementedError
91
+ end
92
+
93
+ def failure(detail)
94
+ Kernel.raise NotImplementedError
95
+ end
96
+
97
+ def retained(list)
98
+ Kernel.raise NotImplementedError
99
+ end
100
+
101
+ # Says what the engine is doing or about to do, when it is none of the above:
102
+ # that a failed stack is being deleted, that a change set was left unexecuted.
103
+ def announce(text)
104
+ Kernel.raise NotImplementedError
105
+ end
106
+ end
107
+
108
+ # The port to CloudFormation. Everything behind it is the AWS SDK, or a fake.
109
+ module CfnClient
110
+ def resolve_identity
111
+ Kernel.raise NotImplementedError
112
+ end
113
+
114
+ def stack_state(name)
115
+ Kernel.raise NotImplementedError
116
+ end
117
+
118
+ def stack_info(name)
119
+ Kernel.raise NotImplementedError
120
+ end
121
+
122
+ # The parameters the stack was last deployed with: { parameter key => value }.
123
+ def stack_parameters(name)
124
+ Kernel.raise NotImplementedError
125
+ end
126
+
127
+ # The tags on the stack: an Array of { key:, value: }.
128
+ def stack_tags(name)
129
+ Kernel.raise NotImplementedError
130
+ end
131
+
132
+ # template is the JSON text of the template, sent inline; template_url,
133
+ # when given, is the URL of the template placed in the bucket, and
134
+ # template is nil then: CloudFormation takes one or the other, never
135
+ # both (architecture §8).
136
+ def create_change_set(name, change_set_name, template:, change_set_type:, parameters:, tags:, capabilities:, on_stack_failure: nil,
137
+ template_url: nil)
138
+ Kernel.raise NotImplementedError
139
+ end
140
+
141
+ def describe_change_set(name, change_set_id)
142
+ Kernel.raise NotImplementedError
143
+ end
144
+
145
+ def execute_change_set(name, change_set_id, disable_rollback: false, retain_except_on_create: true)
146
+ Kernel.raise NotImplementedError
147
+ end
148
+
149
+ def delete_change_set(name, change_set_id)
150
+ Kernel.raise NotImplementedError
151
+ end
152
+
153
+ def delete_stack(name)
154
+ Kernel.raise NotImplementedError
155
+ end
156
+
157
+ def list_resources(name_or_id)
158
+ Kernel.raise NotImplementedError
159
+ end
160
+
161
+ # Every stack that is not deleted.
162
+ def list_stacks
163
+ Kernel.raise NotImplementedError
164
+ end
165
+
166
+ # after is a Time: only the events newer than it, oldest first. nil asks for
167
+ # the events of the operation in progress, leaving the moment to the client,
168
+ # which knows the clock skew between this machine and AWS.
169
+ def events(name_or_id, after:)
170
+ Kernel.raise NotImplementedError
171
+ end
172
+
173
+ def get_template(name)
174
+ Kernel.raise NotImplementedError
175
+ end
176
+
177
+ def update_termination_protection(name, enabled:)
178
+ Kernel.raise NotImplementedError
179
+ end
180
+
181
+ # Optional. The events CloudFormation recorded while it validated a change
182
+ # set that failed to be created. NotImplementedError, or any error, means
183
+ # they cannot be read; the engine then reports the reason alone.
184
+ def operation_events(name, change_set_id)
185
+ Kernel.raise NotImplementedError
186
+ end
187
+ end
188
+
189
+ # The port to S3. Every call takes the expected bucket owner.
190
+ module ObjectStore
191
+ # :mine, :foreign or :absent
192
+ def bucket_state(name, expected_owner:)
193
+ Kernel.raise NotImplementedError
194
+ end
195
+
196
+ def object_exists?(bucket, key, expected_owner:)
197
+ Kernel.raise NotImplementedError
198
+ end
199
+
200
+ def put(bucket, key, body:, expected_owner:)
201
+ Kernel.raise NotImplementedError
202
+ end
203
+ end
204
+ end
205
+ end