From c5cc41e551797b7bb056ee1ecbe935a6a740a613 Mon Sep 17 00:00:00 2001
From: portdirect <pete@port.direct>
Date: Sun, 19 Nov 2017 13:43:33 -0500
Subject: [PATCH] MongoDB: Fix race with setting up auth for DB

This PS moves the post-start script contents into the main launch
script to resolve a race condition and also make debugging issues
simpler.

Change-Id: I0b2216b0804db4d78991782ee31d2500b055447c
---
 .../templates/bin/_setup_admin_user.sh.tpl    | 28 ------------------
 mongodb/templates/bin/_start.sh.tpl           | 29 ++++++++++++++++++-
 mongodb/templates/configmap-bin.yaml          |  2 --
 mongodb/templates/statefulset.yaml            |  5 ----
 4 files changed, 28 insertions(+), 36 deletions(-)
 delete mode 100644 mongodb/templates/bin/_setup_admin_user.sh.tpl

diff --git a/mongodb/templates/bin/_setup_admin_user.sh.tpl b/mongodb/templates/bin/_setup_admin_user.sh.tpl
deleted file mode 100644
index 7a7b7ded86..0000000000
--- a/mongodb/templates/bin/_setup_admin_user.sh.tpl
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/bin/bash
-
-{{/*
-Copyright 2017 The Openstack-Helm Authors.
-
-Licensed under the Apache License, Version 2.0 (the "License");
-you may not use this file except in compliance with the License.
-You may obtain a copy of the License at
-
-   http://www.apache.org/licenses/LICENSE-2.0
-
-Unless required by applicable law or agreed to in writing, software
-distributed under the License is distributed on an "AS IS" BASIS,
-WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
-See the License for the specific language governing permissions and
-limitations under the License.
-*/}}
-
-set -ex
-mongo admin \
-  --username "${ADMIN_USER}" \
-  --password "${ADMIN_PASS}" \
-  --eval "db.changeUserPassword(\"${ADMIN_USER}\", \"${ADMIN_PASS}\")" || \
-    mongo admin \
-      --eval "db.createUser({ user: \"${ADMIN_USER}\", \
-                              pwd: \"${ADMIN_PASS}\", \
-                              roles: [ { role: \"userAdminAnyDatabase\", \
-                                         db: \"admin\" } ] });"
diff --git a/mongodb/templates/bin/_start.sh.tpl b/mongodb/templates/bin/_start.sh.tpl
index 25abe5acf1..33929549c0 100644
--- a/mongodb/templates/bin/_start.sh.tpl
+++ b/mongodb/templates/bin/_start.sh.tpl
@@ -17,4 +17,31 @@ limitations under the License.
 */}}
 
 set -ex
-exec mongod --auth
+
+mongod --auth &
+
+t=0
+until mongo --eval "db.adminCommand('ping')"; do
+  echo "waiting for mongodb to start"
+  sleep 1
+  t=$(($t+1))
+  if [ $t -ge 30 ] ; then
+      echo "mongodb did not start, giving up"
+      exit 1
+  fi
+done
+
+#NOTE(portdirect): stop sending commands to stdout to prevent root password
+# being sent to logs.
+set +x
+mongo admin \
+  --username "${ADMIN_USER}" \
+  --password "${ADMIN_PASS}" \
+  --eval "db.changeUserPassword(\"${ADMIN_USER}\", \"${ADMIN_PASS}\")" || \
+    mongo admin \
+      --eval "db.createUser({ user: \"${ADMIN_USER}\", \
+                              pwd: \"${ADMIN_PASS}\", \
+                              roles: [ { role: \"userAdminAnyDatabase\", \
+                                         db: \"admin\" } ] });"
+set -x
+wait
diff --git a/mongodb/templates/configmap-bin.yaml b/mongodb/templates/configmap-bin.yaml
index b98186f9d5..910719cb4e 100644
--- a/mongodb/templates/configmap-bin.yaml
+++ b/mongodb/templates/configmap-bin.yaml
@@ -24,6 +24,4 @@ metadata:
 data:
   start.sh: |
 {{ tuple "bin/_start.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
-  setup_admin_user.sh: |
-{{ tuple "bin/_setup_admin_user.sh.tpl" . | include "helm-toolkit.utils.template" | indent 4 }}
 {{- end }}
diff --git a/mongodb/templates/statefulset.yaml b/mongodb/templates/statefulset.yaml
index 116e6104f7..cdc920d170 100644
--- a/mongodb/templates/statefulset.yaml
+++ b/mongodb/templates/statefulset.yaml
@@ -52,11 +52,6 @@ spec:
                 key: MONGODB_ROOT_USERNAME
         command:
           - /tmp/start.sh
-        lifecycle:
-          postStart:
-            exec:
-              command:
-                - /tmp/setup_admin_user.sh
         livenessProbe:
           exec:
             command: