blob: b881476e70eacbb7eebb342d9f65e377038ff938 [file] [log] [blame]
Pawel Wieczorek9ee12582020-01-07 15:26:52 +01001#!/usr/bin/env python3
2# -*- coding: utf-8 -*-
3
4"""Create configuration for code search."""
5
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +01006import argparse
Pawel Wieczorek9ee12582020-01-07 15:26:52 +01007import json
8import urllib.request
9import sys
10
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010011DEFAULT_GERRIT = "gerrit.onap.org"
12API_PREFIX = "/r"
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010013API_PROJECTS = "/projects/"
14
15MAGIC_PREFIX = ")]}'"
16
Pawel Wieczorek7be98612020-04-30 17:04:51 +020017CODE_LOCATION = "{path}{anchor}"
Pawel Wieczorek5af76842020-01-07 15:28:53 +010018GITWEB_ANCHOR = "#l{line}"
19GIT_ANCHOR = "#n{line}"
20
Bartek Grzybowskid671e652021-07-01 15:01:22 +020021DEFAULT_POLL = 3600
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010022
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010023def get_projects_list(gerrit):
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010024 """Request list of all available projects from ONAP Gerrit."""
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010025 resp = urllib.request.urlopen("https://{}{}{}".format(gerrit, API_PREFIX, API_PROJECTS))
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010026 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
Bartek Grzybowskid671e652021-07-01 15:01:22 +020035def create_repos_list(projects, gerrit, ssh, git, poll):
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010036 """Create a map of all projects to their repositories' URLs."""
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010037 gerrit_url = "https://{}{}".format(gerrit, API_PREFIX)
Bartek Grzybowski1fc7d0a2021-07-02 11:00:30 +020038 git_url = "git://{}".format(git)
Pawel Wieczorek7be98612020-04-30 17:04:51 +020039 gerrit_project_url_base = "{}/{{}}.git".format(gerrit_url)
40 gitweb_code_url_base = "{}/gitweb?p={{}}.git;hb=HEAD;a=blob;f=".format(gerrit_url)
Bartek Grzybowski1fc7d0a2021-07-02 11:00:30 +020041 git_project_url_base = "{}/{{}}.git".format(git_url)
Pawel Wieczorek5af76842020-01-07 15:28:53 +010042
Pawel Wieczoreke7c5ed52020-01-07 15:28:37 +010043 repos_list = {}
44 for project in projects:
Pawel Wieczorek7be98612020-04-30 17:04:51 +020045 project_url = gerrit_project_url_base.format(project)
46 code_url = gitweb_code_url_base.format(project) + CODE_LOCATION
Pawel Wieczorek8c7373d2020-01-07 15:28:55 +010047 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:
Pawel Wieczorek7be98612020-04-30 17:04:51 +020053 code_url = "https://{}/{}/tree/".format(git, project) + CODE_LOCATION
Bartek Grzybowski1fc7d0a2021-07-02 11:00:30 +020054 project_url = git_project_url_base.format(project)
Pawel Wieczorek5af76842020-01-07 15:28:53 +010055 anchor = GIT_ANCHOR
56
Pawel Wieczoreke7c5ed52020-01-07 15:28:37 +010057 repos_list[project] = {
Pawel Wieczorek5af76842020-01-07 15:28:53 +010058 "url": project_url,
Pawel Wieczoreke7c5ed52020-01-07 15:28:37 +010059 "url-pattern": {
Pawel Wieczorek5af76842020-01-07 15:28:53 +010060 "base-url": code_url,
Bartek Grzybowskid671e652021-07-01 15:01:22 +020061 "anchor": anchor,
62 "ms-between-poll": poll * 1000
Pawel Wieczoreke7c5ed52020-01-07 15:28:37 +010063 }
64 }
65
66 return repos_list
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010067
68
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010069def parse_arguments():
70 """Return parsed command-line arguments."""
71 parser = argparse.ArgumentParser(description=__doc__)
Bartek Grzybowski1fc7d0a2021-07-02 11:00:30 +020072 group = parser.add_mutually_exclusive_group()
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010073 parser.add_argument('--gerrit', help='Gerrit address', default=DEFAULT_GERRIT)
Bartek Grzybowski1fc7d0a2021-07-02 11:00:30 +020074 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)
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010077
78 return parser.parse_args()
79
80
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010081def main():
82 """Main entry point for the script."""
Pawel Wieczoreke2aa8482020-01-07 15:28:51 +010083 arguments = parse_arguments()
84
Pawel Wieczorek5af76842020-01-07 15:28:53 +010085 projects = get_projects_list(arguments.gerrit)
Bartek Grzybowskid671e652021-07-01 15:01:22 +020086 repos = create_repos_list(projects, arguments.gerrit, arguments.ssh, arguments.git, arguments.poll_interval)
Pawel Wieczorek9ee12582020-01-07 15:26:52 +010087 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
96if __name__ == '__main__':
97 sys.exit(main())