@@ -5,36 +5,54 @@ if [ "$#" -ne 1 ]; then
5
5
exit 1
6
6
fi
7
7
8
+ valid_resources=(
9
+ " aws_ecs_task_definition\.task_definition" # TODO: Remove after release
10
+ " aws_s3_object\.appspec_object"
11
+ " module\.web_service\.aws_ecs_task_definition"
12
+ " module\.good_job_service\.aws_ecs_task_definition"
13
+ )
14
+
8
15
tf_stdout=$1
9
- # Check task definition is replaced
10
16
if [[ $( grep -ce " No changes.*Your infrastructure matches the configuration" " $tf_stdout " ) -eq 1 ]]; then
11
17
echo " No changes detected, continuing."
12
18
exit 0
13
19
fi
14
- if [[ $( grep -cE " aws_ecs_task_definition\.task_definition.*(replaced|created)" " $tf_stdout " ) -eq 1 ]]; then
15
- echo " Task definition is being replaced or created"
16
- else
17
- echo " Task definition is not being replaced, aborting."
18
- exit 1
19
- fi
20
- if [[ $( grep -cE " aws_s3_object\.appspec_object.*(updated in-place|created)" " $tf_stdout " ) -eq 1 ]]; then
21
- echo " S3 bucket object is being replaced or created"
22
- else
23
- echo " S3 bucket object is not being replaced, aborting."
24
- exit 1
25
- fi
20
+
26
21
MODIFICATIONS=$( grep -E " [0-9]+ to add, [0-9]+ to change, [0-9]+ to destroy." " $tf_stdout " ) || exit 1
27
22
ADDITIONS=$( echo " $MODIFICATIONS " | sed -E ' s/.*([0-9]+) to add.*/\1/' ) || exit 1
28
- CHANGES=$( echo " $MODIFICATIONS " | sed -E ' s/.*([0-9]+) to change.*/\1/' ) || exit 1
29
23
DELETIONS=$( echo " $MODIFICATIONS " | sed -E ' s/.*([0-9]+) to destroy.*/\1/' ) || exit 1
30
24
if [[ $DELETIONS -gt $ADDITIONS ]]; then
31
- echo " More resources are being destroyed than created."
32
- echo " Other resources than task definition and s3 bucket object are being deleted, aborting."
25
+ echo " ERROR: More resources are being destroyed than created, run infrastructure deploy first."
33
26
exit 1
27
+ else
28
+ echo " CHECK_PASSED: No resources are being destroyed without replacement."
34
29
fi
35
- if [[ $(( CHANGES + ADDITIONS)) -gt 2 ]]; then
36
- echo " More than 2 resources are being changed."
37
- echo " Other changes than task definition and s3 bucket object are being made, aborting."
30
+
31
+ mapfile -t PLANNED_CHANGES < <( grep -E " #.+(replaced|created|updated in-place|destroyed)" " $tf_stdout " || exit 1)
32
+
33
+ invalid_modifications=()
34
+ for change in " ${PLANNED_CHANGES[@]} " ; do
35
+ valid=0
36
+ for resource in " ${valid_resources[@]} " ; do
37
+ if [[ " $change " =~ $resource ]]; then
38
+ valid=1
39
+ break
40
+ fi
41
+ done
42
+ if [ $valid -eq 0 ]; then
43
+ invalid_modifications+=(" $change " )
44
+ fi
45
+ done
46
+
47
+ if [ ! ${# invalid_modifications[@]} -eq 0 ]; then
48
+ echo " FAILED_CHECK: Invalid resources modified"
49
+ for item in " ${invalid_modifications[@]} " ; do
50
+ echo " $item "
51
+ done
52
+ echo " Please run an infrastructure deployment."
38
53
exit 1
54
+ else
55
+ echo " CHECK_PASSED: All modified resources are expected."
39
56
fi
40
- echo " Basic checks passed, only task definition and S3 bucket changes observed."
57
+
58
+ echo " Basic checks passed, if production please evaluate the plan before applying."
0 commit comments