feat: add guarded OCI tenancy infrastructure cleanup
Homelab Main / validate (push) Waiting to run Details
Homelab Main / running-update (push) Blocked by required conditions Details
Homelab Main / debian-ops (push) Blocked by required conditions Details

This commit is contained in:
juvenal.diaz 2026-10-26 14:06:31 -06:00
parent 60a4944283
commit 06825c81de
4 changed files with 128 additions and 2 deletions

View File

@ -39,6 +39,20 @@ If the tenancy has exhausted its NAT gateway quota, set
subnet remains available but does not receive a NAT route until this is
enabled later.
For a deliberate clean-slate infrastructure reset, use the guarded OCI
cleanup command from Debian. It never deletes the tenancy, root compartment,
IAM users, groups, policies, or API keys:
```text
./jeannie oci cleanup plan
./jeannie oci cleanup apply --confirm TENANCY-CLEANUP
```
The first command inventories infrastructure only. The second deletes
instances, volumes, networking, and empty child compartments. Re-run `plan`
after OCI termination completes; IAM remains available for the next Jeannie
foundation run.
For a test alongside an existing foundation, also set
`LAB_OCI_FOUNDATION_STATE_FILE` to a new local state path. This prevents the
test plan from treating the production foundation as resources to destroy.

View File

@ -133,7 +133,7 @@ Observability And GitOps
oci-foundation {status|plan|apply|destroy --yes}
Create the opt-in OCI compartment, network, edge VM, and email VM.
oci-auth {install|init [--force]|check} Install/check OCI CLI and API-key authentication on Debian.
oci {status|doctor} Inspect OCI resources, service limits, and quotas (read-only).
oci {status|doctor|cleanup} Inspect OCI resources or perform guarded infrastructure cleanup.
edge-cache-flush Clear Nginx, Varnish, and Squid edge caches.
promote {plan|validate|rollback APP} Run canary promotion gates and rollback plan.
cert-check Check public DNS, TLS, and edge URL health.
@ -316,7 +316,8 @@ case "${1:-}" in
case "${2:-status}" in
status) oci_status ;;
doctor) oci_doctor ;;
*) echo "Usage: ./jeannie oci {status|doctor}" >&2; exit 2 ;;
cleanup) oci_cleanup "${@:3}" ;;
*) echo "Usage: ./jeannie oci {status|doctor|cleanup plan|cleanup apply --confirm TENANCY-CLEANUP}" >&2; exit 2 ;;
esac
;;
edge-cache-flush)

View File

@ -4004,6 +4004,15 @@ oci_doctor() {
echo 'OCI doctor: PASS'
}
oci_cleanup() {
require_debian_server "oci cleanup"
oci_foundation_load_config
if [[ "${1:-}" == apply ]]; then
record_change_journal "oci-cleanup" "$@"
fi
"${REPO_ROOT}/scripts/oci-cleanup" "$@"
}
storage_status() {
require_debian_server "storage"
local kubeconfig="${KUBECONFIG_PATH}"

102
scripts/oci-cleanup Executable file
View File

@ -0,0 +1,102 @@
#!/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
}
json_ids() {
local command_json="$1"
python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).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 \
| python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).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 | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
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 | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
while IFS= read -r id; do [[ -n "${id}" ]] && block_volumes+=("${compartment}|${id}"); done < <("${oci_bin}" bv volume list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
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 | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))' || true)
while IFS= read -r id; do [[ -n "${id}" ]] && vcns+=("${compartment}|${id}"); done < <("${oci_bin}" network vcn list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
while IFS= read -r id; do [[ -n "${id}" ]] && subnets+=("${compartment}|${id}"); done < <("${oci_bin}" network subnet list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
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 | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
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 | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
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 | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
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 | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
while IFS= read -r id; do [[ -n "${id}" ]] && nsgs+=("${compartment}|${id}"); done < <("${oci_bin}" network nsg list --compartment-id "${compartment}" --all --output json | python3 -c 'import json,sys; print("\\n".join(x["id"] for x in json.load(sys.stdin).get("data", [])))')
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
}
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; 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
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 "${route_tables[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "route table ${id}" "${oci_bin}" network route-table delete --rt-id "${id}" --force; done
for item in "${security_lists[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "security list ${id}" "${oci_bin}" network security-list delete --security-list-id "${id}" --force; done
for item in "${dhcp_options[@]}"; do IFS='|' read -r compartment id <<<"${item}"; delete_resource "DHCP options ${id}" "${oci_bin}" network dhcp-options delete --dhcp-id "${id}" --force; done
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 "${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."