Do not enforce ReviewDB in the gerrit-slave chart

The gerrit-slave chart did expect to use a ReviewDB. Using a NoteDB was
not yet supported.

This change allows to configure, whether to use a ReviewDB. If a NoteDB
is used, a script also checks, whether the repositories containing the
NoteDB are present and waits until this is the case.

Change-Id: I824a8136e087e9287ba3b0a1c0325797092a32a0
diff --git a/container-images/gerrit-init/README.md b/container-images/gerrit-init/README.md
index 3f54bd5..2dc2333 100644
--- a/container-images/gerrit-init/README.md
+++ b/container-images/gerrit-init/README.md
@@ -32,6 +32,13 @@
 * waits for the reviewdb database
 + waits for some selected tables to ensure that the schema is initialized
 
+The `validate_notedb.py`-script
+
+* validates and waits for the repository `All-Projects.git` with the refs
+`refs/meta/config`.
+* validates and waits for the repository `All-Users.git` with the ref
+`refs/meta/config`.
+
 ## How to install/update python packages in container
 
 * Python 3.6 is required
diff --git a/container-images/gerrit-init/tools/validate_notedb.py b/container-images/gerrit-init/tools/validate_notedb.py
new file mode 100755
index 0000000..4fec993
--- /dev/null
+++ b/container-images/gerrit-init/tools/validate_notedb.py
@@ -0,0 +1,77 @@
+#!/usr/bin/python3
+
+# Copyright (C) 2018 The Android Open Source Project
+#
+# 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.
+
+import argparse
+import os
+import subprocess
+import time
+
+from log import get_logger
+
+LOG = get_logger("init")
+
+
+class NoteDbValidator:
+    def __init__(self, site):
+        self.site = site
+
+        self.notedb_repos = ["All-Projects.git", "All-Users.git"]
+        self.required_refs = {
+            "All-Projects.git": ["refs/meta/config"],
+            "All-Users.git": ["refs/meta/config"],
+        }
+
+    def _test_repo_exists(self, repo):
+        return os.path.exists(os.path.join(self.site, "git", repo))
+
+    def _test_ref_exists(self, repo, ref):
+        command = "git --git-dir %s/git/%s show-ref %s" % (self.site, repo, ref)
+        git_show_ref = subprocess.run(
+            command.split(), stdout=subprocess.PIPE, universal_newlines=True
+        )
+
+        return git_show_ref.returncode == 0
+
+    def execute(self):
+        for repo in self.notedb_repos:
+            LOG.info("Waiting for repository %s.", repo)
+            while not self._test_repo_exists(repo):
+                time.sleep(1)
+            LOG.info("Found %s.", repo)
+
+            for ref in self.required_refs[repo]:
+                LOG.info("Waiting for ref %s in repository %s.", ref, repo)
+                while not self._test_ref_exists(repo, ref):
+                    time.sleep(1)
+                LOG.info("Found ref %s in repo %s.", ref, repo)
+
+
+# pylint: disable=C0103
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        "-s",
+        "--site",
+        help="Path to Gerrit site",
+        dest="site",
+        action="store",
+        default="/var/gerrit",
+        required=True,
+    )
+    args = parser.parse_args()
+
+    init = NoteDbValidator(args.site)
+    init.execute()
diff --git a/helm-charts/gerrit-slave/README.md b/helm-charts/gerrit-slave/README.md
index 1569c12..1c2cbab 100644
--- a/helm-charts/gerrit-slave/README.md
+++ b/helm-charts/gerrit-slave/README.md
@@ -62,6 +62,21 @@
 [configuration section](#Configuration) lists the parameters that can be
 configured during installation.
 
+If the NoteDB is used, the Gerrit slave requires the replicated `All-Projects.git`-
+and `All-Users.git`-repositories to be present in the `/var/gerrit/git`-directory.
+The `gerrit-init`-InitContainer will wait for this being the case. A way to do
+this is to access the Gerrit slave pod and to clone the repositories from the
+Gerrit master (Make sure that you have the correct access rights do so.):
+
+```sh
+kubectl exec -it <gerrit-slave-pod> -c gerrit-init bash
+gerrit@<gerrit-slave-pod>:/var/tools$ cd /var/gerrit/git
+gerrit@<gerrit-slave-pod>:/var/gerrit/git$ git clone "http://gerrit-master.com/All-Projects" --mirror
+Cloning into bare repository 'All-Projects.git'...
+gerrit@<gerrit-slave-pod>:/var/gerrit/git$ git clone "http://gerrit-master.com/All-Users" --mirror
+Cloning into bare repository 'All-Users.git'...
+```
+
 ## Configuration
 
 The following sections list the configurable values in `values.yaml`. To configure
diff --git a/helm-charts/gerrit-slave/templates/gerrit-slave.deployment.yaml b/helm-charts/gerrit-slave/templates/gerrit-slave.deployment.yaml
index 68cfed3..b158e3e 100644
--- a/helm-charts/gerrit-slave/templates/gerrit-slave.deployment.yaml
+++ b/helm-charts/gerrit-slave/templates/gerrit-slave.deployment.yaml
@@ -57,7 +57,13 @@
         - -ce
         args:
         - |
+          mkdir -p /var/gerrit/etc
+          ln -sf /var/config/secure.config /var/gerrit/etc/secure.config
+
           /var/tools/gerrit_init.py \
+            {{- if .Values.gerritSlave.reviewdb.enabled }}
+            --reviewdb \
+            {{- end }}
             -s /var/gerrit \
             -p singleusergroup
 
@@ -66,6 +72,9 @@
         volumeMounts:
         - name: gerrit-site
           mountPath: "/var/gerrit"
+        - name: gerrit-slave-secure-config
+          mountPath: "/var/config/secure.config"
+          subPath: secure.config
       {{- end }}
       # Wait for database to be ready and, if configured, run initialization
       # taking the given Gerrit configuration and persisted volumes into account.
@@ -94,13 +103,20 @@
 
           {{ if .Values.gerritSlave.initializeTestSite.enabled -}}
           /var/tools/gerrit_init.py \
+            {{- if .Values.gerritSlave.reviewdb.enabled }}
+            --reviewdb \
+            {{- end }}
             -s /var/gerrit \
             -p singleusergroup
 
           symlink_config_to_site
           {{- end }}
 
+          {{- if .Values.gerritSlave.reviewdb.enabled }}
           /var/tools/validate_db.py -s /var/gerrit
+          {{- else }}
+          /var/tools/validate_notedb.py -s /var/gerrit
+          {{- end }}
         volumeMounts:
         - name: gerrit-site
           mountPath: "/var/gerrit"
diff --git a/helm-charts/gerrit-slave/values.yaml b/helm-charts/gerrit-slave/values.yaml
index abd3fdb..919e4f9 100644
--- a/helm-charts/gerrit-slave/values.yaml
+++ b/helm-charts/gerrit-slave/values.yaml
@@ -192,6 +192,9 @@
   # automatic encoding using helm does not work here.
   keystore:
 
+  reviewdb:
+    enabled: true
+
   config:
     # Some values are expected to have a specific value for the deployment installed
     # by this chart to work. These are marked with `# FIXED`.