Skip to content

Commit

Permalink
CB-27475 Cleaning up express onboarding outputs
Browse files Browse the repository at this point in the history
  • Loading branch information
daszabo committed Oct 24, 2024
1 parent f73494c commit 33e78f7
Show file tree
Hide file tree
Showing 7 changed files with 609 additions and 95 deletions.
1 change: 1 addition & 0 deletions .github/workflows/push-quickstart-script-to-s3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
68 changes: 65 additions & 3 deletions aws/destroy.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,71 @@
# 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
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=(
". \"${HOME}\"/variables.sh && cdp login --account-id \"${ACCOUNT_ID}\" --use-device-code 2>> \"${CMD_OUTPUTS}\""
". \"${HOME}\"/variables.sh && cd \"${HOME}\"/cdp-tf-quickstarts/aws && \"${HOME}\"/terraform destroy -auto-approve >> \"${CMD_OUTPUTS}\" 2>&1"
)

declare -rx STEPS=(
'renewing CDP session'
'tearing down resources'
)

main() {
printf "%sDestroying express onboarding environment\n" $COLOR_RESET

start
}

main
195 changes: 157 additions & 38 deletions aws/quickstart.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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:-""}"
Expand All @@ -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 <<EOF > $output_file
cat <<EOF > $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}"
Expand All @@ -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 <<EOF > $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 <<EOF > $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 <<EOF > $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
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
69 changes: 66 additions & 3 deletions azure/destroy.sh
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,71 @@
# 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
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}/destroy.out"

declare -rx CMDS=(
". \"${HOME}\"/variables.sh && cdp login --account-id \"${ACCOUNT_ID}\" --use-device-code 2>> \"${CMD_OUTPUTS}\""
". \"${HOME}\"/variables.sh && cd \"${HOME}\"/cdp-tf-quickstarts/aws && \"${HOME}\"/terraform destroy -auto-approve >> \"${CMD_OUTPUTS}\" 2>&1"
)

declare -rx STEPS=(
'renewing CDP session'
'tearing down resources'
)

main() {
printf "%sDestroying express onboarding environment\n" $COLOR_RESET

start
}

main
Loading

0 comments on commit 33e78f7

Please sign in to comment.