-
Notifications
You must be signed in to change notification settings - Fork 22
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
PietroPasotti
wants to merge
1
commit into
main
Choose a base branch
from
download-plugin-action
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would put this inside a |
||
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: | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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).