From 6ee4f65a4995f11ef7820b5b41a5b4bb94ae32c7 Mon Sep 17 00:00:00 2001 From: "juvenal.diaz" Date: Thu, 29 Oct 2026 13:20:23 -0600 Subject: [PATCH] Canonicalize OCI foundation state and preserve managed VMs --- lib/jeannie/commands.sh | 49 ++++++++++++++++++++++++++++++++++++++--- 1 file changed, 46 insertions(+), 3 deletions(-) diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 9ec3edb..2810044 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3776,8 +3776,44 @@ oci_foundation_config_file() { } oci_foundation_load_config() { - local file + local file canonical config_dir candidate candidate_state + canonical="${HOME}/.config/homelab/oci-foundation.env" file="$(oci_foundation_config_file)" + + # Keep one canonical config path. Older runs could leave a separate + # *-fresh.env containing the only valid state-file pointer; adopt that + # file as the input source and save the normalized values back to the + # canonical path after reconciliation. An explicitly selected config + # remains authoritative for deliberate side-by-side test stacks. + if [[ -z "${LAB_OCI_FOUNDATION_CONFIG_FILE:-}" && "${file}" == "${canonical}" ]]; then + config_dir="$(dirname "${canonical}")" + candidate_state="" + if [[ -r "${file}" ]]; then + candidate_state="$( + unset LAB_OCI_FOUNDATION_STATE_FILE + # shellcheck disable=SC1090 + source "${file}" + printf '%s' "${LAB_OCI_FOUNDATION_STATE_FILE:-}" + ) 2>/dev/null)" + fi + if [[ -z "${candidate_state}" || ! -f "${candidate_state}" ]]; then + for candidate in "${config_dir}/oci-foundation-fresh.env" "${config_dir}"/oci-foundation-*.env; do + [[ -r "${candidate}" && "${candidate}" != "${file}" ]] || continue + candidate_state="$( + unset LAB_OCI_FOUNDATION_STATE_FILE + # shellcheck disable=SC1090 + source "${candidate}" + printf '%s' "${LAB_OCI_FOUNDATION_STATE_FILE:-}" + ) 2>/dev/null)" + if [[ -n "${candidate_state}" && -f "${candidate_state}" ]]; then + file="${candidate}" + export LAB_OCI_FOUNDATION_CONFIG_FILE="${canonical}" + printf 'Adopting OCI foundation config %s as canonical input.\n' "${candidate}" >&2 + break + fi + done + fi + fi [[ -r "${file}" ]] || return 0 # The file contains only generated parameter-assignment lines using shell # quoting; each assignment preserves an explicitly supplied environment value. @@ -3842,9 +3878,16 @@ oci_foundation_reconcile_existing() { recovered_output="$(tofu -chdir="${REPO_ROOT}/bootstrap/oci" output -state="${homelab_state_file}" -raw compartment_id 2>/dev/null || true)" [[ "${recovered_output}" == ocid1.compartment.* && -z "${LAB_OCI_COMPARTMENT_ID:-}" ]] && export LAB_OCI_COMPARTMENT_ID="${recovered_output}" recovered_output="$(tofu -chdir="${REPO_ROOT}/bootstrap/oci" output -state="${homelab_state_file}" -raw edge_instance_id 2>/dev/null || true)" - [[ "${recovered_output}" == ocid1.instance.* && -z "${LAB_OCI_EDGE_EXISTING_INSTANCE_ID:-}" ]] && export LAB_OCI_EDGE_EXISTING_INSTANCE_ID="${recovered_output}" + # A resource still present in this state must remain managed by its + # resource block. Setting *_EXISTING_INSTANCE_ID for it changes the + # count to zero and makes OpenTofu plan a destructive replacement. + if [[ "${edge_state_managed}" != "true" && "${recovered_output}" == ocid1.instance.* && -z "${LAB_OCI_EDGE_EXISTING_INSTANCE_ID:-}" ]]; then + export LAB_OCI_EDGE_EXISTING_INSTANCE_ID="${recovered_output}" + fi recovered_output="$(tofu -chdir="${REPO_ROOT}/bootstrap/oci" output -state="${homelab_state_file}" -raw email_instance_id 2>/dev/null || true)" - [[ "${recovered_output}" == ocid1.instance.* && -z "${LAB_OCI_EMAIL_EXISTING_INSTANCE_ID:-}" ]] && export LAB_OCI_EMAIL_EXISTING_INSTANCE_ID="${recovered_output}" + if [[ "${email_state_managed}" != "true" && "${recovered_output}" == ocid1.instance.* && -z "${LAB_OCI_EMAIL_EXISTING_INSTANCE_ID:-}" ]]; then + export LAB_OCI_EMAIL_EXISTING_INSTANCE_ID="${recovered_output}" + fi recovered_output="$(tofu -chdir="${REPO_ROOT}/bootstrap/oci" output -state="${homelab_state_file}" -raw bastion_id 2>/dev/null || true)" [[ "${recovered_output}" == ocid1.bastion.* && -z "${LAB_OCI_BASTION_ID:-}" ]] && export LAB_OCI_BASTION_ID="${recovered_output}" [[ -n "${LAB_OCI_EXISTING_VCN_ID:-}" ]] && printf 'Recovered OCI foundation OCIDs from state outputs.\n'