Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

install-plugin action #311

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions actions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,18 @@ get-admin-password:
Get the dashboard url and initial admin password for the Grafana web interface. Initial
admin password is generated at charm deployment time. If the password has been changed,
a notice of that fact will be returned by this action instead.

install-plugin:
description: |
Installs a grafana plugin.
params:
name:
type: string
description: |
Name of the plugin. Must be hosted at https://grafana.com/grafana/plugins`.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it make more sense to have url as the param?
That would lift the grafana.com restriction and the /plugins/{name}/versions/{version}/download assumption.

Or, if we want to keep the grafana.com restriction, then we could use grafana cli for that, which would be great help with uninstalling (which is currently missing in the PR).

For example "grafana-snowflake-datasource" or "grafana-piechart-panel".
version:
type: string
description: |
Full version of the plugin you want to install.
Must include major, minor, and revision. For example "1.2.3" or "0.0.1"
37 changes: 37 additions & 0 deletions src/charm.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,13 @@
import socket
import string
import time
import zipfile
from io import StringIO
from pathlib import Path
from typing import Any, Callable, Dict, cast, Optional
from urllib.parse import urlparse
import subprocess
from urllib.request import urlretrieve

import yaml
from charms.catalogue_k8s.v1.catalogue import CatalogueConsumer, CatalogueItem
Expand Down Expand Up @@ -1152,6 +1154,41 @@ def _generate_datasource_config(self) -> str:
datasources_string = yaml.dump(datasources_dict)
return datasources_string

def _on_install_plugin(self, event: ActionEvent) -> None:
"""Install an official grafana plugin."""
name = event.params['name']
version = event.params['version']
url = f"https://grafana.com/api/plugins/{name}/versions/{version}/download"
path = Path('/var/lib/grafana/plugins')
Comment on lines +1161 to +1162
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would extract path and url to constants

if not path.exists():
path.mkdir()

download_path = path / 'download'

try:
urlretrieve(url, download_path)
except Exception as e:
logger.exception(f"cannot download plugin {name}:{version}")
event.fail(f"failed to download plugin from {url}, please make sure it's correct.")
return

with zipfile.ZipFile(download_path, 'r') as zip_ref:
try:
zip_ref.extractall(path)
except Exception as e:
logger.exception(f"cannot unzip {download_path}")
event.fail(f"failed to unzip downloaded file at {download_path}")
return

installed_plugins = [o.strip() for o in subprocess.getoutput("grafana cli plugin ls").splitlines()[1:]]
download_path.unlink()
Comment on lines +1183 to +1184
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would put this inside a try...except blick

event.set_results(
{"success": name in installed_plugins,
"installed_plugins": ", ".join(installed_plugins)}
)

self.containers['workload'].restart("grafana")

def _on_get_admin_password(self, event: ActionEvent) -> None:
"""Returns the grafana url and password for the admin user as an action response."""
if not self.grafana_service.is_ready:
Expand Down
Loading