Skip to content

Commit 42a8f93

Browse files
committed
add better checks for responsiveness in scripts
1 parent ca1092a commit 42a8f93

File tree

6 files changed

+145
-27
lines changed

6 files changed

+145
-27
lines changed

bootstrap.sh

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -259,10 +259,20 @@ else
259259
fi
260260
261261
# Check if MongoDB is responsive - use mongosh with --tls flags
262-
if ! mongosh --host \$HOSTNAME --port \$MONGO_PORT \$MONGOSH_TLS_ARG -u \$DB_USERNAME -p \$DB_PASSWORD --authenticationDatabase admin --eval "db.adminCommand('ping')" &>/dev/null; then
262+
log "Checking if MongoDB is responsive..."
263+
log "Running command: mongosh --host \$HOSTNAME --port \$MONGO_PORT \$MONGOSH_TLS_ARG -u \$DB_USERNAME -p [PASSWORD] --authenticationDatabase admin --eval \"db.adminCommand('ping')\""
264+
265+
# Create a temporary file to capture the output and errors
266+
MONGO_CHECK_OUTPUT=\$(mktemp)
267+
if ! mongosh --host \$HOSTNAME --port \$MONGO_PORT \$MONGOSH_TLS_ARG -u \$DB_USERNAME -p \$DB_PASSWORD --authenticationDatabase admin --eval "db.adminCommand('ping')" > \$MONGO_CHECK_OUTPUT 2>&1; then
263268
log "❌ ERROR: MongoDB is not responsive. Backup aborted."
269+
log "Error output from command:"
270+
cat \$MONGO_CHECK_OUTPUT >> \$LOG_FILE
271+
rm -f \$MONGO_CHECK_OUTPUT
264272
exit 1
265273
fi
274+
log "✅ MongoDB is responsive."
275+
rm -f \$MONGO_CHECK_OUTPUT
266276
267277
# Create backup - use mongodump with --ssl flags
268278
log "Creating backup at \$BACKUP_PATH"

connection_info.sh

Lines changed: 40 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -58,32 +58,54 @@ CONNECTION_DOMAIN="$DOMAIN"
5858
# Try to connect using the domain name first (if not localhost)
5959
if [ "$DOMAIN" != "localhost" ]; then
6060
echo "Attempting to connect to MongoDB using domain name: $DOMAIN"
61-
if mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval "JSON.stringify(rs.status())" > $TEMP_FILE 2>/dev/null; then
61+
echo "Running command: mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p [PASSWORD] --authenticationDatabase admin --quiet --eval \"JSON.stringify(rs.status())\""
62+
63+
# Create a temporary file to capture errors
64+
ERROR_LOG=$(mktemp)
65+
if mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval "JSON.stringify(rs.status())" > $TEMP_FILE 2> $ERROR_LOG; then
6266
echo "✅ Successfully connected to MongoDB using domain name: $DOMAIN"
6367
else
64-
echo "Connection using domain name failed. Trying localhost..."
68+
echo "Connection using domain name failed. Error output:"
69+
cat $ERROR_LOG
70+
rm -f $ERROR_LOG
71+
72+
echo "Trying localhost instead..."
73+
echo "Running command: mongosh --host localhost --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p [PASSWORD] --authenticationDatabase admin --quiet --eval \"JSON.stringify(rs.status())\""
74+
6575
# If that fails, try connecting using localhost
66-
if mongosh --host localhost --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval "JSON.stringify(rs.status())" > $TEMP_FILE 2>/dev/null; then
76+
if mongosh --host localhost --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval "JSON.stringify(rs.status())" > $TEMP_FILE 2> $ERROR_LOG; then
77+
echo "✅ Successfully connected to MongoDB using localhost."
78+
# Note: We're not changing CONNECTION_DOMAIN, only the DOMAIN for the current connection
79+
DOMAIN="localhost"
80+
else
81+
echo "❌ ERROR: Failed to connect to MongoDB using both domain name and localhost."
82+
echo "Error output from localhost attempt:"
83+
cat $ERROR_LOG
84+
rm -f $ERROR_LOG
85+
rm -f $TEMP_FILE
86+
exit 1
87+
fi
88+
fi
89+
rm -f $ERROR_LOG
90+
else
91+
# Just try localhost
92+
echo "Attempting to connect to MongoDB using localhost"
93+
echo "Running command: mongosh --host localhost --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p [PASSWORD] --authenticationDatabase admin --quiet --eval \"JSON.stringify(rs.status())\""
94+
95+
# Create a temporary file to capture errors
96+
ERROR_LOG=$(mktemp)
97+
if mongosh --host localhost --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval "JSON.stringify(rs.status())" > $TEMP_FILE 2> $ERROR_LOG; then
6798
echo "✅ Successfully connected to MongoDB using localhost."
68-
# Note: We're not changing CONNECTION_DOMAIN, only the DOMAIN for the current connection
69-
DOMAIN="localhost"
7099
else
71-
echo "❌ ERROR: Failed to connect to MongoDB using both domain name and localhost."
72-
rm $TEMP_FILE
100+
echo "❌ ERROR: Failed to connect to MongoDB using localhost."
101+
echo "Error output:"
102+
cat $ERROR_LOG
103+
rm -f $ERROR_LOG
104+
rm -f $TEMP_FILE
73105
exit 1
74106
fi
107+
rm -f $ERROR_LOG
75108
fi
76-
else
77-
# Just try localhost
78-
echo "Attempting to connect to MongoDB using localhost"
79-
if mongosh --host localhost --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --quiet --eval "JSON.stringify(rs.status())" > $TEMP_FILE 2>/dev/null; then
80-
echo "✅ Successfully connected to MongoDB using localhost."
81-
else
82-
echo "❌ ERROR: Failed to connect to MongoDB using localhost."
83-
rm $TEMP_FILE
84-
exit 1
85-
fi
86-
fi
87109

88110
# Check if the command was successful
89111
if [ $? -ne 0 ] || [ ! -s $TEMP_FILE ]; then

provision_ssl.sh

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,14 +139,35 @@ if sudo systemctl is-active --quiet mongod; then
139139
DB_PASSWORD=$(jq -r '.db_password' "$CONFIG_FILE")
140140
MONGO_PORT=$(jq -r '.mongo_port' "$CONFIG_FILE")
141141

142+
# Get domain name from config.json
143+
DOMAIN_CONFIG=$(jq -r '.domain_name' "$CONFIG_FILE")
144+
if [ -n "$DOMAIN_CONFIG" ] && [ "$DOMAIN_CONFIG" != "null" ] && [ "$DOMAIN_CONFIG" != "your.domain.com" ]; then
145+
DOMAIN="$DOMAIN_CONFIG"
146+
echo "Using domain name from config.json: $DOMAIN"
147+
else
148+
DOMAIN="localhost"
149+
echo "Domain name not set in config.json. Using localhost for connection."
150+
fi
151+
142152
if command -v mongosh &> /dev/null; then
143153
# Try with domain name and client certificate
144-
echo "Attempting to verify TLS using domain name"
145-
if mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "db.adminCommand({ getParameter: 1, tlsMode: 1 })" 2>/dev/null | grep -q "requireTLS"; then
146-
echo "✅ MongoDB TLS mode verified using domain name: requireTLS is active"
154+
echo "Attempting to verify TLS using domain name: $DOMAIN"
155+
echo "Running command: mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p [PASSWORD] --authenticationDatabase admin --eval \"db.adminCommand({ getParameter: 1, tlsMode: 1 })\""
156+
157+
# Create a temporary file to capture the output and errors
158+
TLS_CHECK_OUTPUT=$(mktemp)
159+
if mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "db.adminCommand({ getParameter: 1, tlsMode: 1 })" > $TLS_CHECK_OUTPUT 2>&1; then
160+
if grep -q "requireTLS" $TLS_CHECK_OUTPUT; then
161+
echo "✅ MongoDB TLS mode verified using domain name: requireTLS is active"
162+
else
163+
echo "⚠️ WARNING: Command succeeded but TLS mode could not be verified."
164+
echo "Output from command:"
165+
cat $TLS_CHECK_OUTPUT
166+
fi
147167
else
148168
echo "⚠️ WARNING: MongoDB is running but TLS mode could not be verified."
149-
echo "This is expected because client certificates are required."
169+
echo "Error output from command:"
170+
cat $TLS_CHECK_OUTPUT
150171
echo ""
151172
echo "IMPORTANT: To connect to MongoDB, you will need:"
152173
echo "1. A client certificate signed by your CA"
@@ -155,6 +176,7 @@ if sudo systemctl is-active --quiet mongod; then
155176
echo "Example connection command:"
156177
echo "mongosh --host $DOMAIN --port $MONGO_PORT --tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem -u $DB_USERNAME -p <password> --authenticationDatabase admin"
157178
fi
179+
rm -f $TLS_CHECK_OUTPUT
158180
else
159181
echo "⚠️ mongosh not available to verify TLS configuration."
160182
echo "MongoDB is running, but please verify TLS configuration manually."

utils/create_backup.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,27 @@ create_backup() {
4646

4747
# Use the exact command that works
4848
if [ "$tls_enabled" = "true" ]; then
49+
# For mongodump, use --ssl flags
4950
TLS_ARG="--ssl --sslCAFile $CA_FILE --sslPEMKeyFile /etc/ssl/mongodb/client.pem"
51+
# For mongosh, use --tls flags
52+
MONGOSH_TLS_ARG="--tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem"
5053
echo "NOTE: Client certificates are required for connections."
5154
echo " Ensure the client certificate exists at /etc/ssl/mongodb/client.pem"
55+
56+
# Check if MongoDB is responsive first
57+
echo "Checking if MongoDB is responsive..."
58+
echo "Running command: mongosh --host $host --port $MONGO_PORT $MONGOSH_TLS_ARG -u $DB_USERNAME -p [PASSWORD] --authenticationDatabase admin --eval \"db.adminCommand('ping')\""
59+
60+
MONGO_CHECK_OUTPUT=$(mktemp)
61+
if ! mongosh --host $host --port $MONGO_PORT $MONGOSH_TLS_ARG -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "db.adminCommand('ping')" > $MONGO_CHECK_OUTPUT 2>&1; then
62+
echo "❌ ERROR: MongoDB is not responsive. Backup aborted."
63+
echo "Error output from command:"
64+
cat $MONGO_CHECK_OUTPUT
65+
rm -f $MONGO_CHECK_OUTPUT
66+
return 1
67+
fi
68+
echo "✅ MongoDB is responsive."
69+
rm -f $MONGO_CHECK_OUTPUT
5270
else
5371
TLS_ARG=""
5472
fi

utils/replica_sets.sh

Lines changed: 32 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,27 +51,55 @@ execute_mongo_command() {
5151
# Try to connect using the domain name first (if not localhost)
5252
if [ "$DOMAIN" != "localhost" ]; then
5353
echo "Attempting to connect to MongoDB using domain name: $DOMAIN"
54-
if mongosh --host $DOMAIN --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "$command" 2>/dev/null; then
54+
echo "Running command: mongosh --host $DOMAIN --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p [PASSWORD] --authenticationDatabase admin --eval \"$command\""
55+
56+
# Create a temporary file to capture the output and errors
57+
MONGO_OUTPUT=$(mktemp)
58+
if mongosh --host $DOMAIN --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "$command" > $MONGO_OUTPUT 2>&1; then
5559
echo "✅ Successfully connected to MongoDB using domain name: $DOMAIN"
60+
echo "Command output:"
61+
cat $MONGO_OUTPUT
62+
rm -f $MONGO_OUTPUT
5663
return 0
5764
else
58-
echo "Connection using domain name failed. Trying localhost..."
59-
if mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "$command" 2>/dev/null; then
65+
echo "Connection using domain name failed. Error output:"
66+
cat $MONGO_OUTPUT
67+
rm -f $MONGO_OUTPUT
68+
69+
echo "Trying localhost instead..."
70+
MONGO_OUTPUT=$(mktemp)
71+
echo "Running command: mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p [PASSWORD] --authenticationDatabase admin --eval \"$command\""
72+
if mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "$command" > $MONGO_OUTPUT 2>&1; then
6073
echo "✅ Successfully connected to MongoDB using localhost."
74+
echo "Command output:"
75+
cat $MONGO_OUTPUT
76+
rm -f $MONGO_OUTPUT
6177
return 0
6278
else
6379
echo "❌ ERROR: Failed to connect to MongoDB using both domain name and localhost."
80+
echo "Error output from localhost attempt:"
81+
cat $MONGO_OUTPUT
82+
rm -f $MONGO_OUTPUT
6483
return 1
6584
fi
6685
fi
6786
else
6887
# Just try localhost
6988
echo "Attempting to connect to MongoDB using localhost"
70-
if mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "$command" 2>/dev/null; then
89+
echo "Running command: mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p [PASSWORD] --authenticationDatabase admin --eval \"$command\""
90+
91+
MONGO_OUTPUT=$(mktemp)
92+
if mongosh --host localhost --port $MONGO_PORT $TLS_ARGS -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "$command" > $MONGO_OUTPUT 2>&1; then
7193
echo "✅ Successfully connected to MongoDB using localhost."
94+
echo "Command output:"
95+
cat $MONGO_OUTPUT
96+
rm -f $MONGO_OUTPUT
7297
return 0
7398
else
7499
echo "❌ ERROR: Failed to connect to MongoDB using localhost."
100+
echo "Error output:"
101+
cat $MONGO_OUTPUT
102+
rm -f $MONGO_OUTPUT
75103
return 1
76104
fi
77105
fi

utils/restore_backup.sh

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,9 +51,27 @@ restore_backup() {
5151

5252
# Use the exact command that works
5353
if [ "$tls_enabled" = "true" ]; then
54+
# For mongorestore, use --ssl flags
5455
TLS_ARG="--ssl --sslCAFile $CA_FILE --sslPEMKeyFile /etc/ssl/mongodb/client.pem"
56+
# For mongosh, use --tls flags
57+
MONGOSH_TLS_ARG="--tls --tlsCAFile $CA_FILE --tlsCertificateKeyFile /etc/ssl/mongodb/client.pem"
5558
echo "NOTE: Client certificates are required for connections."
5659
echo " Ensure the client certificate exists at /etc/ssl/mongodb/client.pem"
60+
61+
# Check if MongoDB is responsive first
62+
echo "Checking if MongoDB is responsive..."
63+
echo "Running command: mongosh --host $host --port $MONGO_PORT $MONGOSH_TLS_ARG -u $DB_USERNAME -p [PASSWORD] --authenticationDatabase admin --eval \"db.adminCommand('ping')\""
64+
65+
MONGO_CHECK_OUTPUT=$(mktemp)
66+
if ! mongosh --host $host --port $MONGO_PORT $MONGOSH_TLS_ARG -u $DB_USERNAME -p $DB_PASSWORD --authenticationDatabase admin --eval "db.adminCommand('ping')" > $MONGO_CHECK_OUTPUT 2>&1; then
67+
echo "❌ ERROR: MongoDB is not responsive. Restore aborted."
68+
echo "Error output from command:"
69+
cat $MONGO_CHECK_OUTPUT
70+
rm -f $MONGO_CHECK_OUTPUT
71+
return 1
72+
fi
73+
echo "✅ MongoDB is responsive."
74+
rm -f $MONGO_CHECK_OUTPUT
5775
else
5876
TLS_ARG=""
5977
fi

0 commit comments

Comments
 (0)