forked from medenizoren/mongodb-github-action
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathstart-mongodb.sh
More file actions
128 lines (99 loc) · 3.2 KB
/
start-mongodb.sh
File metadata and controls
128 lines (99 loc) · 3.2 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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
#!/bin/sh
# Map input values from the GitHub Actions workflow to shell variables
MONGODB_VERSION=$1
MONGODB_REPLICA_SET=$2
MONGODB_PORT=$3
MONGODB_DB=$4
MONGODB_USERNAME=$5
MONGODB_PASSWORD=$6
MONGODB_NAME=$7
# `mongosh` is used starting from MongoDB 5.x
MONGODB_CLIENT="mongosh --quiet"
if [ -z "$MONGODB_VERSION" ]; then
echo ""
echo "Missing MongoDB version in the [mongodb-version] input. Received value: $MONGODB_VERSION"
echo ""
exit 2
fi
if [ -z "$MONGODB_NAME" ]; then
MONGODB_NAME="mongodb"
fi
echo "::group::Selecting correct MongoDB client"
if [ "`echo $MONGODB_VERSION | cut -c 1`" = "4" ]; then
MONGODB_CLIENT="mongo"
fi
echo " - Using MongoDB client: [$MONGODB_CLIENT]"
echo ""
echo "::endgroup::"
# Helper function to wait for MongoDB to be started before moving on
wait_for_mongodb () {
echo "::group::Waiting for MongoDB to accept connections"
sleep 1
TIMER=0
MONGODB_ARGS=""
if [ -z "$MONGODB_REPLICA_SET" ]
then
if [ -z "$MONGODB_USERNAME" ]
then
MONGODB_ARGS=""
else
# no replica set, but username given: use them as args
MONGODB_ARGS="--username $MONGODB_USERNAME --password $MONGODB_PASSWORD"
fi
fi
# until ${WAIT_FOR_MONGODB_COMMAND}
until docker exec --tty $MONGODB_NAME $MONGODB_CLIENT --port $MONGODB_PORT $MONGODB_ARGS --eval "db.serverStatus()"
do
echo "."
sleep 1
TIMER=$((TIMER + 1))
if [[ $TIMER -eq 20 ]]; then
echo "MongoDB did not initialize within 20 seconds. Exiting."
exit 2
fi
done
echo "::endgroup::"
}
if [ -z "$MONGODB_REPLICA_SET" ]; then
echo "::group::Starting single-node instance, no replica set"
echo " - port [$MONGODB_PORT]"
echo " - version [$MONGODB_VERSION]"
echo " - database [$MONGODB_DB]"
echo " - credentials [$MONGODB_USERNAME:$MONGODB_PASSWORD]"
echo ""
docker run --name $MONGODB_NAME --publish $MONGODB_PORT:$MONGODB_PORT -e MONGO_INITDB_DATABASE=$MONGODB_DB -e MONGO_INITDB_ROOT_USERNAME=$MONGODB_USERNAME -e MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD --detach mongo:$MONGODB_VERSION --port $MONGODB_PORT
if [ $? -ne 0 ]; then
echo "Error starting MongoDB Docker container"
exit 2
fi
echo "::endgroup::"
wait_for_mongodb
exit 0
fi
echo "::group::Starting MongoDB as single-node replica set"
echo " - port [$MONGODB_PORT]"
echo " - version [$MONGODB_VERSION]"
echo " - replica set [$MONGODB_REPLICA_SET]"
echo ""
docker run --name $MONGODB_NAME --publish $MONGODB_PORT:$MONGODB_PORT --detach mongo:$MONGODB_VERSION --replSet $MONGODB_REPLICA_SET --port $MONGODB_PORT
if [ $? -ne 0 ]; then
echo "Error starting MongoDB Docker container"
exit 2
fi
echo "::endgroup::"
wait_for_mongodb
echo "::group::Initiating replica set [$MONGODB_REPLICA_SET]"
docker exec --tty $MONGODB_NAME $MONGODB_CLIENT --port $MONGODB_PORT --eval "
rs.initiate({
\"_id\": \"$MONGODB_REPLICA_SET\",
\"members\": [ {
\"_id\": 0,
\"host\": \"localhost:$MONGODB_PORT\"
} ]
})
"
echo "Success! Initiated replica set [$MONGODB_REPLICA_SET]"
echo "::endgroup::"
echo "::group::Checking replica set status [$MONGODB_REPLICA_SET]"
docker exec --tty $MONGODB_NAME $MONGODB_CLIENT --port $MONGODB_PORT --eval "rs.status()"
echo "::endgroup::"