ruby-cdk 0.2.0 → 0.3.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 +4 -4
- data/CHANGELOG.md +253 -0
- data/README.md +28 -5
- data/lib/ruby_cdk/assets/hasher.rb +4 -0
- data/lib/ruby_cdk/assets.rb +4 -8
- data/lib/ruby_cdk/cli/aws.rb +23 -5
- data/lib/ruby_cdk/cli/drift.rb +94 -0
- data/lib/ruby_cdk/cli/import.rb +306 -0
- data/lib/ruby_cdk/cli/lookup.rb +214 -0
- data/lib/ruby_cdk/cli/session.rb +114 -3
- data/lib/ruby_cdk/cli/watch.rb +297 -0
- data/lib/ruby_cdk/cli.rb +76 -1
- data/lib/ruby_cdk/constructs/function.rb +24 -3
- data/lib/ruby_cdk/constructs/hosted_zone.rb +342 -0
- data/lib/ruby_cdk/constructs/record_set.rb +330 -0
- data/lib/ruby_cdk/constructs/ssm_parameter.rb +174 -0
- data/lib/ruby_cdk/constructs/vpc.rb +822 -0
- data/lib/ruby_cdk/constructs/vpc_network.rb +159 -0
- data/lib/ruby_cdk/constructs.rb +5 -1
- data/lib/ruby_cdk/core/app.rb +28 -2
- data/lib/ruby_cdk/core/construct.rb +2 -0
- data/lib/ruby_cdk/core/context.rb +138 -0
- data/lib/ruby_cdk/core/stack.rb +4 -0
- data/lib/ruby_cdk/core.rb +1 -0
- data/lib/ruby_cdk/deploy/deployer.rb +283 -8
- data/lib/ruby_cdk/deploy/drift.rb +389 -0
- data/lib/ruby_cdk/deploy/ec2_adapter.rb +214 -0
- data/lib/ruby_cdk/deploy/errors.rb +8 -0
- data/lib/ruby_cdk/deploy/fakes.rb +230 -22
- data/lib/ruby_cdk/deploy/output/base.rb +8 -0
- data/lib/ruby_cdk/deploy/output/json.rb +7 -0
- data/lib/ruby_cdk/deploy/poller.rb +10 -0
- data/lib/ruby_cdk/deploy/ports.rb +124 -8
- data/lib/ruby_cdk/deploy/sdk_adapters.rb +141 -6
- data/lib/ruby_cdk/deploy/ssm_adapter.rb +111 -0
- data/lib/ruby_cdk/deploy.rb +1 -0
- data/lib/ruby_cdk/diff/compare.rb +1 -1
- data/lib/ruby_cdk/diff/identity_center.rb +64 -0
- data/lib/ruby_cdk/diff/render.rb +147 -8
- data/lib/ruby_cdk/diff/security.rb +195 -10
- data/lib/ruby_cdk/diff/security_group.rb +94 -0
- data/lib/ruby_cdk/nag/rules/security_group_ingress.rb +2 -0
- data/lib/ruby_cdk/schema/sources/provider_type.rb +3 -0
- data/lib/ruby_cdk/test/private/cyclic.rb +4 -0
- data/lib/ruby_cdk/test/private/js.rb +1 -3
- data/lib/ruby_cdk/version.rb +1 -1
- metadata +43 -1
|
@@ -11,10 +11,88 @@ require_relative "hotswap"
|
|
|
11
11
|
|
|
12
12
|
module RubyCdk
|
|
13
13
|
module Deploy
|
|
14
|
+
# The option checks the CDK makes when a deploy starts, before it asks
|
|
15
|
+
# CloudFormation for anything (cdk-toolkit.ts:2469-2475 and
|
|
16
|
+
# util/cloudformation.ts, in aws/aws-cdk-cli at
|
|
17
|
+
# f7c9dca95f440a076c2abd61b66b984624b8548e). The engine runs them where
|
|
18
|
+
# its options are built, and the command line builds its options before
|
|
19
|
+
# it builds a client: a bad value is a usage error and never a request.
|
|
20
|
+
module Checks
|
|
21
|
+
# validateSnsTopicArn: the five segments, the service, and one resource
|
|
22
|
+
# segment of letters, numbers, hyphens and underscores, in any case.
|
|
23
|
+
# Topics in every partition are taken.
|
|
24
|
+
SNS_PARTITION = /\Aaws(-[a-z]+)*\z/
|
|
25
|
+
SNS_REGION = /\A[a-z0-9-]+\z/
|
|
26
|
+
SNS_ACCOUNT = /\A[0-9]+\z/
|
|
27
|
+
SNS_RESOURCE = /\A[a-z0-9_-]+\z/i
|
|
28
|
+
|
|
29
|
+
# validateCloudWatchAlarmArn: metric and composite alarms, in any case.
|
|
30
|
+
ALARM_ARN = /\Aarn:[a-z0-9-]+:cloudwatch:[a-z0-9-]+:[0-9]+:alarm:.+\z/i
|
|
31
|
+
|
|
32
|
+
# The most CloudFormation takes of each kind
|
|
33
|
+
# (util/cloudformation.ts:24-31).
|
|
34
|
+
MAX_ROLLBACK_TRIGGERS = 5
|
|
35
|
+
MAX_MONITORING_TIME_IN_MINUTES = 180
|
|
36
|
+
|
|
37
|
+
module_function
|
|
38
|
+
|
|
39
|
+
# The ARNs of the SNS topics CloudFormation notifies, as they were
|
|
40
|
+
# given. nil is not given: the request carries no NotificationARNs, and
|
|
41
|
+
# whatever the stack has is left alone.
|
|
42
|
+
def notification_arns(arns)
|
|
43
|
+
return nil if arns.nil?
|
|
44
|
+
|
|
45
|
+
arns.each do |arn|
|
|
46
|
+
prefix, partition, service, region, account, *resource = arn.to_s.split(":")
|
|
47
|
+
valid = prefix == "arn" && SNS_PARTITION.match?(partition) && service == "sns" && SNS_REGION.match?(region) &&
|
|
48
|
+
SNS_ACCOUNT.match?(account) && resource.length == 1 && SNS_RESOURCE.match?(resource.first)
|
|
49
|
+
next if valid
|
|
50
|
+
|
|
51
|
+
Kernel.raise RubyCdk::UsageError, "Notification arn #{arn.inspect} is not a valid arn for an SNS topic. Nothing was deployed.\n" \
|
|
52
|
+
"Run the command again with ARNs of the form arn:PARTITION:sns:REGION:ACCOUNT:TOPIC."
|
|
53
|
+
end
|
|
54
|
+
arns
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
# The rollback configuration as it was given, validated as the CDK
|
|
58
|
+
# validates it (util/cloudformation.ts, toCloudFormationRollbackConfiguration):
|
|
59
|
+
# at most five triggers, each a CloudWatch alarm ARN, and a monitoring
|
|
60
|
+
# time that is a whole number of minutes between 0 and 180. nil is not
|
|
61
|
+
# given: the request carries no RollbackConfiguration, and the triggers
|
|
62
|
+
# the stack has are left alone.
|
|
63
|
+
def rollback_configuration(config)
|
|
64
|
+
return nil if config.nil?
|
|
65
|
+
|
|
66
|
+
triggers = config.triggers || []
|
|
67
|
+
if triggers.length > MAX_ROLLBACK_TRIGGERS
|
|
68
|
+
Kernel.raise RubyCdk::UsageError, "a maximum of #{MAX_ROLLBACK_TRIGGERS} rollback triggers can be specified, " \
|
|
69
|
+
"got #{triggers.length}. Nothing was deployed.\n" \
|
|
70
|
+
"Run the command again with at most #{MAX_ROLLBACK_TRIGGERS} alarm ARNs."
|
|
71
|
+
end
|
|
72
|
+
|
|
73
|
+
triggers.each do |arn|
|
|
74
|
+
next if ALARM_ARN.match?(arn.to_s)
|
|
75
|
+
|
|
76
|
+
Kernel.raise RubyCdk::UsageError, "rollback trigger arn #{arn.inspect} is not a valid CloudWatch alarm arn. " \
|
|
77
|
+
"Nothing was deployed.\n" \
|
|
78
|
+
"Run the command again with ARNs of the form arn:PARTITION:cloudwatch:REGION:ACCOUNT:alarm/NAME."
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
monitoring = config.monitoring_time_in_minutes
|
|
82
|
+
if !monitoring.nil? && (!monitoring.is_a?(Integer) || monitoring.negative? || monitoring > MAX_MONITORING_TIME_IN_MINUTES)
|
|
83
|
+
Kernel.raise RubyCdk::UsageError, "monitoring time must be a whole number between 0 and #{MAX_MONITORING_TIME_IN_MINUTES} minutes, " \
|
|
84
|
+
"got #{monitoring.inspect}. Nothing was deployed.\n" \
|
|
85
|
+
"Run the command again with a whole number of minutes."
|
|
86
|
+
end
|
|
87
|
+
|
|
88
|
+
config
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
|
|
14
92
|
# What a deploy or a destroy may do.
|
|
15
93
|
class DeployOptions
|
|
16
94
|
attr_reader :parameters, :tags, :yes, :no_execute, :on_failure, :no_rollback, :outputs_file, :account, :force, :profile,
|
|
17
|
-
:hotswap, :hotswap_fallback, :use_previous_parameters
|
|
95
|
+
:hotswap, :hotswap_fallback, :use_previous_parameters, :notification_arns, :rollback_configuration, :import_existing_resources
|
|
18
96
|
|
|
19
97
|
# force deploys even when the template and the parameters are unchanged.
|
|
20
98
|
# profile is the name of the credential profile; it is shown on the target
|
|
@@ -32,8 +110,32 @@ module RubyCdk
|
|
|
32
110
|
# f7c9dca95f440a076c2abd61b66b984624b8548e); only the CDK's deployStack
|
|
33
111
|
# call itself documents false (deploy-stack.ts:170-177), and every
|
|
34
112
|
# caller passes the flag through.
|
|
113
|
+
#
|
|
114
|
+
# notification_arns is the CDK's --notification-arns: the SNS topics
|
|
115
|
+
# CloudFormation notifies, sent as NotificationARNs on the change set
|
|
116
|
+
# and on the direct update alike (deploy-stack.ts, commonPrepareOptions).
|
|
117
|
+
# nil is the flag not given: the field is not sent, and whatever the
|
|
118
|
+
# stack carries is left alone (cdk-toolkit.ts:2462-2466). What is given
|
|
119
|
+
# is validated as the CDK validates it (Checks).
|
|
120
|
+
#
|
|
121
|
+
# rollback_configuration is the CDK's rollback-configuration option
|
|
122
|
+
# (deployments.ts:74), built at its command line from
|
|
123
|
+
# --rollback-trigger-alarm-arns and --monitoring-time-minutes: the
|
|
124
|
+
# CloudWatch alarms CloudFormation monitors while the operation runs,
|
|
125
|
+
# and the minutes it keeps watching after. nil is the flags not given:
|
|
126
|
+
# the field is not sent, and the triggers the stack has are left alone
|
|
127
|
+
# (deploy-stack.ts:1012-1019). What is given is validated as the CDK
|
|
128
|
+
# validates it (Checks).
|
|
129
|
+
#
|
|
130
|
+
# import_existing_resources is the CDK's --import-existing-resources
|
|
131
|
+
# (parse-command-line-arguments.ts:558-562, default false): the change
|
|
132
|
+
# set imports resources that already exist instead of failing on them.
|
|
133
|
+
# It is a field of the CreateChangeSet only, never of the direct
|
|
134
|
+
# update, as in the CDK (deploy-stack.ts:640; its --method=direct
|
|
135
|
+
# refuses the flag, cli.ts:905-908).
|
|
35
136
|
def initialize(parameters: {}, tags: {}, yes: false, no_execute: false, on_failure: :delete, no_rollback: false, outputs_file: nil, account: nil,
|
|
36
|
-
force: false, profile: nil, hotswap: false, hotswap_fallback: false, use_previous_parameters: true
|
|
137
|
+
force: false, profile: nil, hotswap: false, hotswap_fallback: false, use_previous_parameters: true, notification_arns: nil,
|
|
138
|
+
rollback_configuration: nil, import_existing_resources: false)
|
|
37
139
|
@parameters = parameters
|
|
38
140
|
@tags = tags
|
|
39
141
|
@yes = yes
|
|
@@ -47,6 +149,9 @@ module RubyCdk
|
|
|
47
149
|
@hotswap = hotswap
|
|
48
150
|
@hotswap_fallback = hotswap_fallback
|
|
49
151
|
@use_previous_parameters = use_previous_parameters
|
|
152
|
+
@notification_arns = Checks.notification_arns(notification_arns)
|
|
153
|
+
@rollback_configuration = Checks.rollback_configuration(rollback_configuration)
|
|
154
|
+
@import_existing_resources = !!import_existing_resources
|
|
50
155
|
end
|
|
51
156
|
end
|
|
52
157
|
|
|
@@ -93,6 +198,10 @@ module RubyCdk
|
|
|
93
198
|
# left behind is removed before a new one takes its name.
|
|
94
199
|
CHANGE_SET_NAME = "ruby-cdk-deploy-change-set"
|
|
95
200
|
|
|
201
|
+
# The name of an import's change set, kept for the same reason a
|
|
202
|
+
# deploy's is.
|
|
203
|
+
IMPORT_CHANGE_SET_NAME = "ruby-cdk-import-change-set"
|
|
204
|
+
|
|
96
205
|
# CloudFormation takes at most this many bytes of template in a request.
|
|
97
206
|
INLINE_TEMPLATE_LIMIT = 51_200
|
|
98
207
|
|
|
@@ -272,7 +381,8 @@ module RubyCdk
|
|
|
272
381
|
template_url = place_template(stack, template)
|
|
273
382
|
begin
|
|
274
383
|
client.update_stack(name, template: template_url ? nil : template, template_url: template_url,
|
|
275
|
-
parameters: parameters.api, tags: tags, capabilities: capabilities
|
|
384
|
+
parameters: parameters.api, tags: tags, capabilities: capabilities, notification_arns: options.notification_arns,
|
|
385
|
+
rollback_configuration: options.rollback_configuration)
|
|
276
386
|
rescue RubyCdk::PreconditionError => e
|
|
277
387
|
raise unless /No updates are to be performed\./.match?(e.message)
|
|
278
388
|
|
|
@@ -380,8 +490,23 @@ module RubyCdk
|
|
|
380
490
|
change_set = change_set_with_rejection(name, nil, creating) do
|
|
381
491
|
client.create_change_set(name, CHANGE_SET_NAME, template: template_url ? nil : template,
|
|
382
492
|
change_set_type: creating ? :create : :update, parameters: parameters.api, tags: tags, capabilities: capabilities,
|
|
383
|
-
on_stack_failure: creating ? on_failure : nil, template_url: template_url
|
|
493
|
+
on_stack_failure: creating ? on_failure : nil, template_url: template_url, notification_arns: options.notification_arns,
|
|
494
|
+
rollback_configuration: options.rollback_configuration,
|
|
495
|
+
import_existing_resources: options.import_existing_resources)
|
|
384
496
|
end
|
|
497
|
+
|
|
498
|
+
run_change_set(name, change_set, creating, info, stack, capabilities, options, "deploy",
|
|
499
|
+
"Deploy these changes to stack #{name} in account #{identity.account} (#{identity.region})?",
|
|
500
|
+
before_approval: method(:refuse_replacement))
|
|
501
|
+
end
|
|
502
|
+
|
|
503
|
+
# From a created change set to the operation's end, the one walk the
|
|
504
|
+
# change-set deploy and an import share: wait for it to be created, the
|
|
505
|
+
# nothing-to-do answer, the rejected set, the change view, the approval,
|
|
506
|
+
# the execution, and the operation followed to a terminal status.
|
|
507
|
+
# before_approval, when given, stands between the change view and the
|
|
508
|
+
# question, where the deploy refuses --no-rollback over a replacement.
|
|
509
|
+
def run_change_set(name, change_set, creating, info, stack, capabilities, options, verb, question, before_approval: nil)
|
|
385
510
|
change_set = wait_for_change_set(name, change_set.id)
|
|
386
511
|
|
|
387
512
|
# The stack exists from here on, with an ID of its own. Later calls use
|
|
@@ -426,13 +551,14 @@ module RubyCdk
|
|
|
426
551
|
outputs: outputs_of(current))
|
|
427
552
|
end
|
|
428
553
|
|
|
429
|
-
|
|
554
|
+
before_approval&.call(name, changes, options, creating)
|
|
430
555
|
|
|
431
|
-
unless approved?(options,
|
|
432
|
-
|
|
556
|
+
unless approved?(options, question)
|
|
557
|
+
refused = verb == "deploy" ? "the changes to" : "the import into"
|
|
558
|
+
raise ApprovalRequired, "Approval for #{refused} stack #{name} was not given, so nothing was #{verb}. " \
|
|
433
559
|
"The change set was deleted#{creating ? ", with the new stack" : ""}." \
|
|
434
560
|
"#{discard(name, change_set.id, creating, creating && wanted_protection)}" \
|
|
435
|
-
"\nRun the command again in a terminal to approve it, or with --yes to
|
|
561
|
+
"\nRun the command again in a terminal to approve it, or with --yes to #{verb} without asking."
|
|
436
562
|
end
|
|
437
563
|
|
|
438
564
|
change_set_with_rejection(name, change_set.id, creating, was_protected: creating && wanted_protection) do
|
|
@@ -453,6 +579,76 @@ module RubyCdk
|
|
|
453
579
|
outcome: :failed, changes: changes, exit_code: 5)
|
|
454
580
|
end
|
|
455
581
|
|
|
582
|
+
# The import the CDK's `cdk import` performs (cdk-toolkit.ts:916-1061
|
|
583
|
+
# and api/resource-import/importer.ts at f7c9dca95f440a076c2abd61b66b984624b8548e):
|
|
584
|
+
# existing resources brought under the stack, through one change set
|
|
585
|
+
# whose type is IMPORT and whose ResourcesToImport name each one,
|
|
586
|
+
# executed and followed like a deploy's.
|
|
587
|
+
#
|
|
588
|
+
# resources is what the caller collected before this ran: an Array of
|
|
589
|
+
# { logical_resource_id:, resource_identifier: }, the identifier
|
|
590
|
+
# properties and values that name each existing physical resource --
|
|
591
|
+
# the CDK's resource map (importer.ts:93). The type of each one is read
|
|
592
|
+
# from the template, as the CDK reads it off the diff
|
|
593
|
+
# (importer.ts:451); a logical id the template does not hold is
|
|
594
|
+
# refused before any request.
|
|
595
|
+
def import(stack:, template:, resources:, identity:, options:, change_set_name: IMPORT_CHANGE_SET_NAME)
|
|
596
|
+
# The CLI passes its --change-set-name option through, nil included;
|
|
597
|
+
# the import holds its own default then, as deploy's does at its call
|
|
598
|
+
# site (cli/import.rb, 2026-12-01: the nil reached create_change_set,
|
|
599
|
+
# and the SDK refused the request for the missing name).
|
|
600
|
+
change_set_name ||= IMPORT_CHANGE_SET_NAME
|
|
601
|
+
name = stack.stack_name
|
|
602
|
+
check_account(name, stack, identity, options)
|
|
603
|
+
|
|
604
|
+
body = parse_template(template)
|
|
605
|
+
to_import = resources_to_import(resources, body, name)
|
|
606
|
+
tags = tags_of(stack.tags)
|
|
607
|
+
show_target(identity, options)
|
|
608
|
+
|
|
609
|
+
state, info = prepare_stack(name, client.stack_state(name))
|
|
610
|
+
current = info || client.stack_info(name)
|
|
611
|
+
target = current&.id || name
|
|
612
|
+
|
|
613
|
+
# The template of an IMPORT change set is the deployed one with the
|
|
614
|
+
# additions in it, never the synthesized one: CloudFormation rejects
|
|
615
|
+
# an import that touches the resources that are not being imported
|
|
616
|
+
# (importer.ts, currentTemplateWithAdditions, 306-324). Each
|
|
617
|
+
# addition carries DeletionPolicy Retain where it has none
|
|
618
|
+
# (importer.ts:522-531): what the import brings in must not be lost
|
|
619
|
+
# if the operation fails.
|
|
620
|
+
merged = merged_template(target, body, to_import)
|
|
621
|
+
merged_text = JSON.generate(merged)
|
|
622
|
+
ensure_template_placeable(merged_text)
|
|
623
|
+
capabilities = Capabilities.for(merged)
|
|
624
|
+
# The import carries no parameter values: the ones the stack was last
|
|
625
|
+
# deployed with go back as UsePreviousValue, the CDK's
|
|
626
|
+
# usePreviousParameters for its import (cdk-toolkit.ts:1017).
|
|
627
|
+
parameters = Parameters.new(merged, {}, deployed: info ? client.stack_parameters(target) : nil)
|
|
628
|
+
|
|
629
|
+
template_url = place_template(stack, merged_text)
|
|
630
|
+
output.announce("#{name}: importing resources into stack...")
|
|
631
|
+
unless state == :absent
|
|
632
|
+
client.delete_change_set(name, change_set_name)
|
|
633
|
+
wait_for_change_set_gone(name, change_set_name)
|
|
634
|
+
end
|
|
635
|
+
|
|
636
|
+
creating = %i[absent review].include?(state)
|
|
637
|
+
change_set = change_set_with_rejection(name, nil, creating) do
|
|
638
|
+
# OnStackFailure stays out: it is a creation's field, and the CDK
|
|
639
|
+
# sends none on its import either (deploy-stack.ts:633-645). A new
|
|
640
|
+
# stack the import fails on sits in ROLLBACK_COMPLETE, where the
|
|
641
|
+
# next deploy deletes it and starts again (section 9).
|
|
642
|
+
client.create_change_set(name, change_set_name, template: template_url ? nil : merged_text,
|
|
643
|
+
change_set_type: :import, parameters: parameters.api, tags: tags, capabilities: capabilities,
|
|
644
|
+
on_stack_failure: nil, template_url: template_url, notification_arns: options.notification_arns,
|
|
645
|
+
resources_to_import: to_import)
|
|
646
|
+
end
|
|
647
|
+
|
|
648
|
+
run_change_set(name, change_set, creating, info, stack, capabilities, options, "import",
|
|
649
|
+
"Import these resources into stack #{name} in account #{identity.account} (#{identity.region})?")
|
|
650
|
+
end
|
|
651
|
+
|
|
456
652
|
def destroy(stack:, identity:, options:)
|
|
457
653
|
name = stack.stack_name
|
|
458
654
|
# The account rule holds for a destroy as for a deploy: a stack with an
|
|
@@ -644,12 +840,47 @@ module RubyCdk
|
|
|
644
840
|
deployed = JSON.parse(client.get_template(info.id || name).to_s)
|
|
645
841
|
return false unless deployed == body
|
|
646
842
|
return false if parameters.changes?(deployed_parameters)
|
|
843
|
+
return false unless same_notification_arns?(info, options)
|
|
844
|
+
return false unless same_rollback_configuration?(info, options)
|
|
647
845
|
|
|
648
846
|
tag_pairs(client.stack_tags(info.id || name)) == tag_pairs(tags)
|
|
649
847
|
rescue JSON::ParserError, TypeError
|
|
650
848
|
false
|
|
651
849
|
end
|
|
652
850
|
|
|
851
|
+
# The stack's notification ARNs against the ones asked for, as sets, as
|
|
852
|
+
# the CDK compares them (deploy-stack.ts:992-994 at
|
|
853
|
+
# f7c9dca95f440a076c2abd61b66b984624b8548e, arrayEquals): a deploy
|
|
854
|
+
# without the flag against a stack that has ARNs is a difference, and
|
|
855
|
+
# the change set is created.
|
|
856
|
+
def same_notification_arns?(info, options)
|
|
857
|
+
deployed = info&.notification_arns || []
|
|
858
|
+
asked = options.notification_arns || []
|
|
859
|
+
deployed.all? { |arn| asked.include?(arn) } && asked.all? { |arn| deployed.include?(arn) }
|
|
860
|
+
end
|
|
861
|
+
|
|
862
|
+
# The deployed rollback configuration against the desired one, as the
|
|
863
|
+
# CDK compares them (deploy-stack.ts:1079-1089,
|
|
864
|
+
# rollbackConfigurationEquals): the triggers order-independently, by
|
|
865
|
+
# their ARN and their type, and a monitoring time not given a zero. A
|
|
866
|
+
# deploy that does not manage the configuration is never compared:
|
|
867
|
+
# nil, the option, leaves whatever the stack has alone
|
|
868
|
+
# (deploy-stack.ts:1012-1019).
|
|
869
|
+
def same_rollback_configuration?(info, options)
|
|
870
|
+
desired = options.rollback_configuration
|
|
871
|
+
return true if desired.nil?
|
|
872
|
+
|
|
873
|
+
deployed = info&.rollback_configuration
|
|
874
|
+
return false if (deployed&.monitoring_time_in_minutes || 0) != (desired.monitoring_time_in_minutes || 0)
|
|
875
|
+
|
|
876
|
+
deployed_triggers = deployed&.triggers || []
|
|
877
|
+
desired_triggers = desired.triggers || []
|
|
878
|
+
return false if deployed_triggers.length != desired_triggers.length
|
|
879
|
+
|
|
880
|
+
keys = deployed_triggers.map { |trigger| "#{trigger.arn}|#{trigger.type}" }
|
|
881
|
+
desired_triggers.all? { |arn| keys.include?("#{arn}|#{RollbackTrigger::ALARM}") }
|
|
882
|
+
end
|
|
883
|
+
|
|
653
884
|
# --- change sets
|
|
654
885
|
|
|
655
886
|
# CloudFormation can refuse a change set when the request is made, or when
|
|
@@ -766,6 +997,50 @@ module RubyCdk
|
|
|
766
997
|
"or with --no-execute to create the change set and read what replaces what first."
|
|
767
998
|
end
|
|
768
999
|
|
|
1000
|
+
# The request's ResourcesToImport from the resources the caller
|
|
1001
|
+
# collected: the type of each one read from the template, the CDK's
|
|
1002
|
+
# makeResourcesToImport (importer.ts:448-454). A logical id the
|
|
1003
|
+
# template does not hold is refused before any request: there is
|
|
1004
|
+
# nothing in the stack's definition to import it as.
|
|
1005
|
+
def resources_to_import(resources, body, name)
|
|
1006
|
+
declared = body["Resources"].is_a?(Hash) ? body["Resources"] : {}
|
|
1007
|
+
resources.map do |resource|
|
|
1008
|
+
logical_id = resource[:logical_resource_id] || resource["logical_resource_id"]
|
|
1009
|
+
identifier = resource[:resource_identifier] || resource["resource_identifier"]
|
|
1010
|
+
definition = declared[logical_id]
|
|
1011
|
+
unless definition
|
|
1012
|
+
raise ImportableMissing, "The template of stack #{name} has no resource #{logical_id.inspect}, so there is nothing to " \
|
|
1013
|
+
"import it as. Nothing was imported.\n" \
|
|
1014
|
+
"Run `ruby-cdk diff #{name}` to see what the app defines, then build the import from its resources."
|
|
1015
|
+
end
|
|
1016
|
+
|
|
1017
|
+
ResourceToImport.new(logical_resource_id: logical_id, resource_type: definition["Type"],
|
|
1018
|
+
resource_identifier: (identifier || {}).to_h { |key, value| [ key.to_s, value.to_s ] })
|
|
1019
|
+
end
|
|
1020
|
+
end
|
|
1021
|
+
|
|
1022
|
+
# The deployed template with the additions in it. A copy, read fresh:
|
|
1023
|
+
# neither the synthesized body this was built from nor the deployed
|
|
1024
|
+
# text CloudFormation holds is mutated by it, which is what the CDK's
|
|
1025
|
+
# structuredClone guards (its issue 1575).
|
|
1026
|
+
def merged_template(target, body, to_import)
|
|
1027
|
+
deployed = client.get_template(target)
|
|
1028
|
+
merged = deployed.nil? || deployed.to_s.strip.empty? ? {} : JSON.parse(deployed.to_s)
|
|
1029
|
+
unless merged.is_a?(Hash)
|
|
1030
|
+
raise InvalidTemplate, "The deployed template of stack #{target} is not a JSON object, so it cannot be the template of an import."
|
|
1031
|
+
end
|
|
1032
|
+
|
|
1033
|
+
merged["Resources"] = {} unless merged["Resources"].is_a?(Hash)
|
|
1034
|
+
to_import.each do |resource|
|
|
1035
|
+
definition = body["Resources"][resource.logical_resource_id]
|
|
1036
|
+
definition["DeletionPolicy"] ||= "Retain"
|
|
1037
|
+
merged["Resources"][resource.logical_resource_id] = definition
|
|
1038
|
+
end
|
|
1039
|
+
merged
|
|
1040
|
+
rescue JSON::ParserError, TypeError
|
|
1041
|
+
raise InvalidTemplate, "The deployed template of stack #{target} is not valid JSON, so it cannot be the template of an import."
|
|
1042
|
+
end
|
|
1043
|
+
|
|
769
1044
|
def reconcile_protection(target, current, wanted)
|
|
770
1045
|
# A wish that is not set counts as false, so a stack that stops asking
|
|
771
1046
|
# loses the protection on the next deploy. The CDK reconciles the same
|