-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfetch.py
30 lines (24 loc) · 948 Bytes
/
fetch.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
import os
import json
from io import StringIO
from time import sleep
import subprocess
LOGS_TAIL = 5000
print("Gathering running services")
cmd = ['docker', 'service', 'ls', '--format', '{{json . }}']
result = subprocess.run(cmd, capture_output=True, text=True)
# Parse the output into a list of dictionaries
result = [json.loads(line) for line in result.stdout.strip().split('\n')]
services = [service['Name'] for service in result]
print("Fetching logs for services")
for service in services:
print(f"Service: {service}")
cmd = ['docker', 'service', 'logs', '--tail', str(LOGS_TAIL), service]
try:
result = subprocess.run(cmd, capture_output=True, text=True, timeout=5)
output = result.stdout + "\n\n" + result.stderr
except subprocess.TimeoutExpired as e:
print("Timeout")
output = e.stdout + "\n\n" + e.stderr
with open(f"logs/docker/{service}.log", "w") as f:
f.write(output)