Skip to content

Commit

Permalink
Allow plugins to be installed from URL (#16)
Browse files Browse the repository at this point in the history
* Update plugins.py
* Update README.md
* Adding unit test cases
  • Loading branch information
mayurvaid-redvest authored Nov 22, 2022
1 parent 93fc910 commit 0ed8bef
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 5 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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"
```
20 changes: 15 additions & 5 deletions plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
87 changes: 87 additions & 0 deletions plugins_test.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 0ed8bef

Please sign in to comment.