[DOC] Update documentation with recent additions
[integration.git] / bootstrap / codesearch / create_config.py
1 #!/usr/bin/env python3
2 # -*- coding: utf-8 -*-
3
4 """Create configuration for code search."""
5
6 import argparse
7 import json
8 import urllib.request
9 import sys
10
11 DEFAULT_GERRIT = "gerrit.onap.org"
12 API_PREFIX = "/r"
13 API_PROJECTS = "/projects/"
14
15 MAGIC_PREFIX = ")]}'"
16
17 CODE_LOCATION = "{path}{anchor}"
18 GITWEB_ANCHOR = "#l{line}"
19 GIT_ANCHOR = "#n{line}"
20
21 DEFAULT_POLL = 3600
22
23 def get_projects_list(gerrit):
24     """Request list of all available projects from ONAP Gerrit."""
25     resp = urllib.request.urlopen("https://{}{}{}".format(gerrit, API_PREFIX, API_PROJECTS))
26     resp_body = resp.read()
27
28     no_magic = resp_body[len(MAGIC_PREFIX):]
29     decoded = no_magic.decode("utf-8")
30     projects = json.loads(decoded)
31
32     return projects.keys()
33
34
35 def create_repos_list(projects, gerrit, ssh, git, poll):
36     """Create a map of all projects to their repositories' URLs."""
37     gerrit_url = "https://{}{}".format(gerrit, API_PREFIX)
38     git_url = "git://{}".format(git)
39     gerrit_project_url_base = "{}/{{}}.git".format(gerrit_url)
40     gitweb_code_url_base = "{}/gitweb?p={{}}.git;hb=HEAD;a=blob;f=".format(gerrit_url)
41     git_project_url_base = "{}/{{}}.git".format(git_url)
42
43     repos_list = {}
44     for project in projects:
45         project_url = gerrit_project_url_base.format(project)
46         code_url = gitweb_code_url_base.format(project) + CODE_LOCATION
47         anchor = GITWEB_ANCHOR
48
49         if ssh and len(ssh) == 2:
50             user, port = ssh[0], ssh[1]
51             project_url = "ssh://{}@{}:{}/{}.git".format(user, gerrit, port, project)
52         if git:
53             code_url = "https://{}/{}/tree/".format(git, project) + CODE_LOCATION
54             project_url = git_project_url_base.format(project)
55             anchor = GIT_ANCHOR
56
57         repos_list[project] = {
58             "url": project_url,
59             "url-pattern": {
60                 "base-url": code_url,
61                 "anchor": anchor,
62                 "ms-between-poll": poll * 1000
63             }
64         }
65
66     return repos_list
67
68
69 def parse_arguments():
70     """Return parsed command-line arguments."""
71     parser = argparse.ArgumentParser(description=__doc__)
72     group = parser.add_mutually_exclusive_group()
73     parser.add_argument('--gerrit', help='Gerrit address', default=DEFAULT_GERRIT)
74     group.add_argument('--ssh', help='SSH information for Gerrit access: user, port', nargs=2)
75     group.add_argument('--git', help='External git address. Does not support --ssh')
76     parser.add_argument('--poll-interval', help='Repositories polling interval in seconds', type=int, default=DEFAULT_POLL)
77
78     return parser.parse_args()
79
80
81 def main():
82     """Main entry point for the script."""
83     arguments = parse_arguments()
84
85     projects = get_projects_list(arguments.gerrit)
86     repos = create_repos_list(projects, arguments.gerrit, arguments.ssh, arguments.git, arguments.poll_interval)
87     config = {
88         "max-concurrent-indexers": 2,
89         "dbpath": "data",
90         "health-check-uri": "/healthz",
91         "repos": repos
92     }
93     print(json.dumps(config, sort_keys=True, indent=4, separators=(',', ': ')))
94
95
96 if __name__ == '__main__':
97     sys.exit(main())