-
Notifications
You must be signed in to change notification settings - Fork 13
/
skyring-pre.sh
executable file
·36 lines (36 loc) · 1.61 KB
/
skyring-pre.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
#!/bin/bash
pwd=`python -c 'import json;print json.loads(open("/etc/skyring/skyring.conf").read())["dbconfig"]["password"]'`
# Check the db status
/bin/systemctl status mongod > /dev/null 2>&1
if [ $? -eq 0 ]; then
# Check the db connection and fetch the user name
UNAME=`/bin/mongo -quiet skyring --eval 'db.getUser("admin")["user"]' -u admin -p ${pwd}`
if [ $? -ne 0 ]; then
# Reset the auth flag to false because the password is changed
# disabling auth mode temporarily for checking and will be enabled back again
/bin/grep "^auth = true" /etc/mongodb.conf > /dev/null 2>&1
if [ $? -eq 0 ]; then
sed -i -e 's/auth = true/#auth = true/g' /etc/mongodb.conf
/bin/systemctl restart mongod > /dev/null 2>&1
fi
/bin/systemctl status mongod > /dev/null 2>&1
if [ $? -eq 0 ]; then
# Check admin user exists already
USERNAME=`/bin/mongo -quiet skyring --eval 'db.getUser("admin")["user"]'`
if [ $? -ne 0 ] || [ "${USERNAME}" != 'admin' ]; then
# Admin user does not exists
cmd="/bin/mongo skyring --eval 'db.createUser( { \"user\" : \"admin\", \"pwd\": \"$pwd\", \"roles\" : [\"readWrite\", \"dbAdmin\", \"userAdmin\"] })'"
else
# Admin user already exists. Update the password
cmd="/bin/mongo skyring --eval 'db.changeUserPassword(\"admin\", \"${pwd}\")'"
fi
eval $cmd > /dev/null 2>&1 || :
# Update the security flag and restart the mongod service
/bin/grep "^#auth = true" /etc/mongodb.conf > /dev/null 2>&1
if [ $? -eq 0 ]; then
sed -i -e 's/#auth = true/auth = true/g' /etc/mongodb.conf
fi
/bin/systemctl restart mongod > /dev/null 2>&1
fi
fi
fi