diff --git a/bootstrap/oci/README.md b/bootstrap/oci/README.md index fcb88be..75c9d9c 100644 --- a/bootstrap/oci/README.md +++ b/bootstrap/oci/README.md @@ -34,6 +34,10 @@ Always Free compute slot, set `LAB_OCI_COMPUTE_ENABLED=false`. Edge/email subnets and security groups are still created; the VM resources are deferred until the variable is set back to `true`. +For a test alongside an existing foundation, also set +`LAB_OCI_FOUNDATION_STATE_FILE` to a new local state path. This prevents the +test plan from treating the production foundation as resources to destroy. + 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 diff --git a/lib/jeannie/commands.sh b/lib/jeannie/commands.sh index e0cb153..8889097 100644 --- a/lib/jeannie/commands.sh +++ b/lib/jeannie/commands.sh @@ -3600,6 +3600,7 @@ oci_foundation_config_names=( 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_FOUNDATION_STATE_FILE LAB_OCI_MONITORING_ENABLED LAB_OCI_MONITORING_CIDR LAB_EMAIL_PUBLIC_IP LAB_OCI_EDGE_METRICS_TARGET LAB_OCI_EMAIL_METRICS_TARGET ) @@ -3761,10 +3762,16 @@ oci_foundation_manage() { local action="${1:-status}" shift || true local stack="${REPO_ROOT}/bootstrap/oci" + local state_file="${LAB_OCI_FOUNDATION_STATE_FILE:-}" + local -a tofu_state_args=() + if [[ -n "${state_file}" ]]; then + mkdir -p "$(dirname "${state_file}")" + tofu_state_args=("-state=${state_file}") + fi case "${action}" in status) - tofu -chdir="${stack}" show -no-color 2>/dev/null || echo "OCI foundation has no state yet." + tofu -chdir="${stack}" show "${tofu_state_args[@]}" -no-color 2>/dev/null || echo "OCI foundation has no state yet." ;; plan|apply) if [[ "${TF_VAR_enabled:-false}" != "true" ]]; then @@ -3783,15 +3790,15 @@ oci_foundation_manage() { oci_foundation_save_config tofu -chdir="${stack}" init if [[ "${action}" == "plan" ]]; then - tofu -chdir="${stack}" plan "$@" + tofu -chdir="${stack}" plan "${tofu_state_args[@]}" "$@" else record_change_journal "oci-foundation" apply "$@" - tofu -chdir="${stack}" apply "$@" -auto-approve + tofu -chdir="${stack}" apply "${tofu_state_args[@]}" "$@" -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)" + discovered_edge_ip="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -raw edge_public_ip 2>/dev/null || true)" + discovered_email_ip="$(tofu -chdir="${stack}" output "${tofu_state_args[@]}" -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}" @@ -3837,7 +3844,7 @@ oci_foundation_manage() { shift record_change_journal "oci-foundation" destroy "$@" tofu -chdir="${stack}" init - tofu -chdir="${stack}" destroy "$@" -auto-approve + tofu -chdir="${stack}" destroy "${tofu_state_args[@]}" "$@" -auto-approve ;; *) echo "Usage: ./jeannie oci-foundation {status|plan|apply|destroy --yes}" >&2