Skip to content

Commit

Permalink
Add new script to sync dashboards
Browse files Browse the repository at this point in the history
With a flag to check if dashboards are updated
(so `make check-dashboard-updates` still works).

Also run the script to sync the dashboards.
  • Loading branch information
samuelallan72 committed Apr 11, 2024
1 parent fbee674 commit ed8747f
Show file tree
Hide file tree
Showing 6 changed files with 4,279 additions and 31 deletions.
12 changes: 9 additions & 3 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,14 +51,20 @@ And add the corresponding command to `./scripts/update-charm-libs.sh`.

## Checking for dashboard updates

The openstack exporter dashboard is a shared file managed by the upstream repository which is
hosted in the [Sunbeam](https://opendev.org/openstack/sunbeam-charms). To check if a new version of
the dashboard is available you can run the make target:
The openstack exporter dashboards are shared files managed
in the [upstream repository (sunbeam-charms)](https://opendev.org/openstack/sunbeam-charms).
To check if a new version of the dashboards are available you can run the make target:

```shell
make check-dashboard-updates
```

If it reports updates, you can update them by running:

```shell
make sync-dashboards
```

## Build the charm

Build the charm in this git repository using:
Expand Down
8 changes: 6 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ help:
@echo " make - show help text"
@echo " make update-charm-libs - update charm's libraries"
@echo " make check-dashboard-updates - check if there's a new dashboard from the upstream"
@echo " make sync-dashboards - update the dashboards from upstream"
@echo " make clean - remove unneeded files"
@echo " make download-snap - download snap release from github release assets"
@echo " make build - build the charm"
Expand All @@ -23,7 +24,10 @@ update-charm-libs:
./scripts/update-charm-libs.sh

check-dashboard-updates:
./scripts/check-dashboard-updates.sh
./scripts/sync-dashboards --check

sync-dashboards:
./scripts/sync-dashboards

clean:
@echo "Cleaning existing build"
Expand All @@ -42,4 +46,4 @@ download-snap:
integration: build download-snap
CHARM_LOCATION=${CHARM_LOCATION} CHARM_SNAP_LOCATION=${CHARM_SNAP_LOCATION} tox -e integration

.PHONY: help update-charm-libs check-dashboard-updates clean build download-snap integration
.PHONY: help update-charm-libs check-dashboard-updates clean build download-snap integration sync-dashboards
26 changes: 0 additions & 26 deletions scripts/check-dashboard-updates.sh

This file was deleted.

72 changes: 72 additions & 0 deletions scripts/sync-dashboards
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
#!/usr/bin/env python3

import os
import sys
import argparse
from urllib.request import urlretrieve

# sync the dashboards from https://opendev.org/openstack/sunbeam-charms/src/branch/main/charms/openstack-exporter-k8s/src/grafana_dashboards
BASE_URL = "https://opendev.org/openstack/sunbeam-charms/raw/branch/main/charms/openstack-exporter-k8s/src/grafana_dashboards/"
BASE_LOCAL_PATH = "./src/grafana_dashboards/"
DASHBOARDS = [
"hypervisor-openstack-exporter-dashboard.json",
"openstack-exporter.json",
"overview-openstack-exporter-dashboard.json",
]


def sync_dashboard(url: str, local_path: str, check_mode: bool) -> bool:
"""Sync dashboard from url.
If check_mode is True, don't actually update the file.
Return True if the dashboard changed (or would be changed).
"""
new_dashboard_path, _ = urlretrieve(url)

changed = False
if os.path.exists(local_path):
with open(new_dashboard_path) as f1, open(local_path) as f2:
changed = f1.read() != f2.read()
else:
changed = True

if changed and not check_mode:
os.replace(new_dashboard_path, local_path)

return changed


def main(args: argparse.Namespace) -> bool:
"""Main entry point.
Return False to indicate dashboards not up to date in check mode.
"""
result = True
for dashboard in DASHBOARDS:
changed = sync_dashboard(
BASE_URL + dashboard, BASE_LOCAL_PATH + dashboard, args.check
)
if changed:
if args.check:
result = False
print(f"ERROR: {dashboard} not up to date")
else:
print(f"UPDATED: {dashboard} synced from remote source")
else:
print(f"OK: {dashboard} up to date")

return result


if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"-c",
"--check",
action="store_true",
help="If any dashboards are outdated, don't update them, but exit with value 1.",
)
args = parser.parse_args()

if not main(args):
sys.exit(1)
Loading

0 comments on commit ed8747f

Please sign in to comment.