-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
remove fontawesome packags, use js. Add kill script. Fix Mongo
- Loading branch information
1 parent
977c1de
commit ce751d6
Showing
13 changed files
with
169 additions
and
140 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,100 +1,120 @@ | ||
#!/bin/bash | ||
set -e | ||
set -x # Enable debug mode for logging each command | ||
|
||
echo "Hostname: $(hostname)" | ||
|
||
# Environment Variables with Defaults | ||
MONGO_LOG="/var/log/mongodb/mongod.log" | ||
MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME:-"root"} | ||
MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD:-"rootpassword"} | ||
MONGO_LOG=${MONGO_LOG:-"/var/log/mongodb/mongod.log"} | ||
MONGO_INITDB_ROOT_USERNAME=${MONGO_INITDB_ROOT_USERNAME:-"chilicilantro"} | ||
MONGO_INITDB_ROOT_PASSWORD=${MONGO_INITDB_ROOT_PASSWORD:-"Ch1l1C1l4ntr0"} | ||
MONGO_REPLICA_SET_NAME=${MONGO_REPLICA_SET_NAME:-"rs0"} | ||
MONGO_BIND_IP=${MONGO_BIND_IP:-"0.0.0.0"} | ||
MONGO_PORT=${MONGO_PORT:-27017} | ||
MONGO_KEYFILE=${MONGO_KEYFILE:-"/tmp/replica.key"} | ||
MONGO_DB_PATH=${MONGO_DB_PATH:-"/data/db"} | ||
MAX_ATTEMPTS=30 | ||
RETRY_INTERVAL=${RETRY_INTERVAL:-30} | ||
SETUP_COMPLETE_FILE="/data/db/.setup_complete" | ||
|
||
start_mongo_noauth() { | ||
echo "Starting MongoDB without authentication..." | ||
mongod --replSet "$MONGO_REPLICA_SET_NAME" --bind_ip_all --dbpath "$MONGO_DB_PATH" --logpath "$MONGO_LOG" --fork | ||
echo "Starting MongoDB without authentication..." | ||
mongod --replSet "$MONGO_REPLICA_SET_NAME" --bind_ip_all --dbpath "$MONGO_DB_PATH" --logpath "$MONGO_LOG" --fork | ||
} | ||
|
||
start_mongo_auth_foreground() { | ||
echo "Starting MongoDB with authentication in the foreground..." | ||
exec mongod --auth --replSet "$MONGO_REPLICA_SET_NAME" --bind_ip_all --dbpath "$MONGO_DB_PATH" --keyFile "$MONGO_KEYFILE" | ||
wait_for_mongo() { | ||
echo "Waiting for MongoDB to be ready..." | ||
for i in $(seq 1 $MAX_ATTEMPTS); do | ||
if mongosh --quiet --eval "db.runCommand({ ping: 1 })" &>/dev/null; then | ||
echo "MongoDB is ready." | ||
return 0 | ||
fi | ||
echo "Attempt $i: MongoDB not ready yet, retrying in $RETRY_INTERVAL seconds..." | ||
sleep $RETRY_INTERVAL | ||
done | ||
echo "Failed to connect to MongoDB after $MAX_ATTEMPTS attempts." | ||
return 1 | ||
} | ||
|
||
stop_mongo() { | ||
echo "Stopping MongoDB..." | ||
mongosh --quiet --eval 'db.getSiblingDB("admin").shutdownServer({ force: true });' || true | ||
sleep 5 | ||
initialize_replica_set() { | ||
echo "Initializing replica set..." | ||
local host="localhost" | ||
mongosh --quiet --eval " | ||
rs.initiate({ | ||
_id: '$MONGO_REPLICA_SET_NAME', | ||
members: [{ _id: 0, host: '$host:$MONGO_PORT' }] | ||
}) | ||
" | ||
} | ||
|
||
wait_for_mongo() { | ||
echo "Waiting for MongoDB to be ready..." | ||
until mongosh --quiet --eval 'db.runCommand({ ping: 1 })' >/dev/null 2>&1; do | ||
echo "MongoDB not ready yet, retrying..." | ||
sleep 2 | ||
done | ||
echo "MongoDB is ready." | ||
wait_for_primary() { | ||
echo "Waiting for replica set to become PRIMARY..." | ||
for i in $(seq 1 $MAX_ATTEMPTS); do | ||
if [ "$1" = "auth" ]; then | ||
status=$(mongosh admin --quiet --eval "db.auth('$MONGO_INITDB_ROOT_USERNAME', '$MONGO_INITDB_ROOT_PASSWORD'); rs.status().myState") | ||
else | ||
status=$(mongosh --quiet --eval "rs.status().myState") | ||
fi | ||
if [ "$status" = "1" ]; then | ||
echo "Replica set is now PRIMARY." | ||
return 0 | ||
fi | ||
echo "Attempt $i: Replica set is not PRIMARY yet, retrying in $RETRY_INTERVAL seconds..." | ||
sleep $RETRY_INTERVAL | ||
done | ||
echo "Replica set did not become PRIMARY within the expected time." | ||
return 1 | ||
} | ||
|
||
create_root_user() { | ||
echo "Creating root user if it does not exist..." | ||
mongosh --quiet --eval " | ||
const adminDb = db.getSiblingDB('admin'); | ||
if (!adminDb.getUser('$MONGO_INITDB_ROOT_USERNAME')) { | ||
adminDb.createUser({ | ||
user: '$MONGO_INITDB_ROOT_USERNAME', | ||
pwd: '$MONGO_INITDB_ROOT_PASSWORD', | ||
roles: [{ role: 'root', db: 'admin' }] | ||
}); | ||
print('Root user created.'); | ||
} else { | ||
print('Root user already exists.'); | ||
} | ||
" | ||
echo "Creating root user..." | ||
mongosh admin --quiet --eval " | ||
db.createUser({ | ||
user: '$MONGO_INITDB_ROOT_USERNAME', | ||
pwd: '$MONGO_INITDB_ROOT_PASSWORD', | ||
roles: ['root'] | ||
}); | ||
" | ||
} | ||
|
||
initialize_replica_set() { | ||
echo "Initializing replica set..." | ||
mongosh --quiet --eval " | ||
const status = rs.status(); | ||
if (status.codeName === 'NotYetInitialized') { | ||
rs.initiate({ | ||
_id: '$MONGO_REPLICA_SET_NAME', | ||
members: [{ _id: 0, host: 'localhost:$MONGO_PORT' }] | ||
}); | ||
print('Replica set initialized.'); | ||
} else { | ||
print('Replica set already initialized.'); | ||
} | ||
" | ||
start_mongo_auth() { | ||
echo "Starting MongoDB with authentication..." | ||
mongod --auth --replSet "$MONGO_REPLICA_SET_NAME" --bind_ip_all --dbpath "$MONGO_DB_PATH" --logpath "$MONGO_LOG" --keyFile "$MONGO_KEYFILE" --fork | ||
} | ||
|
||
wait_for_primary() { | ||
echo "Waiting for replica set to become PRIMARY..." | ||
until mongosh --quiet --eval 'db.isMaster().ismaster' | grep -q 'true'; do | ||
echo "Replica set is not PRIMARY yet, retrying..." | ||
sleep 2 | ||
done | ||
echo "Replica set is now PRIMARY." | ||
start_mongo_auth_foreground() { | ||
echo "Starting MongoDB with authentication in the foreground..." | ||
exec mongod --auth --replSet "$MONGO_REPLICA_SET_NAME" --bind_ip_all --dbpath "$MONGO_DB_PATH" --keyFile "$MONGO_KEYFILE" | ||
} | ||
|
||
# Main Execution Flow | ||
if [ -f "$SETUP_COMPLETE_FILE" ]; then | ||
echo "Setup already completed. Starting MongoDB in foreground mode." | ||
start_mongo_auth_foreground | ||
exit 0 | ||
fi | ||
|
||
# Main execution flow | ||
echo "Starting MongoDB setup..." | ||
|
||
# Start MongoDB without authentication | ||
start_mongo_noauth | ||
wait_for_mongo | ||
wait_for_mongo || exit 1 | ||
|
||
# Initialize the replica set | ||
echo "Initializing replica set..." | ||
initialize_replica_set | ||
wait_for_primary | ||
wait_for_primary || exit 1 | ||
|
||
# Create the root user | ||
echo "Creating root user..." | ||
create_root_user | ||
|
||
# Restart MongoDB with authentication | ||
stop_mongo | ||
start_mongo_auth_foreground | ||
echo "Shutting down MongoDB to restart with authentication..." | ||
mongod --shutdown | ||
|
||
echo "Starting MongoDB with authentication..." | ||
start_mongo_auth | ||
wait_for_mongo || exit 1 | ||
wait_for_primary "auth" || exit 1 | ||
|
||
# Mark setup as complete | ||
touch "$SETUP_COMPLETE_FILE" | ||
|
||
echo "MongoDB setup completed successfully. Starting MongoDB in foreground mode." | ||
start_mongo_auth_foreground |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,36 @@ | ||
#!/bin/bash | ||
|
||
MAX_ATTEMPTS=30 | ||
RETRY_INTERVAL=10 | ||
|
||
# Wait for MongoDB to start | ||
until mongosh --quiet --eval 'db.runCommand({ ping: 1 })' >/dev/null 2>&1; do | ||
echo "Waiting for MongoDB to start..." | ||
sleep 5 | ||
for i in $(seq 1 $MAX_ATTEMPTS); do | ||
if mongosh --quiet --eval 'db.runCommand({ ping: 1 })' >/dev/null 2>&1; then | ||
echo "[HC] MongoDB is responsive." | ||
break | ||
fi | ||
echo "[HC] Attempt $i/$MAX_ATTEMPTS: Waiting for MongoDB to start..." | ||
sleep $RETRY_INTERVAL | ||
if [ $i -eq $MAX_ATTEMPTS ]; then | ||
echo "[HC] MongoDB failed to start after $MAX_ATTEMPTS attempts." | ||
exit 1 | ||
fi | ||
done | ||
|
||
# Check if MongoDB replica set is ready | ||
is_master=$(mongosh --quiet --eval 'db.isMaster().ismaster' --authenticationDatabase admin -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD") | ||
if [ "$is_master" != "true" ]; then | ||
echo "MongoDB replica set is not yet ready." | ||
exit 1 | ||
fi | ||
for i in $(seq 1 $MAX_ATTEMPTS); do | ||
rs_status=$(mongosh --quiet --eval 'rs.status().ok' --authenticationDatabase admin -u "$MONGO_INITDB_ROOT_USERNAME" -p "$MONGO_INITDB_ROOT_PASSWORD" || echo "0") | ||
if [ "$rs_status" = "1" ]; then | ||
echo "[HC] MongoDB replica set is ready." | ||
exit 0 | ||
fi | ||
echo "[HC] Attempt $i/$MAX_ATTEMPTS: Waiting for MongoDB replica set to be ready..." | ||
sleep $RETRY_INTERVAL | ||
if [ $i -eq $MAX_ATTEMPTS ]; then | ||
echo "[HC] MongoDB replica set failed to initialize after $MAX_ATTEMPTS attempts." | ||
exit 1 | ||
fi | ||
done | ||
|
||
echo "MongoDB is up and running with replica set." | ||
exit 0 | ||
echo "[HC] MongoDB healthcheck failed." | ||
exit 1 |
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.