diff options
Diffstat (limited to 'modules/role')
3 files changed, 88 insertions, 0 deletions
diff --git a/modules/role/manifests/cryptoportfolio/front.pp b/modules/role/manifests/cryptoportfolio/front.pp index e676395..a6b25c4 100644 --- a/modules/role/manifests/cryptoportfolio/front.pp +++ b/modules/role/manifests/cryptoportfolio/front.pp | |||
@@ -143,6 +143,31 @@ class role::cryptoportfolio::front inherits role::cryptoportfolio { | |||
143 | refreshonly => true, | 143 | refreshonly => true, |
144 | } | 144 | } |
145 | 145 | ||
146 | # Slack logger | ||
147 | $slack_logger = lookup("role::cryptoportfolio::front::slack_logger") | ||
148 | unless empty($slack_logger) { | ||
149 | file { "/usr/local/bin/api_logger": | ||
150 | mode => "0755", | ||
151 | content => template("role/cryptoportfolio/api_logger.py.erb"), | ||
152 | } | ||
153 | -> | ||
154 | file { "/etc/systemd/system/cryptoportfolio-log.service": | ||
155 | mode => "0644", | ||
156 | owner => "root", | ||
157 | group => "root", | ||
158 | content => template("role/cryptoportfolio/cryptoportfolio-log.service.erb"), | ||
159 | notify => Exec["systemctl daemon-reload"], | ||
160 | } | ||
161 | -> | ||
162 | service { 'cryptoportfolio-log': | ||
163 | enable => true, | ||
164 | ensure => "running", | ||
165 | require => [ | ||
166 | Service["cryptoportfolio-app"], | ||
167 | ], | ||
168 | } | ||
169 | } | ||
170 | |||
146 | unless empty($webhook_url) { | 171 | unless empty($webhook_url) { |
147 | exec { "front-slack-notify": | 172 | exec { "front-slack-notify": |
148 | refreshonly => true, | 173 | refreshonly => true, |
diff --git a/modules/role/templates/cryptoportfolio/api_logger.py.erb b/modules/role/templates/cryptoportfolio/api_logger.py.erb new file mode 100644 index 0000000..e5f0b30 --- /dev/null +++ b/modules/role/templates/cryptoportfolio/api_logger.py.erb | |||
@@ -0,0 +1,52 @@ | |||
1 | #!/usr/bin/python | ||
2 | |||
3 | import time | ||
4 | import urllib3 | ||
5 | import json | ||
6 | from systemd import journal | ||
7 | |||
8 | urllib3.disable_warnings() | ||
9 | http = urllib3.PoolManager() | ||
10 | |||
11 | webhook_url = "<%= @slack_logger %>" | ||
12 | webhook_icon = "https://git.immae.eu/releases/logger.png" | ||
13 | |||
14 | def send_to_discord(message): | ||
15 | def send_chunk(chunk): | ||
16 | data = { | ||
17 | "avatar_url": webhook_icon, | ||
18 | "username": "Logger", | ||
19 | "content": "```\n{}\n```".format(chunk), | ||
20 | } | ||
21 | encoded = json.dumps(data).encode('utf-8') | ||
22 | |||
23 | r = http.request("POST", webhook_url, | ||
24 | headers={'Content-Type': 'application/json'}, | ||
25 | body=encoded) | ||
26 | if r.status == 429: | ||
27 | b = json.loads(r.data) | ||
28 | time.sleep(b["retry_after"] / 900) | ||
29 | r = http.request("POST", webhook_url, | ||
30 | headers={'Content-Type': 'application/json'}, | ||
31 | body=encoded) | ||
32 | assert r.status < 299 | ||
33 | |||
34 | chunk = "" | ||
35 | for line in message.split("\n"): | ||
36 | if len(line) + len(chunk) > 2000 - 17: | ||
37 | send_chunk(chunk) | ||
38 | chunk = line | ||
39 | else: | ||
40 | chunk += "\n" + line | ||
41 | send_chunk(chunk) | ||
42 | |||
43 | j = journal.Reader() | ||
44 | j.add_match(_SYSTEMD_UNIT="cryptoportfolio-app.service") | ||
45 | j.seek_tail() | ||
46 | list(j) | ||
47 | |||
48 | while True: | ||
49 | lines = [entry["MESSAGE"] for entry in list(j)] | ||
50 | if len(lines) > 0: | ||
51 | send_to_discord("\n".join(lines)) | ||
52 | time.sleep(3) | ||
diff --git a/modules/role/templates/cryptoportfolio/cryptoportfolio-log.service.erb b/modules/role/templates/cryptoportfolio/cryptoportfolio-log.service.erb new file mode 100644 index 0000000..a5f40ea --- /dev/null +++ b/modules/role/templates/cryptoportfolio/cryptoportfolio-log.service.erb | |||
@@ -0,0 +1,11 @@ | |||
1 | [Unit] | ||
2 | Description=Cryptoportfolio logger | ||
3 | |||
4 | [Service] | ||
5 | Type=simple | ||
6 | |||
7 | UMask=007 | ||
8 | |||
9 | ExecStart=/usr/local/bin/api_logger | ||
10 | |||
11 | Restart=on-failure | ||