diff --git a/.github/workflows/push-quickstart-script-to-s3.yml b/.github/workflows/push-quickstart-script-to-s3.yml index 41b4dbb..4662dee 100644 --- a/.github/workflows/push-quickstart-script-to-s3.yml +++ b/.github/workflows/push-quickstart-script-to-s3.yml @@ -59,4 +59,5 @@ jobs: for csp in aws azure gcp do aws s3 cp ${csp}/quickstart.sh s3://${{ vars.S3_BUCKET }}/${csp}/${{ env.S3_FOLDER }}/ + aws s3 cp ${csp}/destroy.sh s3://${{ vars.S3_BUCKET }}/${csp}/${{ env.S3_FOLDER }}/ done \ No newline at end of file diff --git a/aws/destroy.sh b/aws/destroy.sh old mode 100644 new mode 100755 index c44e19e..fa22a5f --- a/aws/destroy.sh +++ b/aws/destroy.sh @@ -13,9 +13,89 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-}" != "dumb" ]; then + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +else + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +fi -cd cdp-tf-quickstarts/aws +start() { + local step=0 + local total_steps="${#CMDS[@]}" -source variables.sh + tput civis -- invisible -${HOME}/terraform destroy -auto-approve \ No newline at end of file + while [ "$step" -lt "$total_steps" ]; do + # Format step number for display + local step_num=$((step + 1)) + + echo -ne "\\r[ * ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + # Check if it's a function or a command + if declare -F "${CMDS[$step]}" > /dev/null; then + # If it's a function, call the function directly + "${CMDS[$step]}" + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" + fi + + exit_code=$? + + # Print success (✔) or failure (✖) based on the exit code + if [ $exit_code -eq 0 ]; then + echo -ne "\\r[ ${COLOR_FG_GREEN}✔${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]}\\n" + else + echo -ne "\\r[ ${COLOR_FG_RED}✖${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]} (failed with exit code $exit_code)\\n" + printf "%sERROR: Step failed, check '${CMD_OUTPUTS}' for more details.%s\n" $COLOR_FG_RED $COLOR_RESET + tput cnorm -- normal # Restore cursor before exiting + exit $exit_code # Exit with the command's exit code + fi + + # Move to the next command or function + step=$((step + 1)) + done + + tput cnorm -- normal +} + +declare -rx CMD_OUTPUTS="${HOME}/destroy.out" + +declare -rx CMDS=( + "cdp_login 2>> \"${CMD_OUTPUTS}\"" + "terraform_destroy >> \"${CMD_OUTPUTS}\" 2>&1" +) + +declare -rx STEPS=( + 'renewing CDP session' + 'tearing down resources' +) + +cdp_login() { + . "${HOME}/variables.sh" + cdp login --account-id "${ACCOUNT_ID}" --use-device-code + exit_code=$? + + return $exit_code +} + +terraform_destroy() { + cd ${HOME}/cdp-tf-quickstarts/aws + + . "${HOME}/variables.sh" + ${HOME}/terraform destroy -auto-approve + exit_code=$? + + return $exit_code +} + +main() { + printf "%sDestroying express onboarding environment\n" $COLOR_RESET + + start +} + +main \ No newline at end of file diff --git a/aws/quickstart.sh b/aws/quickstart.sh index e3c68ee..8ce69dd 100644 --- a/aws/quickstart.sh +++ b/aws/quickstart.sh @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +export CDP_QUICKSTART_VERSON="v0.8.5" + export TF_VAR_aws_region="${1:-""}" export TF_VAR_env_prefix="${2:-""}" export ACCOUNT_ID="${3:-""}" @@ -26,10 +28,11 @@ export TF_VAR_environment_async_creation="true" export TF_VAR_datalake_async_creation="true" export TF_VAR_datalake_scale="LIGHT_DUTY" -# Save TF variables to file -output_file="variables.sh" +prepare_destroy_script() { + # Save TF variables to file + output_file="variables.sh" -cat < $output_file + cat < $output_file export TF_VAR_aws_region="${TF_VAR_aws_region}" export TF_VAR_env_prefix="${TF_VAR_env_prefix}" export ACCOUNT_ID="${ACCOUNT_ID}" @@ -42,42 +45,158 @@ export TF_VAR_ingress_extra_cidrs_and_ports='${TF_VAR_ingress_extra_cidrs_and_po export TF_VAR_datalake_scale="${TF_VAR_datalake_scale}" EOF -destroy_file="destroy.sh" - -cat < $destroy_file -cd cdp-tf-quickstarts/aws - -source ${HOME}/variables.sh - -${HOME}/terraform destroy -auto-approve -EOF - -# Make the file executable -chmod +x $output_file -chmod +x $destroy_file - -# Install Terraform -curl -fsSL https://releases.hashicorp.com/terraform/1.7.1/terraform_1.7.1_linux_amd64.zip -o terraform.zip -unzip -o terraform.zip -d ${HOME} -rm terraform.zip - -# Checkout CDP Quickstart Repository -git clone --branch v0.8.3 https://github.com/cloudera-labs/cdp-tf-quickstarts.git -cd cdp-tf-quickstarts/aws - -# Install CDP CLI and Log In -pip install cdpcli - -config_file="${HOME}/.cdp/config" -mkdir -p "$(dirname "$config_file")" -cat < $config_file + destroy_file="${HOME}/destroy.sh" + + curl -S https://quickstart-dev.cloudera-labs.com/aws/latest/destroy.sh -o ${destroy_file} + exit_code=$? + if [ $exit_code -ne 0 ]; then + return $exit_code + fi + + # Make the file executable + chmod +x $output_file + chmod +x $destroy_file + + return $exit_code +} + +install_terraform() { + # Install Terraform + curl -fsSL https://releases.hashicorp.com/terraform/1.7.1/terraform_1.7.1_linux_amd64.zip -o terraform.zip + exit_code=$? + if [ $exit_code -ne 0 ]; then + return $exit_code + fi + + unzip -o terraform.zip -d ${HOME} + exit_code=$? + if [ $exit_code -ne 0 ]; then + return $exit_code + fi + + rm terraform.zip + return $exit_code +} + +checkout_cdp_tf_qs() { + rm -rf ${HOME}/cdp-tf-quickstarts + + # Checkout CDP Quickstart Repository + git clone --branch ${CDP_QUICKSTART_VERSON} https://github.com/cloudera-labs/cdp-tf-quickstarts.git ${HOME}/cdp-tf-quickstarts + exit_code=$? + + return $exit_code +} + +configure_cdpcli() { + pip install cdpcli + exit_code=$? + if [ $exit_code -ne 0 ]; then + return $exit_code + fi + + rm -rf "${HOME}/.cdp" + config_file="${HOME}/.cdp/config" + mkdir -p "$(dirname "$config_file")" + cat < $config_file [default] cdp_region = ${CDP_REGION} EOF -cdp login --account-id "${ACCOUNT_ID}" --use-device-code - -# Apply Terraform Quickstart Module -${HOME}/terraform init - -${HOME}/terraform apply -auto-approve \ No newline at end of file + return $exit_code +} + +cdp_login() { + cdp login --account-id "${ACCOUNT_ID}" --use-device-code + exit_code=$? + + return $exit_code +} + + +terraform_apply() { + cd ${HOME}/cdp-tf-quickstarts/aws + ${HOME}/terraform init + + ${HOME}/terraform apply -auto-approve + exit_code=$? + + return $exit_code +} + +if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-}" != "dumb" ]; then + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +else + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +fi + +start() { + local step=0 + local total_steps="${#CMDS[@]}" + + tput civis -- invisible + + while [ "$step" -lt "$total_steps" ]; do + # Format step number for display + local step_num=$((step + 1)) + + echo -ne "\\r[ * ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + # Check if it's a function or a command + if declare -F "${CMDS[$step]}" > /dev/null; then + # If it's a function, call the function directly + "${CMDS[$step]}" + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" + fi + + exit_code=$? + + # Print success (✔) or failure (✖) based on the exit code + if [ $exit_code -eq 0 ]; then + echo -ne "\\r[ ${COLOR_FG_GREEN}✔${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]}\\n" + else + echo -ne "\\r[ ${COLOR_FG_RED}✖${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]} (failed with exit code $exit_code)\\n" + printf "%sERROR: Step failed, check '${CMD_OUTPUTS}' for more details.%s\n" $COLOR_FG_RED $COLOR_RESET + tput cnorm -- normal # Restore cursor before exiting + exit $exit_code # Exit with the command's exit code + fi + + # Move to the next command or function + step=$((step + 1)) + done + + tput cnorm -- normal +} + +declare -rx CMD_OUTPUTS="${HOME}/quickstart.out" + +declare -rx CMDS=( + "configure_cdpcli >> \"${CMD_OUTPUTS}\" 2>&1" + "cdp_login 2>> \"${CMD_OUTPUTS}\"" + "install_terraform >> \"${CMD_OUTPUTS}\" 2>&1" + "checkout_cdp_tf_qs >> \"${CMD_OUTPUTS}\" 2>&1" + "prepare_destroy_script >> \"${CMD_OUTPUTS}\" 2>&1" + "terraform_apply >> \"${CMD_OUTPUTS}\" 2>&1" +) + +declare -rx STEPS=( + 'configuring CDP CLI', + 'creating CDP session', + 'installing Terraform CLI', + 'checking out the CDP Terraform Quickstart project', + 'backing up quickstart configuration', + 'deploying quickstart environment' +) + +main() { + printf "%sDeploying express onboarding environment\n" $COLOR_RESET + + start +} + +main \ No newline at end of file diff --git a/azure/destroy.sh b/azure/destroy.sh old mode 100644 new mode 100755 index 0989227..4ef8ef8 --- a/azure/destroy.sh +++ b/azure/destroy.sh @@ -13,9 +13,89 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-}" != "dumb" ]; then + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +else + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +fi -cd cdp-tf-quickstarts/azure +start() { + local step=0 + local total_steps="${#CMDS[@]}" -source variables.sh + tput civis -terraform destroy -auto-approve \ No newline at end of file + while [ "$step" -lt "$total_steps" ]; do + # Format step number for display + local step_num=$((step + 1)) + + echo -ne "\\r[ * ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + # Check if it's a function or a command + if declare -F "${CMDS[$step]}" > /dev/null; then + # If it's a function, call the function directly + "${CMDS[$step]}" + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" + fi + + exit_code=$? + + # Print success (✔) or failure (✖) based on the exit code + if [ $exit_code -eq 0 ]; then + echo -ne "\\r[ ${COLOR_FG_GREEN}✔${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]}\\n" + else + echo -ne "\\r[ ${COLOR_FG_RED}✖${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]} (failed with exit code $exit_code)\\n" + printf "%sERROR: Step failed, check '${CMD_OUTPUTS}' for more details.%s\n" $COLOR_FG_RED $COLOR_RESET + tput cnorm # Restore cursor before exiting + exit $exit_code # Exit with the command's exit code + fi + + # Move to the next command or function + step=$((step + 1)) + done + + tput cnorm +} + +declare -rx CMD_OUTPUTS="${HOME}/destroy.out" + +declare -rx CMDS=( + "cdp_login 2>> \"${CMD_OUTPUTS}\"" + "terraform_destroy >> \"${CMD_OUTPUTS}\" 2>&1" +) + +declare -rx STEPS=( + 'renewing CDP session' + 'tearing down resources' +) + +cdp_login() { + . "${HOME}/variables.sh" + cdp login --account-id "${ACCOUNT_ID}" --use-device-code + exit_code=$? + + return $exit_code +} + +terraform_destroy() { + cd ${HOME}/cdp-tf-quickstarts/azure + + . "${HOME}/variables.sh" + terraform destroy -auto-approve + exit_code=$? + + return $exit_code +} + +main() { + printf "%sDestroying express onboarding environment\n" $COLOR_RESET + + start +} + +main \ No newline at end of file diff --git a/azure/quickstart.sh b/azure/quickstart.sh index 62dd33c..f51a626 100644 --- a/azure/quickstart.sh +++ b/azure/quickstart.sh @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +export CDP_QUICKSTART_VERSON="v0.8.5" + export TF_VAR_azure_region="${1:-""}" export TF_VAR_env_prefix="${2:-""}" export ACCOUNT_ID="${3:-""}" @@ -26,10 +28,11 @@ export TF_VAR_datalake_async_creation="true" export TF_VAR_datalake_scale="LIGHT_DUTY" export TF_VAR_azure_subscription_id="$(az account show --query id --output tsv)" -# Save TF variables to file -output_file="variables.sh" +prepare_destroy_script() { + # Save TF variables to file + output_file="variables.sh" -cat < $output_file + cat < $output_file export TF_VAR_azure_region="${TF_VAR_azure_region}" export TF_VAR_env_prefix="${TF_VAR_env_prefix}" export ACCOUNT_ID="${ACCOUNT_ID}" @@ -42,37 +45,137 @@ export TF_VAR_datalake_scale="${TF_VAR_datalake_scale}" export TF_VAR_azure_subscription_id="${TF_VAR_azure_subscription_id}" EOF -destroy_file="destroy.sh" + destroy_file="${HOME}/destroy.sh" -cat < $destroy_file -cd cdp-tf-quickstarts/azure + curl -S https://quickstart-dev.cloudera-labs.com/aws/latest/destroy.sh -o ${destroy_file} + exit_code=$? + if [ $exit_code -ne 0 ]; then + return $exit_code + fi -source ${HOME}/variables.sh + # Make the file executable + chmod +x $output_file + chmod +x $destroy_file -${HOME}/terraform destroy -auto-approve -EOF + return $exit_code +} + +checkout_cdp_tf_qs() { + rm -rf ${HOME}/cdp-tf-quickstarts -# Make the file executable -chmod +x $output_file -chmod +x $destroy_file + # Checkout CDP Quickstart Repository + git clone --branch ${CDP_QUICKSTART_VERSON} https://github.com/cloudera-labs/cdp-tf-quickstarts.git ${HOME}/cdp-tf-quickstarts + exit_code=$? -# Checkout CDP Quickstart Repository -git clone --branch v0.8.3 https://github.com/cloudera-labs/cdp-tf-quickstarts.git -cd cdp-tf-quickstarts/azure + return $exit_code +} -# Install CDP CLI and Log In -pip install cdpcli +configure_cdpcli() { + pip install cdpcli + exit_code=$? + if [ $exit_code -ne 0 ]; then + return $exit_code + fi -config_file="${HOME}/.cdp/config" -mkdir -p "$(dirname "$config_file")" -cat < $config_file + rm -rf "${HOME}/.cdp" + config_file="${HOME}/.cdp/config" + mkdir -p "$(dirname "$config_file")" + cat < $config_file [default] cdp_region = ${CDP_REGION} EOF -cdp login --account-id "${ACCOUNT_ID}" --use-device-code + return $exit_code +} + +cdp_login() { + cdp login --account-id "${ACCOUNT_ID}" --use-device-code + exit_code=$? + + return $exit_code +} + +terraform_apply() { + cd ${HOME}/cdp-tf-quickstarts/azure + terraform init + + terraform apply -auto-approve + exit_code=$? + + return $exit_code +} + +if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-}" != "dumb" ]; then + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +else + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +fi + +start() { + local step=0 + local total_steps="${#CMDS[@]}" + + tput civis + + while [ "$step" -lt "$total_steps" ]; do + # Format step number for display + local step_num=$((step + 1)) + + echo -ne "\\r[ * ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + # Check if it's a function or a command + if declare -F "${CMDS[$step]}" > /dev/null; then + # If it's a function, call the function directly + "${CMDS[$step]}" + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" + fi + + exit_code=$? + + # Print success (✔) or failure (✖) based on the exit code + if [ $exit_code -eq 0 ]; then + echo -ne "\\r[ ${COLOR_FG_GREEN}✔${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]}\\n" + else + echo -ne "\\r[ ${COLOR_FG_RED}✖${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]} (failed with exit code $exit_code)\\n" + printf "%sERROR: Step failed, check '${CMD_OUTPUTS}' for more details.%s\n" $COLOR_FG_RED $COLOR_RESET + tput cnorm # Restore cursor before exiting + exit $exit_code # Exit with the command's exit code + fi + + # Move to the next command or function + step=$((step + 1)) + done + + tput cnorm +} + +declare -rx CMD_OUTPUTS="${HOME}/quickstart.out" + +declare -rx CMDS=( + "configure_cdpcli >> \"${CMD_OUTPUTS}\" 2>&1" + "cdp_login 2>> \"${CMD_OUTPUTS}\"" + "checkout_cdp_tf_qs >> \"${CMD_OUTPUTS}\" 2>&1" + "prepare_destroy_script >> \"${CMD_OUTPUTS}\" 2>&1" + "terraform_apply >> \"${CMD_OUTPUTS}\" 2>&1" +) + +declare -rx STEPS=( + 'configuring CDP CLI', + 'creating CDP session', + 'checking out the CDP Terraform Quickstart project', + 'backing up quickstart configuration', + 'deploying quickstart environment' +) + +main() { + printf "%sDeploying express onboarding environment\n" $COLOR_RESET -# Apply Terraform Quickstart Module -terraform init + start +} -terraform apply -auto-approve \ No newline at end of file +main \ No newline at end of file diff --git a/gcp/destroy.sh b/gcp/destroy.sh index 3121afc..805a6a2 100644 --- a/gcp/destroy.sh +++ b/gcp/destroy.sh @@ -13,9 +13,89 @@ # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. # See the License for the specific language governing permissions and # limitations under the License. +if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-}" != "dumb" ]; then + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +else + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +fi -cd cdp-tf-quickstarts/gcp +start() { + local step=0 + local total_steps="${#CMDS[@]}" -source variables.sh + tput civis -terraform destroy -auto-approve \ No newline at end of file + while [ "$step" -lt "$total_steps" ]; do + # Format step number for display + local step_num=$((step + 1)) + + echo -ne "\\r[ * ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + # Check if it's a function or a command + if declare -F "${CMDS[$step]}" > /dev/null; then + # If it's a function, call the function directly + "${CMDS[$step]}" + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" + fi + + exit_code=$? + + # Print success (✔) or failure (✖) based on the exit code + if [ $exit_code -eq 0 ]; then + echo -ne "\\r[ ${COLOR_FG_GREEN}✔${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]}\\n" + else + echo -ne "\\r[ ${COLOR_FG_RED}✖${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]} (failed with exit code $exit_code)\\n" + printf "%sERROR: Step failed, check '${CMD_OUTPUTS}' for more details.%s\n" $COLOR_FG_RED $COLOR_RESET + tput cnorm # Restore cursor before exiting + exit $exit_code # Exit with the command's exit code + fi + + # Move to the next command or function + step=$((step + 1)) + done + + tput cnorm +} + +declare -rx CMD_OUTPUTS="${HOME}/destroy.out" + +declare -rx CMDS=( + "cdp_login 2>> \"${CMD_OUTPUTS}\"" + "terraform_destroy >> \"${CMD_OUTPUTS}\" 2>&1" +) + +declare -rx STEPS=( + 'renewing CDP session' + 'tearing down resources' +) + +cdp_login() { + . "${HOME}/variables.sh" + ${HOME}/.local/bin/cdp login --account-id "${ACCOUNT_ID}" --use-device-code + exit_code=$? + + return $exit_code +} + +terraform_destroy() { + cd ${HOME}/cdp-tf-quickstarts/gcp + + . "${HOME}/variables.sh" + terraform destroy -auto-approve + exit_code=$? + + return $exit_code +} + +main() { + printf "%sDestroying express onboarding environment\n" $COLOR_RESET + + start +} + +main \ No newline at end of file diff --git a/gcp/quickstart.sh b/gcp/quickstart.sh index efb5839..0f0475e 100644 --- a/gcp/quickstart.sh +++ b/gcp/quickstart.sh @@ -14,6 +14,8 @@ # See the License for the specific language governing permissions and # limitations under the License. +export CDP_QUICKSTART_VERSON="v0.8.5" + export TF_VAR_gcp_project="${1:-""}" export TF_VAR_gcp_region="${2:-""}" export TF_VAR_env_prefix="${3:-""}" @@ -26,10 +28,11 @@ export TF_VAR_environment_async_creation="true" export TF_VAR_datalake_async_creation="true" export TF_VAR_datalake_scale="LIGHT_DUTY" -# Save TF variables to file -output_file="variables.sh" +prepare_destroy_script() { + # Save TF variables to file + output_file="variables.sh" -cat < $output_file + cat < $output_file export TF_VAR_gcp_project="${TF_VAR_gcp_project}" export TF_VAR_gcp_region="${TF_VAR_gcp_region}" export TF_VAR_env_prefix='${TF_VAR_env_prefix}' @@ -42,37 +45,137 @@ export TF_VAR_ingress_extra_cidrs_and_ports='${TF_VAR_ingress_extra_cidrs_and_po export TF_VAR_datalake_scale="${TF_VAR_datalake_scale}" EOF -destroy_file="destroy.sh" + destroy_file="${HOME}/destroy.sh" -cat < $destroy_file -cd cdp-tf-quickstarts/gcp + curl -S https://quickstart-dev.cloudera-labs.com/aws/latest/destroy.sh -o ${destroy_file} + exit_code=$? + if [ $exit_code -ne 0 ]; then + return $exit_code + fi -source ${HOME}/variables.sh + # Make the file executable + chmod +x $output_file + chmod +x $destroy_file -${HOME}/terraform destroy -auto-approve -EOF + return $exit_code +} + +checkout_cdp_tf_qs() { + rm -rf ${HOME}/cdp-tf-quickstarts -# Make the file executable -chmod +x $output_file -chmod +x $destroy_file + # Checkout CDP Quickstart Repository + git clone --branch ${CDP_QUICKSTART_VERSON} https://github.com/cloudera-labs/cdp-tf-quickstarts.git ${HOME}/cdp-tf-quickstarts + exit_code=$? -# Checkout CDP Quickstart Repository -git clone --branch v0.8.3 https://github.com/cloudera-labs/cdp-tf-quickstarts.git -cd cdp-tf-quickstarts/gcp + return $exit_code +} -# Install CDP CLI and Log In -pip install cdpcli +configure_cdpcli() { + pip install cdpcli + exit_code=$? + if [ $exit_code -ne 0 ]; then + return $exit_code + fi -config_file="${HOME}/.cdp/config" -mkdir -p "$(dirname "$config_file")" -cat < $config_file + rm -rf "${HOME}/.cdp" + config_file="${HOME}/.cdp/config" + mkdir -p "$(dirname "$config_file")" + cat < $config_file [default] cdp_region = ${CDP_REGION} EOF -cdp login --account-id "${ACCOUNT_ID}" --use-device-code + return $exit_code +} + +cdp_login() { + ${HOME}/.local/bin/cdp login --account-id "${ACCOUNT_ID}" --use-device-code + exit_code=$? + + return $exit_code +} + +terraform_apply() { + cd ${HOME}/cdp-tf-quickstarts/gcp + terraform init + + terraform apply -auto-approve + exit_code=$? + + return $exit_code +} + +if [ -t 1 ] && [ -z "${NO_COLOR:-}" ] && [ "${TERM:-}" != "dumb" ]; then + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +else + COLOR_RESET='' + COLOR_FG_RED='' + COLOR_FG_GREEN='' +fi + +start() { + local step=0 + local total_steps="${#CMDS[@]}" + + tput civis + + while [ "$step" -lt "$total_steps" ]; do + # Format step number for display + local step_num=$((step + 1)) + + echo -ne "\\r[ * ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + # Check if it's a function or a command + if declare -F "${CMDS[$step]}" > /dev/null; then + # If it's a function, call the function directly + "${CMDS[$step]}" + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" + fi + + exit_code=$? + + # Print success (✔) or failure (✖) based on the exit code + if [ $exit_code -eq 0 ]; then + echo -ne "\\r[ ${COLOR_FG_GREEN}✔${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]}\\n" + else + echo -ne "\\r[ ${COLOR_FG_RED}✖${COLOR_RESET} ] Step [$step_num/$total_steps]: ${STEPS[$step]} (failed with exit code $exit_code)\\n" + printf "%sERROR: Step failed, check '${CMD_OUTPUTS}' for more details.%s\n" $COLOR_FG_RED $COLOR_RESET + tput cnorm # Restore cursor before exiting + exit $exit_code # Exit with the command's exit code + fi + + # Move to the next command or function + step=$((step + 1)) + done + + tput cnorm +} + +declare -rx CMD_OUTPUTS="${HOME}/quickstart.out" + +declare -rx CMDS=( + "configure_cdpcli >> \"${CMD_OUTPUTS}\" 2>&1" + "cdp_login 2>> \"${CMD_OUTPUTS}\"" + "checkout_cdp_tf_qs >> \"${CMD_OUTPUTS}\" 2>&1" + "prepare_destroy_script >> \"${CMD_OUTPUTS}\" 2>&1" + "terraform_apply >> \"${CMD_OUTPUTS}\" 2>&1" +) + +declare -rx STEPS=( + 'configuring CDP CLI', + 'creating CDP session', + 'checking out the CDP Terraform Quickstart project', + 'backing up quickstart configuration', + 'deploying quickstart environment' +) + +main() { + printf "%sDeploying express onboarding environment\n" $COLOR_RESET -# Apply Terraform Quickstart Module -terraform init + start +} -terraform apply -auto-approve \ No newline at end of file +main \ No newline at end of file