-
Notifications
You must be signed in to change notification settings - Fork 2
/
mongodb-setup-scripts.yaml
66 lines (55 loc) · 2.04 KB
/
mongodb-setup-scripts.yaml
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
apiVersion: v1
kind: ConfigMap
metadata:
name: {{ template "hook-test.fullname" . }}-setup-script
labels:
app: {{ template "hook-test.name" . }}
chart: {{ .Chart.Name }}-{{ .Chart.Version }}
heritage: {{ .Release.Service }}
release: {{ .Release.Name }}
data:
setup.sh: |
#!/bin/sh
# Setup script to create MongoDB users
errorExit () {
echo; echo "ERROR: $1"; echo; exit 1
}
echo "Waiting for mongodb to come up"
until mongo --host {{ .Release.Name }}-mongodb --port 27017 --eval "db.adminCommand('ping')" > /dev/null 2>&1; do
echo "Waiting for db availability"
sleep 2
done
echo "DB ready. Configuring..."
mongo --host {{ .Release.Name }}-mongodb --port 27017 < /scripts/createMongoDBUsers.js || errorExit "DB user setup failed"
echo "DB config done"
createMongoDBUsers.js: |
// Creating default admin user
var adminUser = {
user: "{{ .Values.db.adminUser }}",
pwd: "{{ .Values.db.adminPassword }}",
roles: ["root"],
customData: {
createdBy: "App installer"
}
}
db.getSiblingDB("admin").createUser(adminUser)
// Creating default app user
var appUser = {
user: "{{ .Values.db.nodeUser }}",
pwd: "{{ .Values.db.nodePassword }}",
roles: ["dbOwner"],
customData: {
createdBy: "App installer"
}
}
// Authenticating as admin to create user
var loginOutput = db.getSiblingDB("admin").auth(adminUser.user,adminUser.pwd)
db.getSiblingDB("{{ .Values.db.nodeUser }}").createUser(appUser)
// Check if user exists before creation
function createUserDB(dbName, dbUser) {
db.getSiblingDB(dbName).getUser(dbUser.user) || db.getSiblingDB(dbName).createUser(dbUser);
}
createUserDB("{{ .Values.db.name }}", appUser);
db.users.insert({name:"Jainish Shah", age:27, email:"[email protected]"});
db.users.insert({name:"Elon Mask", age:46, email:"[email protected]"});
db.users.insert({name:"Jack Ma", age:53, email:"[email protected]"});