]> git.immae.eu Git - perso/Immae/Projets/Puppet.git/commitdiff
Add logger for cryptoportfolio
authorIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 13 May 2018 12:42:51 +0000 (14:42 +0200)
committerIsmaël Bouya <ismael.bouya@normalesup.org>
Sun, 13 May 2018 12:49:10 +0000 (14:49 +0200)
modules/role/manifests/cryptoportfolio/front.pp
modules/role/templates/cryptoportfolio/api_logger.py.erb [new file with mode: 0644]
modules/role/templates/cryptoportfolio/cryptoportfolio-log.service.erb [new file with mode: 0644]

index e676395607f5f80ce4d8274a9f422f3388858c1d..a6b25c4dc484a641c5717c2d90fe9d5095bece78 100644 (file)
@@ -143,6 +143,31 @@ class role::cryptoportfolio::front inherits role::cryptoportfolio {
     refreshonly => true,
   }
 
+  # Slack logger
+  $slack_logger = lookup("role::cryptoportfolio::front::slack_logger")
+  unless empty($slack_logger) {
+    file { "/usr/local/bin/api_logger":
+      mode    => "0755",
+      content => template("role/cryptoportfolio/api_logger.py.erb"),
+    }
+    ->
+    file { "/etc/systemd/system/cryptoportfolio-log.service":
+      mode    => "0644",
+      owner   => "root",
+      group   => "root",
+      content => template("role/cryptoportfolio/cryptoportfolio-log.service.erb"),
+      notify  => Exec["systemctl daemon-reload"],
+    }
+    ->
+    service { 'cryptoportfolio-log':
+      enable    => true,
+      ensure    => "running",
+      require   => [
+        Service["cryptoportfolio-app"],
+      ],
+    }
+  }
+
   unless empty($webhook_url) {
     exec { "front-slack-notify":
       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 (file)
index 0000000..e5f0b30
--- /dev/null
@@ -0,0 +1,52 @@
+#!/usr/bin/python
+
+import time
+import urllib3
+import json
+from systemd import journal
+
+urllib3.disable_warnings()
+http = urllib3.PoolManager()
+
+webhook_url = "<%= @slack_logger %>"
+webhook_icon = "https://git.immae.eu/releases/logger.png"
+
+def send_to_discord(message):
+    def send_chunk(chunk):
+        data = {
+                "avatar_url": webhook_icon,
+                "username": "Logger",
+                "content": "```\n{}\n```".format(chunk),
+                }
+        encoded = json.dumps(data).encode('utf-8')
+
+        r = http.request("POST", webhook_url,
+                headers={'Content-Type': 'application/json'},
+                body=encoded)
+        if r.status == 429:
+            b = json.loads(r.data)
+            time.sleep(b["retry_after"] / 900)
+            r = http.request("POST", webhook_url,
+                    headers={'Content-Type': 'application/json'},
+                    body=encoded)
+        assert r.status < 299
+
+    chunk = ""
+    for line in message.split("\n"):
+        if len(line) + len(chunk) > 2000 - 17:
+            send_chunk(chunk)
+            chunk = line
+        else:
+            chunk += "\n" + line
+    send_chunk(chunk)
+
+j = journal.Reader()
+j.add_match(_SYSTEMD_UNIT="cryptoportfolio-app.service")
+j.seek_tail()
+list(j)
+
+while True:
+    lines = [entry["MESSAGE"] for entry in list(j)]
+    if len(lines) > 0:
+        send_to_discord("\n".join(lines))
+    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 (file)
index 0000000..a5f40ea
--- /dev/null
@@ -0,0 +1,11 @@
+[Unit]
+Description=Cryptoportfolio logger
+[Service]
+Type=simple
+  
+UMask=007
+  
+ExecStart=/usr/local/bin/api_logger
+Restart=on-failure