diff --git a/README.md b/README.md index 7506cbb..1b49ade 100644 --- a/README.md +++ b/README.md @@ -18,3 +18,9 @@ To test that the python script works you need to set a environment variable. ```shell export GRAFANA_PLUGINS="grafana-clock-panel:1.0.1,grafana-simple-json-datasource:1.3.5" ``` + +For Custom Plugins/urls + +```shell +export GRAFANA_PLUGINS="url:https://grafana.com/api/plugins/grafana-simple-json-datasource_latest" +``` diff --git a/plugins.py b/plugins.py index eedb547..6f3a159 100755 --- a/plugins.py +++ b/plugins.py @@ -32,18 +32,28 @@ def getPlugins(): if pluginsKey in os.environ and not (not os.environ[pluginsKey]): plugins = os.environ[pluginsKey].split(",") for plugin in plugins: - parts = plugin.split(":") + parts = plugin.split(":",1) + plugin_url = "" + name = "" if len(parts) == 2: - result.append((parts[0], parts[1])) + if parts[0] == "url": + plugin_url= parts[1] + name = "/tmp/%s.zip" % plugin_url.rsplit('/', 1)[-1] + else: + plugin_url = f"https://grafana.com/api/plugins/{parts[0]}/versions/{parts[1]}/download?os={OS}&arch={ARCH}" + name = f"/tmp/{parts[0]}_{parts[1]}.zip" + elif len(parts) == 1: + plugin_url = f"https://grafana.com/api/plugins/{parts[0]}/versions/latest/download?os={OS}&arch={ARCH}" + name = f"/tmp/{parts[0]}_latest.zip" else: print("Invalid syntax (version missing?): " + plugin) + + result.append((plugin_url, name)) return result def downloadPlugin(plugin): - name, version = plugin - url = f"https://grafana.com/api/plugins/{name}/versions/{version}/download?os={OS}&arch={ARCH}" - file_name = "/tmp/%s_%s.zip" % plugin + url,file_name = plugin print(f"downloading {url}") with urllib.request.urlopen(url) as response, open(file_name, "wb") as out_file: shutil.copyfileobj(response, out_file) diff --git a/plugins_test.py b/plugins_test.py new file mode 100644 index 0000000..c3ed94f --- /dev/null +++ b/plugins_test.py @@ -0,0 +1,87 @@ +import unittest +from plugins import getPlugins +import os +import platform + +OS = os.environ.get("OS", platform.system().lower()) + +class TestGetPlugins(unittest.TestCase): + def test_get_plugin_with_version(self): + os.environ["GRAFANA_PLUGINS"] = "grafana-clock-panel:1.0.1,grafana-simple-json-datasource:1.3.5" + actual = getPlugins() + expected = [ + ('https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_1.0.1.zip'), + ('https://grafana.com/api/plugins/grafana-simple-json-datasource/versions/1.3.5/download?os='+OS+'&arch=x86_64', '/tmp/grafana-simple-json-datasource_1.3.5.zip') + ] + self.assertEqual(actual, expected) + + def test_get_plugin_without_version(self): + os.environ["GRAFANA_PLUGINS"] = "grafana-clock-panel:1.0.1,grafana-simple-json-datasource" + actual = getPlugins() + expected = [ + ('https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_1.0.1.zip'), + ('https://grafana.com/api/plugins/grafana-simple-json-datasource/versions/latest/download?os='+OS+'&arch=x86_64', '/tmp/grafana-simple-json-datasource_latest.zip') + ] + self.assertEqual(actual, expected) + + def test_get_plugin_without_multiple(self): + os.environ["GRAFANA_PLUGINS"] = "grafana-clock-panel:1.0.1" + actual = getPlugins() + expected = [ + ('https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_1.0.1.zip'), + ] + self.assertEqual(actual, expected) + + def test_get_plugin_withurl(self): + os.environ["GRAFANA_PLUGINS"] = "url:https://grafana.com/api/plugins/grafana-clock-panel" + actual = getPlugins() + expected = [ + ('https://grafana.com/api/plugins/grafana-clock-panel', '/tmp/grafana-clock-panel.zip'), + ] + + self.assertEqual(actual, expected) + + def test_get_plugin_withurl_multiple(self): + os.environ["GRAFANA_PLUGINS"] = "url:https://grafana.com/api/plugins/grafana-clock-panel,grafana-clock-panel:1.0.1" + actual = getPlugins() + expected = [ + ('https://grafana.com/api/plugins/grafana-clock-panel', '/tmp/grafana-clock-panel.zip'), + ('https://grafana.com/api/plugins/grafana-clock-panel/versions/1.0.1/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_1.0.1.zip') + ] + + self.assertEqual(actual, expected) + + def test_get_plugin_withurl_multiple_similar(self): + os.environ["GRAFANA_PLUGINS"] = "url:https://grafana.com/api/plugins/grafana-clock-panel,url:https://grafana.com/api/plugins/grafana-simple-json-datasource" + actual = getPlugins() + expected = [ + ('https://grafana.com/api/plugins/grafana-clock-panel', '/tmp/grafana-clock-panel.zip'), + ('https://grafana.com/api/plugins/grafana-simple-json-datasource', '/tmp/grafana-simple-json-datasource.zip') + ] + + self.assertEqual(actual, expected) + + def test_get_plugin_withurl_multiple_withoutversion(self): + os.environ["GRAFANA_PLUGINS"] = "url:https://grafana.com/api/plugins/grafana-clock-panel,grafana-clock-panel" + actual = getPlugins() + expected = [ + ('https://grafana.com/api/plugins/grafana-clock-panel', '/tmp/grafana-clock-panel.zip'), + ('https://grafana.com/api/plugins/grafana-clock-panel/versions/latest/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_latest.zip') + ] + + self.assertEqual(actual, expected) + + def test_get_plugin_withurl_multiple_withoutversion_reverse(self): + os.environ["GRAFANA_PLUGINS"] = "grafana-clock-panel,url:https://grafana.com/api/plugins/grafana-clock-panel" + actual = getPlugins() + expected = [ + ('https://grafana.com/api/plugins/grafana-clock-panel/versions/latest/download?os='+OS+'&arch=x86_64', '/tmp/grafana-clock-panel_latest.zip'), + ('https://grafana.com/api/plugins/grafana-clock-panel', '/tmp/grafana-clock-panel.zip') + ] + + self.assertEqual(actual, expected) + + + +if __name__ == '__main__': + unittest.main()