feat: rebuild OCI foundation when adopted resources are gone
This commit is contained in:
parent
40d93c3b6e
commit
4cb90fb9e3
|
|
@ -3366,6 +3366,63 @@ oci_foundation_save_config() {
|
|||
} >"${file}"
|
||||
}
|
||||
|
||||
oci_foundation_reconcile_existing() {
|
||||
local oci_bin="" output_file status
|
||||
if command -v oci >/dev/null 2>&1; then
|
||||
oci_bin="$(command -v oci)"
|
||||
elif [[ -x "${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci" ]]; then
|
||||
oci_bin="${HOME}/.local/share/homelab/venvs/oci-cli/bin/oci"
|
||||
else
|
||||
return 0
|
||||
fi
|
||||
|
||||
[[ -n "${LAB_OCI_EDGE_EXISTING_INSTANCE_ID:-}" ]] || return 0
|
||||
output_file="$(mktemp)"
|
||||
if ! "${oci_bin}" compute instance get \
|
||||
--instance-id "${LAB_OCI_EDGE_EXISTING_INSTANCE_ID}" \
|
||||
--query 'data.id' --raw-output >"${output_file}" 2>&1; then
|
||||
if grep -q '"status": 404' "${output_file}"; then
|
||||
printf '%s\n' 'Persisted OCI edge/network resources are absent; switching foundation to create mode.' >&2
|
||||
export LAB_OCI_CREATE_COMPARTMENT=true
|
||||
unset LAB_OCI_COMPARTMENT_ID LAB_OCI_EDGE_EXISTING_INSTANCE_ID
|
||||
unset LAB_OCI_EXISTING_VCN_ID LAB_OCI_EXISTING_PUBLIC_SUBNET_ID LAB_OCI_EXISTING_EMAIL_SUBNET_ID
|
||||
rm -f "${output_file}"
|
||||
return 0
|
||||
fi
|
||||
cat "${output_file}" >&2
|
||||
rm -f "${output_file}"
|
||||
return 1
|
||||
fi
|
||||
rm -f "${output_file}"
|
||||
|
||||
for status in vcn subnet; do
|
||||
local resource_id=""
|
||||
case "${status}" in
|
||||
vcn) resource_id="${LAB_OCI_EXISTING_VCN_ID:-}"; [[ -n "${resource_id}" ]] || continue ;;
|
||||
subnet) resource_id="${LAB_OCI_EXISTING_PUBLIC_SUBNET_ID:-}"; [[ -n "${resource_id}" ]] || continue ;;
|
||||
esac
|
||||
output_file="$(mktemp)"
|
||||
if [[ "${status}" == "vcn" ]]; then
|
||||
"${oci_bin}" network vcn get --vcn-id "${resource_id}" >"${output_file}" 2>&1 || true
|
||||
else
|
||||
"${oci_bin}" network subnet get --subnet-id "${resource_id}" >"${output_file}" 2>&1 || true
|
||||
fi
|
||||
if grep -q '"status": 404' "${output_file}"; then
|
||||
printf '%s\n' 'Persisted OCI edge/network resources are absent; switching foundation to create mode.' >&2
|
||||
export LAB_OCI_CREATE_COMPARTMENT=true
|
||||
unset LAB_OCI_COMPARTMENT_ID LAB_OCI_EDGE_EXISTING_INSTANCE_ID
|
||||
unset LAB_OCI_EXISTING_VCN_ID LAB_OCI_EXISTING_PUBLIC_SUBNET_ID LAB_OCI_EXISTING_EMAIL_SUBNET_ID
|
||||
rm -f "${output_file}"
|
||||
return 0
|
||||
elif grep -q '"status": [45][0-9][0-9]' "${output_file}"; then
|
||||
cat "${output_file}" >&2
|
||||
rm -f "${output_file}"
|
||||
return 1
|
||||
fi
|
||||
rm -f "${output_file}"
|
||||
done
|
||||
}
|
||||
|
||||
oci_foundation_manage() {
|
||||
require_debian_server "oci-foundation"
|
||||
oci_foundation_load_config
|
||||
|
|
@ -3391,6 +3448,11 @@ oci_foundation_manage() {
|
|||
if [[ "${LAB_OCI_CLI_AUTO_INSTALL:-true}" == "true" ]]; then
|
||||
"${REPO_ROOT}/scripts/oci-auth" install
|
||||
fi
|
||||
oci_foundation_reconcile_existing
|
||||
unset TF_VAR_enabled TF_VAR_region TF_VAR_tenancy_id TF_VAR_admin_cidr
|
||||
unset TF_VAR_edge_availability_domain TF_VAR_email_availability_domain
|
||||
unset TF_VAR_existing_vcn_id TF_VAR_existing_public_subnet_id TF_VAR_existing_email_subnet_id
|
||||
export_oci_foundation_tf_vars
|
||||
oci_foundation_save_config
|
||||
tofu -chdir="${stack}" init
|
||||
if [[ "${action}" == "plan" ]]; then
|
||||
|
|
|
|||
Loading…
Reference in New Issue