-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: A few wrappers to work better with Terraform
- Loading branch information
JJ
committed
Oct 1, 2024
1 parent
ec0fad3
commit 5fc3311
Showing
1 changed file
with
67 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
set -o pipefail | ||
|
||
# | ||
# General utilities | ||
# | ||
utils::panic() { | ||
local -r message="${1}" | ||
local -r exit_code="${2}" | ||
|
||
[[ -n "${message}" ]] && echo -e "${message}" | ||
return "${exit_code}" | ||
} | ||
|
||
# | ||
# Configurations | ||
# | ||
DEFAULT_CLOUD_DEPLOYMENT_WORKSPACE="${HOME}/workspace/ibm/quantum/projects/infra/cloud-deployment" | ||
DEFAULT_PLAN_FILENAME="this.tfplan" | ||
|
||
CLOUD_DEPLOYMENT_WORKSPACE=${CLOUD_DEPLOYMENT_WORKSPACE:-${DEFAULT_CLOUD_DEPLOYMENT_WORKSPACE}} | ||
PLAN_FILENAME=${PLAN_FILENAME:-${DEFAULT_PLAN_FILENAME}} | ||
|
||
[[ -d "${CLOUD_DEPLOYMENT_WORKSPACE}" ]] || utils::panic "Warning: I couldn't find the ${CLOUD_DEPLOYMENT_WORKSPACE}" 1 | ||
[[ -x "${CLOUD_DEPLOYMENT_WORKSPACE}/tools/tfinit.sh" ]] || utils::panic "Warning: I couldn't find the ${CLOUD_DEPLOYMENT_WORKSPACE}/tfinit.sh script" 2 | ||
|
||
|
||
# | ||
# Wrappers | ||
# | ||
terraform::state::init() { | ||
${CLOUD_DEPLOYMENT_WORKSPACE}/tools/tfinit.sh | ||
} | ||
|
||
terraform::state::cleanup() { | ||
[[ -d ".terraform" ]] && rm -fr .terraform | ||
[[ -f "${PLAN_FILENAME}" ]] && rm -f "${PLAN_FILENAME}" | ||
} | ||
|
||
terraform::state::upgrade() { | ||
terraform::state::cleanup | ||
${CLOUD_DEPLOYMENT_WORKSPACE}/tools/tfinit.sh --upgrade --force | ||
} | ||
|
||
terraform::plan() { | ||
terraform plan --lock=false --out="${PLAN_FILENAME}" | ||
} | ||
|
||
terraform::apply() { | ||
[[ -f "${PLAN_FILENAME}" ]] && terraform apply "${PLAN_FILENAME}" | ||
} | ||
|
||
|
||
# autoloads | ||
autoload terraform::state::init | ||
autoload terraform::state::cleanup | ||
autoload terraform::state::upgrade | ||
autoload terraform::plan | ||
autoload terraform::apply | ||
|
||
|
||
# aliases | ||
alias tf='terraform' | ||
alias tfi='terraform::state::init' | ||
alias tfc='terraform::state::cleanup' | ||
alias tfu='terraform::state::upgrade' | ||
alias tfp='terraform::plan' | ||
alias tfa='terraform::apply' |