2022-07-11 21:59:11 +00:00
|
|
|
TERRAFORM_BIN=$(which-command terraform | head -1)
|
|
|
|
if [[ -x $TERRAFORM_BIN ]]; then
|
|
|
|
function tfcontext() {
|
|
|
|
[[ -n $(find $PWD -name '*.tf') ]] || return -1
|
|
|
|
[[ -d _logs ]] || mkdir _logs
|
|
|
|
TF_VAR_FILE_NAME=${TF_VAR_FILE_CLI-:$(basename $PWD).tfvars}
|
|
|
|
[[ -r $TF_VAR_FILE_NAME ]] || unset TF_VAR_FILE_NAME
|
|
|
|
TF_VAR_FILE_CLI=${TF_VAR_FILE_CLI-:-var-file='$TF_VAR_FILE_NAME'}
|
|
|
|
|
|
|
|
basename $PWD
|
|
|
|
}
|
|
|
|
|
2022-08-02 11:27:25 +00:00
|
|
|
function _tf_sedFullStop() {
|
2022-07-11 21:59:11 +00:00
|
|
|
sed --unbuffered '/^===FULLSTOP===$/q' | sed --unbuffered '/^===FULLSTOP===$/d'
|
|
|
|
}
|
|
|
|
|
2022-08-02 11:27:25 +00:00
|
|
|
function less-tf() {
|
2022-07-25 15:21:39 +00:00
|
|
|
local _TF_LOG_FILE=${1-_logs/0_0_lastrun.log}
|
|
|
|
[[ "$1" == "-" ]] && _TF_LOG_FILE='_logs/0_0_lastrun.log'
|
2022-08-02 11:27:25 +00:00
|
|
|
[[ -z "$SZ_DEBUG" ]] || echo "lessts: ${@} | LESS_NO_WAIT = '${LESS_NO_WAIT}'"
|
2022-07-25 15:21:39 +00:00
|
|
|
[[ "$1" == "-" ]] && {
|
|
|
|
[[ -z "$SZ_DEBUG" ]] || echo "tailing..."
|
|
|
|
trap : INT;
|
2022-08-02 11:27:25 +00:00
|
|
|
tail -f $_TF_LOG_FILE | _tf_sedFullStop;
|
2022-07-25 15:21:39 +00:00
|
|
|
} \
|
|
|
|
|| { [[ -z "${LESS_NO_WAIT}" ]] \
|
|
|
|
&& {
|
|
|
|
[[ -z "$SZ_DEBUG" ]] || echo "Invoking less..."
|
|
|
|
less \
|
|
|
|
--no-init \
|
|
|
|
--raw-control-chars \
|
|
|
|
--line-numbers \
|
|
|
|
--quiet \
|
|
|
|
--hilite-unread \
|
|
|
|
--incsearch \
|
|
|
|
--ignore-case \
|
|
|
|
--force \
|
|
|
|
"${@:2}" -- \
|
|
|
|
"$_TF_LOG_FILE"
|
|
|
|
#\
|
|
|
|
#'+G?([^\w\W][\[0-9m+])Plan:' \
|
|
|
|
#'+3k'
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-02 11:27:25 +00:00
|
|
|
alias tf!=_tfSetLogTS
|
2022-07-25 15:21:39 +00:00
|
|
|
function _tfSetLogTS() {
|
|
|
|
export TF_LOG_TS=$(date -d "today" +"%Y%m%d%H%M%S")
|
|
|
|
}
|
|
|
|
|
2022-07-11 21:59:11 +00:00
|
|
|
function tf() {
|
|
|
|
[[ -z "$TF_LOG_TS" ]] && _tfSetLogTS
|
|
|
|
local NAME=$1
|
|
|
|
[[ "${@}" =~ "-destroy" ]] && NAME="$1-destroy"
|
|
|
|
|
|
|
|
echo "===_logs/0_$NAME.log===" > _logs/0_0_lastrun.log
|
|
|
|
[[ -z "$SZ_DEBUG" ]] || echo "Executing: terraform ${@}"
|
|
|
|
{
|
|
|
|
terraform "${@}" 2>&1 \
|
|
|
|
| tee _logs/${TF_LOG_TS}_$NAME.log \
|
|
|
|
| awk 'BEGIN {p=1}; /<<\W*EOT/ {print; p=0}; /^\W*EOT/ {p=1}; p; fflush();' \
|
|
|
|
| tee --append _logs/0_0_lastrun.log \
|
|
|
|
>> _logs/0_$NAME.log
|
|
|
|
|
|
|
|
echo "===FULLSTOP===" >> _logs/0_0_lastrun.log
|
|
|
|
} &
|
|
|
|
|
2022-08-02 11:27:25 +00:00
|
|
|
less-tf -
|
2022-07-11 21:59:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 15:21:39 +00:00
|
|
|
function tf-plan() {
|
2022-07-11 21:59:11 +00:00
|
|
|
[[ -z "$SZ_DEBUG" ]] || echo "DEBUG: tfplan ${@}"
|
|
|
|
local _TF_TARGET=""
|
|
|
|
[[ ${#TF_TARGET} -gt 0 ]] && _TF_TARGET="$(printf -- '--target=%s ' ${TF_TARGET[@]})"
|
|
|
|
[[ "${@}" =~ "-destroy" ]] && [[ ${#TF_DESTROY_TARGET} -gt 0 ]] && _TF_TARGET="$(printf -- '--target=%s ' ${TF_DESTROY_TARGET[@]})"
|
|
|
|
|
|
|
|
tf plan ${_TF_TARGET} "${@}"
|
|
|
|
}
|
|
|
|
|
2022-07-25 15:21:39 +00:00
|
|
|
function get-tf-env() {
|
|
|
|
local PATTERN="${1-}"
|
|
|
|
compgen -v | grep "^TF_${PATTERN}"
|
2022-07-11 21:59:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 15:21:39 +00:00
|
|
|
function clear-tf-env() {
|
|
|
|
local PATTERN="${1}"
|
|
|
|
local TOCLEAR=$(get-tf-env "${PATTERN}")
|
|
|
|
[[ -z "$TOCLEAR" ]] && {
|
|
|
|
echo "Could not find environment variables matching: $(echo "^TF_${PATTERN}")"
|
|
|
|
} || {
|
|
|
|
unset $TOCLEAR
|
2022-08-05 14:06:12 +00:00
|
|
|
echo "Cleared the following vars: $(echo "$TOCLEAR" | xargs echo)"
|
2022-07-25 15:21:39 +00:00
|
|
|
}
|
2022-07-11 21:59:11 +00:00
|
|
|
}
|
|
|
|
|
2022-08-05 14:06:12 +00:00
|
|
|
function get-tf-env-plan() {
|
2022-08-09 16:08:50 +00:00
|
|
|
set | grep '^TF\(_\(VAR_[a-z]\)\|\([A-Z_]*TARGET=\)\)' | grep '\b='
|
2022-08-05 14:06:12 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 15:21:39 +00:00
|
|
|
function clear-tf-env-vars() {
|
|
|
|
clear-tf-env 'VAR_[a-z]'
|
2022-07-11 21:59:11 +00:00
|
|
|
}
|
|
|
|
|
2022-07-25 15:21:39 +00:00
|
|
|
function clear-tf-env-targets() {
|
|
|
|
clear-tf-env '[A-Z_]*TARGET$'
|
2022-07-11 21:59:11 +00:00
|
|
|
}
|
2022-07-25 15:21:39 +00:00
|
|
|
|
|
|
|
alias tf-plan-destroy='tf-plan --destroy '
|
|
|
|
alias tf-apply='tf apply '
|
|
|
|
|
|
|
|
alias tfp='tf-plan '
|
|
|
|
alias tfpd='tf-plan-destroy '
|
|
|
|
alias tfa='tf-apply '
|
|
|
|
|
2022-07-11 21:59:11 +00:00
|
|
|
fi
|
|
|
|
|