Merge "Refactoring adds SshUtil class"
diff --git a/.gitignore b/.gitignore
index e5d39e2..961867f 100644
--- a/.gitignore
+++ b/.gitignore
@@ -4,3 +4,4 @@
 /gc-executor.jar
 /gc.config
 /startup.log
+*.iml
diff --git a/src/test/docker/README.md b/src/test/docker/README.md
new file mode 100644
index 0000000..dab5898
--- /dev/null
+++ b/src/test/docker/README.md
@@ -0,0 +1,110 @@
+# Gerrit gc-conductor docker setup
+
+The Docker Compose project in the docker directory contains a simple
+test environment consisting of PostgreSQL database and a Gerrit website
+with gc-conductor and gc-executor plugins.
+
+## How to build and run
+
+Use docker-compose to build and run environment following the steps below:
+
+1. Build gc-executor_deploy.jar and place it to the directory where you'd like to use it from
+2. Go to docker directory: plugins/gc-conductor/src/test/docker
+3. Run docker_setup.sh script with three arguments:
+
+```
+--gc-conductor-path = path_to_gc_conductor.jar
+--gc-executor-path = path_to_gc_executor_deploy.jar
+--postgres-driver-path = path_to_postgresql-42.2.5.jar
+```
+
+The example bellow is the command to kick off the environment setup
+(expression in square brackets should be substituted with value):
+
+```
+$ sh docker_setup.sh \
+--gc-conductor-path [path_to_gc-conductor.jar] \
+--gc-executor-path [path_to_gc-executor_deploy.jar] \
+--postgres-driver-path [path_to_postgresql-42.2.5.jar]
+```
+
+You can add one of two optional arguments or both of them:
+
+```
+--postgres-image-path = postgresql_image_location
+--gerrit-image-path = gerrit_image_location
+```
+
+In case these parameters are not set, default values will be used:
+
+```
+--postgres-image-path = postgres
+--gerrit-image-path = gerritcodereview/gerrit
+```
+
+Once done, gerrit site will be available following the link:
+
+```
+http://localhost:8080
+```
+
+## How to debug Gerrit server
+
+The debug port 5005 is used when Gerrit is started. It allows user to debug Gerrit server.
+To learn how to attach IntelliJ to the Gerrit server remotely, see
+[Debugging a remote Gerrit server](https://gerrit-review.googlesource.com/Documentation/dev-intellij.html#remote-debug).
+
+## How to stop
+
+Use docker-compose with 'down' target to stop containers:
+
+```
+$ docker-compose down
+```
+
+Please use 'down' target with -v flag to stop containers
+and remove created volumes with initial setup:
+
+```
+$ docker-compose down -v
+```
+
+## How to run environment in detached mode
+
+If you want to run docker environment in detached mode, please add --detached-mode as shown below:
+
+```
+$ sh docker_setup.sh \
+--gc-conductor-path [path_to_gc-conductor.jar] \
+--gc-executor-path [path_to_gc-executor_deploy.jar] \
+--postgres-driver-path [path_to_postgresql-42.2.5.jar] \
+--detached-mode -d
+```
+
+## Examples
+
+Please see executable command example with values passed to arguments.
+This example uses path to gc-executor as /local/tmp/gc-executor_deploy.jar,
+that is provided in argument --gc-executor-path,
+and downloads postgres driver from https://repo1.maven.org/maven2/org/postgresql/postgresql/42.2.5/postgresql-42.2.5.jar,
+that is provided in argument --postgres-driver-path:
+
+```
+$ sh docker_setup.sh \
+--gc-conductor-path /local/tmp/gc-conductor.jar \
+--gc-executor-path /local/tmp/gc-executor_deploy.jar \
+--postgres-driver-path https://repo1.maven.org/maven2/org/postgresql/postgresql/42.2.5/postgresql-42.2.5.jar
+```
+
+Please see the same command but with optional arguments, where
+postgreSQL image source is provided in optional argument --postgres-image-path,
+gerrit image source is provided in optional argument --gerrit-image-path:
+
+```
+sh docker_setup.sh \
+--gc-conductor-path /local/tmp/gc-conductor.jar \
+--gc-executor-path /local/tmp/gc-executor_deploy.jar \
+--postgres-driver-path https://repo1.maven.org/maven2/org/postgresql/postgresql/42.2.5/postgresql-42.2.5.jar \
+--postgres-image-path postgres \
+--gerrit-image-path gerritcodereview/gerrit
+```
diff --git a/src/test/docker/docker-compose.yaml b/src/test/docker/docker-compose.yaml
new file mode 100644
index 0000000..5686ed9
--- /dev/null
+++ b/src/test/docker/docker-compose.yaml
@@ -0,0 +1,51 @@
+version: '3'
+
+services:
+
+  postgresql-gc:
+    build: postgresql
+    restart: always
+    ports:
+      - "5433:5432"
+    networks:
+      gerrit-net:
+        ipv4_address: 172.23.0.2
+    environment:
+      - POSTGRES_USER=postgres
+      - POSTGRES_PASSWORD=postgres
+    volumes:
+      - db:/var/lib/postgresql/data
+
+  gerrit-gc:
+    build: gerrit
+    restart: always
+    ports:
+      - "8080:8080"
+      - "29418:29418"
+      - "5005:5005"
+    networks:
+      gerrit-net:
+        ipv4_address: 172.23.0.3
+    volumes:
+      - gitvolume:/var/gerrit/git
+      - etcvolume:/var/gerrit/etc
+      - logsvolume:/var/gerrit/logs
+      - ./etc/gerrit.config:/var/gerrit/etc/gerrit.config.orig
+      - ./etc/gc.config:/var/gerrit/etc/gc.config.orig
+      - ./etc/log4j2.xml:/var/gerrit/etc/log4j2.xml.orig
+      - $GC_CONDUCTOR_PATH:/var/gerrit/plugins/gc-conductor.jar
+      - $GC_EXECUTOR_PATH:/var/gerrit/plugins/gc-executor.jar
+    depends_on:
+      - postgresql-gc
+
+networks:
+  gerrit-net:
+    driver: bridge
+    ipam:
+      config:
+        - subnet: 172.23.0.0/28
+volumes:
+  gitvolume:
+  etcvolume:
+  logsvolume:
+  db:
diff --git a/src/test/docker/docker_setup.sh b/src/test/docker/docker_setup.sh
new file mode 100755
index 0000000..7805009
--- /dev/null
+++ b/src/test/docker/docker_setup.sh
@@ -0,0 +1,73 @@
+#!/bin/bash -e
+
+if [ $# -eq 0 ]; then
+  echo "please add arguments --gc-executor-path and --postgres-driver-path"
+else
+  while [ $# -ne 0 ]; do
+    case "$1" in
+    "--help")
+      echo "Usage: sh $0 [--option $value]"
+      echo
+      echo "[--gc-conductor-path]            Path to gc-conductor.jar"
+      echo
+      echo "[--gc-executor-path]            Path to gc-executor.jar"
+      echo
+      echo "[--postgres-driver-path]        Path to postgresql-42.2.5.jar"
+      echo
+      echo "[--postgres-image-path]         PostgreSQL image path without image name (optional)"
+      echo
+      echo "[--gerrit-image-path]           Gerrit image path without image name (optional)"
+      echo
+      echo "[--detached-mode]               Argument to enable detached mode, [-d]"
+      exit 0
+      ;;
+    "--gc-conductor-path")
+          export GC_CONDUCTOR_PATH=$2
+          shift
+          shift
+          ;;
+    "--gc-executor-path")
+      export GC_EXECUTOR_PATH=$2
+      shift
+      shift
+      ;;
+    "--postgres-driver-path")
+      export POSTGRES_DRIVER_PATH=$2
+      shift
+      shift
+      ;;
+    "--postgres-image-path")
+      export POSTGRES_IMAGE_PATH=$2
+      shift
+      shift
+      ;;
+    "--gerrit-image-path")
+      export GERRIT_IMAGE_PATH=$2
+      shift
+      shift
+      ;;
+    "--detached-mode")
+      export DETACHED_MODE=$2
+      shift
+      shift
+      ;;
+    *)
+      echo "Unknown option argument: $1"
+      shift
+      shift
+      ;;
+    esac
+  done
+
+  if [ -z "$POSTGRES_IMAGE_PATH" ]; then
+    export POSTGRES_IMAGE_PATH=postgres
+  fi
+  if [ -z "$GERRIT_IMAGE_PATH" ]; then
+    export GERRIT_IMAGE_PATH=gerritcodereview/gerrit
+  fi
+
+  docker-compose build --build-arg POSTGRES_DRIVER=$POSTGRES_DRIVER_PATH \
+    --build-arg POSTGRES_IMAGE=$POSTGRES_IMAGE_PATH \
+    --build-arg GERRIT_IMAGE=$GERRIT_IMAGE_PATH
+  docker-compose up $DETACHED_MODE
+fi
diff --git a/src/test/docker/etc/gc.config b/src/test/docker/etc/gc.config
new file mode 100644
index 0000000..694c8f3
--- /dev/null
+++ b/src/test/docker/etc/gc.config
@@ -0,0 +1,20 @@
+[jvm]
+	javaHome = /usr/lib/jvm/java-11-openjdk-amd64/
+	javaOptions = -Xrunjdwp:transport=dt_socket,address=localhost:8788,server=y,suspend=n
+	javaOptions = -XX:MaxGCPauseMillis=2000
+	javaOptions = -Dlog4j2.configurationFile=file:/var/gerrit/etc/log4j2.xml
+	javaOptions = -Dlog4j2.debug=true
+
+[core]
+	executors = 2
+	pickOwnHostOnly = false
+	delay = 0
+
+[db]
+	databaseUrl = jdbc:postgresql://172.23.0.2:5432
+	databaseName = gc
+	username = gc
+	password = gc
+
+[evaluation]
+	repositoriesPath = /var/gerrit/git
diff --git a/src/test/docker/etc/gerrit.config b/src/test/docker/etc/gerrit.config
new file mode 100644
index 0000000..7396c38
--- /dev/null
+++ b/src/test/docker/etc/gerrit.config
@@ -0,0 +1,31 @@
+[gerrit]
+	basePath = git
+	canonicalWebUrl = http://localhost:8080/
+	serverId = f7696647-8efd-41b1-bd60-d321bc071ea9
+[index]
+	type = LUCENE
+[auth]
+	type = DEVELOPMENT_BECOME_ANY_ACCOUNT
+	cookiedomain = localhost
+[sendemail]
+	smtpServer = localhost
+[sshd]
+	listenAddress = *:29418
+[httpd]
+	listenUrl = proxy-http://*:8080/
+	requestLog = true
+[cache]
+	directory = cache
+[container]
+	user = gerrit
+[download]
+	scheme = http
+	scheme = ssh
+	scheme = anon_http
+[plugin "gc-conductor"]
+	databaseUrl = jdbc:postgresql://172.23.0.2:5432
+	username = gc
+	databaseName = gc
+	packed = 40
+	loose = 400
+	expireTimeRecheck = 5m
diff --git a/src/test/docker/etc/log4j2.xml b/src/test/docker/etc/log4j2.xml
new file mode 100644
index 0000000..3a81837
--- /dev/null
+++ b/src/test/docker/etc/log4j2.xml
@@ -0,0 +1,24 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<Configuration status="WARN" shutdownHook="disable">
+  <Properties>
+    <Property name="log.path">/var/gerrit/logs</Property>
+  </Properties>
+  <Appenders>
+    <RollingFile name="RollingFile" fileName="${log.path}/gc.log"
+        filePattern="${log.path}/gc.log.%d{yyyy-MM-dd}.gz"
+        ignoreExceptions="false">
+      <PatternLayout
+          pattern="%d{yyy-MM-dd HH:mm:ss.SSS} %-5level %logger{1} - %t - %msg%n"/>
+      <Policies>
+        <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
+        <SizeBasedTriggeringPolicy size="30 MB"/>
+      </Policies>
+    </RollingFile>
+  </Appenders>
+  <Loggers>
+    <Logger name="com.ericsson.gerrit.plugins.gcconductor" level="info"/>
+    <Root level="error">
+      <AppenderRef ref="RollingFile"/>
+    </Root>
+  </Loggers>
+</Configuration>
diff --git a/src/test/docker/gerrit/Dockerfile b/src/test/docker/gerrit/Dockerfile
new file mode 100644
index 0000000..34c1f8f
--- /dev/null
+++ b/src/test/docker/gerrit/Dockerfile
@@ -0,0 +1,23 @@
+ARG GERRIT_IMAGE
+
+FROM $GERRIT_IMAGE
+
+ENV GERRIT_BRANCH=stable-3.5
+
+ENV GERRIT_CI_URL=https://gerrit-ci.gerritforge.com/job
+ARG POSTGRES_DRIVER
+
+ADD --chown=gerrit:gerrit $GERRIT_CI_URL/plugin-javamelody-bazel-master-$GERRIT_BRANCH/lastSuccessfulBuild/artifact/bazel-bin/plugins/javamelody/javamelody.jar /var/gerrit/plugins/javamelody.jar
+ADD --chown=gerrit:gerrit $POSTGRES_DRIVER /var/gerrit/lib/postgresql-42.2.5.jar
+
+USER root
+
+ADD start_gerrit.sh /bin/
+
+RUN rm -Rf /var/gerrit/{git,index,cache}/*
+
+ARG GERRIT_UID=1000
+RUN usermod -u ${GERRIT_UID} gerrit &> /dev/null
+
+ENTRYPOINT ["/usr/bin/env"]
+CMD /bin/start_gerrit.sh
diff --git a/src/test/docker/gerrit/start_gerrit.sh b/src/test/docker/gerrit/start_gerrit.sh
new file mode 100755
index 0000000..28ff843
--- /dev/null
+++ b/src/test/docker/gerrit/start_gerrit.sh
@@ -0,0 +1,16 @@
+#!/bin/bash -e
+
+cp /var/gerrit/etc/gerrit.config.orig /var/gerrit/etc/gerrit.config
+cp /var/gerrit/etc/gc.config.orig /var/gerrit/etc/gc.config
+cp /var/gerrit/etc/log4j2.xml.orig /var/gerrit/etc/log4j2.xml
+
+echo "Initializing Gerrit site ..."
+java -jar /var/gerrit/bin/gerrit.war init -d /var/gerrit --batch
+
+touch /var/gerrit/logs/{gc_log,error_log,httpd_log,sshd_log,replication_log} && tail -f /var/gerrit/logs/* | grep --line-buffered -v 'HEAD /' &
+
+echo "Running gc-executor ..."
+java -DconfigFile=/var/gerrit/etc/gc.config -jar /var/gerrit/plugins/gc-executor.jar --console-log &
+
+echo "Running Gerrit ..."
+java -jar -agentlib:jdwp=transport=dt_socket,server=y,suspend=n,address=*:5005 /var/gerrit/bin/gerrit.war daemon -d /var/gerrit
diff --git a/src/test/docker/postgresql/Dockerfile b/src/test/docker/postgresql/Dockerfile
new file mode 100644
index 0000000..da40c6f
--- /dev/null
+++ b/src/test/docker/postgresql/Dockerfile
@@ -0,0 +1,5 @@
+ARG POSTGRES_IMAGE
+
+FROM $POSTGRES_IMAGE
+
+ADD init_setup.sql /docker-entrypoint-initdb.d/
diff --git a/src/test/docker/postgresql/init_setup.sql b/src/test/docker/postgresql/init_setup.sql
new file mode 100644
index 0000000..2e0746b
--- /dev/null
+++ b/src/test/docker/postgresql/init_setup.sql
@@ -0,0 +1,7 @@
+CREATE USER gc WITH PASSWORD 'gc';
+ALTER USER gc CREATEDB;
+CREATE DATABASE gc OWNER gc;
+
+CREATE USER root;
+ALTER USER root CREATEDB;
+CREATE DATABASE root OWNER root;