From fe57fc3aa352512680683b1d998bf9506ceb4daa Mon Sep 17 00:00:00 2001 From: cytopia Date: Sat, 6 Jul 2019 15:51:20 +0200 Subject: [PATCH 1/2] Be 100% posix and remove bash as a dependency to shrink size --- Dockerfile | 3 +- data/fmt.sh | 70 ++++++++++++++++++++++++++++++++ data/terragrunt-fmt.sh | 91 ++++++++++-------------------------------- 3 files changed, 91 insertions(+), 73 deletions(-) create mode 100755 data/fmt.sh diff --git a/Dockerfile b/Dockerfile index 235c3e9..4ebfa4b 100644 --- a/Dockerfile +++ b/Dockerfile @@ -37,10 +37,9 @@ FROM alpine:3.9 LABEL \ maintainer="cytopia " \ repo="https://github.com/cytopia/docker-terragrunt-fmt" -RUN set -eux \ - && apk add --no-cache bash COPY --from=builder /usr/bin/terraform /usr/bin/terraform COPY data/terragrunt-fmt.sh /terragrunt-fmt.sh +COPY data/fmt.sh /fmt.sh WORKDIR /data ENTRYPOINT ["/terragrunt-fmt.sh"] diff --git a/data/fmt.sh b/data/fmt.sh new file mode 100755 index 0000000..f0fab61 --- /dev/null +++ b/data/fmt.sh @@ -0,0 +1,70 @@ +#!/bin/sh + +# Be strict +set -e +set -u + +### +### Inputs +### +_list="${1}" +_write="${2}" +_diff="${3}" +_check="${4}" +_file="${5}" +# shellcheck disable=SC2155 +_temp="/tmp/$(basename "${_file}").tf" +_ret=0 + + +### +### Build command (only append if default values are overwritten) +### +_cmd="terraform fmt" +if [ "${_list}" = "0" ]; then + _cmd="${_cmd} -list=false" +else + _cmd="${_cmd} -list=true" +fi +if [ "${_write}" = "1" ]; then + _cmd="${_cmd} -write=true" +else + _cmd="${_cmd} -write=false" +fi +if [ "${_diff}" = "1" ]; then + _cmd="${_cmd} -diff" +fi +if [ "${_check}" = "1" ]; then + _cmd="${_cmd} -check" +fi + +### +### Output and execute command +### +echo "${_cmd} ${_file}" +cp -f "${_file}" "${_temp}" +if ! eval "${_cmd} ${_temp}"; then + _ret=1 +fi + +### +### If -write was specified, copy file back +### +if [ "${_write}" = "1" ]; then + # Get owner and permissions of current file + _UID="$(stat -c %u "${_file}")" + _GID="$(stat -c %g "${_file}")" + _PERM="$(stat -c %a "${_file}")" + + # Adjust permissions of temporary file + chown ${_UID}:${_GID} "${_temp}" + chmod ${_PERM} "${_temp}" + + # Overwrite existing file + mv -f "${_temp}" "${_file}" +fi + +### +### Exit +### +exit "${_ret}" diff --git a/data/terragrunt-fmt.sh b/data/terragrunt-fmt.sh index 7d5f174..ac4d8b1 100755 --- a/data/terragrunt-fmt.sh +++ b/data/terragrunt-fmt.sh @@ -1,9 +1,8 @@ -#!/usr/bin/env bash +#!/bin/sh # Be strict set -e set -u -set -o pipefail # -------------------------------------------------------------------------------- @@ -55,60 +54,6 @@ print_usage() { } -_fmt() { - local list="${1}" - local write="${2}" - local diff="${3}" - local check="${4}" - local file="${5}" - # shellcheck disable=SC2155 - local temp="/tmp/$(basename "${file}").tf" - local ret=0 - - # Build command (only append if default values are overwritten) - local cmd="terraform fmt" - if [ "${list}" = "0" ]; then - cmd="${cmd} -list=false" - else - cmd="${cmd} -list=true" - fi - if [ "${write}" = "1" ]; then - cmd="${cmd} -write=true" - else - cmd="${cmd} -write=false" - fi - if [ "${diff}" = "1" ]; then - cmd="${cmd} -diff" - fi - if [ "${check}" = "1" ]; then - cmd="${cmd} -check" - fi - - # Output and execute command - echo "${cmd} ${file}" - cp -f "${file}" "${temp}" - if ! eval "${cmd} ${temp}"; then - ret=$(( ret + 1 )) - fi - - # If -write was specified, copy file back - if [ "${write}" = "1" ]; then - # Get owner and permissions of current file - _UID="$(stat -c %u "${file}")" - _GID="$(stat -c %g "${file}")" - _PERM="$(stat -c %a "${file}")" - - # Adjust permissions of temporary file - chown ${_UID}:${_GID} "${temp}" - chmod ${_PERM} "${temp}" - - # Overwrite existing file - mv -f "${temp}" "${file}" - fi - return "${ret}" -} - - # -------------------------------------------------------------------------------- # ENTRYPOINT (EVALUATE ARGUMENTS) # -------------------------------------------------------------------------------- @@ -301,7 +246,7 @@ fi ### (1/3) Single file ### if [ -f "${ARG_PATH}" ]; then - _fmt "${ARG_LIST}" "${ARG_WRITE}" "${ARG_DIFF}" "${ARG_CHECK}" "${ARG_PATH}" + /fmt.sh "${ARG_LIST}" "${ARG_WRITE}" "${ARG_DIFF}" "${ARG_CHECK}" "${ARG_PATH}" exit "${?}" else ### @@ -311,33 +256,37 @@ else # evaluate ignore paths if [ -n "${ARG_IGNORE}" ]; then - _EXCLUDE=" -not \( -path \"${ARG_PATH}/${ARG_IGNORE//,/*\" -o -path \"${ARG_PATH}\/}*\" \)" + _EXCLUDE=" -not \( -path \"${ARG_PATH}/$( echo "${ARG_IGNORE}" | sed 's/,/*/g' )\" -o -path \"${ARG_PATH}\/}*\" \)" else _EXCLUDE="" fi - find_cmd="find ${ARG_PATH}${_EXCLUDE} -name '*.hcl' -type f -print0" + # Store exit code + echo "0" > "/tmp/exit.txt" + + find_cmd="find ${ARG_PATH}${_EXCLUDE} -name '*.hcl' -type f" echo "[INFO] Finding files: ${find_cmd}" - ret=0 - while IFS= read -rd '' file; do - if ! _fmt "${ARG_LIST}" "${ARG_WRITE}" "${ARG_DIFF}" "${ARG_CHECK}" "${file}"; then - ret="1" - fi - done < <(eval "${find_cmd}") - exit "${ret}" + eval "${find_cmd} -print0 | xargs -n1 -0 sh -c '\ + if [ -f \"\${1}\" ]; then \ + if ! /fmt.sh \"${ARG_LIST}\" \"${ARG_WRITE}\" \"${ARG_DIFF}\" \"${ARG_CHECK}\" \"\${1}\"; then \ + echo 1 > /tmp/exit.txt; \ + fi \ + fi' --" + + # Read exit code and return it + exit "$( cat /tmp/exit.txt )" ### ### (3/3) Current directory only ### else - find_cmd="ls ${ARG_PATH}/*.hcl" - echo "[INFO] Finding files: ${find_cmd}" + echo "[INFO] Finding files: for file in *.hcl; do" ret=0 - while IFS= read -r file; do - if ! _fmt "${ARG_LIST}" "${ARG_WRITE}" "${ARG_DIFF}" "${ARG_CHECK}" "${file}"; then + for file in *.hcl; do + if ! /fmt.sh "${ARG_LIST}" "${ARG_WRITE}" "${ARG_DIFF}" "${ARG_CHECK}" "${file}"; then ret="1" fi - done < <(eval "${find_cmd}" 2>/dev/null) + done exit "${ret}" fi fi From 89bd6e4602e87c68819457e4ca6c265522a15131 Mon Sep 17 00:00:00 2001 From: cytopia Date: Sat, 6 Jul 2019 15:58:47 +0200 Subject: [PATCH 2/2] Use alpine 3.8 for smaller image size --- Dockerfile | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Dockerfile b/Dockerfile index 4ebfa4b..be2b9d8 100644 --- a/Dockerfile +++ b/Dockerfile @@ -33,7 +33,7 @@ RUN set -eux \ && chmod +x /usr/bin/terraform # Use a clean tiny image to store artifacts in -FROM alpine:3.9 +FROM alpine:3.8 LABEL \ maintainer="cytopia " \ repo="https://github.com/cytopia/docker-terragrunt-fmt"