Recover stale OCI edge identities safely
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-28 20:55:43 -06:00
parent 95f59e6771
commit 69a29ac036
2 changed files with 36 additions and 13 deletions

View File

@ -3753,7 +3753,7 @@ oci_foundation_reconcile_existing() {
--compartment-id "${bastion_compartment_id}" --all \
--query "data[?name==\`${LAB_OCI_BASTION_NAME:-homelab-bastion}\`].id | [0]" \
--raw-output 2>/dev/null || true)"
if [[ -n "${discovered_bastion_id}" && "${discovered_bastion_id}" != "null" ]]; then
if [[ "${discovered_bastion_id}" == ocid1.bastion.* ]]; then
export LAB_OCI_BASTION_ID="${discovered_bastion_id}"
printf 'Reusing existing OCI Bastion (%s).\n' "${discovered_bastion_id}"
elif [[ "${LAB_OCI_BASTION_NAME:-homelab-bastion}" == "homelab-bastion" && -n "${LAB_OCI_COMPARTMENT_ID:-}" ]]; then
@ -3971,10 +3971,10 @@ oci_foundation_manage() {
discovered_backup_bucket_namespace="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw backup_bucket_namespace 2>/dev/null || true)"
discovered_backup_bucket_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw backup_bucket_id 2>/dev/null || true)"
discovered_edge_instance_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_instance_id 2>/dev/null || true)"
if [[ -n "${discovered_bastion_id}" && "${discovered_bastion_id}" != "null" ]]; then
if [[ "${discovered_bastion_id}" == ocid1.bastion.* ]]; then
export LAB_OCI_BASTION_ID="${discovered_bastion_id}"
fi
if [[ -n "${discovered_edge_instance_id}" && "${discovered_edge_instance_id}" != "null" ]]; then
if [[ "${discovered_edge_instance_id}" == ocid1.instance.* ]]; then
export LAB_OCI_EDGE_INSTANCE_ID="${discovered_edge_instance_id}"
if [[ "${LAB_OCI_COMPUTE_ENABLED:-true}" == "true" && -z "${LAB_OCI_EDGE_EXISTING_INSTANCE_ID:-}" ]]; then
export LAB_OCI_EDGE_INSTANCE_MANAGED=true
@ -3993,13 +3993,13 @@ oci_foundation_manage() {
export LAB_OCI_BACKUP_BUCKET_ID="${discovered_backup_bucket_id}"
fi
if truthy "${LAB_OCI_BASTION_ENABLED:-true}"; then
if [[ -n "${discovered_edge_instance_id}" && "${discovered_edge_instance_id}" != "null" ]]; then
if [[ "${discovered_edge_instance_id}" == ocid1.instance.* ]]; then
printf 'Enabling OCI Bastion plugin on edge instance...\n'
oci_enable_bastion_plugin "${discovered_edge_instance_id}" || post_apply_failures=$((post_apply_failures + 1))
fi
local discovered_email_instance_id
discovered_email_instance_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw email_instance_id 2>/dev/null || true)"
if [[ -n "${discovered_email_instance_id}" && "${discovered_email_instance_id}" != "null" ]]; then
if [[ "${discovered_email_instance_id}" == ocid1.instance.* ]]; then
printf 'Enabling OCI Bastion plugin on email instance...\n'
oci_enable_bastion_plugin "${discovered_email_instance_id}" || post_apply_failures=$((post_apply_failures + 1))
fi

View File

@ -7981,14 +7981,14 @@ edge_apply() {
if [[ -n "${configured_edge_key}" ]]; then
export TF_VAR_edge_ssh_key_path="${configured_edge_key}"
fi
refresh_oci_edge_endpoint
refresh_oci_edge_endpoint || return 1
refresh_oci_edge_host_key
if [[ -n "${LAB_OCI_BASTION_ID:-}" && -n "${LAB_OCI_EDGE_INSTANCE_ID:-}" &&
"${LAB_OCI_BASTION_ENABLED:-true}" != "false" ]] &&
declare -F oci_enable_bastion_plugin >/dev/null 2>&1; then
oci_enable_bastion_plugin "${LAB_OCI_EDGE_INSTANCE_ID}"
oci_enable_bastion_plugin "${LAB_OCI_EDGE_INSTANCE_ID}" || return 1
fi
prepare_oci_edge_bastion
prepare_oci_edge_bastion || return 1
run_tofu_stack "bootstrap/edge"
local edge_host="${TF_VAR_edge_host:-${LAB_EDGE_HOST:-}}"
@ -8005,11 +8005,13 @@ refresh_oci_edge_endpoint() {
local discovered_edge_ip=""
local discovered_edge_private_ip=""
local discovered_edge_instance_id=""
local state_bastion_id=""
local oci_bin=""
local vnic_id=""
local instance_compartment="${LAB_OCI_COMPARTMENT_ID:-}"
local candidate_compartment=""
local candidate_id=""
local edge_identity_stale=false
# These are derived per run; never retain a proxy/private address from a
# previous long-lived API invocation.
@ -8028,9 +8030,11 @@ refresh_oci_edge_endpoint() {
fi
discovered_edge_ip="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_public_ip 2>/dev/null || true)"
discovered_edge_instance_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_instance_id 2>/dev/null || true)"
if [[ -z "${LAB_OCI_BASTION_ID:-}" ]]; then
LAB_OCI_BASTION_ID="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw bastion_id 2>/dev/null || true)"
[[ -n "${LAB_OCI_BASTION_ID}" && "${LAB_OCI_BASTION_ID}" != "null" ]] && export LAB_OCI_BASTION_ID
state_bastion_id="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw bastion_id 2>/dev/null || true)"
if [[ "${state_bastion_id}" == ocid1.bastion.* ]]; then
export LAB_OCI_BASTION_ID="${state_bastion_id}"
elif [[ "${LAB_OCI_BASTION_ID:-}" != ocid1.bastion.* ]]; then
unset LAB_OCI_BASTION_ID
fi
# The API service may not inherit the interactive shell's OCI compartment
# variables. Recover the managed compartment from foundation state so a
@ -8065,15 +8069,29 @@ refresh_oci_edge_endpoint() {
discovered_edge_private_ip="$("${oci_bin}" network vnic get \
--vnic-id "${vnic_id}" \
--query 'data."private-ip"' --raw-output 2>/dev/null || true)"
else
edge_identity_stale=true
fi
fi
fi
# A state file or persisted config can retain an OCID for an instance that
# was destroyed outside OpenTofu. Clear that identity before managed-edge
# adoption so the current Jeannie-tagged instance can be found.
if [[ "${edge_identity_stale}" == "true" ]]; then
printf 'Persisted OCI edge instance %s is absent; rediscovering the Jeannie-managed edge.\n' \
"${discovered_edge_instance_id}" >&2
discovered_edge_instance_id=""
discovered_edge_ip=""
discovered_edge_private_ip=""
fi
# 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}" ) &&
"${LAB_OCI_EDGE_INSTANCE_MANAGED:-false}" != "true" ]] &&
-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
oci_bin="${oci_bin:-$(oci_cli_resolve 2>/dev/null || true)}"
if [[ -n "${oci_bin}" ]]; then
@ -8128,7 +8146,7 @@ refresh_oci_edge_endpoint() {
else
unset TF_VAR_edge_ssh_host LAB_OCI_EDGE_PRIVATE_IP
fi
if [[ -n "${discovered_edge_instance_id}" && "${discovered_edge_instance_id}" != "null" ]]; then
if [[ "${discovered_edge_instance_id}" == ocid1.instance.* ]]; then
export LAB_OCI_EDGE_INSTANCE_ID="${discovered_edge_instance_id}"
fi
if declare -F oci_foundation_save_config >/dev/null 2>&1; then
@ -8338,6 +8356,11 @@ ensure_edge_tailscale_routes() {
echo "LAB_EDGE_HOST or TF_VAR_edge_host is required to configure edge Tailscale routes." >&2
exit 1
fi
if truthy "${LAB_OCI_BASTION_ENABLED:-false}" && [[ -n "${LAB_OCI_BASTION_ID:-}" ]] &&
[[ -z "${TF_VAR_edge_ssh_host:-}" || -z "${LAB_OCI_EDGE_BASTION_PROXY_COMMAND:-}" ]]; then
echo "OCI Bastion is enabled but no private edge SSH route/session is ready; refusing public SSH fallback." >&2
exit 1
fi
case "${router}" in
debian)