diff options
-rwxr-xr-x | Dockerfile | 9 | ||||
-rwxr-xr-x | app.py | 2 | ||||
-rw-r--r-- | cache.py | 18 | ||||
-rw-r--r-- | database.py | 13 | ||||
-rwxr-xr-x | docker-compose.yml | 3 | ||||
-rw-r--r-- | entrypoint.sh | 3 | ||||
-rwxr-xr-x | services.py | 24 | ||||
-rw-r--r-- | static/images/cloudfree.png | bin | 0 -> 3080 bytes | |||
-rwxr-xr-x | templates/services.html.j2 | 3 | ||||
-rwxr-xr-x | templates/template.html.j2 | 1 |
10 files changed, 51 insertions, 25 deletions
@@ -3,9 +3,12 @@ MAINTAINER Eden Attenborough "eden.attenborough@outlook.com" ENV TZ=Europe/London RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone RUN apt-get update -y -RUN apt-get install -y python3-pip python3-dev build-essential clang libffi-dev libxml2-dev libxslt-dev libjpeg-dev zlib1g-dev +RUN apt-get install -y python3-pip python3-dev build-essential clang libffi-dev libxml2-dev libxslt-dev libjpeg-dev zlib1g-dev tmux cron COPY . /app WORKDIR /app RUN pip3 install -r requirements.txt -ENTRYPOINT ["python3"] -CMD ["app.py", "--production"] + +RUN echo "*/30 * * * * root python3 /app/cache.py > /proc/1/fd/1 2>/proc/1/fd/2" > /etc/crontab + +ENTRYPOINT ["bash"] +CMD ["entrypoint.sh"] @@ -84,7 +84,7 @@ def serve_services(): return flask.render_template( "services.html.j2", **get_template_items("services", db), - docker = services.get_all_docker_containers(CONFIG.get("ssh", "docker_key_path")), + docker = services.get_all_docker_containers(), trans = services.get_torrent_stats(), pihole = services.get_pihole_stats() ) diff --git a/cache.py b/cache.py new file mode 100644 index 0000000..5b66e43 --- /dev/null +++ b/cache.py @@ -0,0 +1,18 @@ +import database +import services + +def update_cache(): + print("Updating cache...") + with database.Database() as db: + db.update_commit_cache(services.request_recent_commits(since = db.get_last_commit_time())) + print("Finished adding github commits...") + db.append_qnas(services.scrape_whispa(db.config.get("qnas", "url"), since = db.get_oldest_qna())) + print("Finished parsing Q&As...") + + print("Started getting docker information with SSH...") + print(services.cache_all_docker_containers(services.CONFIG.get("ssh", "docker_key_path"))) + print("Finished caching.") + + +if __name__ == "__main__": + update_cache()
\ No newline at end of file diff --git a/database.py b/database.py index cd15cec..49de9ef 100644 --- a/database.py +++ b/database.py @@ -21,7 +21,7 @@ class Database: def __enter__(self): self.config = configparser.ConfigParser(interpolation = None) - self.config.read("edaweb.conf") + self.config.read(os.path.join(os.path.dirname(__file__), "edaweb.conf")) if self.safeLogin: self.__connection = pymysql.connect( @@ -146,7 +146,6 @@ class Database: return [(i[0], "https://%s/%s/status/%d" % (self.config.get("nitter", "outsideurl"), i[2], i[1])) for i in cursor.fetchall()] def get_cached_commits(self, since = None, recurse = True): - threading.Thread(target = update_cache).start() with self.__connection.cursor() as cursor: if since is not None: cursor.execute("SELECT DISTINCT message, url, commitTime, additions, deletions, total FROM commitCache WHERE commitTime > %s ORDER BY commitTime DESC;", (since, )) @@ -243,17 +242,7 @@ class Database: cursor.execute("SELECT * FROM qnas;") return sorted(cursor.fetchall(), key = operator.itemgetter(2), reverse = True) -def update_cache(): - print("Updating cache...") - with Database() as db: - db.update_commit_cache(services.request_recent_commits(since = db.get_last_commit_time())) - print("Finished adding github commits...") - db.append_qnas(services.scrape_whispa(db.config.get("qnas", "url"), since = db.get_oldest_qna())) - print("Finished parsing Q&As...") - -if __name__ == "__main__": - update_cache() diff --git a/docker-compose.yml b/docker-compose.yml index 47c5d5f..3d5c58e 100755 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -9,6 +9,7 @@ services: - /tmp/:/media/ISOs/ - ./static/:/app/static/ - ./edaweb.conf:/app/edaweb.conf + - ./edaweb-docker.pem:/keys/docker-key.pem ports: - "6969:6969" networks: @@ -46,5 +47,3 @@ networks: external: name: mariadb -volumes: - nitter-redis: diff --git a/entrypoint.sh b/entrypoint.sh new file mode 100644 index 0000000..ecd86dc --- /dev/null +++ b/entrypoint.sh @@ -0,0 +1,3 @@ +printenv | grep -v "no_proxy" >> /etc/environment +tmux new-session -d -s "cron" 'cron -f || bash && bash'; +python3 /app/app.py --production
\ No newline at end of file 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()) diff --git a/static/images/cloudfree.png b/static/images/cloudfree.png Binary files differnew file mode 100644 index 0000000..71c6ff2 --- /dev/null +++ b/static/images/cloudfree.png diff --git a/templates/services.html.j2 b/templates/services.html.j2 index 9c9e5ec..9f42c7f 100755 --- a/templates/services.html.j2 +++ b/templates/services.html.j2 @@ -4,7 +4,7 @@ <section id=docker> <h2>docker</h2> <ul> - {% for host, containers in docker.items() %} + {% for host, containers in docker["containers"].items() %} <h4>{{ "%s - %s" % (host[0], host[1]) }}</h4> <table> {% for name, status, image in containers %} @@ -21,6 +21,7 @@ </table> {% endfor %} </ul> + <p>{{ docker["cachetime"] }}</p> </section> <section id="torrents"> diff --git a/templates/template.html.j2 b/templates/template.html.j2 index 1c9e99e..86618bc 100755 --- a/templates/template.html.j2 +++ b/templates/template.html.j2 @@ -67,6 +67,7 @@ <img src="/img/www.gif"> <img src="/img/bob.gif"> <img src="/img/sun.gif"> + <img src="/img/cloudfree.png"> </div> <iframe src="https://john.citrons.xyz/embed?ref=eda.gay" style="padding-top:20px;margin-left:auto;display:block;margin-right:auto;max-width:732px;width:100%;height:94px;border:none;"></iframe> </footer> |