From 41d467546c6c42b9ecf3e8b5d0c93e74a9577226 Mon Sep 17 00:00:00 2001 From: Jamie Van Dyke Date: Fri, 22 Dec 2023 11:18:54 +0000 Subject: [PATCH 1/3] add a decommission status script that gathers all info for orgs --- scripts/decommission_organisation.rb | 12 ++++++++--- scripts/get-decommission-details.sh | 32 +++++++++++++++++++++++++--- 2 files changed, 38 insertions(+), 6 deletions(-) diff --git a/scripts/decommission_organisation.rb b/scripts/decommission_organisation.rb index fed75e2544..7a1ba60809 100755 --- a/scripts/decommission_organisation.rb +++ b/scripts/decommission_organisation.rb @@ -6,6 +6,8 @@ require "json" require "optparse" +@errors = [] + # Helpers def puts_ok(msg) puts "✅ #{msg}" @@ -13,6 +15,7 @@ def puts_ok(msg) def puts_err(msg) puts "❌ #{msg}" + @errors << msg end def puts_dry_run(msg) @@ -100,7 +103,7 @@ def try_remove_role(role_guid) end opts.on("--dry-run", TrueClass) do |dry_run| - puts "Dry run? #{dry_run}" + puts CLI::UI.fmt "Dry run? #{dry_run ? '{{green:Yes}}' : '{{red:No}}}'}" options[:dry_run] = dry_run end @@ -173,11 +176,14 @@ def try_remove_role(role_guid) unless can_decommission puts "\n" - puts "Cannot decommission org '#{options[:org]}'. It may have one or more apps, service instances, and spaces, or it may not be suspended" + puts CLI::UI.fmt "{{red:BLOCKED:}} Cannot decommission org '#{options[:org]}'." + @errors.each do |err| + puts CLI::UI.fmt " -> {{red:#{err}}}" + end exit 1 end -continue = CLI::UI::Prompt.confirm("Continue to decomission org '#{options[:org]}'?") +continue = CLI::UI::Prompt.confirm(CLI::UI.fmt("Continue to decommission org '{{bold:#{options[:org]}}}'?")) unless continue exit 1 end diff --git a/scripts/get-decommission-details.sh b/scripts/get-decommission-details.sh index 6bf0f11663..b5917914fe 100755 --- a/scripts/get-decommission-details.sh +++ b/scripts/get-decommission-details.sh @@ -15,14 +15,30 @@ if ! cf target > /dev/null 2>&1; then exit 1 fi +optional_orgs=$1 + +if [ "$optional_orgs" = "help" ] || [ "$optional_orgs" = "--help" ] || [ "$optional_orgs" = "-h" ]; then + echo "Usage: $0 [comma separated list of org names]" + echo "If no orgs are specified, all orgs will be checked" + exit 0 +fi + +if [ -n "$optional_orgs" ]; then + echo "Filtering to a comma separated list of orgs: $optional_orgs" +fi + # Prepare output: -echo "organization_name,running_app_count,non_running_app_count,owner,ready_for_decommission" +echo "organization_name,running_app_count,non_running_app_count,service_count,owner,ready_for_decommission,suspended" declare -A orgs declare -A owners # Get organizations: -orgs_query=$(cf curl "/v3/organizations?per_page=5000&order_by=name" | jq -rc ".resources[] | {guid: .guid, name: .name, owner: .metadata.annotations.owner}") +if [ -n "$optional_orgs" ]; then + orgs_query=$(cf curl "/v3/organizations?names=$optional_orgs&per_page=5000&order_by=name" | jq -rc '.resources[] | {guid: .guid, name: .name, owner: .metadata.annotations.owner}') +else + orgs_query=$(cf curl "/v3/organizations?per_page=5000&order_by=name" | jq -rc '.resources[] | {guid: .guid, name: .name, owner: .metadata.annotations.owner}') +fi while IFS= read -r line; do org_guid=$(echo "$line" | jq -r '.guid') @@ -46,6 +62,7 @@ for org_guid in "${!orgs[@]}"; do continue fi + suspended=$(cf curl "/v3/organizations/$org_guid" | jq -r '.suspended') apps=$(cf curl "/v3/apps?organization_guids=$org_guid&per_page=5000") # Count running and non-running apps: @@ -59,6 +76,15 @@ for org_guid in "${!orgs[@]}"; do ready_for_decommission=no fi + running_services=$(cf curl "/v3/service_instances?organization_guids=$org_guid&per_page=5000" | jq '.pagination.total_results') + + # ready for decommissioning if both counts are zero + if [[ $running_services != 0 ]]; then + if [[ $ready_for_decommission == yes ]]; then + ready_for_decommission=no + fi + fi + # Append result: - echo "$org_name,$running_app_count,$non_running_app_count,\"$org_owner\",$ready_for_decommission" + echo "$org_name,$running_app_count,$non_running_app_count,$running_services,\"$org_owner\",$ready_for_decommission,$suspended" done From 1bc401977854b1955ffc0d82f4c27df1f04dd50b Mon Sep 17 00:00:00 2001 From: Jamie Van Dyke Date: Thu, 29 Feb 2024 11:28:07 +0000 Subject: [PATCH 2/3] script information added --- scripts/get-decommission-details.sh | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/scripts/get-decommission-details.sh b/scripts/get-decommission-details.sh index b5917914fe..799b8a74ad 100755 --- a/scripts/get-decommission-details.sh +++ b/scripts/get-decommission-details.sh @@ -1,4 +1,19 @@ #!/usr/bin/env bash +# +# This script gathers information on all orgs in the current CF environment. +# Use it to find orgs that are ready for decommissioning, and orgs that already are. +# It will output a CSV with the following columns: +# +# - organization_name +# - running_app_count +# - non_running_app_count +# - service_count +# - owner +# - ready_for_decommission +# - suspended +# +# The ready_for_decommission column will be "yes" if there are no running or non-running apps, and no services. +# The suspended column will be "true" if the org is suspended, and "false" if it is not. set -e From 768cdf8b95edb34eee6cd38915a277bac4286109 Mon Sep 17 00:00:00 2001 From: Jamie Van Dyke Date: Thu, 29 Feb 2024 11:32:02 +0000 Subject: [PATCH 3/3] add a little more help usage --- scripts/get-decommission-details.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/scripts/get-decommission-details.sh b/scripts/get-decommission-details.sh index 799b8a74ad..2b55ed4fc8 100755 --- a/scripts/get-decommission-details.sh +++ b/scripts/get-decommission-details.sh @@ -14,6 +14,13 @@ # # The ready_for_decommission column will be "yes" if there are no running or non-running apps, and no services. # The suspended column will be "true" if the org is suspended, and "false" if it is not. +# +# I run it like this: +# +# cf login -a api.london.cloud.service.gov.uk --sso +# ./scripts/get-decommission-details.sh > ~/Desktop/status.csv +# cf login -a api.cloud.service.gov.uk --sso +# ./scripts/get-decommission-details.sh >> ~/Desktop/status.csv set -e