-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreateuser.sh
executable file
·58 lines (50 loc) · 1.84 KB
/
createuser.sh
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
#!/bin/sh
echo "ROOT USER CREATION"
if [ "$ROOT_PASSWORD_SECRET" != "" ]; then
PASSWORD_FILE="/run/secrets/$ROOT_PASSWORD_SECRET"
echo "Waiting for replicaset to be ready..."
while true; do
mongo mongodb://localhost --eval "db.isMaster()" | grep $REPLICA_SET_NAME
if [ "$?" = "0" ]; then
echo "Replicaset ready. Verifying PRIMARY election"
#this will work if no other nodes has already added a user password
mongo --eval "rs.status()" | grep PRIMARY
if [ "$?" = "0" ]; then
echo "PRIMARY node is present in this replicaset"
break
fi
#this will verify if any other node has already created and user. exit if so
mongo --eval "rs.status()" | grep Unauthorized
if [ "$?" = "0" ]; then
echo "Replicaset already protected by other node. Skipping user creation"
exit
fi
fi
sleep 1
echo "[user]"
done
mongo --eval "db.isMaster().ismaster" | grep true
if [ "$?" = "0" ]; then
echo "This node is master"
tee "/createuser.js" > /dev/null <<EOT
use admin
db.createUser( { user: "$ROOT_USERNAME",
pwd: "$(cat $PASSWORD_FILE)",
roles: [ { role: "root", db: "admin" }, { role: "userAdminAnyDatabase", db: "admin" }, { role: "clusterAdmin", db: "admin" } ]
}
)
EOT
set +e
echo "Creating user '$ROOT_USERNAME'..."
echo /createuser.js
mongo < /createuser.js
set +e
if [ "$?" = "0" ]; then
echo "ROOT USER CREATED"
else
echo "ROOT USER NOT CREATED. IGNORING"
fi
else
echo "This node is secundary. User creation aborted."
fi
fi