my-homelab-configs/scripts/oci-cleanup

124 lines
8.1 KiB
Bash
Executable File

#!/usr/bin/env bash
set -euo pipefail
# Destructive OCI infrastructure cleanup. IAM users, groups, policies, keys,
# tenancy, and the root compartment are intentionally never touched.
action="${1:-plan}"
confirm="${2:-}"
oci_bin="${OCI_CLI_BIN:-}"
if [[ -z "${oci_bin}" ]]; then
if command -v oci >/dev/null 2>&1; then
oci_bin="$(command -v oci)"
else
oci_bin="${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci"
fi
fi
[[ -x "${oci_bin}" ]] || { echo "OCI CLI is not installed; run ./jeannie oci-auth install." >&2; exit 2; }
tenancy="${LAB_OCI_TENANCY_ID:-}"
region="${LAB_OCI_REGION:-${OCI_CLI_REGION:-}}"
[[ -n "${tenancy}" && -n "${region}" ]] || {
echo "Set LAB_OCI_TENANCY_ID and LAB_OCI_REGION first." >&2
exit 2
}
oci_json_ids() {
local command_json="$(cat)"
python3 -c 'import json,sys; raw=sys.stdin.read().strip(); data=json.loads(raw) if raw else {}; print("\n".join(x["id"] for x in data.get("data", [])))' <<<"${command_json}"
}
mapfile -t compartments < <(
printf '%s\n' "${tenancy}"
"${oci_bin}" iam compartment list --compartment-id "${tenancy}" \
--compartment-id-in-subtree true --all --output json 2>/dev/null \
| python3 -c 'import json,sys; raw=sys.stdin.read().strip(); data=json.loads(raw) if raw else {}; print("\n".join(x["id"] for x in data.get("data", [])))'
)
declare -a instances=() boot_volumes=() block_volumes=() public_ips=() vcns=()
declare -a subnets=() gateways=() route_tables=() security_lists=() dhcp_options=() nsgs=()
for compartment in "${compartments[@]}"; do
[[ -n "${compartment}" ]] || continue
while IFS= read -r id; do [[ -n "${id}" ]] && instances+=("${compartment}|${id}"); done < <("${oci_bin}" compute instance list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
while IFS= read -r id; do [[ -n "${id}" ]] && boot_volumes+=("${compartment}|${id}"); done < <("${oci_bin}" bv boot-volume list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
while IFS= read -r id; do [[ -n "${id}" ]] && block_volumes+=("${compartment}|${id}"); done < <("${oci_bin}" bv volume list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
while IFS= read -r id; do [[ -n "${id}" ]] && public_ips+=("${compartment}|${id}"); done < <("${oci_bin}" network public-ip list --compartment-id "${compartment}" --scope REGION --region "${region}" --all --output json 2>/dev/null | oci_json_ids || true)
while IFS= read -r id; do [[ -n "${id}" ]] && vcns+=("${compartment}|${id}"); done < <("${oci_bin}" network vcn list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
while IFS= read -r id; do [[ -n "${id}" ]] && subnets+=("${compartment}|${id}"); done < <("${oci_bin}" network subnet list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
for resource in internet-gateway nat-gateway service-gateway local-peering-gateway; do
while IFS= read -r id; do [[ -n "${id}" ]] && gateways+=("${resource}|${compartment}|${id}"); done < <("${oci_bin}" network "${resource}" list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
done
while IFS= read -r id; do [[ -n "${id}" ]] && route_tables+=("${compartment}|${id}"); done < <("${oci_bin}" network route-table list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
while IFS= read -r id; do [[ -n "${id}" ]] && security_lists+=("${compartment}|${id}"); done < <("${oci_bin}" network security-list list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
while IFS= read -r id; do [[ -n "${id}" ]] && dhcp_options+=("${compartment}|${id}"); done < <("${oci_bin}" network dhcp-options list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
while IFS= read -r id; do [[ -n "${id}" ]] && nsgs+=("${compartment}|${id}"); done < <("${oci_bin}" network nsg list --compartment-id "${compartment}" --all --output json 2>/dev/null | oci_json_ids)
done
echo "OCI infrastructure cleanup (${action})"
echo "Preserved: tenancy, root compartment, IAM users/groups/policies/API keys."
printf 'compartments=%d instances=%d boot_volumes=%d block_volumes=%d public_ips=%d vcns=%d subnets=%d\n' "${#compartments[@]}" "${#instances[@]}" "${#boot_volumes[@]}" "${#block_volumes[@]}" "${#public_ips[@]}" "${#vcns[@]}" "${#subnets[@]}"
if [[ "${action}" == plan ]]; then
printf '%s\n' "Re-run with: ./jeannie oci cleanup apply --confirm TENANCY-CLEANUP"
exit 0
fi
if [[ "${action}" != apply || "${confirm}" != --confirm || "${3:-}" != TENANCY-CLEANUP ]]; then
echo "Refusing destructive cleanup. Required: apply --confirm TENANCY-CLEANUP" >&2
exit 2
fi
delete_resource() {
local label="$1"; shift
echo "Deleting ${label}..."
"$@" || echo "WARN: failed to delete ${label}; rerun cleanup after dependencies settle." >&2
}
wait_instance_terminated() {
local id="$1" state="" attempt
for attempt in {1..36}; do
state="$("${oci_bin}" compute instance get --instance-id "${id}" --query 'data."lifecycle-state"' --raw-output 2>/dev/null || true)"
[[ "${state}" == "TERMINATED" || -z "${state}" ]] && return 0
sleep 5
done
echo "WARN: instance ${id} is still ${state}; dependent VNICs may block subnet deletion." >&2
}
for item in "${instances[@]}"; do
IFS='|' read -r compartment id <<<"${item}"
delete_resource "instance ${id}" "${oci_bin}" compute instance terminate --instance-id "${id}" --preserve-boot-volume false --force
wait_instance_terminated "${id}"
done
for item in "${subnets[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "subnet ${id}" "${oci_bin}" network subnet delete --subnet-id "${id}" --force; done
# OCI does not allow deletion of default route tables, security lists, or DHCP
# options. Clear route rules so gateways can be removed, delete only custom
# route tables, and let VCN deletion remove the default objects.
for item in "${route_tables[@]}"; do
IFS='|' read -r compartment id <<<"${item}"
delete_resource "route rules ${id}" "${oci_bin}" network route-table update --rt-id "${id}" --route-rules '[]'
is_default="$("${oci_bin}" network route-table get --rt-id "${id}" --query 'data."is-default"' --raw-output 2>/dev/null || true)"
[[ "${is_default}" == true ]] || delete_resource "route table ${id}" "${oci_bin}" network route-table delete --rt-id "${id}" --force
done
echo "Skipping direct deletion of security lists and DHCP options; OCI removes them with their VCN."
for item in "${nsgs[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "NSG ${id}" "${oci_bin}" network nsg delete --nsg-id "${id}" --force; done
for item in "${gateways[@]}"; do
IFS='|' read -r resource compartment id <<<"${item}"
case "${resource}" in
internet-gateway) flag=ig-id ;;
nat-gateway) flag=nat-id ;;
service-gateway) flag=service-gateway-id ;;
local-peering-gateway) flag=local-peering-gateway-id ;;
esac
delete_resource "${resource} ${id}" "${oci_bin}" network "${resource}" delete "--${flag}" "${id}" --force
done
for item in "${public_ips[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "public IP ${id}" "${oci_bin}" network public-ip delete --public-ip-id "${id}" --force; done
for item in "${block_volumes[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "block volume ${id}" "${oci_bin}" bv volume delete --volume-id "${id}" --force; done
for item in "${boot_volumes[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "boot volume ${id}" "${oci_bin}" bv boot-volume delete --boot-volume-id "${id}" --force; done
for item in "${vcns[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "VCN ${id}" "${oci_bin}" network vcn delete --vcn-id "${id}" --force; done
# Delete only empty child compartments; the tenancy/root compartment is never
# included. A second cleanup run handles resources that were still terminating.
for ((i=${#compartments[@]}-1; i>0; i--)); do
compartment="${compartments[$i]}"
delete_resource "compartment ${compartment}" "${oci_bin}" iam compartment delete --compartment-id "${compartment}" --force
done
echo "Cleanup requested. Re-run './jeannie oci cleanup plan' after termination settles."