Allow to specify global projects.

Replication and multi-site plugins accept projects filtering. This
functionality allows split projects into two types:
1. Global - available across all multi-site sites
2. Local - available only inside a single site

Add configuration parameter to specify a list of patterns used for
projects filtering

Feature: Issue 13361
Change-Id: Ic4f5a113aa248bda32e3677c8ed29ad490b6f79e
diff --git a/dual-master/Makefile b/dual-master/Makefile
index 5697fb0..1921528 100644
--- a/dual-master/Makefile
+++ b/dual-master/Makefile
@@ -89,6 +89,9 @@
 ifdef REMOTE_REPLICATION_TARGET_HOST
 		$(eval REMOTE_OPTIONAL_PARAMS := $(REMOTE_OPTIONAL_PARAMS) ParameterKey=RemoteReplicationTargetHost,ParameterValue=$(REMOTE_REPLICATION_TARGET_HOST))
 endif
+ifdef MULTISITE_GLOBAL_PROJECTS
+		$(eval REMOTE_OPTIONAL_PARAMS := $(REMOTE_OPTIONAL_PARAMS) ParameterKey=MultiSiteGlobalProjects,ParameterValue=\"$(MULTISITE_GLOBAL_PROJECTS)\")
+endif
 
 	$(AWS_FC_COMMAND) create-stack \
 		--stack-name $(SERVICE_MASTER1_STACK_NAME) \
@@ -140,6 +143,9 @@
 ifdef REMOTE_REPLICATION_TARGET_HOST
 		$(eval REMOTE_OPTIONAL_PARAMS := $(REMOTE_OPTIONAL_PARAMS) ParameterKey=RemoteReplicationTargetHost,ParameterValue=$(REMOTE_REPLICATION_TARGET_HOST))
 endif
+ifdef MULTISITE_GLOBAL_PROJECTS
+		$(eval REMOTE_OPTIONAL_PARAMS := $(REMOTE_OPTIONAL_PARAMS) ParameterKey=MultiSiteGlobalProjects,ParameterValue=\"$(MULTISITE_GLOBAL_PROJECTS)\")
+endif
 
 	$(AWS_FC_COMMAND) create-stack \
 		--stack-name $(SERVICE_MASTER2_STACK_NAME) \
diff --git a/dual-master/README.md b/dual-master/README.md
index 519aa05..fbdb39e 100644
--- a/dual-master/README.md
+++ b/dual-master/README.md
@@ -163,6 +163,10 @@
 store/retrieve information.
 Constraint: a slash-separated ('/') string not starting with a slash ('/')
 "gerrit/multi-site" by default.
+* `MULTISITE_GLOBAL_PROJECTS`: Optional. Comma separated list of patterns (see [projects.pattern](https://gerrit.googlesource.com/plugins/multi-site/+/refs/heads/stable-3.2/src/main/resources/Documentation/config.md))
+to specify which projects are available across all sites. This parametes applies to both multi-site
+and replication service remote destinations.
+Empty by default which means that all projects are available across all sites.
 
 ### 2 - Deploy
 
diff --git a/dual-master/cf-service-master.yml b/dual-master/cf-service-master.yml
index 2b80436..415439b 100644
--- a/dual-master/cf-service-master.yml
+++ b/dual-master/cf-service-master.yml
@@ -156,6 +156,10 @@
     Description: The fully qualified domain name of a remote replication target
     Type: String
     Default: ''
+  MultiSiteGlobalProjects:
+    Description: The list of global project patterns
+    Type: CommaDelimitedList
+    Default: ''
   MultiSiteEnabled:
     Description: Whether this gerrit is part of a multi-site cluster deployment
     Type: String
@@ -302,6 +306,8 @@
                       Value: !Join [',', !Ref MetricsCloudwatchExcludeMetrics]
                     - Name: REMOTE_REPLICATION_TARGET_HOST
                       Value: !Ref RemoteReplicationTargetHost
+                    - Name: MULTISITE_GLOBAL_PROJECTS
+                      Value: !Join [',', !Ref MultiSiteGlobalProjects]
                     - Name: MULTISITE_ENABLED
                       Value: !Ref MultiSiteEnabled
                     - Name: MULTISITE_KAFKA_BROKERS
diff --git a/gerrit/etc/multi-site.config b/gerrit/etc/multi-site.config
deleted file mode 100644
index 7bf8c32..0000000
--- a/gerrit/etc/multi-site.config
+++ /dev/null
@@ -1,4 +0,0 @@
-[index]
-  maxTries = 6
-  retryInterval = 30000
-  numStripedLocks = 100
\ No newline at end of file
diff --git a/gerrit/etc/multi-site.config.template b/gerrit/etc/multi-site.config.template
new file mode 100644
index 0000000..6b5fae9
--- /dev/null
+++ b/gerrit/etc/multi-site.config.template
@@ -0,0 +1,12 @@
+[index]
+  maxTries = 6
+  retryInterval = 30000
+  numStripedLocks = 100
+
+{%- if MULTISITE_GLOBAL_PROJECTS is defined and MULTISITE_GLOBAL_PROJECTS|length %}
+{%- set globalProjectsList = MULTISITE_GLOBAL_PROJECTS.split(',') %}
+[projects]
+ {%- for pattern in globalProjectsList %}
+  pattern = {{ pattern }}
+ {%- endfor %}
+{% endif %}
\ No newline at end of file
diff --git a/gerrit/etc/replication.config.template b/gerrit/etc/replication.config.template
index 6a16b5e..07d1067 100644
--- a/gerrit/etc/replication.config.template
+++ b/gerrit/etc/replication.config.template
@@ -22,6 +22,12 @@
 [remote "{{REMOTE_TARGET_URL}}"]
   url = {{ REMOTE_TARGET_URL }}
   adminUrl = {{ REMOTE_ADMIN_TARGET_URL }}
+{%- if MULTISITE_GLOBAL_PROJECTS is defined and MULTISITE_GLOBAL_PROJECTS|length -%}
+ {%- set globalProjectsList = MULTISITE_GLOBAL_PROJECTS.split(',') %}
+ {%- for pattern in globalProjectsList %}
+  projects = {{ pattern }}
+ {%- endfor -%}
+{% endif %}
   mirror = true
   push = +refs/*:refs/*
   threads = 10
diff --git a/gerrit/setup_gerrit.py b/gerrit/setup_gerrit.py
index 79cfa1f..1e63b32 100755
--- a/gerrit/setup_gerrit.py
+++ b/gerrit/setup_gerrit.py
@@ -182,7 +182,8 @@
                 REMOTE_TARGET=REMOTE_TARGET,
                 REMOTE_TARGET_URL="git://" + REMOTE_TARGET + ":" + os.getenv('GIT_PORT') + "/${name}.git",
                 REMOTE_ADMIN_TARGET_URL="ssh://gerrit@" + REMOTE_TARGET + ":" + os.getenv('GIT_SSH_PORT') + "/var/gerrit/git/${name}.git",
-                REPLICATE_ON_STARTUP=REPLICATE_ON_STARTUP
+                REPLICATE_ON_STARTUP=REPLICATE_ON_STARTUP,
+                MULTISITE_GLOBAL_PROJECTS=os.getenv('MULTISITE_GLOBAL_PROJECTS', '')
                 ))
 
 if (setupHA):
@@ -207,3 +208,12 @@
             MULTISITE_ZOOKEEPER_CONNECT_STRING=os.getenv('MULTISITE_ZOOKEEPER_CONNECT_STRING'),
             MULTISITE_ZOOKEEPER_ROOT_NODE=os.getenv('MULTISITE_ZOOKEEPER_ROOT_NODE')
         ))
+
+    CONFIGURATION_TARGET = GERRIT_CONFIG_DIRECTORY + "multi-site.config"
+
+    print("*** "+ CONFIGURATION_TARGET)
+    template = env.get_template("multi-site.config.template")
+    with open(CONFIGURATION_TARGET, 'w', encoding='utf-8') as f:
+        f.write(template.render(
+            MULTISITE_GLOBAL_PROJECTS=os.getenv('MULTISITE_GLOBAL_PROJECTS', '')
+        ))