Setup gerrit local multi-site environment

Change-Id: I9e9c2e87892021f81b1c71658235f020feba07ae
diff --git a/setup_local_env/README.md b/setup_local_env/README.md
new file mode 100644
index 0000000..41fbee3
--- /dev/null
+++ b/setup_local_env/README.md
@@ -0,0 +1,55 @@
+# Local environment setup
+
+This script configures a full environment to simulate a Gerrit Multi-Site setup.
+The environment is composed by:
+ - 2 gerrit instances deployed by default in /tmp
+ - 1 kafka node and 1 zookeeper node
+ - 1 HA-PROXY
+
+## Requirements
+ - java
+ - docker and docker-compose
+ - wget
+ - envsubst
+ - haproxy
+
+## Examples
+Simplest setup with all default values and cleanup previous deployment
+```bash
+sh setup_local_env/setup.sh --release-war-file /path/to/release.war --multisite-plugin-file /path/to/multi-site.jar
+```
+Cleanup the previous deployments
+```bash
+sh setup_local_env/setup.sh --just-cleanup-env true
+```
+Help
+```bash
+Usage: sh setup.sh [--option ]
+
+[--release-war-file]            Location to release.war file
+[--multisite-plugin-file]       Location to plugin multi-site.jar file
+
+[--new-deployment]              Cleans up previous gerrit deployment and re-installs it. default true
+[--get-websession-plugin]       Download websession-flatfile plugin from CI lastSuccessfulBuild; default true
+[--deployment-location]         Base location for the test deployment; default /tmp
+
+[--gerrit-canonical-host]       The default host for Gerrit to be accessed through; default localhost
+[--gerrit-canonical-port]       The default port for Gerrit to be accessed throug; default 8080
+
+[--gerrit-ssh-advertised-port]  Gerrit Instance 1 sshd port; default 29418
+
+[--gerrit1-httpd-port]          Gerrit Instance 1 http port; default 18080
+[--gerrit1-sshd-port]           Gerrit Instance 1 sshd port; default 39418
+
+[--gerrit2-httpd-port]          Gerrit Instance 2 http port; default 18081
+[--gerrit2-sshd-port]           Gerrit Instance 2 sshd port; default 49418
+
+[--replication-type]            Options [file,ssh]; default ssh
+[--replication-ssh-user]        SSH user for the replication plugin; default $(whoami)
+[--just-cleanup-env]            Cleans up previous deployment; default false
+```
+
+## Limitations
+	- Assumes the ssh replication is done always on port 22 on both instances
+	- When cloning projects via ssh, public keys entries are added to `known_hosts`
+		- Clean up the old entries when doing a new deploymet, otherwise just use HTTP
\ No newline at end of file
diff --git a/setup_local_env/configs/gerrit.config b/setup_local_env/configs/gerrit.config
new file mode 100644
index 0000000..4dcbb41
--- /dev/null
+++ b/setup_local_env/configs/gerrit.config
@@ -0,0 +1,36 @@
+[gerrit]
+    basePath = git
+    serverId = 69ec38f0-350e-4d9c-96d4-bc956f2faaac
+    canonicalWebUrl = http://$GERRIT_CANONICAL_HOSTNAME:$GERRIT_CANONICAL_PORT/
+[database]
+    type = h2
+    database = $LOCATION_TEST_SITE/db/ReviewDB
+[noteDb "changes"]
+    autoMigrate = true
+    disableReviewDb = true
+    primaryStorage = note db
+    read = true
+    sequence = true
+    write = true
+[container]
+    javaOptions = "-Dflogger.backend_factory=com.google.common.flogger.backend.log4j.Log4jBackendFactory#getInstance"
+    javaOptions = "-Dflogger.logging_context=com.google.gerrit.server.logging.LoggingContext#getInstance"
+[index]
+    type = LUCENE
+[auth]
+    type = DEVELOPMENT_BECOME_ANY_ACCOUNT
+[receive]
+    enableSignedPush = false
+[sendemail]
+    smtpServer = localhost
+[sshd]
+    listenAddress = *:$GERRIT_SSHD_PORT
+    advertisedAddress = *:$SSH_ADVERTISED_PORT
+[httpd]
+    listenUrl = proxy-http://*:$GERRIT_HTTPD_PORT/
+[cache]
+    directory = cache
+[plugins]
+    allowRemoteAdmin = true
+[plugin "websession-flatfile"]
+    directory = $FAKE_NFS
\ No newline at end of file
diff --git a/setup_local_env/configs/healthcheck.config b/setup_local_env/configs/healthcheck.config
new file mode 100644
index 0000000..69f87f7
--- /dev/null
+++ b/setup_local_env/configs/healthcheck.config
@@ -0,0 +1,9 @@
+[healthcheck]
+  timeout = 10s
+
+[healthcheck "querychanges"]
+  query = status:open OR status:merged OR status:abandoned
+  limit = 0 # there are no changes
+
+[healthcheck "auth"]
+  userame = "admin"
\ No newline at end of file
diff --git a/setup_local_env/configs/multi-site.config b/setup_local_env/configs/multi-site.config
new file mode 100644
index 0000000..f88c788
--- /dev/null
+++ b/setup_local_env/configs/multi-site.config
@@ -0,0 +1,18 @@
+[index]
+	maxTries = 50
+	retryInterval = 30000
+[kafka]
+	bootstrapServers = localhost:$KAFKA_PORT
+	securityProtocol = PLAINTEXT
+	indexEventTopic = gerrit_index
+	streamEventTopic = gerrit_stream
+	projectListEventTopic = gerrit_list_project
+	cacheEventTopic = gerrit_cache_eviction
+[kafka "subscriber"]
+	enabled = true
+	pollingIntervalMs = 1000
+	KafkaProp-enableAutoCommit = true
+	KafkaProp-autoCommitIntervalMs = 1000
+	KafkaProp-autoOffsetReset = latest
+[kafka "publisher"]
+	enabled = true
\ No newline at end of file
diff --git a/setup_local_env/configs/replication.config b/setup_local_env/configs/replication.config
new file mode 100644
index 0000000..d866228
--- /dev/null
+++ b/setup_local_env/configs/replication.config
@@ -0,0 +1,12 @@
+[remote "Replication"]
+    $REPLICATION_URL
+    push = +refs/*:refs/*
+    timeout = 600
+    rescheduleDelay = 15
+    replicationDelay = 5
+[gerrit]
+    autoReload = true
+    replicateOnStartup = true
+[replication]
+    lockErrorMaxRetries = 5
+    maxRetries = 5
\ No newline at end of file
diff --git a/setup_local_env/docker-compose.kafka-broker.yaml b/setup_local_env/docker-compose.kafka-broker.yaml
new file mode 100644
index 0000000..b7e91f0
--- /dev/null
+++ b/setup_local_env/docker-compose.kafka-broker.yaml
@@ -0,0 +1,15 @@
+version: '3'
+services:
+  zookeeper:
+    image: wurstmeister/zookeeper:latest
+    ports:
+      - "2181:2181"
+    container_name: zk_test_node
+  kafka:
+    image: wurstmeister/kafka:2.12-2.1.0
+    ports:
+      - "9092:9092"
+    container_name: kafka_test_node
+    environment:
+      KAFKA_ADVERTISED_HOST_NAME: 127.0.0.1
+      KAFKA_ZOOKEEPER_CONNECT: zookeeper:2181
diff --git a/setup_local_env/haproxy-config/haproxy.cfg b/setup_local_env/haproxy-config/haproxy.cfg
new file mode 100644
index 0000000..40bbda6
--- /dev/null
+++ b/setup_local_env/haproxy-config/haproxy.cfg
@@ -0,0 +1,66 @@
+global
+    log 127.0.0.1   local0
+    log 127.0.0.1   local1 debug
+    maxconn 4096
+
+defaults
+    log     global
+    mode    http
+    option  httplog
+    option  dontlognull
+    retries 3
+    option redispatch
+    maxconn 2000
+    timeout connect      5000
+    timeout client      50000
+    timeout server      50000
+
+frontend haproxynode
+    bind *:$HA_GERRIT_CANONICAL_PORT
+    mode http
+    acl redirect_reads url_reg -i git-upload-pack
+    acl redirect_reads url_reg -i clone.bundle
+    acl redirect_writes url_reg -i git-receive-pack
+    use_backend read-backendnodes if redirect_reads
+    use_backend write-backendnodes if redirect_writes
+    default_backend read-backendnodes
+
+frontend git_ssh
+    bind *:$SSH_ADVERTISED_PORT
+    option tcplog
+    mode tcp
+    timeout client  5m
+    default_backend ssh
+
+
+backend read-backendnodes
+    mode http
+    balance source
+    option forwardfor
+    http-request set-header X-Forwarded-Port %[dst_port]
+    default-server inter 10s fall 3 rise 2
+    option httpchk GET /config/server/healthcheck~status HTTP/1.0
+    http-check expect status 200
+    server node1 $HA_GERRIT_SITE1_HOSTNAME:$HA_GERRIT_SITE1_HTTPD_PORT check inter 10s
+    server node2 $HA_GERRIT_SITE2_HOSTNAME:$HA_GERRIT_SITE2_HTTPD_PORT check inter 10s
+
+backend write-backendnodes
+    mode http
+    balance roundrobin
+    option forwardfor
+    http-request set-header X-Forwarded-Port %[dst_port]
+    default-server inter 10s fall 3 rise 2
+    option httpchk GET /config/server/healthcheck~status HTTP/1.0
+    http-check expect status 200
+    server node1 $HA_GERRIT_SITE1_HOSTNAME:$HA_GERRIT_SITE1_HTTPD_PORT check inter 10s
+    server node2 $HA_GERRIT_SITE2_HOSTNAME:$HA_GERRIT_SITE2_HTTPD_PORT check inter 10s backup
+
+backend ssh
+    mode tcp
+    option redispatch
+    option httpchk GET /config/server/healthcheck~status HTTP/1.0
+    balance roundrobin
+    timeout connect 10s
+    timeout server 5m
+    server ssh_node1 $HA_GERRIT_SITE1_HOSTNAME:$HA_GERRIT_SITE1_SSHD_PORT check inter 10s check port $HA_GERRIT_SITE1_HTTPD_PORT inter 10s
+    server ssh_node2 $HA_GERRIT_SITE2_HOSTNAME:$HA_GERRIT_SITE2_SSHD_PORT check inter 10s check port $HA_GERRIT_SITE2_HTTPD_PORT inter 10s backup
\ No newline at end of file
diff --git a/setup_local_env/setup.sh b/setup_local_env/setup.sh
new file mode 100755
index 0000000..9cd69bc
--- /dev/null
+++ b/setup_local_env/setup.sh
@@ -0,0 +1,359 @@
+#!/bin/bash
+
+# Copyright (C) 2019 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.
+
+
+SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
+
+function check_application_requirements {
+	type haproxy >/dev/null 2>&1 || { echo >&2 "Require haproxy but it's not installed. Aborting."; exit 1; }
+	type java >/dev/null 2>&1 || { echo >&2 "Require java but it's not installed. Aborting."; exit 1; }
+	type docker >/dev/null 2>&1 || { echo >&2 "Require docker but it's not installed. Aborting."; exit 1; }
+	type docker-compose >/dev/null 2>&1 || { echo >&2 "Require docker-compose but it's not installed. Aborting."; exit 1; }
+	type wget >/dev/null 2>&1 || { echo >&2 "Require wget but it's not installed. Aborting."; exit 1; }
+	type envsubst >/dev/null 2>&1 || { echo >&2 "Require envsubst but it's not installed. Aborting."; exit 1; }
+}
+
+function get_replication_url {
+	REPLICATION_LOCATION_TEST_SITE=$1
+	REPLICATION_HOSTNAME=$2
+	USER=$REPLICATION_SSH_USER
+
+	if [ "$REPLICATION_TYPE" = "file" ];then
+		echo "url = file://$REPLICATION_LOCATION_TEST_SITE/git/#{name}#.git"
+	elif [ "$REPLICATION_TYPE" = "ssh" ];then
+		echo "url = ssh://$USER@$REPLICATION_HOSTNAME:$REPLICATION_LOCATION_TEST_SITE/git/#{name}#.git"
+	fi
+}
+
+function copy_config_files {
+	for file in `ls $SCRIPT_DIR/configs/*.config`
+	do
+		file_name=`basename $file`
+
+		CONFIG_TEST_SITE=$1
+		export GERRIT_HTTPD_PORT=$2
+		export LOCATION_TEST_SITE=$3
+		export GERRIT_SSHD_PORT=$4
+		export REPLICATION_HTTPD_PORT=$5
+		export REPLICATION_LOCATION_TEST_SITE=$6
+		export GERRIT_HOSTNAME=$7
+		export REPLICATION_HOSTNAME=$8
+		export REPLICATION_URL=$(get_replication_url $REPLICATION_LOCATION_TEST_SITE $REPLICATION_HOSTNAME)
+
+		echo "Replacing variables for file $file and copying to $CONFIG_TEST_SITE/$file_name"
+
+		cat $file | envsubst | sed 's/#{name}#/${name}/g' > $CONFIG_TEST_SITE/$file_name
+	done
+}
+function start_ha_proxy {
+
+	export HA_GERRIT_CANONICAL_HOSTNAME=$GERRIT_CANONICAL_HOSTNAME
+	export HA_GERRIT_CANONICAL_PORT=$GERRIT_CANONICAL_PORT
+
+	export HA_GERRIT_SITE1_HOSTNAME=$GERRIT_1_HOSTNAME
+	export HA_GERRIT_SITE2_HOSTNAME=$GERRIT_2_HOSTNAME
+	export HA_GERRIT_SITE1_HTTPD_PORT=$GERRIT_1_HTTPD_PORT
+	export HA_GERRIT_SITE2_HTTPD_PORT=$GERRIT_2_HTTPD_PORT
+
+	export HA_GERRIT_SITE1_SSHD_PORT=$GERRIT_1_SSHD_PORT
+	export HA_GERRIT_SITE2_SSHD_PORT=$GERRIT_2_SSHD_PORT
+
+	cat $SCRIPT_DIR/haproxy-config/haproxy.cfg | envsubst > $HA_PROXY_CONFIG_DIR/haproxy.cfg
+
+	echo "Starting HA-PROXY..."
+	haproxy -f $HA_PROXY_CONFIG_DIR/haproxy.cfg &
+}
+
+function deploy_config_files {
+	# KAFKA configuration
+	export KAFKA_PORT=9092
+
+	# SITE 1
+	GERRIT_SITE1_HOSTNAME=$1
+	GERRIT_SITE1_HTTPD_PORT=$2
+	GERRIT_SITE1_SSHD_PORT=$3
+	CONFIG_TEST_SITE_1=$LOCATION_TEST_SITE_1/etc
+	# SITE 2
+	GERRIT_SITE2_HOSTNAME=$4
+	GERRIT_SITE2_HTTPD_PORT=$5
+	GERRIT_SITE2_SSHD_PORT=$6
+	CONFIG_TEST_SITE_2=$LOCATION_TEST_SITE_2/etc
+
+	# Set config SITE1
+	copy_config_files $CONFIG_TEST_SITE_1 $GERRIT_SITE1_HTTPD_PORT $LOCATION_TEST_SITE_1 $GERRIT_SITE1_SSHD_PORT $GERRIT_SITE2_HTTPD_PORT $LOCATION_TEST_SITE_2 $GERRIT_SITE1_HOSTNAME $GERRIT_SITE2_HOSTNAME
+
+
+	# Set config SITE2
+	copy_config_files $CONFIG_TEST_SITE_2 $GERRIT_SITE2_HTTPD_PORT $LOCATION_TEST_SITE_2 $GERRIT_SITE2_SSHD_PORT $GERRIT_SITE1_HTTPD_PORT $LOCATION_TEST_SITE_1 $GERRIT_SITE1_HOSTNAME $GERRIT_SITE2_HOSTNAME
+}
+
+
+function cleanup_environment {
+	echo "Killing existing HA-PROXY setup"
+	kill $(ps -ax | grep haproxy | grep "gerrit_setup/ha-proxy-config" | awk '{print $1}') 2> /dev/null
+	echo "Stoping kafka and zk"
+	docker-compose -f $SCRIPT_DIR/docker-compose.kafka-broker.yaml down 2> /dev/null
+
+	echo "Stoping GERRIT instances"
+	$1/bin/gerrit.sh stop 2> /dev/null
+	$2/bin/gerrit.sh stop 2> /dev/null
+
+	echo "REMOVING setup directory $3"
+	rm -rf $3 2> /dev/null
+}
+
+function check_if_kafka_is_running {
+	echo $(docker inspect kafka_test_node 2> /dev/null | grep '"Running": true' | wc -l)
+}
+
+while [ $# -ne 0 ]
+do
+case "$1" in
+  "--help" )
+		echo "Usage: sh $0 [--option $value]"
+		echo
+		echo "[--release-war-file]				Location to release.war file"
+		echo "[--multisite-plugin-file]			Location to plugin multi-site.jar file"
+		echo
+		echo "[--new-deployment]				Cleans up previous gerrit deployment and re-installs it. default true"
+		echo "[--get-websession-plugin]			Download websession-flatfile plugin from CI lastSuccessfulBuild; default true"
+		echo "[--deployment-location]			Base location for the test deployment; default /tmp"
+		echo
+		echo "[--gerrit-canonical-host]			The default host for Gerrit to be accessed through; default localhost"
+		echo "[--gerrit-canonical-port]			The default port for Gerrit to be accessed throug; default 8080"
+		echo
+		echo "[--gerrit-ssh-advertised-port]	Gerrit Instance 1 sshd port; default 29418"
+		echo
+		echo "[--gerrit1-httpd-port]			Gerrit Instance 1 http port; default 18080"
+		echo "[--gerrit1-sshd-port]				Gerrit Instance 1 sshd port; default 39418"
+		echo
+		echo "[--gerrit2-httpd-port]			Gerrit Instance 2 http port; default 18081"
+		echo "[--gerrit2-sshd-port]				Gerrit Instance 2 sshd port; default 49418"
+		echo
+		echo "[--replication-type]				Options [file,ssh]; default ssh"
+		echo "[--replication-ssh-user]			SSH user for the replication plugin; default $(whoami)"
+		echo "[--just-cleanup-env]				Cleans up previous deployment; default false"
+		echo
+		exit 0
+  ;;
+  "--new-deployment")
+        NEW_INSTALLATION=$2
+		shift
+		shift
+  ;;
+  "--get-websession-plugin")
+		DOWNLOAD_WEBSESSION_FLATFILE=$2
+		shift
+		shift
+  ;;
+  "--deployment-location" )
+		DEPLOYMENT_LOCATION=$2
+		shift
+		shift
+  ;;
+  "--release-war-file" )
+		RELEASE_WAR_FILE_LOCATION=$2
+		shift
+		shift
+  ;;
+  "--multisite-plugin-file" )
+		MULTISITE_PLUGIN_LOCATION=$2
+		shift
+		shift
+  ;;
+  "--gerrit-canonical-host" )
+		export GERRIT_CANONICAL_HOSTNAME=$2
+		shift
+		shift
+  ;;
+  "--gerrit-canonical-port" )
+		export GERRIT_CANONICAL_PORT=$2
+		shift
+		shift
+  ;;
+  "--gerrit-ssh-advertised-port" )
+		export SSH_ADVERTISED_PORT=$2
+		shift
+		shift
+  ;;
+  "--gerrit1-httpd-port" )
+       	GERRIT_1_HTTPD_PORT=$2
+		shift
+		shift
+  ;;
+  "--gerrit2-httpd-port" )
+       	GERRIT_2_HTTPD_PORT=$2
+		shift
+		shift
+  ;;
+  "--gerrit1-sshd-port" )
+       	GERRIT_1_SSHD_PORT=$2
+		shift
+		shift
+  ;;
+  "--gerrit2-sshd-port" )
+       	GERRIT_2_SSHD_PORT=$2
+		shift
+		shift
+  ;;
+  "--replication-ssh-user" )
+		export REPLICATION_SSH_USER=$2
+		shift
+		shift
+  ;;
+  "--replication-type")
+		export REPLICATION_TYPE=$2
+		shift
+		shift
+  ;;
+  "--just-cleanup-env" )
+       	JUST_CLEANUP_ENV=$2
+		shift
+		shift
+  ;;
+  *	   )
+		echo "Unknown option argument: $1"
+		shift
+		shift
+  ;;
+esac
+done
+
+# Check application requirements
+check_application_requirements
+
+# Defaults
+NEW_INSTALLATION=${NEW_INSTALLATION:-"true"}
+DOWNLOAD_WEBSESSION_FLATFILE=${DOWNLOAD_WEBSESSION_FLATFILE:-"true"}
+DEPLOYMENT_LOCATION=${DEPLOYMENT_LOCATION:-"/tmp"}
+export GERRIT_CANONICAL_HOSTNAME=${GERRIT_CANONICAL_HOSTNAME:-"localhost"}
+export GERRIT_CANONICAL_PORT=${GERRIT_CANONICAL_PORT:-"8080"}
+GERRIT_1_HOSTNAME=${GERRIT_1_HOSTNAME:-"localhost"}
+GERRIT_2_HOSTNAME=${GERRIT_2_HOSTNAME:-"localhost"}
+GERRIT_1_HTTPD_PORT=${GERRIT_1_HTTPD_PORT:-"18080"}
+GERRIT_2_HTTPD_PORT=${GERRIT_2_HTTPD_PORT:-"18081"}
+GERRIT_1_SSHD_PORT=${GERRIT_1_SSHD_PORT:-"39418"}
+GERRIT_2_SSHD_PORT=${GERRIT_2_SSHD_PORT:-"49418"}
+REPLICATION_TYPE=${REPLICATION_TYPE:-"ssh"}
+REPLICATION_SSH_USER=${REPLICATION_SSH_USER:-$(whoami)}
+export SSH_ADVERTISED_PORT=${SSH_ADVERTISED_PORT:-"29418"}
+
+COMMON_LOCATION=$DEPLOYMENT_LOCATION/gerrit_setup
+LOCATION_TEST_SITE_1=$COMMON_LOCATION/instance-1
+LOCATION_TEST_SITE_2=$COMMON_LOCATION/instance-2
+HA_PROXY_CONFIG_DIR=$COMMON_LOCATION/ha-proxy-config
+
+RELEASE_WAR_FILE_LOCATION=${RELEASE_WAR_FILE_LOCATION:-bazel-bin/release.war}
+MULTISITE_PLUGIN_LOCATION=${MULTISITE_PLUGIN_LOCATION:-bazel-genfiles/plugins/multi-site/multi-site.jar}
+
+
+export FAKE_NFS=$COMMON_LOCATION/fake_nfs
+
+if [ "$JUST_CLEANUP_ENV" = "true" ];then
+	cleanup_environment $LOCATION_TEST_SITE_1 $LOCATION_TEST_SITE_2 $COMMON_LOCATION
+	exit 0
+fi
+
+if [ -z $RELEASE_WAR_FILE_LOCATION ];then
+	echo "A release.war file is required. Usage: sh $0 --release-war-file /path/to/release.war"
+	exit 1
+else
+	cp -f $RELEASE_WAR_FILE_LOCATION $DEPLOYMENT_LOCATION/gerrit.war >/dev/null 2>&1 || { echo >&2 "$RELEASE_WAR_FILE_LOCATION: Not able to copy the file. Aborting"; exit 1; }
+fi
+if [ -z $MULTISITE_PLUGIN_LOCATION ];then
+	echo "The multi-site plugin is required. Usage: sh $0 --multisite-plugin-file /path/to/multi-site.jar"
+	exit 1
+else
+	cp -f $MULTISITE_PLUGIN_LOCATION $DEPLOYMENT_LOCATION/multi-site.jar  >/dev/null 2>&1 || { echo >&2 "$MULTISITE_PLUGIN_LOCATION: Not able to copy the file. Aborting"; exit 1; }
+fi
+if [ $DOWNLOAD_WEBSESSION_FLATFILE = "true" ];then
+	echo "Downloading websession-flatfile plugin stable 2.16"
+	wget https://gerrit-ci.gerritforge.com/view/Plugins-stable-2.16/job/plugin-websession-flatfile-bazel-master-stable-2.16/lastSuccessfulBuild/artifact/bazel-genfiles/plugins/websession-flatfile/websession-flatfile.jar \
+	-O $DEPLOYMENT_LOCATION/websession-flatfile.jar
+	wget https://gerrit-ci.gerritforge.com/view/Plugins-stable-2.16/job/plugin-healthcheck-bazel-stable-2.16/lastSuccessfulBuild/artifact/bazel-genfiles/plugins/healthcheck/healthcheck.jar \
+	-O $DEPLOYMENT_LOCATION/healthcheck.jar
+else
+	echo "Without the websession-flatfile; user login via haproxy will fail."
+fi
+
+if [ "$REPLICATION_TYPE" = "ssh" ];then
+	echo "Using 'SSH' replication type"
+	echo "Make sure ~/.ssh/authorized_keys and ~/.ssh/known_hosts are configured correctly"
+fi
+
+# New installation
+if [ $NEW_INSTALLATION = "true" ]; then
+
+	cleanup_environment $LOCATION_TEST_SITE_1 $LOCATION_TEST_SITE_2 $COMMON_LOCATION
+
+	echo "Setting up directories"
+	mkdir -p $LOCATION_TEST_SITE_1 $LOCATION_TEST_SITE_2 $HA_PROXY_CONFIG_DIR $FAKE_NFS
+	java -jar $DEPLOYMENT_LOCATION/gerrit.war init --batch --no-auto-start --install-all-plugins --dev -d $LOCATION_TEST_SITE_1
+
+	echo "Copy multi-site plugin"
+	cp -f $DEPLOYMENT_LOCATION/multi-site.jar $LOCATION_TEST_SITE_1/plugins/multi-site.jar
+
+	echo "Copy websession-flatfile plugin"
+	cp -f $DEPLOYMENT_LOCATION/websession-flatfile.jar $LOCATION_TEST_SITE_1/plugins/websession-flatfile.jar
+
+	echo "Copy healthcheck plugin"
+	cp -f $DEPLOYMENT_LOCATION/healthcheck.jar $LOCATION_TEST_SITE_1/plugins/healthcheck.jar
+
+	echo "Re-indexing"
+	java -jar $DEPLOYMENT_LOCATION/gerrit.war reindex -d $LOCATION_TEST_SITE_1
+	# Replicating environment
+	echo "Replicating environment"
+	cp -fR $LOCATION_TEST_SITE_1/* $LOCATION_TEST_SITE_2
+fi
+
+
+IS_KAFKA_RUNNING=$(check_if_kafka_is_running)
+if [ $IS_KAFKA_RUNNING -lt 1 ];then
+
+	echo "Starting zk and kafka"
+	docker-compose -f $SCRIPT_DIR/docker-compose.kafka-broker.yaml up -d
+	echo "Waiting for kafka to start..."
+	while [[ $(check_if_kafka_is_running) -lt 1 ]];do sleep 10s; done
+fi
+
+echo "Re-deploying configuration files"
+deploy_config_files $GERRIT_1_HOSTNAME $GERRIT_1_HTTPD_PORT $GERRIT_1_SSHD_PORT $GERRIT_2_HOSTNAME $GERRIT_2_HTTPD_PORT $GERRIT_2_SSHD_PORT
+echo "Starting gerrit site 1"
+$LOCATION_TEST_SITE_1/bin/gerrit.sh restart
+echo "Starting gerrit site 2"
+$LOCATION_TEST_SITE_2/bin/gerrit.sh restart
+
+
+if [[ $(ps -ax | grep haproxy | grep "gerrit_setup/ha-proxy-config" | awk '{print $1}' | wc -l) -lt 1 ]];then
+	echo "Starting haproxy"
+	start_ha_proxy
+fi
+
+echo "==============================="
+echo "Current gerrit multi-site setup"
+echo "==============================="
+echo "deployment-location=$DEPLOYMENT_LOCATION"
+echo "replication-type=$REPLICATION_TYPE"
+echo "replication-ssh-user=$REPLICATION_SSH_USER"
+echo
+echo "GERRIT HA-PROXY: http://$GERRIT_CANONICAL_HOSTNAME:$GERRIT_CANONICAL_PORT"
+echo "GERRIT-1: http://$GERRIT_1_HOSTNAME:$GERRIT_1_HTTPD_PORT"
+echo "GERRIT-2: http://$GERRIT_2_HOSTNAME:$GERRIT_2_HTTPD_PORT"
+echo
+echo "Site-1: $LOCATION_TEST_SITE_1"
+echo "Site-2: $LOCATION_TEST_SITE_2"
+
+exit $?
\ No newline at end of file