From 548ea2991ff4295ffa33cf8873ed7ebbf1301bad Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Tue, 27 Oct 2026 12:44:22 -0600 Subject: [PATCH] fix: reconcile existing OCI compartments --- lib/jeannie/commands.sh | 40 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 36 insertions(+), 4 deletions(-) diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 9e8b3b1..c47d4f7 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3638,10 +3638,7 @@ oci_foundation_save_config() { } oci_foundation_reconcile_existing() { - local oci_bin="" output_file status - if disabled_value "${LAB_OCI_COMPUTE_ENABLED:-true}"; then - return 0 - fi + local oci_bin="" output_file status compartment_id compartment_name if command -v oci >/dev/null 2>&1; then oci_bin="$(command -v oci)" elif [[ -x "${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci" ]]; then @@ -3650,6 +3647,41 @@ oci_foundation_reconcile_existing() { return 0 fi + # Reuse named child compartments when a previous state file was lost or a + # prior apply completed only partway. OCI rejects a second compartment with + # the same name, so discover the active OCID before Terraform plans. + for status in homelab email; do + case "${status}" in + homelab) + [[ "${LAB_OCI_CREATE_COMPARTMENT:-true}" == "true" ]] || continue + compartment_id="${LAB_OCI_COMPARTMENT_ID:-}" + compartment_name="${LAB_OCI_COMPARTMENT_NAME:-homelab-learning}" + ;; + email) + [[ "${LAB_OCI_EMAIL_CREATE_COMPARTMENT:-true}" == "true" ]] || continue + compartment_id="${LAB_OCI_EMAIL_COMPARTMENT_ID:-}" + compartment_name="${LAB_OCI_EMAIL_COMPARTMENT_NAME:-homelab-email}" + ;; + esac + [[ -n "${compartment_id}" && "${compartment_id}" != "null" ]] || { + compartment_id="$("${oci_bin}" iam compartment list \ + --compartment-id "${LAB_OCI_TENANCY_ID}" \ + --name "${compartment_name}" --all \ + --query 'data[0].id' --raw-output 2>/dev/null || true)" + if [[ -n "${compartment_id}" && "${compartment_id}" != "null" ]]; then + if [[ "${status}" == "homelab" ]]; then + export LAB_OCI_COMPARTMENT_ID="${compartment_id}" + export LAB_OCI_CREATE_COMPARTMENT=false + else + export LAB_OCI_EMAIL_COMPARTMENT_ID="${compartment_id}" + export LAB_OCI_EMAIL_CREATE_COMPARTMENT=false + fi + printf 'Reusing existing OCI %s compartment %s (%s).\n' \ + "${status}" "${compartment_name}" "${compartment_id}" + fi + } + done + [[ -n "${LAB_OCI_EDGE_EXISTING_INSTANCE_ID:-}" ]] || return 0 output_file="$(mktemp)" if ! "${oci_bin}" compute instance get \