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..1a419af --- 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)) + + # 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]}" & pid=$! + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" & pid=$! + fi + + # Spinner loop while the command runs + while ps -p $pid &>/dev/null; do + echo -ne "\\r[ ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + + for k in "${!FRAME[@]}"; do + echo -ne "\\r[ ${FRAME[k]} ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + sleep $FRAME_INTERVAL + done + done + + # Check the exit code of the command or function + wait $pid + 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 -x FRAME=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏") +declare -x FRAME_INTERVAL=0.1 + +declare -rx CMDS=( + 'cd ${HOME}/cdp-tf-quickstarts/aws >> ${CMD_OUTPUTS} 2>&1' + '. variables.sh > ${CMD_OUTPUTS} 2>&1' + 'cdp login --account-id "${ACCOUNT_ID}" --use-device-code 2>> ${CMD_OUTPUTS}' + '${HOME}/terraform destroy -auto-approve >> ${CMD_OUTPUTS} 2>&1' +) + +declare -rx STEPS=( + 'changing to AWS Quickstart directory' + 'importing variables' + 'renewing CDP session' + 'tearing down resources' +) + +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..76390d6 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.3" + 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,169 @@ 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} + + # Make the file executable + chmod +x $output_file + chmod +x $destroy_file + + return 0 +} + +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=$? + + cd ${HOME}/cdp-tf-quickstarts/aws + 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() { + ${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)) + + # 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]}" & pid=$! + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" & pid=$! + fi + + # Spinner loop while the command runs + while ps -p $pid &>/dev/null; do + echo -ne "\\r[ ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + + for k in "${!FRAME[@]}"; do + echo -ne "\\r[ ${FRAME[k]} ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + sleep $FRAME_INTERVAL + done + done + + # Check the exit code of the command or function + wait $pid + 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 -x FRAME=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏") +declare -x FRAME_INTERVAL=0.1 + +declare -rx CMDS=( + 'configure_cdpcli >> ${CMD_OUTPUTS} 2>&1', + 'cdp_login', + 'install_terraform', + 'checkout_cdp_tf_qs', + 'prepare_destroy_script', + 'terraform_apply' +) + +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 + +# TODO redirect output \ No newline at end of file diff --git a/azure/destroy.sh b/azure/destroy.sh old mode 100644 new mode 100755 index 0989227..5a45407 --- a/azure/destroy.sh +++ b/azure/destroy.sh @@ -14,8 +14,89 @@ # See the License for the specific language governing permissions and # limitations under the License. -cd cdp-tf-quickstarts/azure +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 -source variables.sh +start() { + local step=0 + local total_steps="${#CMDS[@]}" -terraform destroy -auto-approve \ No newline at end of file + tput civis -- invisible + + while [ "$step" -lt "$total_steps" ]; do + # Format step number for display + local step_num=$((step + 1)) + + # 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]}" & pid=$! + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" & pid=$! + fi + + # Spinner loop while the command runs + while ps -p $pid &>/dev/null; do + echo -ne "\\r[ ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + + for k in "${!FRAME[@]}"; do + echo -ne "\\r[ ${FRAME[k]} ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + sleep $FRAME_INTERVAL + done + done + + # Check the exit code of the command or function + wait $pid + 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 -x FRAME=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏") +declare -x FRAME_INTERVAL=0.1 + +declare -rx CMDS=( + 'cd ${HOME}/cdp-tf-quickstarts/azure >> ${CMD_OUTPUTS} 2>&1' + '. variables.sh > ${CMD_OUTPUTS} 2>&1' + 'cdp login --account-id "${ACCOUNT_ID}" --use-device-code 2>> ${CMD_OUTPUTS}' + '${HOME}/terraform destroy -auto-approve >> ${CMD_OUTPUTS} 2>&1' +) + +declare -rx STEPS=( + 'changing to Azure Quickstart directory' + 'importing variables' + 'renewing CDP session' + 'tearing down resources' +) + +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..6fc568d 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.3" + 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,147 @@ 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/azure/latest/destroy.sh -o ${destroy_file} -source ${HOME}/variables.sh + # Make the file executable + chmod +x $output_file + chmod +x $destroy_file -${HOME}/terraform destroy -auto-approve -EOF + return 0 +} + +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 + cd ${HOME}/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() { + ${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)) + + # 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]}" & pid=$! + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" & pid=$! + fi + + # Spinner loop while the command runs + while ps -p $pid &>/dev/null; do + echo -ne "\\r[ ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + + for k in "${!FRAME[@]}"; do + echo -ne "\\r[ ${FRAME[k]} ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + sleep $FRAME_INTERVAL + done + done + + # Check the exit code of the command or function + wait $pid + 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 -x FRAME=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏") +declare -x FRAME_INTERVAL=0.1 + +declare -rx CMDS=( + 'configure_cdpcli >> ${CMD_OUTPUTS} 2>&1', + 'cdp_login', + 'checkout_cdp_tf_qs', + 'prepare_destroy_script', + 'terraform_apply' +) + +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..5a45407 100644 --- a/gcp/destroy.sh +++ b/gcp/destroy.sh @@ -14,8 +14,89 @@ # See the License for the specific language governing permissions and # limitations under the License. -cd cdp-tf-quickstarts/gcp +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 -source variables.sh +start() { + local step=0 + local total_steps="${#CMDS[@]}" -terraform destroy -auto-approve \ No newline at end of file + tput civis -- invisible + + while [ "$step" -lt "$total_steps" ]; do + # Format step number for display + local step_num=$((step + 1)) + + # 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]}" & pid=$! + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" & pid=$! + fi + + # Spinner loop while the command runs + while ps -p $pid &>/dev/null; do + echo -ne "\\r[ ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + + for k in "${!FRAME[@]}"; do + echo -ne "\\r[ ${FRAME[k]} ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + sleep $FRAME_INTERVAL + done + done + + # Check the exit code of the command or function + wait $pid + 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 -x FRAME=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏") +declare -x FRAME_INTERVAL=0.1 + +declare -rx CMDS=( + 'cd ${HOME}/cdp-tf-quickstarts/azure >> ${CMD_OUTPUTS} 2>&1' + '. variables.sh > ${CMD_OUTPUTS} 2>&1' + 'cdp login --account-id "${ACCOUNT_ID}" --use-device-code 2>> ${CMD_OUTPUTS}' + '${HOME}/terraform destroy -auto-approve >> ${CMD_OUTPUTS} 2>&1' +) + +declare -rx STEPS=( + 'changing to Azure Quickstart directory' + 'importing variables' + 'renewing CDP session' + 'tearing down resources' +) + +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..e52dc6d 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.3" + 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,147 @@ 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/gcp/latest/destroy.sh -o ${destroy_file} -source ${HOME}/variables.sh + # Make the file executable + chmod +x $output_file + chmod +x $destroy_file -${HOME}/terraform destroy -auto-approve -EOF + return 0 +} + +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 + cd ${HOME}/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() { + cdp login --account-id "${ACCOUNT_ID}" --use-device-code + exit_code=$? + + return $exit_code +} + +terraform_apply() { + ${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)) + + # 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]}" & pid=$! + else + # Otherwise, treat it as a shell command + eval "${CMDS[$step]}" & pid=$! + fi + + # Spinner loop while the command runs + while ps -p $pid &>/dev/null; do + echo -ne "\\r[ ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + + for k in "${!FRAME[@]}"; do + echo -ne "\\r[ ${FRAME[k]} ] Step [$step_num/$total_steps]: ${STEPS[$step]} ..." + sleep $FRAME_INTERVAL + done + done + + # Check the exit code of the command or function + wait $pid + 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 -x FRAME=("⠋" "⠙" "⠹" "⠸" "⠼" "⠴" "⠦" "⠧" "⠇" "⠏") +declare -x FRAME_INTERVAL=0.1 + +declare -rx CMDS=( + 'configure_cdpcli >> ${CMD_OUTPUTS} 2>&1', + 'cdp_login', + 'checkout_cdp_tf_qs', + 'prepare_destroy_script', + 'terraform_apply' +) + +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