From 700f968f6d63dd4e7173ec16a26c815b560e2973 Mon Sep 17 00:00:00 2001 From: Greg O'Grady Date: Wed, 11 Dec 2024 16:55:24 -0500 Subject: [PATCH] refactor: un-nest if branching --- ...vfcli-create-or-get-free-env-from-pool.yml | 39 +++--- .../vfcli/vfcli-delete-or-release-env.yml | 112 ++++++++++-------- 2 files changed, 83 insertions(+), 68 deletions(-) diff --git a/src/commands/vfcli/vfcli-create-or-get-free-env-from-pool.yml b/src/commands/vfcli/vfcli-create-or-get-free-env-from-pool.yml index 360c8d04..caecfaf8 100644 --- a/src/commands/vfcli/vfcli-create-or-get-free-env-from-pool.yml +++ b/src/commands/vfcli/vfcli-create-or-get-free-env-from-pool.yml @@ -33,29 +33,30 @@ steps: # otherwise creates a new environment with the provided name # and stores the env name in the cache force="<< parameters.force >>" - echo "<< parameters.env-name >>" >env_name.txt # default use the passed in env name - if [[ -n "<< parameters.pool-type >>" ]]; then - if [[ $force == true ]]; then - echo "Force option is enabled... Proceeding with environment creation." - else - result=$(vfcli pool get-free-env --pool-type "<< parameters.pool-type >>" --output json) - if [[ -z $result ]] || [[ $(echo "$result" | jq 'keys | length') -eq 0 ]]; then - echo "No free environment found. Proceeding with environment creation." - else - echo "Free environment found: $result" - env_name=$(echo "$result" | jq -r '.name') - echo ${env_name} > env_name.txt - cat env_name.txt - echo "export ENV_NAME=${env_name}" >> $BASH_ENV - vfcli pool use-env --env-name ${env_name} - exit 0 - fi - fi - else + + if [[ -z "<< parameters.pool-type >>" ]]; then echo "Pool type not provided." + exit 0 fi + if [[ $force == true ]]; then + echo "Force option is enabled... Proceeding with environment creation." + exit 0 + fi + + result=$(vfcli pool get-free-env --pool-type "<< parameters.pool-type >>" --output json) + if [[ -z $result ]] || [[ $(echo "$result" | jq 'keys | length') -eq 0 ]]; then + echo "No free environment found. Proceeding with environment creation." + exit 0 + fi + + echo "Free environment found: $result" + env_name=$(echo "$result" | jq -r '.name') + echo ${env_name} >env_name.txt + cat env_name.txt + vfcli pool use-env --env-name ${env_name} + - save_cache: key: env_name_cache-{{ .Environment.CIRCLE_PROJECT_REPONAME }}-{{ .Environment.CIRCLE_WORKFLOW_ID }} paths: diff --git a/src/commands/vfcli/vfcli-delete-or-release-env.yml b/src/commands/vfcli/vfcli-delete-or-release-env.yml index 8fd15e13..965d716e 100644 --- a/src/commands/vfcli/vfcli-delete-or-release-env.yml +++ b/src/commands/vfcli/vfcli-delete-or-release-env.yml @@ -30,55 +30,69 @@ steps: force="<< parameters.force >>" if [[ $force == true ]]; then vfcli env delete --name "<< parameters.env-name >>" --interactive false - else - if [ -f << parameters.env-name-path >> ] && [ "$(cat << parameters.env-name-path >> )" != "null" ]; then - env_name=$(cat << parameters.env-name-path >>) - echo "Env: $env_name will be released" - else - env_name="null" - echo "Env name file was not found. Will proceed to deleting the environment." + exit 0 + fi + + if [ ! -f << parameters.env-name-path >> ] || + [ "$(cat << parameters.env-name-path >> )" == "null" ] || + [ -z "$(cat << parameters.env-name-path >> )" ]; then + + echo "Env name file was not found. Will proceed to deleting the environment." + echo "Kick-starting the deletion of the environment." + vfcli env delete --name "<< parameters.env-name >>" --interactive false + exit 0 + fi + + env_name=$(cat << parameters.env-name-path >>) + echo "Env: $env_name will be released" + + echo "Releasing the environment..... $env_name" + vfcli pool release-env --env-name "$env_name" + + parse_response() { + local http_status="${1-}" + local response_body="${2-}" + + if [[ ! ($http_status == "201" || $http_status == "200") ]]; then + echo "Failed to trigger pipeline. HTTP status code: $http_status" + echo "Response body: $response_body" + return 1 + fi + + if ! (echo "$response_body" | jq empty 2>/dev/null); then + echo "Response is not valid JSON. Response body: $response_body" + return 1 fi - if [[ "$env_name" != "null" ]] && [[ -n "$env_name" ]]; then - echo "Releasing the environment..... $env_name" - vfcli pool release-env --env-name "$env_name" - if [[ << parameters.reset-db >> == true ]]; then - echo "Resetting the database for $env_name" - echo "Triggering pipeline with env-name parameter" - response=$(curl -s -w "\n%{http_code}" \ - --request POST \ - --url "https://circleci.com/api/v2/project/gh/voiceflow/env-release-system/pipeline" \ - --header "Circle-Token: ${CIRCLECI_API_TOKEN}" \ - --header "content-type: application/json" \ - --data "{\"parameters\":{\"e2e_env_name\":\"$env_name\",\"trigger_pipeline\":true}}") - http_status=$(echo "$response" | tail -n1) - response_body=$(echo "$response" | sed '$d') - if [[ $http_status == "201" || $http_status == "200" ]]; then - if echo "$response_body" | jq empty 2>/dev/null; then - pipeline_id=$(echo "$response_body" | jq -r '.id // empty') - pipeline_number=$(echo "$response_body" | jq -r '.number // empty') - if [[ -n $pipeline_number ]]; then - pipeline_url="https://app.circleci.com/pipelines/github/voiceflow/env-release-system/$pipeline_number" - echo "Env Release Pipeline triggered successfully." - echo "Pipeline URL: $pipeline_url" - echo "Pipeline ID: $pipeline_id" - echo "Exiting with success." - exit 0 - else - echo "Failed to retrieve pipeline number. Response body: $response_body" - fi - else - echo "Response is not valid JSON. Response body: $response_body" - fi - else - echo "Failed to trigger pipeline. HTTP status code: $http_status" - echo "Response body: $response_body" - fi - fi - vfcli env resume "$env_name" --interactive false - vfcli track attach --branch master --components all --name "$env_name" --interactive false --no-circleci - vfcli pool free-env --env-name "$env_name" - else - echo "Kick-starting the deletion of the environment." - vfcli env delete --name "<< parameters.env-name >>" --interactive false + + pipeline_id=$(echo "$response_body" | jq -r '.id // empty') + pipeline_number=$(echo "$response_body" | jq -r '.number // empty') + if [[ -z $pipeline_number ]]; then + echo "Failed to retrieve pipeline number. Response body: $response_body" + return 1 fi + + pipeline_url="https://app.circleci.com/pipelines/github/voiceflow/env-release-system/$pipeline_number" + echo "Env Release Pipeline triggered successfully." + echo "Pipeline URL: $pipeline_url" + echo "Pipeline ID: $pipeline_id" + echo "Exiting with success." + exit 0 + } + + if [[ << parameters.reset-db >> == true ]]; then + echo "Resetting the database for $env_name" + echo "Triggering pipeline with env-name parameter" + response=$(curl -s -w "\n%{http_code}" \ + --request POST \ + --url "https://circleci.com/api/v2/project/gh/voiceflow/env-release-system/pipeline" \ + --header "Circle-Token: ${CIRCLECI_API_TOKEN}" \ + --header "content-type: application/json" \ + --data "{\"parameters\":{\"e2e_env_name\":\"$env_name\",\"trigger_pipeline\":true}}") + http_status=$(echo "$response" | tail -n1) + response_body=$(echo "$response" | sed '$d') + parse_response "${http_status}" "${response_body}" fi + + vfcli env resume "$env_name" --interactive false + vfcli track attach --branch master --components all --name "$env_name" --interactive false --no-circleci + vfcli pool free-env --env-name "$env_name"