diff --git a/bootstrap/oci/README.md b/bootstrap/oci/README.md index 4f396d1..554b19e 100644 --- a/bootstrap/oci/README.md +++ b/bootstrap/oci/README.md @@ -28,3 +28,19 @@ At minimum, set `LAB_OCI_FOUNDATION_ENABLED=true`, `LAB_OCI_REGION`, the image and availability-domain variables for both VMs. Use `LAB_OCI_CREATE_COMPARTMENT=false` with `LAB_OCI_COMPARTMENT_ID` when the tenancy policy does not allow creating child compartments. + +After a successful apply, Jeannie persists the discovered edge IP and uses it +as the next-run edge target instead of the static inventory value. To update +DuckDNS automatically when OCI assigns a replacement public IP, provide the +token outside Git and list the DuckDNS subdomain labels to update: + +```bash +export LAB_DUCKDNS_TOKEN='REDACTED_DUCKDNS_TOKEN' +export LAB_DUCKDNS_DOMAINS='lab2025,api,auth,grafana,office,docs' +``` + +The update is enabled by default when a token is present. Set +`LAB_OCI_AUTO_UPDATE_DUCKDNS=false` to disable it. The email VM baseline +(Postfix, Dovecot, fail2ban, UFW, and node-exporter) is also installed +automatically after the new VM accepts SSH; set +`LAB_OCI_AUTO_CONFIGURE_EMAIL=false` to defer that step. diff --git a/jeannie b/jeannie index 020b530..77bbcd2 100755 --- a/jeannie +++ b/jeannie @@ -32,6 +32,9 @@ source "${REPO_ROOT}/lib/jeannie/commands.sh" trap 'rm -f "${BUILDX_CONFIG}"' EXIT +# Load persisted OCI discoveries before inventory defaults so a replacement +# edge VM's public IP takes precedence over the static bootstrap inventory. +oci_foundation_load_config load_homelab_inventory_defaults export_homelab_inventory_tf_vars KUBECONFIG_PATH="${KUBECONFIG_PATH:-${TF_VAR_kubeconfig_path:-${LAB_KUBECONFIG_PATH:-/home/jv/.kube/config}}}" diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index 29e76ae..1debd5b 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3599,6 +3599,7 @@ oci_foundation_config_names=( LAB_OCI_EMAIL_CREATE_COMPARTMENT LAB_OCI_EMAIL_COMPARTMENT_ID LAB_OCI_EMAIL_COMPARTMENT_NAME LAB_OCI_EMAIL_SUBNET_CIDR LAB_OCI_EDGE_AVAILABILITY_DOMAIN LAB_OCI_EMAIL_AVAILABILITY_DOMAIN LAB_OCI_OPEN_MAIL_PORTS LAB_OCI_AUTO_CONFIGURE_EDGE LAB_EDGE_HOST + LAB_OCI_AUTO_CONFIGURE_EMAIL LAB_OCI_AUTO_UPDATE_DUCKDNS LAB_DUCKDNS_DOMAINS LAB_OCI_MONITORING_ENABLED LAB_OCI_MONITORING_CIDR LAB_EMAIL_PUBLIC_IP LAB_OCI_EDGE_METRICS_TARGET LAB_OCI_EMAIL_METRICS_TARGET ) @@ -3689,6 +3690,61 @@ oci_foundation_reconcile_existing() { done } +oci_wait_for_ssh() { + local target="$1" + local key_path="$2" + local timeout_seconds="${3:-180}" + local elapsed=0 + + while ((elapsed < timeout_seconds)); do + if ssh -i "${key_path}" -o BatchMode=yes -o ConnectTimeout=5 \ + -o StrictHostKeyChecking=accept-new "${target}" true >/dev/null 2>&1; then + return 0 + fi + sleep 5 + elapsed=$((elapsed + 5)) + done + echo "Timed out waiting for SSH on ${target} after ${timeout_seconds}s." >&2 + return 1 +} + +update_duckdns_edge_ip() { + local edge_ip="$1" + local token="${LAB_DUCKDNS_TOKEN:-}" + local domains="${LAB_DUCKDNS_DOMAINS:-}" + local response + + if disabled_value "${LAB_OCI_AUTO_UPDATE_DUCKDNS:-true}"; then + echo "DuckDNS update disabled by LAB_OCI_AUTO_UPDATE_DUCKDNS." + return 0 + fi + if [[ -z "${token}" ]]; then + echo "DuckDNS update skipped: LAB_DUCKDNS_TOKEN is not set." + return 0 + fi + if [[ -z "${domains}" ]]; then + domains="${LAB_DOMAIN%%.*}" + fi + command -v curl >/dev/null 2>&1 || { + echo "curl is required for the DuckDNS update." >&2 + return 1 + } + + echo "Updating DuckDNS domains ${domains} to edge IP ${edge_ip}..." + response="$(curl -fsS --get 'https://www.duckdns.org/update' \ + --data-urlencode "domains=${domains}" \ + --data-urlencode "token=${token}" \ + --data-urlencode "ip=${edge_ip}")" || { + echo "DuckDNS update request failed." >&2 + return 1 + } + if [[ "${response}" != OK* ]]; then + echo "DuckDNS rejected the update: ${response}" >&2 + return 1 + fi + echo "DuckDNS update succeeded." +} + oci_foundation_manage() { require_debian_server "oci-foundation" oci_foundation_load_config @@ -3728,14 +3784,16 @@ oci_foundation_manage() { tofu -chdir="${stack}" apply "$@" -auto-approve local discovered_edge_ip local discovered_email_ip + local post_apply_failures=0 discovered_edge_ip="$(tofu -chdir="${stack}" output -raw edge_public_ip 2>/dev/null || true)" discovered_email_ip="$(tofu -chdir="${stack}" output -raw email_public_ip 2>/dev/null || true)" if [[ -n "${discovered_edge_ip}" && "${discovered_edge_ip}" != "null" ]]; then export LAB_EDGE_HOST="${discovered_edge_ip}" export TF_VAR_edge_host="${discovered_edge_ip}" + update_duckdns_edge_ip "${discovered_edge_ip}" || post_apply_failures=$((post_apply_failures + 1)) if [[ "${LAB_OCI_AUTO_CONFIGURE_EDGE:-true}" == "true" ]]; then printf 'Configuring edge services at %s...\n' "${discovered_edge_ip}" - edge_apply + edge_apply || post_apply_failures=$((post_apply_failures + 1)) else printf 'Edge services not configured automatically. LAB_EDGE_HOST=%s\n' "${discovered_edge_ip}" fi @@ -3743,11 +3801,27 @@ oci_foundation_manage() { if [[ -n "${discovered_email_ip}" && "${discovered_email_ip}" != "null" ]]; then export LAB_EMAIL_PUBLIC_IP="${discovered_email_ip}" export LAB_OCI_EMAIL_METRICS_TARGET="${discovered_email_ip}:9100" + if truthy "${LAB_OCI_AUTO_CONFIGURE_EMAIL:-true}"; then + local email_key="${LAB_EMAIL_SSH_PRIVATE_KEY:-${HOME}/.ssh/id_ed25519}" + local email_user="${LAB_EMAIL_SSH_USER:-ubuntu}" + printf 'Waiting for email VM SSH at %s@%s...\n' "${email_user}" "${discovered_email_ip}" + if oci_wait_for_ssh "${email_user}@${discovered_email_ip}" "${email_key}" "${LAB_OCI_EMAIL_SSH_WAIT_SECONDS:-180}"; then + LAB_EMAIL_PUBLIC_IP="${discovered_email_ip}" "${REPO_ROOT}/scripts/email-server" install || post_apply_failures=$((post_apply_failures + 1)) + else + post_apply_failures=$((post_apply_failures + 1)) + fi + else + echo "Email baseline not configured automatically. Run ./jeannie email-vm setup." + fi fi if [[ -n "${discovered_edge_ip}" && "${discovered_edge_ip}" != "null" ]]; then export LAB_OCI_EDGE_METRICS_TARGET="${discovered_edge_ip}:9100" fi oci_foundation_save_config + if ((post_apply_failures > 0)); then + echo "OCI foundation was created, but ${post_apply_failures} post-apply configuration step(s) failed." >&2 + return 1 + fi fi ;; destroy)