diff --git a/lib/jeannie/pipeline.sh b/lib/jeannie/pipeline.sh index 7958942..c297f74 100644 --- a/lib/jeannie/pipeline.sh +++ b/lib/jeannie/pipeline.sh @@ -8260,6 +8260,7 @@ prepare_oci_edge_bastion() { local session_id="" local session_error local attempt + local session_name="jeannie-edge-${target_id##*.}" local proxy_command unset TF_VAR_edge_bastion_host TF_VAR_edge_bastion_user TF_VAR_edge_bastion_port @@ -8293,8 +8294,21 @@ prepare_oci_edge_bastion() { bastion_host="host.bastion.${region}.oci.oraclecloud.com" session_error="$(mktemp)" for attempt in 1 2 3 4 5 6; do + # The CLI can return the asynchronous work-request OCID even after + # waiting for SUCCEEDED. Reuse the active session created for this + # target before creating another one. + session_id="$(${oci_bin} bastion session list \ + --bastion-id "${bastion_id}" \ + --display-name "${session_name}" \ + --session-lifecycle-state ACTIVE --all \ + --query 'data[0].id' --raw-output 2>/dev/null || true)" + if [[ "${session_id}" == ocid1.bastionsession.* ]]; then + break + fi + session_id="" session_json="$(${oci_bin} bastion session create-managed-ssh \ --bastion-id "${bastion_id}" \ + --display-name "${session_name}" \ --target-resource-id "${target_id}" \ --target-os-username "${target_user}" \ --ssh-public-key-file "${public_key}" \ @@ -8303,15 +8317,19 @@ prepare_oci_edge_bastion() { if [[ -n "${session_json}" ]]; then session_id="$(printf '%s' "${session_json}" | python3 -c 'import json,sys; print(json.load(sys.stdin)["data"]["id"])' 2>/dev/null || true)" fi - if [[ -n "${session_id}" && "${session_id}" != "null" ]]; then + if [[ "${session_id}" == ocid1.bastionsession.* ]]; then break fi + # A successful work request may need a short delay before the + # resulting session appears in the list API. + session_id="" if (( attempt < 6 )); then printf 'Waiting for OCI Bastion plugin/session readiness (retry %d/6)...\n' "${attempt}" >&2 sleep 10 fi done - if [[ -z "${session_id}" || "${session_id}" == "null" ]]; then + if [[ -z "${session_id}" || "${session_id}" == "null" || + "${session_id}" != ocid1.bastionsession.* ]]; then echo "Unable to create an OCI Bastion managed SSH session for the edge instance." >&2 sed -n '1,80p' "${session_error}" >&2 || true rm -f "${session_error}"