Skip to content

Commit

Permalink
Merge pull request #3549 from alphagov/improve-decommissioning-query-…
Browse files Browse the repository at this point in the history
…script

Add service checking, usage info and org filtering to decommission query script
  • Loading branch information
Jamie Van Dyke committed Feb 29, 2024
2 parents 8c0d443 + 768cdf8 commit aa00152
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 6 deletions.
12 changes: 9 additions & 3 deletions scripts/decommission_organisation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,16 @@
require "json"
require "optparse"

@errors = []

# Helpers
def puts_ok(msg)
puts "✅ #{msg}"
end

def puts_err(msg)
puts "❌ #{msg}"
@errors << msg
end

def puts_dry_run(msg)
Expand Down Expand Up @@ -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

Expand Down Expand Up @@ -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
Expand Down
54 changes: 51 additions & 3 deletions scripts/get-decommission-details.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
#!/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.
#
# 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

Expand All @@ -15,14 +37,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')
Expand All @@ -46,6 +84,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:
Expand All @@ -59,6 +98,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

0 comments on commit aa00152

Please sign in to comment.