aboutsummaryrefslogtreecommitdiffstats
path: root/services.py
diff options
context:
space:
mode:
authorjwansek <eddie.atten.ea29@gmail.com>2025-04-27 21:18:34 +0100
committerjwansek <eddie.atten.ea29@gmail.com>2025-04-27 21:18:34 +0100
commitdd3f431d2f20409ccb490241e70687a5f5c7c79c (patch)
tree5c3f66ca9a0021b03d5ceddeaa1b5e77503471e7 /services.py
parentf0a4fd5c9b632633443f8a5bf4475cbf33fed8d5 (diff)
downloadboymoder.blog-master.tar.gz
boymoder.blog-master.zip
Added caching docker info with cronHEADmaster
Diffstat (limited to 'services.py')
-rwxr-xr-xservices.py24
1 files changed, 18 insertions, 6 deletions
diff --git a/services.py b/services.py
index 5fb7d81..5d25271 100755
--- a/services.py
+++ b/services.py
@@ -15,6 +15,7 @@ import docker
import random
import subprocess
import fabric
+import pickle
import queue
import json
import time
@@ -22,7 +23,7 @@ import os
theLastId = 0
CONFIG = configparser.ConfigParser(interpolation = None)
-CONFIG.read("edaweb.conf")
+CONFIG.read(os.path.join(os.path.dirname(__file__), "edaweb.conf"))
def humanbytes(B):
'Return the given bytes as a human friendly KB, MB, GB, or TB string'
@@ -280,12 +281,23 @@ def get_docker_containers(host, ssh_key_path):
).run('docker ps -a -s --format "table {{.Names}};{{.Status}};{{.Image}}"', hide = True)
return [line.split(";") for line in result.stdout.split("\n")[1:-1]]
-def get_all_docker_containers(ssh_key_path):
+def cache_all_docker_containers(ssh_key_path):
containers = {}
+ containers["containers"] = {}
for host, name in CONFIG["docker_hosts"].items():
print(host)
- containers[(host, name)] = get_docker_containers(host, ssh_key_path)
- return containers
+ containers["containers"][(host, name)] = get_docker_containers(host, ssh_key_path)
+
+ containers["cachetime"] = "Docker information last updated at %s" % str(datetime.datetime.now())
+ with open("/tmp/docker-cache.json", "wb") as f:
+ pickle.dump(containers, f)
+
+def get_all_docker_containers():
+ if not os.path.exists("/tmp/docker-cache.json"):
+ return {"containers": {}, "cachetime": "No cached docker information"}
+
+ with open("/tmp/docker-cache.json", "rb") as f:
+ return pickle.load(f)
def timeout(func):
# cant get this to work with queue.Queue() for some reason?
@@ -343,6 +355,6 @@ if __name__ == "__main__":
# print(request_recent_commits(since = datetime.datetime.now() - datetime.timedelta(days=30)))
# print(scrape_whispa(CONFIG.get("qnas", "url"), datetime.datetime.fromtimestamp(0.0)))
- # print(get_all_docker_containers(os.path.join(os.path.dirname(__file__), "edaweb-docker.pem")))
+ print(cache_all_docker_containers(os.path.join(os.path.dirname(__file__), "edaweb-docker.pem")))
- print(get_torrent_stats())
+ # print(get_torrent_stats())