Skip to content
This repository was archived by the owner on Jul 15, 2025. It is now read-only.

Commit 5297ce2

Browse files
authored
entrypoint: hook scripts support (#12)
* ✨ entrypoint: hook scripts support * 🔖 release 0.5.0 * fix lock for dry-run/non-dry-run
1 parent 7c89a19 commit 5297ce2

File tree

3 files changed

+63
-17
lines changed

3 files changed

+63
-17
lines changed

CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# ChangeLog
22

3+
## 0.5.0
4+
5+
- hook scripts support
6+
7+
## 0.4.0
8+
9+
- CDKTF 0.20.11
10+
311
## 0.3.0
412

513
- CDKTF 0.20.9

Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11

22
FROM registry.access.redhat.com/ubi9/ubi-minimal:9.5-1733767867 AS prod
33

4-
LABEL konflux.additional-tags="cdktf-0.20.9-tf-1.6.6-py-3.11-v0.4.0"
4+
LABEL konflux.additional-tags="cdktf-0.20.11-tf-1.6.6-py-3.11-v0.5.0"
55

66
USER 0
77

entrypoint.sh

Lines changed: 54 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,46 @@ export CDKTF_LOG_LEVEL=${CDKTF_LOG_LEVEL:-"warn"}
3434
OUTPUT_FILE=${OUTPUT_FILE:-"/work/output.json"}
3535
CDKTF_OUT_DIR="$ER_OUTDIR/stacks/CDKTF"
3636
TERRAFORM_CMD="terraform -chdir=$CDKTF_OUT_DIR"
37+
LOCK="-lock=true"
38+
if [[ $DRY_RUN == "True" ]]; then
39+
LOCK="-lock=false"
40+
fi
41+
42+
function run_hook() {
43+
local HOOK_NAME="$1"
44+
shift
45+
local HOOK_DIR="./hooks"
46+
local HOOK_SCRIPT=""
47+
48+
# Possible extensions for the hook scripts
49+
local EXTENSIONS=("sh" "py")
50+
51+
if [ ! -d "$HOOK_DIR" ]; then
52+
# no hook directory
53+
return 0
54+
fi
55+
56+
# Search for a valid hook script
57+
for EXT in "${EXTENSIONS[@]}"; do
58+
if [ -x "${HOOK_DIR}/${HOOK_NAME}.${EXT}" ]; then
59+
HOOK_SCRIPT="${HOOK_DIR}/${HOOK_NAME}.${EXT}"
60+
break
61+
fi
62+
done
63+
64+
if [ -z "$HOOK_SCRIPT" ]; then
65+
# no hook script
66+
return 0
67+
fi
68+
69+
# Export variables for hooks
70+
export DRY_RUN
71+
72+
echo "Running hook: $HOOK_NAME"
73+
"$HOOK_SCRIPT" "$@"
74+
}
75+
76+
run_hook "pre_hook"
3777

3878
# CDKTF init forces the provider re-download to calculate
3979
# Other platform provider SHAs. USing terraform to init the configuration avoids it
@@ -43,28 +83,26 @@ cdktf synth --output "$ER_OUTDIR"
4383
$TERRAFORM_CMD init
4484

4585
if [[ $ACTION == "Apply" ]]; then
46-
if [[ $DRY_RUN == "True" ]]; then
47-
$TERRAFORM_CMD plan -out=plan --lock=false
48-
if [ -f "validate_plan.py" ]; then
49-
$TERRAFORM_CMD show -json "$CDKTF_OUT_DIR"/plan > "$CDKTF_OUT_DIR"/plan.json
50-
python3 validate_plan.py "$CDKTF_OUT_DIR"/plan.json
51-
fi
52-
elif [[ $DRY_RUN == "False" ]]; then
86+
$TERRAFORM_CMD plan -out=plan "$LOCK"
87+
$TERRAFORM_CMD show -json "$CDKTF_OUT_DIR"/plan > "$CDKTF_OUT_DIR"/plan.json
88+
run_hook "validate_plan" "$CDKTF_OUT_DIR"/plan.json
89+
90+
if [[ $DRY_RUN == "False" ]]; then
5391
# cdktf apply isn't reliable for now, using terraform apply instead
54-
$TERRAFORM_CMD apply -auto-approve
92+
$TERRAFORM_CMD apply -auto-approve "$CDKTF_OUT_DIR"/plan
5593
$TERRAFORM_CMD output -json > "$OUTPUT_FILE"
56-
if [ -f "post_checks.py" ]; then
57-
python3 post_checks.py "$OUTPUT_FILE"
58-
fi
94+
run_hook "check_output" "$OUTPUT_FILE"
5995
fi
96+
run_hook "post_apply" "$CDKTF_OUT_DIR"/plan.json
97+
6098
elif [[ $ACTION == "Destroy" ]]; then
6199
if [[ $DRY_RUN == "True" ]]; then
62-
$TERRAFORM_CMD plan -out=plan -destroy --lock=false
63-
if [ -f "validate_plan.py" ]; then
64-
$TERRAFORM_CMD show -json "$CDKTF_OUT_DIR"/plan > "$CDKTF_OUT_DIR"/plan.json
65-
python3 validate_plan.py "$CDKTF_OUT_DIR"/plan.json
66-
fi
100+
$TERRAFORM_CMD plan -out=plan -destroy "$LOCK"
101+
$TERRAFORM_CMD show -json "$CDKTF_OUT_DIR"/plan > "$CDKTF_OUT_DIR"/plan.json
102+
run_hook "validate_plan" "$CDKTF_OUT_DIR"/plan.json
103+
67104
elif [[ $DRY_RUN == "False" ]]; then
68105
$TERRAFORM_CMD destroy --auto-approve
69106
fi
107+
run_hook "post_delete"
70108
fi

0 commit comments

Comments
 (0)