Skip to content

Commit

Permalink
Fix atlas error handling and avoid using jq (mongodb-labs#433)
Browse files Browse the repository at this point in the history
  • Loading branch information
blink1073 authored Apr 22, 2024
1 parent 7649174 commit c18bfed
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 10 deletions.
29 changes: 21 additions & 8 deletions .evergreen/atlas/atlas-utils.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
#!/usr/bin/env bash
set -eu

PREV_SCRIPT_DIR=$SCRIPT_DIR
SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
. $SCRIPT_DIR/../handle-paths.sh

. "$SCRIPT_DIR/../find-python3.sh"
SCRIPT_DIR=$PREV_SCRIPT_DIR

# Create an Atlas M10 deployment - this returns immediately so we'll need to poll until
# the deployment is created.
create_deployment ()
Expand Down Expand Up @@ -28,8 +35,7 @@ create_deployment ()
-X POST \
"${ATLAS_BASE_URL}/groups/${ATLAS_GROUP_ID}/${TYPE}?pretty=true")
echo "$RESP"
STATE=$(echo $RESP | jq ".stateName")
if [[ $STATE != *"CREATING"* ]]; then
if [[ ! "$RESP" =~ '"stateName" : "CREATING"' ]]; then
echo "Exiting due to unexpected response $STATE"
exit 1
fi
Expand Down Expand Up @@ -57,11 +63,10 @@ check_deployment ()

ATLAS_BASE_URL=${ATLAS_BASE_URL:-"https://account-dev.mongodb.com/api/atlas/v1.0"}
TYPE=${DEPLOYMENT_TYPE:-"clusters"}
if [ $TYPE = "serverless" ]; then
match_str=".connectionStrings.standardSrv"
else
match_str=".srvAddress"
fi

echo "Finding Python3 binary..." 1>&2
PYTHON="$(find_python3 2>/dev/null)"
echo "Finding Python3 binary... done." 1>&2

# Don't try longer than 20 minutes.
echo "" 1>&2
Expand All @@ -73,7 +78,14 @@ check_deployment ()
--digest -u "${ATLAS_PUBLIC_API_KEY}:${ATLAS_PRIVATE_API_KEY}" \
-X GET \
"${ATLAS_BASE_URL}/groups/${ATLAS_GROUP_ID}/${TYPE}/${DEPLOYMENT_NAME}")
SRV_ADDRESS=$(echo $RESP | jq -r ${match_str})
if [[ "$RESP" =~ '"stateName":"IDLE"' ]]; then
if [ $TYPE = "serverless" ]; then
PROP="['connectionString']['standardSrv']"
else
PROP="['srvAddress']"
fi
SRV_ADDRESS=$($PYTHON -c "import json;d=json.loads('${RESP}');print(d${PROP})")
fi
count=$(( $count + 1 ))
done

Expand All @@ -82,6 +94,7 @@ check_deployment ()
exit 1
else
# Return the MONGODB_URI
echo "$RESP" 1>&2
echo $SRV_ADDRESS
fi
echo "Waiting for Deployment $DEPLOYMENT_NAME in Group $ATLAS_GROUP_ID... done." 1>&2
Expand Down
3 changes: 2 additions & 1 deletion .evergreen/atlas/setup-atlas-cluster.sh
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,8 @@ create_deployment
echo "export ATLAS_BASE_URL=$ATLAS_BASE_URL" >> ./secrets-export.sh
echo "export CLUSTER_NAME=$DEPLOYMENT_NAME" >> ./secrets-export.sh

URI=$(check_deployment | grep -Eo "[^(\/\/)]*$" | cat)
RAW_URI=$(check_deployment)
URI=$(echo $RAW_URI | grep -Eo "[^(\/\/)]*$" | cat)
MONGODB_URI="mongodb+srv://${DRIVERS_ATLAS_USER}:${DRIVERS_ATLAS_PASSWORD}@${URI}"

# Put the MONGODB_URI in an expansions yml and secrets file.
Expand Down
2 changes: 1 addition & 1 deletion .evergreen/atlas/setup.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

set -o errexit
set -eu

SCRIPT_DIR=$(dirname ${BASH_SOURCE[0]})
. $SCRIPT_DIR/../handle-paths.sh
Expand Down

0 comments on commit c18bfed

Please sign in to comment.