Allow restart of Gerrit instances

Only initialization of the environment was possible. This change
allows the restart of the Gerrit instances to simulate, for example,
an upgrade.

Feature: Issue 10594
Change-Id: I30c0f50d4db84cbe0767630a3d46d195d8d57fbb
diff --git a/dockerised_local_env/.gitignore b/dockerised_local_env/.gitignore
index 714b661..c65f4d5 100644
--- a/dockerised_local_env/.gitignore
+++ b/dockerised_local_env/.gitignore
@@ -6,6 +6,7 @@
 gerrit-1/data
 gerrit-1/bin
 gerrit-1/etc
+gerrit-1/tmp
 gerrit-2/plugins
 gerrit-2/db
 gerrit-2/git
@@ -14,3 +15,4 @@
 gerrit-2/data
 gerrit-2/bin
 gerrit-2/etc
+gerrit-2/tmp
diff --git a/dockerised_local_env/Makefile b/dockerised_local_env/Makefile
index d4f5c39..ae1c85b 100644
--- a/dockerised_local_env/Makefile
+++ b/dockerised_local_env/Makefile
@@ -4,6 +4,8 @@
 GERRIT_2_PLUGINS_DIRECTORY=./gerrit-2/plugins
 GERRIT_1_BIN_DIRECTORY=./gerrit-1/bin
 GERRIT_2_BIN_DIRECTORY=./gerrit-2/bin
+GERRIT_1_ETC_DIRECTORY=./gerrit-1/etc
+GERRIT_2_ETC_DIRECTORY=./gerrit-2/etc
 GERRIT_1_PLUGINS_DIRECTORY=./gerrit-1/plugins
 GERRIT_2_PLUGINS_DIRECTORY=./gerrit-2/plugins
 CORE_PLUGINS=replication
@@ -14,7 +16,7 @@
 all: prepare download build
 
 prepare:
-	-mkdir -p $(GERRIT_1_PLUGINS_DIRECTORY) $(GERRIT_2_PLUGINS_DIRECTORY) $(GERRIT_1_BIN_DIRECTORY) $(GERRIT_2_BIN_DIRECTORY)
+	-mkdir -p $(GERRIT_1_PLUGINS_DIRECTORY) $(GERRIT_2_PLUGINS_DIRECTORY) $(GERRIT_1_BIN_DIRECTORY) $(GERRIT_2_BIN_DIRECTORY) $(GERRIT_1_ETC_DIRECTORY) $(GERRIT_2_ETC_DIRECTORY)
 
 download: gerrit plugin_websession_flatfile \
 	plugin_healthcheck \
@@ -24,7 +26,7 @@
 gerrit: prepare
 	$(WGET) $(CI_URL)/$(GERRIT_JOB)/lastSuccessfulBuild/artifact/gerrit/bazel-bin/release.war -P $(GERRIT_1_BIN_DIRECTORY)
 	cp $(GERRIT_1_BIN_DIRECTORY)/*.war $(GERRIT_2_BIN_DIRECTORY)
-	for plugin in $(CORE_PLUGINS); do $(WGET) $(CI_URL)/$(GERRIT_JOB)/$(BUILD_NUM)/artifact/gerrit/bazel-genfiles/plugins/$$plugin/$$plugin.jar -P $(GERRIT_1_PLUGINS_DIRECTORY); done
+	for plugin in $(CORE_PLUGINS); do $(WGET) $(CI_URL)/$(GERRIT_JOB)/lastSuccessfulBuild/artifact/gerrit/bazel-genfiles/plugins/$$plugin/$$plugin.jar -P $(GERRIT_1_PLUGINS_DIRECTORY); done
 	cp $(GERRIT_1_PLUGINS_DIRECTORY)/*.jar $(GERRIT_2_PLUGINS_DIRECTORY)
 
 plugin_websession_flatfile: prepare
@@ -44,11 +46,9 @@
 	docker build -t $(MYDIR) ./gerrit-2
 
 clean_gerrit: prepare
-	rm -fr gerrit-1/db/ gerrit-1/data/ gerrit-1/cache/ gerrit-1/db/ gerrit-1/git/ gerrit-1/indexes/ gerrit-1/etc/
-	rm -fr gerrit-2/db/ gerrit-2/data/ gerrit-2/cache/ gerrit-2/db/ gerrit-2/git/ gerrit-2/indexes/ gerrit-1/etc/
-	-mkdir -p gerrit-{1,2}/etc/
-	export GERRIT_REPLICATION_INSTANCE=gerrit-2; cat ./gerrit-common/replication.config.template | envsubst '$${GERRIT_REPLICATION_INSTANCE}'  > ./gerrit-1/etc/replication.config
-	export GERRIT_REPLICATION_INSTANCE=gerrit-1; cat ./gerrit-common/replication.config.template | envsubst '$${GERRIT_REPLICATION_INSTANCE}'  > ./gerrit-2/etc/replication.config
+	-rm -fr gerrit-{1,2}/{db,data,cache,db,git,index,etc,bin,tmp}/*
+	export GERRIT_REPLICATION_INSTANCE=gerrit-2 REPLICATE_ON_STARTUP=true; cat ./gerrit-common/replication.config.template | envsubst '$${GERRIT_REPLICATION_INSTANCE} $${REPLICATE_ON_STARTUP}'  > ./gerrit-1/etc/replication.config
+	export GERRIT_REPLICATION_INSTANCE=gerrit-1 REPLICATE_ON_STARTUP=false; cat ./gerrit-common/replication.config.template | envsubst '$${GERRIT_REPLICATION_INSTANCE} $${REPLICATE_ON_STARTUP}'  > ./gerrit-2/etc/replication.config
 	cp ./gerrit-common/gerrit.config ./gerrit-1/etc
 	cp ./gerrit-common/gerrit.config ./gerrit-2/etc
 	cp ./gerrit-common/git-daemon.sh ./gerrit-1/bin
@@ -58,4 +58,12 @@
 	docker-compose down && docker-compose build gerrit-1 && docker-compose build gerrit-2 && docker-compose up -d gerrit-1 && docker-compose up -d gerrit-2
 
 init_all: clean_gerrit download
-		docker-compose down && docker-compose build && docker-compose up -d
+		docker-compose down && docker-compose build && INIT=1 docker-compose up -d
+restart_gerrit_1:
+		cp ./gerrit-common/*.db ./gerrit-1/db
+		-docker-compose kill gerrit-1
+		sleep 3; INIT=0 docker-compose up -d gerrit-1
+restart_gerrit_2:
+		cp ./gerrit-common/*.db ./gerrit-2/db
+		-docker-compose kill gerrit-2
+		sleep 3; INIT=0 docker-compose up -d gerrit-2
diff --git a/dockerised_local_env/README.md b/dockerised_local_env/README.md
index bf7d8e0..099be72 100644
--- a/dockerised_local_env/README.md
+++ b/dockerised_local_env/README.md
@@ -1,5 +1,16 @@
 # Dockerised test environment
 
+## Prerequisites
+
+ * envsubst:
+
+```bash
+brew install gettext
+brew link --force gettext
+```
+
+## Instructions
+
 The docker compose provided in this directory is meant to orchestrate the spin up
 of a dockerised test environment with the latest stable Gerrit version.
 Run it with:
@@ -15,3 +26,10 @@
 ```
 
 *NOTE:* If you want to run any ssh command as admin you can use the ssh keys into the *gerrit-{1,2}/ssh* directory.
+
+If you need to restart one of the Gerrit instances to simulate, for example,
+an upgrade, you can do it this way:
+
+```bash
+make restart_gerrit_1 # (or make restart_gerrit_2)
+```
diff --git a/dockerised_local_env/common-gerrit-config/replication.config.template b/dockerised_local_env/common-gerrit-config/replication.config.template
new file mode 100644
index 0000000..537e8d8
--- /dev/null
+++ b/dockerised_local_env/common-gerrit-config/replication.config.template
@@ -0,0 +1,17 @@
+[remote "Replication"]
+    url = git://${GERRIT_REPLICATION_INSTANCE}:9418/${name}.git
+    adminUrl = ssh://root@sshd:22/var/${GERRIT_REPLICATION_INSTANCE}/git/${name}.git
+    push = +refs/*:refs/*
+    timeout = 600
+    rescheduleDelay = 15
+    replicationDelay = 5
+    mirror = true
+    createMissingRepositories = true
+    replicateProjectDeletions = true
+    replicateHiddenProjects = true
+[gerrit]
+    autoReload = true
+    replicateOnStartup = true
+[replication]
+    lockErrorMaxRetries = 5
+    maxRetries = 5
diff --git a/dockerised_local_env/docker-compose.yaml b/dockerised_local_env/docker-compose.yaml
index 4e66ec9..9bf3fdf 100644
--- a/dockerised_local_env/docker-compose.yaml
+++ b/dockerised_local_env/docker-compose.yaml
@@ -4,28 +4,39 @@
     build: ./gerrit-1
     networks:
       gerrit-net:
+    environment:
+      INIT: ${INIT:-1}
     volumes:
        - ./gerrit-1/git:/var/gerrit/git
        - ./gerrit-1/logs:/var/gerrit/logs
        - ./gerrit-1/ssh:/var/gerrit/.ssh
        - ./gerrit-1/index:/var/gerrit/index
        - ./gerrit-1/data:/var/gerrit/data
+       - ./gerrit-1/etc:/var/gerrit/etc
+       - ./gerrit-1/db:/var/gerrit/db
+       - ./gerrit-1/plugins:/var/gerrit/plugins
+       - ./gerrit-1/tmp:/var/gerrit/tmp
     ports:
        - "29418:29418"
        - "8080:8080"
     depends_on:
-      - gerrit-2
       - sshd
   gerrit-2:
     build: ./gerrit-2
     networks:
       gerrit-net:
+    environment:
+      INIT: ${INIT:-1}
     volumes:
        - ./gerrit-2/git:/var/gerrit/git
        - ./gerrit-2/logs:/var/gerrit/logs
        - ./gerrit-2/ssh:/var/gerrit/.ssh
        - ./gerrit-2/index:/var/gerrit/index
        - ./gerrit-2/data:/var/gerrit/data
+       - ./gerrit-2/etc:/var/gerrit/etc
+       - ./gerrit-2/db:/var/gerrit/db
+       - ./gerrit-2/plugins:/var/gerrit/plugins
+       - ./gerrit-2/tmp:/var/gerrit/tmp
     ports:
        - "39418:29418"
        - "8081:8080"
diff --git a/dockerised_local_env/gerrit-1/Dockerfile b/dockerised_local_env/gerrit-1/Dockerfile
index d71b6c2..0842933 100644
--- a/dockerised_local_env/gerrit-1/Dockerfile
+++ b/dockerised_local_env/gerrit-1/Dockerfile
@@ -6,7 +6,7 @@
     apk add --no-cache git-daemon
 
 COPY --chown=gerrit:gerrit bin/release.war /var/gerrit/bin/gerrit.war
-COPY --chown=gerrit:gerrit plugins/* /var/gerrit/plugins/
+COPY --chown=gerrit:gerrit plugins /var/gerrit/plugins
 COPY --chown=gerrit:gerrit etc /var/gerrit/etc
 
 ADD bin/git-daemon.sh /usr/local/bin/git-daemon.sh
diff --git a/dockerised_local_env/gerrit-1/docker-entrypoint.sh b/dockerised_local_env/gerrit-1/docker-entrypoint.sh
index 5429a52..d0eb5f2 100755
--- a/dockerised_local_env/gerrit-1/docker-entrypoint.sh
+++ b/dockerised_local_env/gerrit-1/docker-entrypoint.sh
@@ -1,16 +1,13 @@
 #!/bin/bash -ex
 
-java -Xmx100g -jar /var/gerrit/bin/gerrit.war init -d /var/gerrit --batch --dev --install-all-plugins
-java -Xmx100g -jar /var/gerrit/bin/gerrit.war reindex -d /var/gerrit --index accounts
-java -Xmx100g -jar /var/gerrit/bin/gerrit.war reindex -d /var/gerrit --index groups
-
-echo "Gerrit configuration:"
-cat /var/gerrit/etc/gerrit.config
-echo "Replication plugin configuration:"
-cat /var/gerrit/etc/replication.config
-
 echo "Starting git daemon"
 /usr/local/bin/git-daemon.sh &
 
-sed -i -e 's/\-\-console-log//g' /var/gerrit/bin/gerrit.sh
-/var/gerrit/bin/gerrit.sh run
+if [[ $INIT == 1 ]]; then
+  echo "Initializing Gerrit..."
+  java -jar /var/gerrit/bin/gerrit.war init -d /var/gerrit --batch --dev --install-all-plugins --no-auto-start
+  java -jar /var/gerrit/bin/gerrit.war reindex -d /var/gerrit --index accounts
+  java -jar /var/gerrit/bin/gerrit.war reindex -d /var/gerrit --index groups
+fi
+
+java -jar /var/gerrit/bin/gerrit.war daemon
diff --git a/dockerised_local_env/gerrit-2/docker-entrypoint.sh b/dockerised_local_env/gerrit-2/docker-entrypoint.sh
index 01f983b..09a3f4d 100755
--- a/dockerised_local_env/gerrit-2/docker-entrypoint.sh
+++ b/dockerised_local_env/gerrit-2/docker-entrypoint.sh
@@ -1,19 +1,16 @@
 #!/bin/bash -ex
 
-java -Xmx100g -jar /var/gerrit/bin/gerrit.war init -d /var/gerrit --batch --dev --no-auto-start --install-all-plugins
-#
-echo "Gerrit configuration:"
-cat /var/gerrit/etc/gerrit.config
-echo "Replication plugin configuration:"
-cat /var/gerrit/etc/replication.config
-
-echo "Remove git repos created during init phase"
-rm -fr /var/gerrit/git/*
-
 echo "Starting git daemon"
 /usr/local/bin/git-daemon.sh &
 
-echo "Waiting for initial replication"
-sleep 120
+if [[ $INIT == 1 ]]; then
+  java -jar /var/gerrit/bin/gerrit.war init -d /var/gerrit --batch --dev --no-auto-start --install-all-plugins
 
-java -Xmx100g -jar /var/gerrit/bin/gerrit.war daemon
+  echo "Remove git repos created during init phase"
+  rm -fr /var/gerrit/git/*
+
+  echo "Waiting for initial replication"
+  sleep 120
+fi
+
+java -jar /var/gerrit/bin/gerrit.war daemon
diff --git a/dockerised_local_env/gerrit-common/ReviewDB.h2.db b/dockerised_local_env/gerrit-common/ReviewDB.h2.db
new file mode 100644
index 0000000..d23d444
--- /dev/null
+++ b/dockerised_local_env/gerrit-common/ReviewDB.h2.db
Binary files differ
diff --git a/dockerised_local_env/gerrit-common/ReviewDB.trace.db b/dockerised_local_env/gerrit-common/ReviewDB.trace.db
new file mode 100644
index 0000000..52681d8
--- /dev/null
+++ b/dockerised_local_env/gerrit-common/ReviewDB.trace.db
@@ -0,0 +1,3 @@
+11-23 08:24:02 jdbc[3]: exception
+org.h2.jdbc.JdbcSQLException: Table "SCHEMA_VERSION" not found; SQL statement:
+SELECT T.version_nbr,T.singleton FROM schema_version T WHERE T.singleton=? [42102-176]
diff --git a/dockerised_local_env/gerrit-common/gerrit.config b/dockerised_local_env/gerrit-common/gerrit.config
index c21242f..cefc723 100644
--- a/dockerised_local_env/gerrit-common/gerrit.config
+++ b/dockerised_local_env/gerrit-common/gerrit.config
@@ -4,8 +4,9 @@
 	canonicalWebUrl = http://localhost:8080/
 [database]
 	type = h2
-	database = /var/gerrit/db-volume-1/db/ReviewDB
+	database = db/ReviewDB
 [noteDb "changes"]
+	autoMigrate = true
 	disableReviewDb = true
 	primaryStorage = note db
 	read = true
diff --git a/dockerised_local_env/gerrit-common/replication.config.template b/dockerised_local_env/gerrit-common/replication.config.template
index 537e8d8..f51530f 100644
--- a/dockerised_local_env/gerrit-common/replication.config.template
+++ b/dockerised_local_env/gerrit-common/replication.config.template
@@ -11,7 +11,7 @@
     replicateHiddenProjects = true
 [gerrit]
     autoReload = true
-    replicateOnStartup = true
+    replicateOnStartup = ${REPLICATE_ON_STARTUP}
 [replication]
     lockErrorMaxRetries = 5
     maxRetries = 5