diff --git a/snapshotter/docker-compose.yml b/snapshotter/docker-compose.yml new file mode 100644 index 0000000..ef6f199 --- /dev/null +++ b/snapshotter/docker-compose.yml @@ -0,0 +1,16 @@ +version: "3" +services: + test: + build: src + volumes: + - ./src/:/app + - ./data:/data + environment: + - "USER=viewer" + - "PASSWORD=viewer" + - "SSH_USER=…" + - "SSH_PASSWORD=…" + - "SSH_HOST=…" + - "PYTHONUNBUFFERED=1" + working_dir: /app + command: python3 scheduler.py \ No newline at end of file diff --git a/snapshotter/src/Dockerfile b/snapshotter/src/Dockerfile new file mode 100644 index 0000000..fec0fd5 --- /dev/null +++ b/snapshotter/src/Dockerfile @@ -0,0 +1,7 @@ +FROM alpine:3.8 +ADD ["requirements.txt", "/"] +RUN apk add --update --no-cache python3 libssl1.0 && \ + apk add --no-cache --virtual .build-deps g++ python3-dev libffi-dev openssl-dev make && \ + pip3 install --upgrade pip && \ + pip3 install -r requirements.txt && \ + apk del .build-deps \ No newline at end of file diff --git a/snapshotter/src/requirements.txt b/snapshotter/src/requirements.txt new file mode 100644 index 0000000..a3e8750 --- /dev/null +++ b/snapshotter/src/requirements.txt @@ -0,0 +1,3 @@ +requests +fs.sshfs +schedule \ No newline at end of file diff --git a/snapshotter/src/scheduler.py b/snapshotter/src/scheduler.py new file mode 100644 index 0000000..a9cfbe8 --- /dev/null +++ b/snapshotter/src/scheduler.py @@ -0,0 +1,22 @@ +import logging +import time + +import schedule + +import snapshot + +log = logging + +def setup(interval=10): + schedule.every(interval).minutes.do(snapshot.update) + +if __name__ == "__main__": + interval = 1 + sleep = (interval*60) / 10.0 + setup(interval) + while True: + try: + time.sleep(sleep) + schedule.run_pending() + except Exception as e: + log.exception(e) \ No newline at end of file diff --git a/snapshotter/src/snapshot.py b/snapshotter/src/snapshot.py new file mode 100644 index 0000000..ec0f02c --- /dev/null +++ b/snapshotter/src/snapshot.py @@ -0,0 +1,62 @@ +from collections import namedtuple +from datetime import date, timedelta, datetime as dt +import os +import shutil +import tempfile + +import fs +import requests + +Day = namedtuple("Day", ["start", "end"]) +SSHConfig = namedtuple("SSHConfig", ["user", "password", "host", "port", "dir"]) + +URL = 'http://192.168.2.30:3000/render/d-solo/j4171pFmk/in-vs-outdoor?orgId=1&panelId=2&from={start}&to={to}&width=1000&height=500&tz=Europe%2FBerlin' + +def today_str(): + return date.today().strftime("temp_%d.%m.%Y.png") + +def today(): + today = dt.today() + t = today.timetuple() + delta = timedelta(hours=t.tm_hour, minutes=t.tm_min, seconds=t.tm_sec) + start = today - delta + end = start + timedelta(days=1) + print(start, end) + start = int(start.timestamp() * 1000) + end = int(end.timestamp() * 1000) + return Day(start=start, end=end) + +def download(url, target, **kwargs): + response = requests.get(url, stream=True, **kwargs) + response.raw.decode_content = True + with open(target, "wb") as out: + shutil.copyfileobj(response.raw, out) + +def upload(local_file, filename, config): + url = f"ssh://{config.user}:{config.password}@{config.host}:{config.port}/{config.dir}" + with fs.open_fs(url) as remote: + return fs.copy.copy_file("/", local_file, remote, filename) + +def _update(auth, ssh_config): + day = today() + url = URL.format(start=day.start, to=day.end) + with tempfile.NamedTemporaryFile() as tmp: + download(url, tmp.name, auth=auth) + upload(tmp.name, today_str(), ssh_config) + +def auth_from_env(): + return (os.getenv("USER"), os.getenv("PASSWORD")) + +def ssh_from_env(): + return SSHConfig(user=os.getenv("SSH_USER"), password=os.getenv("SSH_PASSWORD"), host=os.getenv("SSH_HOST"), port=os.getenv("SSH_PORT", 22), dir=os.getenv("SSH_DIR", "/")) + +def update(): + auth = auth_from_env() + config = ssh_from_env() + _update(auth, config) + + +if __name__ == "__main__": + update() + + \ No newline at end of file