forked from grafana-operator/grafana_plugins_init
-
Notifications
You must be signed in to change notification settings - Fork 0
/
plugins.py
executable file
·61 lines (49 loc) · 1.69 KB
/
plugins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
#!/bin/env python
import os
import zipfile
import urllib.request
import shutil
pluginsKey = "GRAFANA_PLUGINS"
pluginsVolume = "/opt/plugins"
class ZipFileWithPermissions(zipfile.ZipFile):
""" Custom ZipFile class handling file permissions. """
def _extract_member(self, member, targetpath, pwd):
if not isinstance(member, zipfile.ZipInfo):
member = self.getinfo(member)
targetpath = super()._extract_member(member, targetpath, pwd)
attr = member.external_attr >> 16
if attr != 0:
os.chmod(targetpath, attr)
return targetpath
def getPlugins():
result = list()
if pluginsKey in os.environ and not (not os.environ[pluginsKey]):
plugins = os.environ[pluginsKey].split(",")
for plugin in plugins:
parts = plugin.split(":")
if len(parts) == 2:
result.append((parts[0], parts[1]))
else:
print("Invalid syntax (version missing?): " + plugin)
return result
def downloadPlugin(plugin):
url = "https://grafana.com/api/plugins/%s/versions/%s/download" % plugin
file_name = "/tmp/%s_%s.zip" % plugin
with urllib.request.urlopen(url) as response, open(file_name, "wb") as out_file:
shutil.copyfileobj(response, out_file)
return file_name
def extractPlugin(file_name):
zip = ZipFileWithPermissions(file_name)
zip.extractall(pluginsVolume)
zip.close()
def installPlugin(plugin):
try:
file_name = downloadPlugin(plugin)
except:
print("Error downloading %s:%s" % plugin)
else:
extractPlugin(file_name)
def main():
for plugin in getPlugins():
installPlugin(plugin)
main()