Add Hound code search configuration generator

Configuration is generated from Gerrit-supplied data.

Issue-ID: ONAPARC-540
Change-Id: I84d5b87580882926b916ed20dbcd2369be4c77f4
Signed-off-by: Pawel Wieczorek <p.wieczorek2@samsung.com>
diff --git a/bootstrap/codesearch/Vagrantfile b/bootstrap/codesearch/Vagrantfile
index a94b95c..b715ec5 100644
--- a/bootstrap/codesearch/Vagrantfile
+++ b/bootstrap/codesearch/Vagrantfile
@@ -2,6 +2,7 @@
 # -*- coding: utf-8 -*-
 
 host_ip = "192.168.121.1"
+synced_folder = "/vagrant"
 
 $replace_dns = <<-SCRIPT
   HOST_IP="$1"
@@ -11,6 +12,7 @@
 
 Vagrant.configure("2") do |config|
   config.vm.box = "generic/ubuntu1804"
+  config.vm.synced_folder ".", synced_folder, type: "rsync", rsync__exclude: "Vagrantfile"
   config.vm.provision "replace_dns", type: :shell, run: "always", inline: $replace_dns, args: host_ip
   config.vm.provision "dependencies", type: :shell, inline: <<-SHELL
     export DEBIAN_FRONTEND=noninteractive
@@ -21,4 +23,7 @@
     export GOPATH="${HOME}/go"
     go get -u github.com/hound-search/hound/cmds/...
   SHELL
+  config.vm.provision "generate_config", type: :shell, privileged: false, inline: <<-SHELL
+    python3 #{synced_folder}/create_config.py
+  SHELL
 end
diff --git a/bootstrap/codesearch/create_config.py b/bootstrap/codesearch/create_config.py
new file mode 100755
index 0000000..4009e51
--- /dev/null
+++ b/bootstrap/codesearch/create_config.py
@@ -0,0 +1,46 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+
+"""Create configuration for code search."""
+
+import json
+import urllib.request
+import sys
+
+ONAP_GERRIT = "https://gerrit.onap.org/r"
+API_PROJECTS = "/projects/"
+
+MAGIC_PREFIX = ")]}'"
+
+
+def get_projects_list():
+    """Request list of all available projects from ONAP Gerrit."""
+    resp = urllib.request.urlopen(ONAP_GERRIT + API_PROJECTS)
+    resp_body = resp.read()
+
+    no_magic = resp_body[len(MAGIC_PREFIX):]
+    decoded = no_magic.decode("utf-8")
+    projects = json.loads(decoded)
+
+    return projects.keys()
+
+
+def create_repos_list(projects):
+    """Create a map of all projects to their repositories' URLs."""
+    return {p: {"url": "{}/{}.git".format(ONAP_GERRIT, p)} for p in projects}
+
+
+def main():
+    """Main entry point for the script."""
+    repos = create_repos_list(get_projects_list())
+    config = {
+        "max-concurrent-indexers": 2,
+        "dbpath": "data",
+        "health-check-uri": "/healthz",
+        "repos": repos
+    }
+    print(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': ')))
+
+
+if __name__ == '__main__':
+    sys.exit(main())
diff --git a/bootstrap/codesearch/test-requirements.txt b/bootstrap/codesearch/test-requirements.txt
new file mode 100644
index 0000000..897e59f
--- /dev/null
+++ b/bootstrap/codesearch/test-requirements.txt
@@ -0,0 +1,2 @@
+flake8
+pylint
diff --git a/bootstrap/codesearch/tox.ini b/bootstrap/codesearch/tox.ini
new file mode 100644
index 0000000..3d0305b
--- /dev/null
+++ b/bootstrap/codesearch/tox.ini
@@ -0,0 +1,14 @@
+[tox]
+envlist = pep8, pylint
+skipsdist = true
+modules = create_config
+
+[testenv]
+basepython = python3
+deps = -r{toxinidir}/test-requirements.txt
+
+[testenv:pep8]
+commands = flake8 --max-line-length 100
+
+[testenv:pylint]
+commands = pylint -f parseable {[tox]modules}