-
Notifications
You must be signed in to change notification settings - Fork 172
/
bitnami-utils-custom.sh
91 lines (80 loc) · 2.09 KB
/
bitnami-utils-custom.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
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
# MongoDB Utility functions
PROGRAM_OPTIONS="--config $BITNAMI_APP_DIR/conf/mongodb.conf --dbpath $BITNAMI_APP_DIR/data"
create_mongodb_user() {
if [ "$MONGODB_USER" ] && [ -z $MONGODB_PASSWORD ]; then
echo ""
echo "In order to create a MONGODB_USER you need to provide the MONGODB_PASSWORD as well"
echo ""
exit -1
elif [ -z $MONGODB_PASSWORD ]; then
return 0
fi
MONGODB_USER=${MONGODB_USER:-root}
if [ "$MONGODB_USER" != "root" ] && [ ! $MONGODB_DATABASE ]; then
echo ""
echo "In order to use a custom MONGODB_USER you need to provide the MONGODB_DATABASE as well"
echo ""
exit -1
fi
echo ""
echo "==> Creating user $MONGODB_USER..."
# start mongodb server and wait for it to accept connections
mongod $PROGRAM_OPTIONS --logpath /dev/null --bind_ip 127.0.0.1 --fork >/dev/null
timeout=10
while ! mongo --eval "db.adminCommand('listDatabases')" >/dev/null 2>&1
do
timeout=$(($timeout - 1))
if [ $timeout -eq 0 ]; then
echo "Could not connect to server. Aborting..."
exit 1
fi
sleep 1
done
if [ "$MONGODB_USER" == "root" ]; then
cat >> /tmp/initMongo.js <<EOF
db = db.getSiblingDB('admin')
db.createUser(
{
user: "$MONGODB_USER",
pwd: "$MONGODB_PASSWORD",
roles: [ { role: "userAdminAnyDatabase", db: "admin" } ]
}
)
EOF
fi
if [ $MONGODB_DATABASE ]; then
cat >> /tmp/initMongo.js <<EOF
db = db.getSiblingDB('$MONGODB_DATABASE')
db.createUser(
{
user: "$MONGODB_USER",
pwd: "$MONGODB_PASSWORD",
roles: [ { role: "readWrite", db: "$MONGODB_DATABASE" } ]
}
)
EOF
fi
mongo /tmp/initMongo.js >/dev/null
mongod $PROGRAM_OPTIONS --shutdown >/dev/null
# enable authentication in mongo configuration
echo "auth = true" >> $BITNAMI_APP_DIR/conf/mongodb.conf
}
print_mongo_user() {
if [ -z $MONGODB_USER ]; then
echo "**none**"
else
echo $MONGODB_USER
fi
}
print_mongo_password() {
if [ -z $MONGODB_PASSWORD ]; then
echo "**none**"
else
echo $MONGODB_PASSWORD
fi
}
print_mongo_database() {
if [ $MONGODB_DATABASE ]; then
echo "Database: $MONGODB_DATABASE"
fi
}