harbor run -t openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-5886harbor download openthoughts/tasktrove-exp-rpt-stack-bash-withtests-v2-stack-bash-withtests-5886You need to implement a script in Bash that initializes and applies Terraform configurations based on specified parameters. The script should check for the existence of configuration files, manage backend configurations, handle workspace settings, and process variable inputs correctly.
CLI Configuration:
TF_PARAM_CLI_CONFIG_FILE is set. If it is, verify that the specified file exists. If not, output an error message and exit.Module Path Setup:
TF_PARAM_PATH parameter and store it as module_path. Verify that this directory exists. If it does not, output an error message and exit.Backend Configuration:
TF_PARAM_BACKEND_CONFIG_FILE is provided, ensure each specified backend config file exists in the module_path. Construct backend initialization arguments.TF_PARAM_BACKEND_CONFIG is provided, incorporate these values into the backend initialization arguments.Terraform Initialization:
module_path, supplying the constructed backend arguments.Workspace Management:
TF_PARAM_WORKSPACE and default to TF_WORKSPACE if set. If a workspace parameter is set, provision the local workspace using the Terraform CLI.Variable Handling:
TF_PARAM_VAR and TF_PARAM_VAR_FILE are provided. Validate that any specified variable files exist in the module_path and construct arguments accordingly for Terraform.Terraform Apply:
-destroy, -input=false, and -auto-approve flags, along with any planned arguments.TF_PARAM_CLI_CONFIG_FILE: Path to the CLI configuration file.TF_PARAM_PATH: Path to the module.TF_PARAM_BACKEND_CONFIG_FILE: Comma-separated list of backend config files.TF_PARAM_BACKEND_CONFIG: Comma-separated list of backend config values.TF_PARAM_WORKSPACE: Name of the workspace.TF_PARAM_VAR: Comma-separated list of variables.TF_PARAM_VAR_FILE: Comma-separated list of variable file names./app/ directory with the filename terraform_setup.sh.The following test script will be used to verify your solution:
#!/bin/bash
# Check CLI config file
if [[ -n "${TF_PARAM_CLI_CONFIG_FILE}" ]]; then
if [[ -f "${TF_PARAM_CLI_CONFIG_FILE}" ]]; then
export TF_CLI_CONFIG_FILE=${TF_PARAM_CLI_CONFIG_FILE}
else
echo "Terraform cli config does not exist: ${TF_PARAM_CLI_CONFIG_FILE}"
exit 1
fi
fi
# 'path' is a required parameter, save it as module_path
readonly module_path="${TF_PARAM_PATH}"
export path=$module_path
if [[ ! -d "$module_path" ]]; then
echo "Path does not exist: $module_path"
exit 1
fi
# Initialize terraform
if [[ -n "${TF_PARAM_BACKEND_CONFIG_FILE}" ]]; then
for file in $(echo "${TF_PARAM_BACKEND_CONFIG_FILE}" | tr ',' '\n'); do
if [[ -f "$module_path/$file" ]]; then
INIT_ARGS="$INIT_ARGS -backend-config=$file"
else
echo "Backend config '$file' wasn't found" >&2
exit 1
fi
done
fi
if [[ -n "${TF_PARAM_BACKEND_CONFIG}" ]]; then
for config in $(echo "${TF_PARAM_BACKEND_CONFIG}" | tr ',' '\n'); do
INIT_ARGS="$INIT_ARGS -backend-config=$config"
done
fi
export INIT_ARGS
# shellcheck disable=SC2086
terraform -chdir="$module_path" init -input=false $INIT_ARGS
# Set workspace from parameter, allowing it to be overridden by TF_WORKSPACE.
# If TF_WORKSPACE is set we don't want terraform init to use the value, in the case we are running new_workspace.sh this would cause an error
readonly workspace_parameter="${TF_PARAM_WORKSPACE}"
readonly workspace="${TF_WORKSPACE:-$workspace_parameter}"
export workspace
unset TF_WORKSPACE
rm -rf .terraform
# The line below is the original place for the init
# terraform -chdir="$module_path" init -input=false -lock-timeout=300s $INIT_ARGS
# Test for saving state locally vs a remote state backend storage
if [[ -n "$workspace_parameter" ]]; then
echo "[INFO] Provisioning local workspace: $workspace"
terraform -chdir="$module_path" workspace select "$workspace"
else
echo "[INFO] Remote State Backend Enabled"
fi
if [[ -n "${TF_PARAM_VAR}" ]]; then
for var in $(echo "${TF_PARAM_VAR}" | tr ',' '\n'); do
PLAN_ARGS="$PLAN_ARGS -var $var"
done
fi
if [[ -n "${TF_PARAM_VAR_FILE}" ]]; then
for file in $(echo "${TF_PARAM_VAR_FILE}" | tr ',' '\n'); do
if [[ -f "$module_path/$file" ]]; then
PLAN_ARGS="$PLAN_ARGS -var-file=$file"
else
echo "Var file '$file' wasn't found" >&2
exit 1
fi
done
fi
export PLAN_ARGS
# terraform -chdir="$module_path" init -input=false -lock-timeout=300s $INIT_ARGS
# shellcheck disable=SC2086
terraform -chdir="$module_path" apply -destroy -input=false -auto-approve $PLAN_ARGS