-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinit_with_mongo.sh
More file actions
executable file
·79 lines (68 loc) · 2.07 KB
/
init_with_mongo.sh
File metadata and controls
executable file
·79 lines (68 loc) · 2.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#!/bin/bash
set -euo pipefail
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
# Set up environment variables
source "${SCRIPT_DIR}/set_hera_environment.sh"
CONTAINER_NAME="hera-mongo"
MONGO_IMAGE="mongo:5.0"
DATA_DIR="${HOME}/mongo-db-datadir"
INIT_DIR="${SCRIPT_DIR}/mongo-init.d"
SYSTEM_USER="$(whoami)"
echo "=== Hera MongoDB Initialization ==="
# 1. Create required directories
echo "Creating directories..."
mkdir -p "${DATA_DIR}"
mkdir -p "${PYHERA_DIR}/log"
# 2. Pull the MongoDB image
echo "Pulling ${MONGO_IMAGE}..."
docker pull "${MONGO_IMAGE}"
# 3. Remove existing container if present
if docker ps -a --format '{{.Names}}' | grep -q "^${CONTAINER_NAME}$"; then
echo "Removing existing '${CONTAINER_NAME}' container..."
docker rm -f "${CONTAINER_NAME}" >/dev/null
fi
# 4. Start MongoDB container with init scripts
echo "Starting MongoDB container..."
docker run --name "${CONTAINER_NAME}" \
-v "${DATA_DIR}":/data/db \
-v "${INIT_DIR}":/docker-entrypoint-initdb.d \
-p 127.0.0.1:27017:27017 \
-d "${MONGO_IMAGE}"
# 5. Wait for MongoDB to be ready
echo "Waiting for MongoDB to be ready..."
for i in $(seq 1 30); do
if docker exec "${CONTAINER_NAME}" mongosh --quiet --eval "db.runCommand({ping:1})" >/dev/null 2>&1; then
echo "MongoDB is ready."
break
fi
if [ "$i" -eq 30 ]; then
echo "ERROR: MongoDB did not become ready in time."
exit 1
fi
sleep 1
done
# 6. Create config.json
CONFIG_FILE="${PYHERA_DIR}/config.json"
if [ -f "${CONFIG_FILE}" ]; then
echo "config.json already exists at ${CONFIG_FILE}, skipping creation."
else
echo "Creating ${CONFIG_FILE}..."
cat > "${CONFIG_FILE}" <<EOF
{
"${SYSTEM_USER}": {
"dbIP": "127.0.0.1",
"dbName": "olymp",
"password": "heracles",
"username": "hera"
}
}
EOF
fi
echo ""
echo "=== Done ==="
echo "MongoDB is running in container '${CONTAINER_NAME}'."
echo "Config written to ${CONFIG_FILE}."
echo ""
echo "To stop/start MongoDB later:"
echo " docker stop ${CONTAINER_NAME}"
echo " docker start ${CONTAINER_NAME}"