-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmongoSeedJobScript.sh
More file actions
135 lines (106 loc) · 5.14 KB
/
mongoSeedJobScript.sh
File metadata and controls
135 lines (106 loc) · 5.14 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
129
130
131
132
133
134
135
#!/usr/bin/env bash
<<COMMENT
Script is used to seed the inital data for onprem instance:
export ASSETS_PATH=./assets/
export MONGO_URI=...
export MONGODB_ROOT_USER=...
export MONGODB_ROOT_PASSWORD=...
./mongoSeedJobScript.sh
COMMENT
if [[ -n $DEBUG ]]; then
set -o xtrace
fi
ASSETS_PATH=${ASSETS_PATH:-/usr/share/extras/}
MTLS_CERT_PATH=${MTLS_CERT_PATH:-/etc/ssl/mongodb/ca.pem}
MONGODB_DATABASES=(
"archive"
"audit"
"charts-manager"
"cluster-providers"
"codefresh"
"context-manager"
"gitops-dashboard-manager"
"k8s-monitor"
"pipeline-manager"
"platform-analytics-postgres"
"read-models"
"runtime-environment-manager"
"onboarding-status"
"payments"
)
disableMongoTelemetry() {
mongosh --nodb --eval "disableTelemetry()" || true
}
waitForMongoDB() {
while true; do
status=$(mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval "db.adminCommand('ping')" 2>&1)
echo -e "MongoDB status:\n$status"
if $(echo $status | grep 'ok: 1' -q); then
break
fi
echo "Sleeping 3 seconds ..."
sleep 3
done
}
parseMongoURI() {
local proto="$(echo $1 | grep :// | sed -e's,^\(.*://\).*,\1,g')"
local parameters="$(echo $1 | grep '?' | cut -d '?' -f2)"; if [[ -n $parameters ]]; then parameters="?${parameters}"; fi
local url="$(echo ${1/$proto/})"
local userpass="$(echo $url | grep @ | cut -d@ -f1)"
if [[ -z $userpass ]]; then
local hostport="$(echo $url | sed "s/\/\?$parameters//" | sed -re "s/\/\?|@//g" | sed 's/\/$//')"
MONGO_URI="$proto$hostport/${MONGODB_DATABASE}$parameters"
else
local hostport="$(echo $url | sed s/$userpass// | sed "s/\/\?$parameters//" | sed -re "s/\/\?|@//g" | sed 's/\/$//')"
MONGODB_PASSWORD="$(echo $userpass | grep : | cut -d: -f2)"
MONGODB_USER="$(echo $userpass | grep : | cut -d: -f1)"
MONGO_URI="$proto$userpass@$hostport/${MONGODB_DATABASE}$parameters"
fi
if [[ -z $MONGODB_ROOT_OPTIONS ]]; then
MONGODB_ROOT_URI="$proto${MONGODB_ROOT_USER}:${MONGODB_ROOT_PASSWORD}@$hostport/admin$parameters"
else
MONGODB_ROOT_URI="$proto${MONGODB_ROOT_USER}:${MONGODB_ROOT_PASSWORD}@$hostport/admin?${MONGODB_ROOT_OPTIONS}"
fi
}
getMongoVersion() {
MONOGDB_VERSION=$(mongosh ${MONGODB_ROOT_URI} --eval "db.version()" 2>&1 | tail -n1)
}
setSystemAdmin() {
mongosh $MONGO_URI --eval "db.users.update({}, {\$set: {roles: ['User', 'Admin', 'Account Admin']}}, {multi: true})"
}
setPacks() {
PACKS=$(cat ${ASSETS_PATH}packs.json)
mongosh $MONGO_URI --eval "db.accounts.update({}, {\$set: {'build.packs': ${PACKS} }}, {multi: true})"
PAYMENTS_MONGO_URI=${MONGO_URI/\/codefresh/\/payments}
mongosh $PAYMENTS_MONGO_URI --eval "db.accounts.update({}, {\$set: {'plan.packs': ${PACKS} }}, {multi: true})"
}
parseMongoURI $MONGO_URI
if [[ -s ${MTLS_CERT_PATH} ]]; then
MONGO_URI_EXTRA_PARAMS="--tls --tlsCertificateKeyFile ${MTLS_CERT_PATH} --tlsAllowInvalidHostnames --tlsAllowInvalidCertificates"
MONGOIMPORT_EXTRA_PARAMS="--ssl --sslPEMKeyFile ${MTLS_CERT_PATH} --sslAllowInvalidHostnames --sslAllowInvalidCertificates"
else
MONGO_URI_EXTRA_PARAMS=""
MONGOIMPORT_EXTRA_PARAMS=""
fi
disableMongoTelemetry
waitForMongoDB
getMongoVersion
for MONGODB_DATABASE in ${MONGODB_DATABASES[@]}; do
waitForMongoDB
mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval "db.getSiblingDB(\"${MONGODB_DATABASE}\").createUser({user: \"${MONGODB_USER}\", pwd: \"${MONGODB_PASSWORD}\", roles: [\"readWrite\"]})" 2>&1 || true
waitForMongoDB
mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval "db.getSiblingDB(\"${MONGODB_DATABASE}\").changeUserPassword(\"${MONGODB_USER}\",\"${MONGODB_PASSWORD}\")" 2>&1 || true
# MongoDB Atlas
mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval "db = db.getSiblingDB(\"${MONGODB_DATABASE}\"); db[\"${MONGODB_DATABASE}\"].insertOne({ name: \"init\", value: true })" 2>&1 || true
done
mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval "db.getSiblingDB(\"codefresh\").grantRolesToUser( \"${MONGODB_USER}\", [ { role: \"readWrite\", db: \"pipeline-manager\" } ] )" 2>&1 || true
mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval "db.getSiblingDB(\"codefresh\").grantRolesToUser( \"${MONGODB_USER}\", [ { role: \"readWrite\", db: \"platform-analytics-postgres\" } ] )" 2>&1 || true
mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval "db.getSiblingDB(\"codefresh\").changeUserPassword(\"${MONGODB_USER}\",\"${MONGODB_PASSWORD}\")" 2>&1 || true
if [[ $DEVELOPMENT_CHART == "true" ]]; then
mongoimport --uri ${MONGO_URI} ${MONGOIMPORT_EXTRA_PARAMS} --collection accounts --type json --legacy --file ${ASSETS_PATH}accounts-dev.json
setSystemAdmin
setPacks
fi
mongoimport --uri ${MONGO_URI} ${MONGOIMPORT_EXTRA_PARAMS} --collection idps --type json --legacy --file ${ASSETS_PATH}idps.json
mongoimport --uri ${MONGO_URI} ${MONGOIMPORT_EXTRA_PARAMS} --collection accounts --type json --legacy --file ${ASSETS_PATH}accounts.json
mongoimport --uri ${MONGO_URI} ${MONGOIMPORT_EXTRA_PARAMS} --collection users --type json --legacy --file ${ASSETS_PATH}users.json