diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index f7eb373..f8e25fb 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -8008,6 +8008,8 @@ refresh_oci_edge_endpoint() { local state_bastion_id="" local oci_bin="" local vnic_id="" + local tenancy_id="${LAB_OCI_TENANCY_ID:-}" + local oci_config_file="${OCI_CLI_CONFIG_FILE:-${HOME}/.oci/config}" local instance_compartment="${LAB_OCI_COMPARTMENT_ID:-}" local candidate_compartment="" local candidate_id="" @@ -8050,6 +8052,16 @@ refresh_oci_edge_endpoint() { if [[ -z "${instance_compartment}" ]]; then instance_compartment="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw compartment_id 2>/dev/null || true)" fi + # The remote API is long-lived and may not have received the tenancy + # export that was used by an interactive shell. Recover it from the OCI + # profile so compartment-wide discovery remains self-contained. + if [[ -z "${tenancy_id}" && -r "${oci_config_file}" ]]; then + tenancy_id="$(awk -v profile="[${OCI_CLI_PROFILE:-DEFAULT}]" ' + $0 == profile { in_profile=1; next } + /^\[/ { in_profile=0 } + in_profile && $1 ~ /^tenancy=/ { sub(/^tenancy=/, "", $0); print $0; exit } + ' "${oci_config_file}" 2>/dev/null || true)" + fi # Existing-instance mode can retain an old/null public_ip in state. Query # OCI directly from the instance OCID and read the attached VNIC instead. @@ -8097,7 +8109,7 @@ refresh_oci_edge_endpoint() { # State may have been lost even though the Jeannie-created instance still # exists. Recover only the exact managed identity: display name # homelab-edge plus managed_by=jeannie. Never adopt arbitrary jump boxes. - if [[ ( -n "${LAB_OCI_TENANCY_ID:-}" || -n "${instance_compartment}" ) && + if [[ ( -n "${tenancy_id}" || -n "${instance_compartment}" ) && -z "${LAB_OCI_EDGE_EXISTING_INSTANCE_ID:-}" && ( "${LAB_OCI_EDGE_INSTANCE_MANAGED:-false}" != "true" || "${edge_identity_stale}" == "true" || -z "${discovered_edge_instance_id}" ) ]] && declare -F oci_cli_resolve >/dev/null 2>&1; then @@ -8108,21 +8120,24 @@ refresh_oci_edge_endpoint() { # after the original state was removed. Enumerate the tenancy's # compartments so recovery does not depend on the old compartment # OCID being correct. - if [[ -n "${LAB_OCI_TENANCY_ID:-}" ]]; then + if [[ -n "${tenancy_id}" ]]; then while IFS= read -r candidate_compartment; do [[ -n "${candidate_compartment}" ]] && edge_compartments+=("${candidate_compartment}") done < <("${oci_bin}" iam compartment list \ - --compartment-id "${LAB_OCI_TENANCY_ID}" \ + --compartment-id "${tenancy_id}" \ --compartment-id-in-subtree true --all \ --query 'data[].id' --raw-output 2>/dev/null || true) - edge_compartments+=("${LAB_OCI_TENANCY_ID}") + edge_compartments+=("${tenancy_id}") fi for candidate_compartment in "${edge_compartments[@]}"; do [[ -n "${candidate_compartment}" ]] || continue candidate_id="$("${oci_bin}" compute instance list \ - --compartment-id "${candidate_compartment}" --all \ - --query 'data[?"display-name"==`homelab-edge` && "lifecycle-state"!=`TERMINATED`].id | [0]' \ - --raw-output 2>/dev/null || true)" + --compartment-id "${candidate_compartment}" --all --output json 2>/dev/null \ + | python3 -c 'import json,sys +for item in json.load(sys.stdin).get("data", []): + if item.get("display-name") == "homelab-edge" and item.get("lifecycle-state") != "TERMINATED": + print(item.get("id", "")) + break' 2>/dev/null || true)" if [[ -n "${candidate_id}" && "${candidate_id}" != "null" ]]; then discovered_edge_instance_id="${candidate_id}" instance_compartment="${candidate_compartment}"