Skip to content

Instantly share code, notes, and snippets.

@ml4
Created November 19, 2026 14:30
Show Gist options
  • Select an option

  • Save ml4/9012db298c428cd3f4084ff64e58f81c to your computer and use it in GitHub Desktop.

Select an option

Save ml4/9012db298c428cd3f4084ff64e58f81c to your computer and use it in GitHub Desktop.
hc_hcpt_remote_run.sh
#!/usr/bin/env bash
#
## hc_hcpt_remote_run
## 2021-05-28 ml4
## Automate/wrap the terraform run in order to keep config DRY without using terragrunt
#
## Uses a temp directory and downloads every provider and module fresh to ensure cleanliness/standardisation. At cost of time.
#
###############################################################################################################################
readonly SCRIPT_NAME="$(basename "${0}")"
readonly TF_IN_AUTOMATION=true
. ~/.common-functions/commonFunctions.sh
function usage {
echo -e "Usage:\n\n"
echo -e "\thc-hcpt-remote-run.sh <action> <env> <other args>\n"
echo -e "where action is one of [plan,apply]"
echo -e "where env is one of [dev,int,uat,stg,prd]"
echo -e "<other args> where needed"
echo -e "The idea here is to keep the config DRY but simple and ahead of a orchestrated workflow"
exit 1
}
# # ## # # #
## ## # # # ## #
# ## # # # # # # #
# # ###### # # # #
# # # # # # ##
# # # # # # #
## main
#
function main {
# ***** Uncomment this when using in an actual orchestrator-based CLI-driven workflow
# ***** Also add -no-color to the tf commands
# if [[ -n ${TF_PLUGIN_CACHE_DIR} ]]
# then
# log "ERROR" "${FUNCNAME[0]}" "TF_PLUGIN_CACHE_DIR is set - unset this so that providers download properly in the tmpdir"
# exit 1
# fi
export TF_PLUGIN_CACHE_DIR=${HOME}/.terraform.plugin_cache_dir
action=${1}
shift
env=${1}
shift
args=${@}
if [[ ${env} != "dev" && ${env} != "int" && ${env} != "uat" && ${env} != "stg" && ${env} != "prd" ]]
then
env="dev"
fi
if [[ -z ${action} ]]
then
log "ERROR" "${FUNCNAME[0]}" "No action"
usage
fi
## check location
#
here=$(basename $PWD)
if [[ "${here}" != "config" ]]
then
log "WARN" "${FUNCNAME[0]}" "Not in config directory"
## try and find the config dir
## if !in config, either in gitroot, or a dev/int/prd subdir or err
#
if [[ -d './config' ]]
then
cd config
rCode=${?}
if [[ ${rCode} > 0 ]]
then
log "ERROR" "${FUNCNAME[0]}" "cd to ./config failed"
exit ${rCode}
else
log "INFO" "${FUNCNAME[0]}" "Formatting Terraform code"
terraform fmt -recursive ..
fi
elif [[ "${here}" == "dev" || "${here}" == "int" || "${here}" == "uat" || "${here}" == "stg" || "${here}" == "prd" ]]
then
cd ../config
rCode=${?}
if [[ ${rCode} > 0 ]]
then
log "ERROR" "${FUNCNAME[0]}" "cd to ../config from tfvars directory failed"
exit ${rCode}
fi
else
log "ERROR" "${FUNCNAME[0]}" "Run from either mod gitroot, config or env tfvars subdirs only."
exit 1
fi
fi
#
## AT THIS POINT, PWD SHOULD BE THE CONFIG DIRECTORY WHERE THE TERRAFORM FILES ARE
if [[ $(ls -la *.tf | wc -l | awk '{print $1}') == 0 ]]
then
log "ERROR" "${FUNCNAME[0]}" "No Terraform files to process"
exit 1
fi
## create separate temp dir for apt commands given that /tmp has noexec set on CIS benchmarked systems
#
mkdir -p ${HOME}/tmp
tmpdir=$(mktemp -d ${HOME}/tmp/tfrun-XXXX)
logfile="${tmpdir}/tf.log"
reponame=$(basename $(dirname $PWD))
log "INFO" "${FUNCNAME[0]}" "TERRAFORM: ${purple}$(terraform version | head -1 | awk '{print $NF}')${reset}"
log "INFO" "${FUNCNAME[0]}" "REPOSITORY: ${cyan}${reponame}${reset}"
workspace_name=$(grep name terraform.tf | grep -v hostname | awk -F'"' '{print $2}')
if [[ -z "${workspace_name}" ]]
then
workspace_name="WARNING: NEED TO MOVE MAIN.TF TO TERRAFORM.TF FOR THIS WORKSPACE"
fi
log "INFO" "${FUNCNAME[0]}" "WORKSPACE: ${yellow}${workspace_name}${reset}"
log "INFO" "${FUNCNAME[0]}" "TMPDIR: ${blue}${tmpdir}${reset}"
if [[ "${env}" == "dev" ]]
then
log "INFO" "${FUNCNAME[0]}" "ENVIRONMENT: ${green}DEVELOPMENT${reset}"
elif [[ "${env}" == "int" ]]
then
log "INFO" "${FUNCNAME[0]}" "ENVIRONMENT: ${yellow}INTEGRATION${reset}"
elif [[ "${env}" == "uat" ]]
then
log "INFO" "${FUNCNAME[0]}" "ENVIRONMENT: ${yellow}UAT${reset}"
elif [[ "${env}" == "stg" ]]
then
log "INFO" "${FUNCNAME[0]}" "ENVIRONMENT: ${red}STAGING${reset}"
elif [[ "${env}" == "prd" ]]
then
log "INFO" "${FUNCNAME[0]}" "ENVIRONMENT: ${red}PRODUCTION${reset}"
fi
if [[ "${action}" == "plan" ]]
then
log "INFO" "${FUNCNAME[0]}" "ACTION: ${blue}PLAN${reset}"
elif [[ "${action}" == "apply" ]]
then
log "INFO" "${FUNCNAME[0]}" "ACTION: ${purple}APPLY${reset}"
elif [[ "${action}" == "destroy" ]]
then
log "INFO" "${FUNCNAME[0]}" "ACTION: ${red}DESTROY${reset}"
elif [[ "${action}" == "output" ]]
then
log "INFO" "${FUNCNAME[0]}" "ACTION: ${cyan}OUTPUT${reset}"
elif [[ "${action}" == "refresh" ]]
then
log "INFO" "${FUNCNAME[0]}" "ACTION: ${blue}REFRESH${reset}"
elif [[ "${action}" == "stacks" ]]
then
log "INFO" "${FUNCNAME[0]}" "ACTION: ${white}STACKS${reset}"
fi
if [[ -f ".terraform.lock.hcl" ]]
then
lock=".terraform.lock.hcl"
fi
cp -a ${lock} * ${tmpdir}
rCode=${?}
if [[ ${rCode} > 0 ]]
then
log "ERROR" "Return status ${rCode} for command: cp -a ${lock} * ${tmpdir}"
exit ${rCode}
fi
if [[ -d "../${env}" && -r "../${env}/terraform.auto.tfvars" ]]
then
log "INFO" "${FUNCNAME[0]}" "Using ../${env}/terraform.auto.tfvars"
cp "../${env}/terraform.auto.tfvars" "${tmpdir}"
rCode=${?}
if [[ ${rCode} > 0 ]]
then
log "ERROR" "${FUNCNAME[0]}" "Return status ${rCode} for command cp "../${env}/terraform.auto.tfvars" "${tmpdir}""
exit ${rCode}
fi
draw_line purple
log "INFO" "${FUNCNAME[0]}" "Start up complete."
fi
pushd "${tmpdir}" >/dev/null
rCode=${?}
if [[ ${rCode} > 0 ]]
then
log "ERROR" "${FUNCNAME[0]}" "Pushd to tmpdir ${tmpdir} failed"
exit ${rCode}
fi
log "INFO" "${FUNCNAME[0]}" "Running terraform init -upgrade -input=false"
terraform init -upgrade -input=false
rCode=${?}
if [[ ${rCode} > 0 ]]
then
log "ERROR" "${FUNCNAME[0]}" "Return status ${rCode} for command terraform init -upgrade"
popd >/dev/null
rm -rf ${tmpdir}
playOracOff
exit ${rCode}
fi
if [[ "${action}" == "plan" ]]
then
args="-input=false -detailed-exitcode ${args[*]}"
elif [[ "${action}" == "apply" ]]
then
args="-auto-approve ${args[*]}"
elif [[ "${action}" == "output" ]]
then
args="-json ${args[*]}"
fi
log "INFO" "${FUNCNAME[0]}" "Running terraform ${action} ${args}"
terraform ${action} ${args} | tee ${logfile}
rCode=${PIPESTATUS[0]}
if [[ "${action}" == "plan" ]]
then
if [[ ${rCode} == 1 ]]
then
log "ERROR" "${FUNCNAME[0]}" "Return status == 1 (${red}Error${reset}) for command terraform ${action} ${args}"
popd >/dev/null
#rm -rf ${tmpdir}
playOracOff
exit ${rCode}
elif [[ ${rCode} == 2 ]]
then
log "INFO" "${FUNCNAME[0]}" "Return status == 2 (${yellow}OK - CHANGE WOULD BE MADE${reset}) for command terraform ${action} ${args}"
popd >/dev/null
elif [[ ${rCode} == 0 ]]
then
log "INFO" "${FUNCNAME[0]}" "Return status == 0 (${yellow}OK - NO CHANGES WOULD BE MADE${reset}) for command terraform ${action} ${args}"
popd >/dev/null
fi
fi
## using detailed-exitcode
#
if [[ "${action}" == "apply" ]]
then
if [[ ${rCode} == 1 ]]
then
log "ERROR" "${FUNCNAME[0]}" "Return status == 1 (${red}Error${reset}) for command terraform ${action} ${args}"
popd >/dev/null
#rm -rf ${tmpdir}
playOracOff
exit ${rCode}
elif [[ ${rCode} == 0 ]]
then
log "INFO" "${FUNCNAME[0]}" "Return status == 0 (${green}OK${reset}) for command terraform ${action} ${args}"
popd >/dev/null
fi
fi
if [[ -n $(grep Warning ${logfile}) ]]
then
draw_line yellow
log "WARN" "${FUNCNAME[0]}" "terraform ${action} ${args} resulted in warning(s)."
rCode=2
fi
# log "INFO" "${FUNCNAME[0]}" "Removing tmpdir ${tmpdir}"
# rm -rf ${tmpdir}
# rCode=${?}
# if [[ ${rCode} > 0 ]]
# then
# log "ERROR" "${FUNCNAME[0]}" "Return status ${rCode} for command rm -rf ${tmpdir}"
# playOracOff
# exit ${rCode}
# fi
draw_line purple
playZen
exit ${rCode} # should be the tf command rCode
}
main "$@"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment