fix: reconcile recreated OCI edge SSH endpoint

This commit is contained in:
juvenal.diaz 2026-10-28 11:08:28 -06:00
parent 4ed2ebdf4f
commit a82a65c02c
3 changed files with 50 additions and 1 deletions

View File

@ -3593,7 +3593,7 @@ email_vm_manage() {
oci_foundation_config_names=(
LAB_OCI_FOUNDATION_ENABLED LAB_OCI_REGION LAB_OCI_TENANCY_ID LAB_OCI_ADMIN_CIDR
LAB_OCI_SSH_PUBLIC_KEY LAB_OCI_CREATE_COMPARTMENT LAB_OCI_COMPARTMENT_ID LAB_OCI_COMPARTMENT_NAME
LAB_OCI_SSH_PUBLIC_KEY LAB_OCI_SSH_PRIVATE_KEY LAB_EDGE_SSH_KEY_PATH LAB_OCI_CREATE_COMPARTMENT LAB_OCI_COMPARTMENT_ID LAB_OCI_COMPARTMENT_NAME
LAB_OCI_EDGE_ENABLED LAB_OCI_EMAIL_ENABLED LAB_OCI_COMPUTE_ENABLED LAB_OCI_NAT_ENABLED LAB_OCI_EDGE_EXISTING_INSTANCE_ID
LAB_OCI_EXISTING_VCN_ID LAB_OCI_EXISTING_PUBLIC_SUBNET_ID LAB_OCI_EXISTING_EMAIL_SUBNET_ID
LAB_OCI_EMAIL_CREATE_COMPARTMENT LAB_OCI_EMAIL_COMPARTMENT_ID LAB_OCI_EMAIL_COMPARTMENT_NAME

View File

@ -339,6 +339,16 @@ export_email_vm_tf_vars() {
export_oci_foundation_tf_vars() {
# Empty TF_VAR values override Terraform defaults. Remove stale empty image
# overrides so the foundation's region-appropriate defaults can apply.
local oci_private_key="${LAB_OCI_SSH_PRIVATE_KEY:-${LAB_EDGE_SSH_KEY_PATH:-${HOME}/.ssh/id_ed25519}}"
# Derive the public key from the private key when the caller did not
# provide one. This keeps destroy/recreate cycles on the same SSH identity
# and prevents the edge provisioner from losing access.
if [[ -z "${LAB_OCI_SSH_PUBLIC_KEY:-}" && -r "${oci_private_key}" ]] && command -v ssh-keygen >/dev/null 2>&1; then
LAB_OCI_SSH_PUBLIC_KEY="$(ssh-keygen -y -f "${oci_private_key}" 2>/dev/null || true)"
if [[ -n "${LAB_OCI_SSH_PUBLIC_KEY}" ]]; then
export LAB_OCI_SSH_PUBLIC_KEY
fi
fi
if [[ -z "${LAB_OCI_EDGE_IMAGE_ID:-}" && -z "${TF_VAR_edge_image_id:-}" ]]; then
unset TF_VAR_edge_image_id
fi

View File

@ -224,6 +224,10 @@ export_homelab_inventory_tf_vars() {
export_if_unset TF_VAR_pimox_template_guest_ip_prefix "${LAB_LAN_IP_PREFIX:-}"
export_if_unset TF_VAR_edge_host "${LAB_EDGE_HOST:-}"
export_if_unset TF_VAR_edge_user "${LAB_EDGE_USER:-}"
# OCI foundation installs the public half of this key on recreated edge
# instances. Keep the edge provisioner on the matching private key rather
# than silently falling back to a stale/default path.
export_if_unset TF_VAR_edge_ssh_key_path "${LAB_EDGE_SSH_KEY_PATH:-${LAB_OCI_SSH_PRIVATE_KEY:-}}"
export_if_unset TF_VAR_edge_install_dir "${LAB_EDGE_INSTALL_DIR:-}"
local edge_tailscale_auth_key="${LAB_EDGE_TAILSCALE_AUTH_KEY:-}"
if [[ -z "${edge_tailscale_auth_key}" ]]; then
@ -7967,6 +7971,12 @@ platform_apply() {
edge_apply() {
require_debian_server "edge"
local configured_edge_key="${LAB_EDGE_SSH_KEY_PATH:-${LAB_OCI_SSH_PRIVATE_KEY:-}}"
if [[ -n "${configured_edge_key}" ]]; then
export TF_VAR_edge_ssh_key_path="${configured_edge_key}"
fi
refresh_oci_edge_endpoint
run_tofu_stack "bootstrap/edge"
local edge_host="${TF_VAR_edge_host:-${LAB_EDGE_HOST:-}}"
if [[ -n "${edge_host}" ]] && declare -F update_duckdns_edge_ip >/dev/null 2>&1; then
@ -7975,6 +7985,35 @@ edge_apply() {
edge_flush_caches
}
refresh_oci_edge_endpoint() {
local stack="${REPO_ROOT}/bootstrap/oci"
local state_file="${LAB_OCI_FOUNDATION_STATE_FILE:-}"
local -a tofu_state_args=()
local discovered_edge_ip=""
# A recreated OCI instance can receive a new public IP. Prefer the
# foundation output over inventory so a standalone `edge` run is
# idempotent instead of trying the previous address.
if [[ -n "${state_file}" ]]; then
tofu_state_args=("-state=${state_file}")
fi
if [[ ! -d "${stack}" ]] || ! command -v tofu >/dev/null 2>&1; then
return 0
fi
discovered_edge_ip="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_public_ip 2>/dev/null || true)"
if [[ -z "${discovered_edge_ip}" || "${discovered_edge_ip}" == "null" ]]; then
return 0
fi
if [[ "${LAB_EDGE_HOST:-}" != "${discovered_edge_ip}" ]]; then
printf 'Refreshing OCI edge endpoint: %s -> %s\n' "${LAB_EDGE_HOST:-unset}" "${discovered_edge_ip}"
fi
export LAB_EDGE_HOST="${discovered_edge_ip}"
export TF_VAR_edge_host="${discovered_edge_ip}"
if declare -F oci_foundation_save_config >/dev/null 2>&1; then
oci_foundation_save_config
fi
}
edge_flush_caches() {
local edge_host="${TF_VAR_edge_host:-${LAB_EDGE_HOST:-}}"
local edge_user="${TF_VAR_edge_user:-${LAB_EDGE_USER:-ubuntu}}"