@@ -12,9 +12,12 @@ export MONGODB_ROOT_PASSWORD=...
1212
1313COMMENT
1414
15- # set -eou pipefail
15+ if [[ -n $DEBUG ]]; then
16+ set -o xtrace
17+ fi
1618
1719ASSETS_PATH=${ASSETS_PATH:-/ usr/ share/ extras/ }
20+ MTLS_CERT_PATH=${MTLS_CERT_PATH:-/ etc/ ssl/ mongodb/ ca.pem}
1821
1922MONGODB_DATABASES=(
2023 " archive"
@@ -34,12 +37,12 @@ MONGODB_DATABASES=(
3437)
3538
3639disableMongoTelemetry () {
37- mongosh --nodb --eval " disableTelemetry()"
40+ mongosh --nodb --eval " disableTelemetry()" || true
3841}
3942
4043waitForMongoDB () {
4144 while true ; do
42- status=$( mongosh ${MONGODB_ROOT_URI} --eval " db.adminCommand('ping')" 2>&1 )
45+ status=$( mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.adminCommand('ping')" 2>&1 )
4346
4447 echo -e " MongoDB status:\n$status "
4548 if $( echo $status | grep ' ok: 1' -q) ; then
@@ -56,12 +59,23 @@ parseMongoURI() {
5659 local parameters=" $( echo $1 | grep ' ?' | cut -d ' ?' -f2) " ; if [[ -n $parameters ]]; then parameters=" ?${parameters} " ; fi
5760 local url=" $( echo ${1/ $proto / } ) "
5861 local userpass=" $( echo $url | grep @ | cut -d@ -f1) "
59- local hostport=" $( echo $url | sed s/$userpass // | sed " s/\/\?$parameters //" | sed -re " s/\/\?|@//g" | sed ' s/\/$//' ) "
62+ if [[ -z $userpass ]]; then
63+ local hostport=" $( echo $url | sed " s/\/\?$parameters //" | sed -re " s/\/\?|@//g" | sed ' s/\/$//' ) "
64+ MONGO_URI=" $proto$hostport /${MONGODB_DATABASE} $parameters "
65+ else
66+ local hostport=" $( echo $url | sed s/$userpass // | sed " s/\/\?$parameters //" | sed -re " s/\/\?|@//g" | sed ' s/\/$//' ) "
67+ MONGODB_PASSWORD=" $( echo $userpass | grep : | cut -d: -f2) "
68+ MONGODB_USER=" $( echo $userpass | grep : | cut -d: -f1) "
69+ MONGO_URI=" $proto$userpass @$hostport /${MONGODB_DATABASE} $parameters "
70+ fi
71+
72+
73+ if [[ -z $MONGODB_ROOT_OPTIONS ]]; then
74+ MONGODB_ROOT_URI=" $proto ${MONGODB_ROOT_USER} :${MONGODB_ROOT_PASSWORD} @$hostport /admin$parameters "
75+ else
76+ MONGODB_ROOT_URI=" $proto ${MONGODB_ROOT_USER} :${MONGODB_ROOT_PASSWORD} @$hostport /admin?${MONGODB_ROOT_OPTIONS} "
77+ fi
6078
61- MONGODB_PASSWORD=" $( echo $userpass | grep : | cut -d: -f2) "
62- MONGODB_USER=" $( echo $userpass | grep : | cut -d: -f1) "
63- MONGO_URI=" $proto$userpass @$hostport /${MONGODB_DATABASE} $parameters "
64- MONGODB_ROOT_URI=" $proto ${MONGODB_ROOT_USER} :${MONGODB_ROOT_PASSWORD} @$hostport /admin$parameters "
6579}
6680
6781getMongoVersion () {
@@ -82,6 +96,14 @@ setPacks() {
8296
8397parseMongoURI $MONGO_URI
8498
99+ if [[ -s ${MTLS_CERT_PATH} ]]; then
100+ MONGO_URI_EXTRA_PARAMS=" --tls --tlsCertificateKeyFile ${MTLS_CERT_PATH} --tlsAllowInvalidHostnames --tlsAllowInvalidCertificates"
101+ MONGOIMPORT_EXTRA_PARAMS=" --ssl --sslPEMKeyFile ${MTLS_CERT_PATH} --sslAllowInvalidHostnames --sslAllowInvalidCertificates"
102+ else
103+ MONGO_URI_EXTRA_PARAMS=" "
104+ MONGOIMPORT_EXTRA_PARAMS=" "
105+ fi
106+
85107disableMongoTelemetry
86108
87109waitForMongoDB
@@ -90,20 +112,20 @@ getMongoVersion
90112
91113for MONGODB_DATABASE in ${MONGODB_DATABASES[@]} ; do
92114 waitForMongoDB
93- mongosh ${MONGODB_ROOT_URI} --eval " db.getSiblingDB(\" ${MONGODB_DATABASE} \" ).createUser({user: \" ${MONGODB_USER} \" , pwd: \" ${MONGODB_PASSWORD} \" , roles: [\" readWrite\" ]})" 2>&1 || true
115+ 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
94116 waitForMongoDB
95- mongosh ${MONGODB_ROOT_URI} --eval " db.getSiblingDB(\" ${MONGODB_DATABASE} \" ).changeUserPassword(\" ${MONGODB_USER} \" ,\" ${MONGODB_PASSWORD} \" )" 2>&1 || true
117+ mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.getSiblingDB(\" ${MONGODB_DATABASE} \" ).changeUserPassword(\" ${MONGODB_USER} \" ,\" ${MONGODB_PASSWORD} \" )" 2>&1 || true
96118done
97119
98- mongosh ${MONGODB_ROOT_URI} --eval " db.getSiblingDB(\" codefresh\" ).grantRolesToUser( \" ${MONGODB_USER} \" , [ { role: \" readWrite\" , db: \" pipeline-manager\" } ] )" 2>&1 || true
99- mongosh ${MONGODB_ROOT_URI} --eval " db.getSiblingDB(\" codefresh\" ).grantRolesToUser( \" ${MONGODB_USER} \" , [ { role: \" readWrite\" , db: \" platform-analytics-postgres\" } ] )" 2>&1 || true
100- mongosh ${MONGODB_ROOT_URI} --eval " db.getSiblingDB(\" codefresh\" ).changeUserPassword(\" ${MONGODB_USER} \" ,\" ${MONGODB_PASSWORD} \" )" 2>&1 || true
120+ mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.getSiblingDB(\" codefresh\" ).grantRolesToUser( \" ${MONGODB_USER} \" , [ { role: \" readWrite\" , db: \" pipeline-manager\" } ] )" 2>&1 || true
121+ mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.getSiblingDB(\" codefresh\" ).grantRolesToUser( \" ${MONGODB_USER} \" , [ { role: \" readWrite\" , db: \" platform-analytics-postgres\" } ] )" 2>&1 || true
122+ mongosh ${MONGODB_ROOT_URI} ${MONGO_URI_EXTRA_PARAMS} --eval " db.getSiblingDB(\" codefresh\" ).changeUserPassword(\" ${MONGODB_USER} \" ,\" ${MONGODB_PASSWORD} \" )" 2>&1 || true
101123
102124if [[ $DEVELOPMENT_CHART == " true" ]]; then
103125 setSystemAdmin
104126 setPacks
105127fi
106128
107- mongoimport --uri ${MONGO_URI} --collection idps --type json --legacy --file ${ASSETS_PATH} idps.json
108- mongoimport --uri ${MONGO_URI} --collection accounts --type json --legacy --file ${ASSETS_PATH} accounts.json
109- mongoimport --uri ${MONGO_URI} --collection users --type json --legacy --file ${ASSETS_PATH} users.json
129+ mongoimport --uri ${MONGO_URI} ${MONGOIMPORT_EXTRA_PARAMS} --collection idps --type json --legacy --file ${ASSETS_PATH} idps.json
130+ mongoimport --uri ${MONGO_URI} ${MONGOIMPORT_EXTRA_PARAMS} --collection accounts --type json --legacy --file ${ASSETS_PATH} accounts.json
131+ mongoimport --uri ${MONGO_URI} ${MONGOIMPORT_EXTRA_PARAMS} --collection users --type json --legacy --file ${ASSETS_PATH} users.json
0 commit comments