Browse Source

Extract code from configmap

Francois Deppierraz 10 months ago
parent
commit
9009772bbe
4 changed files with 49 additions and 48 deletions
  1. 1 0
      Makefile
  2. 0 48
      configmap.yaml
  3. 9 0
      configmap.yaml.tmpl
  4. 39 0
      logger.py

+ 1 - 0
Makefile

@@ -1,4 +1,5 @@
 deploy:
+	bash -c "cat configmap.yaml.tmpl <(sed s'/^/    /' logger.py) | kubectl apply -f -"
 	kubectl apply -f .
 	kubectl -n power-monitoring delete pod -l app=power-monitoring
 	kubectl -n power-monitoring get all -l app=power-monitoring

+ 0 - 48
configmap.yaml

@@ -1,48 +0,0 @@
-apiVersion: v1
-kind: ConfigMap
-metadata:
-  labels:
-    app: power-monitor
-  name: logger.py
-  namespace: power-monitoring
-data:
-  logger.py: |
-    import time
-    from datetime import datetime
-    import requests
-    import traceback
-    from influxdb import InfluxDBClient
-
-    OUT = "/data/log.csv"
-    URL = "http://62.220.135.196:8080/sensor/{}?version=1.0&interval=minute&unit=watt"
-    SENSORS = (
-      "34cde81adabfb1ce819eca8fea6949b6",
-      "b7755b5f3ec05fcdc67f449241a9912a",
-      "e67e0685f747b30d855108ab781abdfc"
-    )
-
-    db = InfluxDBClient('influxdb', database='power')
-
-    while True:
-      results = []
-      for sensor in SENSORS:
-        try:
-          flukso = requests.get(URL.format(sensor)).json()
-          flukso = list(filter(lambda x: x[1] != 'nan', flukso))
-          (timestamp, value) = flukso[-1]
-          results.append((",".join((str(timestamp), sensor, str(value)))))
-          db.write_points([{
-                     "measurement": sensor,
-                     "time": datetime.fromtimestamp(timestamp).isoformat(),
-                     "fields": { "watts": float(value) }
-                   }])
-        except Exception as e:
-          traceback.print_exc()
-
-      with open(OUT, 'a') as f:
-        f.write("\n".join(results) + "\n")
-
-      sleeptime = 1000000 - datetime.utcnow().microsecond
-      time.sleep(sleeptime/1000000.0)
-
-     

+ 9 - 0
configmap.yaml.tmpl

@@ -0,0 +1,9 @@
+apiVersion: v1
+kind: ConfigMap
+metadata:
+  labels:
+    app: power-monitor
+  name: logger.py
+  namespace: power-monitoring
+data:
+  logger.py: |

+ 39 - 0
logger.py

@@ -0,0 +1,39 @@
+#!/usr/bin/env python3
+
+import time
+from datetime import datetime
+import requests
+import traceback
+from influxdb import InfluxDBClient
+
+OUT = "/data/log.csv"
+URL = "http://62.220.135.196:8080/sensor/{}?version=1.0&interval=minute&unit=watt"
+SENSORS = (
+  "34cde81adabfb1ce819eca8fea6949b6",
+  "b7755b5f3ec05fcdc67f449241a9912a",
+  "e67e0685f747b30d855108ab781abdfc"
+)
+
+db = InfluxDBClient('influxdb', database='power')
+
+while True:
+  results = []
+  for sensor in SENSORS:
+    try:
+      flukso = requests.get(URL.format(sensor)).json()
+      flukso = list(filter(lambda x: x[1] != 'nan', flukso))
+      (timestamp, value) = flukso[-1]
+      results.append((",".join((str(timestamp), sensor, str(value)))))
+      db.write_points([{
+                 "measurement": sensor,
+                 "time": datetime.fromtimestamp(timestamp).isoformat(),
+                 "fields": { "watts": float(value) }
+               }])
+    except Exception as e:
+      traceback.print_exc()
+
+  with open(OUT, 'a') as f:
+    f.write("\n".join(results) + "\n")
+
+  sleeptime = 1000000 - datetime.utcnow().microsecond
+  time.sleep(sleeptime/1000000.0)