Skip to content

Commit

Permalink
Gracefully shutdown the terraform process
Browse files Browse the repository at this point in the history
Signed-off-by: ialidzhikov <i.alidjikov@gmail.com>
  • Loading branch information
ialidzhikov committed Mar 16, 2020
1 parent 5854dcc commit bb85370
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 11 deletions.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,6 @@ COPY --from=builder /tmp/terraformer/terraform /bin/terraform
COPY --from=builder /tmp/terraformer/terraform-provider* /terraform-providers/

ADD ./terraform.sh /terraform.sh
ADD ./terraformer.sh /terraformer.sh

CMD exec /terraform.sh
CMD exec /terraformer.sh
5 changes: 2 additions & 3 deletions example/20-terraformer-validate-pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ spec:
image: eu.gcr.io/gardener-project/gardener/terraformer:latest
imagePullPolicy: IfNotPresent
command:
- sh
- -c
- sh /terraform.sh validate
- /terraformer.sh
- validate
env:
- name: TF_STATE_CONFIG_MAP_NAME
value: some-name.infra.tf-state
Expand Down
5 changes: 2 additions & 3 deletions example/30-terraformer-apply-pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ spec:
image: eu.gcr.io/gardener-project/gardener/terraformer:latest
imagePullPolicy: IfNotPresent
command:
- sh
- -c
- sh /terraform.sh apply 2>&1; [[ -f /success ]] && exit 0 || exit 1
- /terraformer.sh
- apply
env:
- name: TF_STATE_CONFIG_MAP_NAME
value: some-name.infra.tf-state
Expand Down
5 changes: 2 additions & 3 deletions example/40-terraformer-destroy-pod.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,8 @@ spec:
image: eu.gcr.io/gardener-project/gardener/terraformer:0.16.0
imagePullPolicy: IfNotPresent
command:
- sh
- -c
- sh /terraform.sh destroy 2>&1; [[ -f /success ]] && exit 0 || exit 1
- /terraformer.sh
- destroy
env:
- name: TF_STATE_CONFIG_MAP_NAME
value: some-name.infra.tf-state
Expand Down
15 changes: 14 additions & 1 deletion terraform.sh
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,14 @@ function end_execution() {
# Delete trap handler to avoid recursion
trap - HUP QUIT PIPE INT TERM EXIT

if [ -n "$TF_PID" ] && kill -0 "$TF_PID" &>/dev/null; then
echo "$(date) Sending SIGTERM to terraform process $TF_PID."
kill -SIGTERM $TF_PID
echo "$(date) Waiting for terraform process $TF_PID to complete..."
wait $TF_PID
echo "$(date) Terraform process $TF_PID completed."
fi

# check whether the terraform state has changed
if [[ ! -f "$PATH_STATE_OUT" ]] || diff "$PATH_STATE_IN" "$PATH_STATE_OUT" 1> /dev/null; then # passes (returns 0) if there is no diff
# indicate success (exit code gets lost as the surrounding command pipes this script through 'tee')
Expand Down Expand Up @@ -139,6 +147,9 @@ else
# Install trap handler for proper cleanup, summary and result code
trap "exit 100" HUP QUIT PIPE
trap end_execution INT TERM EXIT

TF_PID=

if [[ "$command" == "apply" ]]; then
terraform \
apply \
Expand All @@ -147,7 +158,9 @@ else
-state="$PATH_STATE_IN" \
-state-out="$PATH_STATE_OUT" \
-var-file="$PATH_VARIABLES" \
/tf
/tf &
TF_PID=$!
wait $TF_PID
exitcode=$?
elif [[ "$command" == "destroy" ]]; then
terraform \
Expand Down
44 changes: 44 additions & 0 deletions terraformer.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#!/bin/bash -u
#
# Copyright (c) 2020 SAP SE or an SAP affiliate company. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

function end_execution() {
# Delete trap handler to avoid recursion
trap - HUP QUIT PIPE INT TERM

if [ -n "$PID" ] && kill -0 "$PID" &>/dev/null; then
echo "$(date) Sending SIGTERM to terraform.sh process $PID."
kill -SIGTERM $PID
echo "$(date) Waiting for terraform.sh process $PID to complete..."
wait $PID
echo "$(date) terraform.sh process $PID completed."
fi
}

# determine command
command="${1:-apply}"

trap end_execution INT TERM

PID=

/terraform.sh "$command" 2>&1 &
PID=$!
wait $PID
exitcode=$?

echo "$(date) terraform.sh exited with $exitcode."

[[ -f /success ]] && exit 0 || exit 1

0 comments on commit bb85370

Please sign in to comment.