-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgithub_component_discovery.py
120 lines (95 loc) · 3.68 KB
/
github_component_discovery.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python
"""Github discovery - queries the github API for info about hmpps services and stores the results in the service catalogue
Required environment variables
------------------------------
Alertmanager
- ALERTMANAGER_ENDPOINT: Alertmanager API endpoint
Github (Credentials for Discovery app that has access to the repositories)
- GITHUB_APP_ID: Github App ID
- GITHUB_APP_INSTALLATION_ID: Github App Installation ID
- GITHUB_APP_PRIVATE_KEY: Github App Private Key
Service Catalogue
- SERVICE_CATALOGUE_API_ENDPOINT: Service Catalogue API endpoint
- SERVICE_CATALOGUE_API_KEY: Service
- SLACK_BOT_TOKEN: Slack Bot Token
- CIRCLECI_API_ENDPOINT: CircleCI API endpoint
- CIRCLECI_TOKEN: CircleCI API token
Optional environment variables
- SLACK_NOTIFY_CHANNEL: Slack channel for notifications
- SLACK_ALERT_CHANNEL: Slack channel for alerts
- LOG_LEVEL: Log level (default: INFO)
"""
import os
import logging
import argparse
# Classes for the various parts of the script
# from classes.health import HealthServer
from classes.service_catalogue import ServiceCatalogue
from classes.github import GithubSession
from classes.alertmanager import AlertmanagerData
from classes.circleci import CircleCI
# Components
import processes.components as components
# Set maximum number of concurrent threads to run, try to avoid secondary github api limits.
max_threads = 10
log_level = os.environ.get('LOG_LEVEL', 'INFO').upper()
class Services:
def __init__(self, sc_params, gh_params, am_params, cc_params, log):
self.sc = ServiceCatalogue(sc_params, log)
self.gh = GithubSession(gh_params, log)
self.am = AlertmanagerData(am_params, log)
self.cc = CircleCI(cc_params, log_level)
self.log = log
###########################################################################################################
# Single component discovery
###########################################################################################################
def main():
logging.basicConfig(
format='[%(asctime)s] %(levelname)s %(threadName)s %(message)s', level=log_level
)
log = logging.getLogger(__name__)
parser = argparse.ArgumentParser(description='Process a component.')
parser.add_argument('component_name', help='The name of the component')
args = parser.parse_args()
component_name = args.component_name
# service catalogue parameters
sc_params = {
'url': os.getenv('SERVICE_CATALOGUE_API_ENDPOINT'),
'key': os.getenv('SERVICE_CATALOGUE_API_KEY'),
'filter': os.getenv('SC_FILTER', ''),
}
# Github parameters
gh_params = {
'app_id': int(os.getenv('GITHUB_APP_ID')),
'app_installation_id': int(os.getenv('GITHUB_APP_INSTALLATION_ID')),
'app_private_key': os.getenv('GITHUB_APP_PRIVATE_KEY'),
}
cc_params = {
'url': os.getenv(
'CIRCLECI_API_ENDPOINT',
'https://circleci.com/api/v1.1/project/gh/ministryofjustice/',
),
'token': os.getenv('CIRCLECI_TOKEN'),
}
am_params = {
'url': os.getenv(
'ALERTMANAGER_ENDPOINT',
'http://monitoring-alerts-service.cloud-platform-monitoring-alerts:8080/alertmanager/status',
)
}
services = Services(sc_params, gh_params, am_params, cc_params, log)
component = services.sc.get_record(services.sc.components_get, 'name', component_name)
log.debug(f'Component: {component}')
if component:
log.info(f'Processing component {component_name}...')
processed_components = components.process_sc_component(
component,
components.get_bootstrap_projects(services),
services,
force_update=True,
)
log.info(f'Processed component: {processed_components}')
else:
log.error(f'Component {component_name} not found')
if __name__ == '__main__':
main()