Skip to content
Snippets Groups Projects
Commit e9bdc985 authored by Bjarke Madsen's avatar Bjarke Madsen
Browse files

Let errors propagate now that we are synchronous so they can be alerted on

parent 76b10349
No related branches found
No related tags found
No related merge requests found
......@@ -111,12 +111,9 @@ def create_folder(request: TokenRequest, title):
:param title: folder title
:return: folder definition
"""
try:
data = {'title': title, 'uid': title.replace(' ', '_')}
r = request.post('api/folders', json=data)
except HTTPError:
logger.exception(f'Error when creating folder {title}')
return None
data = {'title': title, 'uid': title.replace(' ', '_')}
r = request.post('api/folders', json=data)
return r.json()
......
......@@ -842,96 +842,93 @@ def _add_service_data(org_config, services, regions):
def _provision_org(config, org, org_config, interfaces, services, regions):
request = AdminRequest(**config)
org_id = org['id']
accounts = []
logger.info(f'--- Provisioning org {org["name"]} (ID #{org_id}) ---')
try:
request = AdminRequest(**config)
org_id = org['id']
accounts = []
# create a service account for provisioning (>grafana 11.0)
account = get_or_create_service_account(request, org_id)
token = create_service_account_token(request, account['id'])
accounts.append((org_id, account))
except Exception:
# we're on a older version of grafana
token = create_api_token(request, org_id)
accounts.append((org_id, token))
token_request = TokenRequest(token=token['key'], **config)
logger.debug(accounts)
all_original_dashboards = list_dashboards(token_request)
all_original_dashboard_uids = {d['uid']: d.get('folderUrl', '') + d['url'] for d in all_original_dashboards}
datasource = _provision_datasource(config, token_request)
ds_name = datasource.get('name', 'PollerInfluxDB')
with ThreadPoolExecutor(max_workers=MAX_THREADS) as thread_executor:
args = (thread_executor, config, org_config, ds_name, token_request)
# initialise the aggregate dashboards with service data, to be used in the provisioning process
# it doesn't create the dashboards, just prepares the data
_add_service_data(org_config, services, regions)
# call to list is needed to queue up the futures
managed_dashboards = [f.result() if isinstance(f, Future) else f for f in list(itertools.chain(
_provision_interfaces(*args, interfaces, services, regions),
_provision_vlan_dashboards(*args, interfaces),
_provision_gws_indirect(*args),
_provision_gws_direct(*args),
_provision_eumetsat_multicast(*args),
_provision_aggregates(*args),
_provision_static_dashboards(*args),
_get_ignored_dashboards(*args)
))]
managed_dashboard_uids = {}
for dashboard in managed_dashboards:
if isinstance(dashboard, Future):
dashboard = dashboard.result()
if dashboard is None:
continue
assert dashboard['uid'] not in managed_dashboard_uids, \
f'Dashboard with UID {dashboard["uid"]} already exists: {dashboard}'
managed_dashboard_uids[dashboard['uid']] = dashboard['url']
difference = set(all_original_dashboard_uids.keys()) - set(managed_dashboard_uids.keys())
for uid in difference:
info = all_original_dashboard_uids[uid]
# delete unmanaged dashboards
logger.info(f'Deleting stale dashboard {info} with UID {uid}')
delete_dashboard(token_request, {'uid': uid})
folders_to_keep = {
# General is a base folder present in Grafana
'General',
# other folders, created outside of the DASHBOARDS list
'GWS Indirect',
'GWS Direct',
'Aggregates',
'EUMETSAT Multicast',
'EAP Dashboard',
'VLAN Interfaces',
}
folders_to_keep.update({dash['folder_name']
for dash in DASHBOARDS.values()})
folders_to_keep.update({dash['folder_name']
for dash in SERVICE_DASHBOARDS.values()})
logger.info(f'--- Provisioning org {org["name"]} (ID #{org_id}) ---')
ignored_folders = config.get('ignored_folders', [])
folders_to_keep.update(ignored_folders)
try:
# create a service account for provisioning (>grafana 11.0)
account = get_or_create_service_account(request, org_id)
token = create_service_account_token(request, account['id'])
accounts.append((org_id, account))
except Exception:
# we're on a older version of grafana
token = create_api_token(request, org_id)
accounts.append((org_id, token))
token_request = TokenRequest(token=token['key'], **config)
logger.debug(accounts)
all_original_dashboards = list_dashboards(token_request)
all_original_dashboard_uids = {d['uid']: d.get('folderUrl', '') + d['url'] for d in all_original_dashboards}
datasource = _provision_datasource(config, token_request)
ds_name = datasource.get('name', 'PollerInfluxDB')
with ThreadPoolExecutor(max_workers=MAX_THREADS) as thread_executor:
args = (thread_executor, config, org_config, ds_name, token_request)
# initialise the aggregate dashboards with service data, to be used in the provisioning process
# it doesn't create the dashboards, just prepares the data
_add_service_data(org_config, services, regions)
# call to list is needed to queue up the futures
managed_dashboards = list(itertools.chain(
_provision_interfaces(*args, interfaces, services, regions),
_provision_vlan_dashboards(*args, interfaces),
_provision_gws_indirect(*args),
_provision_gws_direct(*args),
_provision_eumetsat_multicast(*args),
_provision_aggregates(*args),
_provision_static_dashboards(*args),
_get_ignored_dashboards(*args)
))
managed_dashboard_uids = {}
for dashboard in managed_dashboards:
if isinstance(dashboard, Future):
dashboard = dashboard.result()
if dashboard is None:
continue
assert dashboard['uid'] not in managed_dashboard_uids, \
f'Dashboard with UID {dashboard["uid"]} already exists: {dashboard}'
managed_dashboard_uids[dashboard['uid']] = dashboard['url']
difference = set(all_original_dashboard_uids.keys()) - set(managed_dashboard_uids.keys())
for uid in difference:
info = all_original_dashboard_uids[uid]
# delete unmanaged dashboards
logger.info(f'Deleting stale dashboard {info} with UID {uid}')
delete_dashboard(token_request, {'uid': uid})
folders_to_keep = {
# General is a base folder present in Grafana
'General',
# other folders, created outside of the DASHBOARDS list
'GWS Indirect',
'GWS Direct',
'Aggregates',
'EUMETSAT Multicast',
'EAP Dashboard',
'VLAN Interfaces',
}
folders_to_keep.update({dash['folder_name']
for dash in DASHBOARDS.values()})
folders_to_keep.update({dash['folder_name']
for dash in SERVICE_DASHBOARDS.values()})
ignored_folders = config.get('ignored_folders', [])
folders_to_keep.update(ignored_folders)
delete_unknown_folders(token_request, folders_to_keep)
try:
delete_service_account(request, account['id'])
except Exception:
# we're on a older version of grafana
delete_api_token(request, token['id'], org_id=org_id)
delete_unknown_folders(token_request, folders_to_keep)
try:
delete_service_account(request, account['id'])
except Exception:
logger.exception(f'Error when provisioning org {org["name"]}')
# we're on a older version of grafana
delete_api_token(request, token['id'], org_id=org_id)
def provision(config):
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment