-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathinstall
executable file
·120 lines (95 loc) · 3.61 KB
/
install
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
#!/usr/bin/env python3
import base64
import getpass
import hashlib
import io
import json
import os
import pathlib
import platform
import sys
import ssl
import tarfile
import urllib.error
import urllib.request
if platform.machine() == "amd64":
download = "https://dl.influxdata.com/telegraf/releases/telegraf-1.29.4_freebsd_amd64.tar.gz"
signature = "7d46b577d723f2e5ae1ebb587edd8b2418fb2cc2351e0bc12f4e235603d3c1e2"
else:
download = "https://dl.influxdata.com/telegraf/releases/telegraf-1.29.4_freebsd_i386.tar.gz"
signature = "0811bcd97827a93cf110aec1756c0914bd94cfc5325c620692893bb183ef97f1"
dir = pathlib.Path(__file__).parent.absolute()
def create_binary(blob):
path = os.path.join(dir, "telegraf")
with open(path, "wb") as file:
file.write(blob)
st = os.stat(path)
os.chmod(path, st.st_mode | 0o111)
def create_init_command(command, password, host="localhost", username="root"):
commands = do_request("http://%s/api/v2.0/initshutdownscript/" % host, method="GET", username=username, password=password)
for cmd in commands:
if cmd["command"] == command:
print_msg("command already exists, skipping")
return
payload = {
"command": command,
"comment": "Start Telegraf",
"enabled": True,
"timeout": 30,
"type": "COMMAND",
"when": "POSTINIT",
}
do_request("http://%s/api/v2.0/initshutdownscript/" % host, method="POST", data=json.dumps(payload).encode("ASCII"), content_type="application/json", username=username, password=password)
def do_request(url, username, password, data=None, method="GET", content_type=None):
auth = "Basic %s" % base64.b64encode(("%s:%s" % (username, password)).encode("ASCII")).decode("ASCII")
req = urllib.request.Request("https://localhost/api/v2.0/initshutdownscript/", data=data, method=method)
req.add_header("Authorization", auth)
if content_type:
req.add_header("Content-Type", content_type)
try:
res = urllib.request.urlopen(req, context=ssl._create_unverified_context())
except urllib.error.HTTPError as err:
if err.code == 401:
print_msg("Wrong password")
return
elif err.code == 500:
print_msg("Server error")
return
else:
raise
return json.load(res)
def print_msg(message):
sys.stdout.write("%s\n" % message)
sys.stdout.flush()
print_msg("Downloading %s" % download)
with urllib.request.urlopen(download) as archive:
blob = archive.read()
hash = hashlib.sha256()
hash.update(blob)
if signature != hash.hexdigest():
print_msg("Signature did not match '%s'" % signature)
exit(1)
tar = tarfile.open(fileobj=io.BytesIO(blob), mode="r:gz")
for member in tar.getmembers():
if member.name.endswith("/usr/bin/telegraf"):
with tar.extractfile(member) as binary:
create_binary(binary.read())
print_msg("Extracted telegraf binary")
init_file = os.path.join(dir, "telegraf.init")
rc_file = "/usr/local/etc/rc.d/telegraf"
command = "ln -s %s %s; service telegraf start" % (init_file, rc_file)
manual_msg = "Skipping, you will have add the following as an Post Init Command in the Web UI:\n\n%s\n" % command
try:
os.symlink(init_file, rc_file)
except FileExistsError:
print_msg("%s symlink already exists" % rc_file)
print_msg("Creating init command, you need to enter the password for the root user. If you don't want to do this, press Ctrl+C or enter a blank password")
try:
password = getpass.getpass("root password: ")
if password == "":
print_msg(manual_msg)
else:
create_init_command(command, password)
except KeyboardInterrupt:
print_msg("\n%s" % manual_msg)
print_msg("Run 'service telegraf start' after you create %s/telegraf.conf" % dir)