From 172f3d769a0589a2b6a42b2dc766337901990f5c Mon Sep 17 00:00:00 2001 From: agp8x Date: Thu, 4 Jun 2020 14:18:56 +0200 Subject: [PATCH] configure tinkerforge target from docker-compose --- docker-compose.yml | 18 +++++++++++++++--- importer/Dockerfile | 2 +- importer/import.py | 31 ++++++++++++++++++++----------- snapshotter/src/recover.py | 4 ++-- snapshotter/src/snapshot.py | 15 ++++++++++----- 5 files changed, 48 insertions(+), 22 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index b76b5b2..a7c659c 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -1,24 +1,36 @@ -version: "2" +version: "3" services: import: build: importer - image: docker.clkl.de/weather/import:0.2 + image: docker.clkl.de/weather/import:0.3 + environment: + - PYTHONUNBUFFERED=1 + - TF_HOST=192.168.2.160 + - TF_PORT=4223 + - TF_ID=DYC + - INFLUX_URL=http://influxdb:8086 + - INFLUXDB_DB=mydb influxdb: image: influxdb:1.5-alpine +#TODO: 1.7-alpine command: influxd -config /etc/influxdb/influxdb.conf ports: - "8086:8086" volumes: - "./influxdb/:/var/lib/influxdb" environment: - - INFLUXDB_DB=mydb + - INFLUXDB_DB=mydb # Y U NO WORK?! #- INFLUXDB_USER=user #- INLFUXDB_USER_PASSWORD= grafana: image: grafana/grafana +#TODO: 6.2.5 ports: - "3000:3000" volumes: # - "./grafana_etc/:/etc/grafana/" - "./grafana_lib/:/var/lib/grafana/" + + +# docker-compose exec influxdb influx_inspect export -database mydb -out /var/lib/influxdb/backup-text-dump -datadir /var/lib/influxdb/data -waldir /var/lib/influxdb/wal \ No newline at end of file diff --git a/importer/Dockerfile b/importer/Dockerfile index 6b0c42b..3712631 100644 --- a/importer/Dockerfile +++ b/importer/Dockerfile @@ -1,4 +1,4 @@ -FROM python:3.7-rc-alpine +FROM python:3.7-alpine WORKDIR /app diff --git a/importer/import.py b/importer/import.py index 6727949..4a731ee 100644 --- a/importer/import.py +++ b/importer/import.py @@ -1,11 +1,8 @@ -HOST = "192.168.2.110" -PORT = 4223 -UID = "DYC" -URL = "http://192.168.2.30:8086/write?db=mydb" - import logging import time +from os import getenv + import requests from tinkerforge.ip_connection import IPConnection @@ -13,22 +10,32 @@ from tinkerforge.bricklet_outdoor_weather import BrickletOutdoorWeather log = logging.getLogger(__name__) -def asdf(type, identifier, **kwargs): - log.info(type, identifier, kwargs) + +HOST = getenv("TF_HOST", "192.168.2.160") +PORT = int(getenv("TF_PORT", 4223)) +UID = getenv("TF_ID", "DYC") +URL = getenv("INFLUX_URL", "http://influxdb:8086) +DB = getenv("INFLUXDB_DB", "mydb") + + + +def influx(type, identifier, **kwargs): + log.info(f"{type}, {identifier}, {kwargs}") time_ns = time.time_ns() data = [] for unit in kwargs: data.append(f"{unit},type={type},identifier={identifier} value={kwargs[unit]} {time_ns}") try: - r = requests.post(URL, data="\n".join(data)) - log.info(r, r.text) + url = f"{URL}/write?db={DB}" + r = requests.post(url, data="\n".join(data)) + log.info(f"{r}, {r.text}") except Exception as e: log.exception(e) def cb_station(identifier, temperature, humidity, wind_speed, gust_speed, rain, wind_direction, battery_low): - asdf(type="station", identifier=identifier, temperature=temperature, humidity=humidity, wind_speed=wind_speed, gust_speed=gust_speed, rain=rain, wind_direction=wind_direction, battery_low=battery_low) + influx(type="station", identifier=identifier, temperature=temperature, humidity=humidity, wind_speed=wind_speed, gust_speed=gust_speed, rain=rain, wind_direction=wind_direction, battery_low=battery_low) def cb_sensor(identifier, temperature, humidity): - asdf(type="sensor", identifier=identifier, temperature=temperature, humidity=humidity) + influx(type="sensor", identifier=identifier, temperature=temperature, humidity=humidity) if __name__ == "__main__": logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO) @@ -40,6 +47,8 @@ if __name__ == "__main__": ow.set_sensor_callback_configuration(True) ow.register_callback(ow.CALLBACK_STATION_DATA, cb_station) ow.register_callback(ow.CALLBACK_SENSOR_DATA, cb_sensor) + log.info("try to create influx db…") + requests.post(f"{URL}/query?q=CREATE DATABASE {DB}") log.info("now we play the waiting game…") while True: try: diff --git a/snapshotter/src/recover.py b/snapshotter/src/recover.py index 9d594cc..f91f059 100644 --- a/snapshotter/src/recover.py +++ b/snapshotter/src/recover.py @@ -14,6 +14,6 @@ def update(start, end): if __name__ == "__main__": - start = dt(2018, 8 , 1) - end = dt(2018, 8 , 1) + start = dt(2018, 8 , 3) + end = dt(2018, 8 , 3) update(start, end) \ No newline at end of file diff --git a/snapshotter/src/snapshot.py b/snapshotter/src/snapshot.py index 2a92214..aa49c68 100644 --- a/snapshotter/src/snapshot.py +++ b/snapshotter/src/snapshot.py @@ -54,12 +54,17 @@ def auth_from_env(): 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(): +def update(save=False): log.info("run update") - auth = auth_from_env() - config = ssh_from_env() - _update(auth, config) - + try: + auth = auth_from_env() + config = ssh_from_env() + _update(auth, config) + except Exception as e: + if save: + log.e(e) + else: + raise e if __name__ == "__main__": logging.basicConfig(level=logging.DEBUG, format='%(asctime)s %(message)s', datefmt='%m/%d/%Y %I:%M:%S %p')