c99koder / personal-influxdb

Import data from various APIs into InfluxDB
Apache License 2.0
263 stars 36 forks source link

Docker Container/K8s cronjob of the librelinkup code #24

Closed evanrich closed 1 year ago

evanrich commented 1 year ago

Hey there! Great work on writing a bunch of these things out. I have a cat who was recently diagnosed as diabetic, and had a freestyle libre2 attached to him to monitor his blood sugar levels constantly. I work in DevOps, and so I immediately googled to see if there was an API available I could query to get his data, and came across your repo.

I took the liberty (hope you don't mind of taking the librelinkup and config python files, writing a requirements.txt, creating a docker file, and running it in Kubernetes.

The docker image: https://hub.docker.com/repository/docker/evanrich/freelibre2influx/general

requirements.txt:

influxdb==5.3.1
requests==2.25.1
colorlog==6.7.0
pytz==2022.1

Here's the dockerfile:

FROM python:3.11.4-alpine3.18

LABEL maintainer="Evan Richardson (evanrich81[at]gmail.com)"

# Set the working directory to /app
WORKDIR /app

# Copy the requirements.txt file into the container at /app
COPY requirements.txt /app/

# Install the Python dependencies specified in requirements.txt
RUN pip install --no-cache-dir -r requirements.txt

# Copy the link.py and config.py files to the root directory
COPY librelinkup.py /
COPY config.py /

ENV INFLUXDB_HOST="default_host"
ENV INFLUXDB_PORT="default_port"
ENV INFLUXDB_USER="default_username"
ENV INFLUXDB_PASSWORD="default_password"
ENV LIBRELINKUP_DATABASE="database"
ENV LIBRELINKUP_USERNAME="librelinkup_username"
ENV LIBRELINKUP_PASSWORD="librelinkup_password"

# Run the Python script link.py
CMD ["python", "/librelinkup.py"]

I run this in a cronjob on kubernetes with the following code:

apiVersion: batch/v1
kind: CronJob
metadata:
  name: freelibre-cron
  namespace: default
spec:
  schedule: "0 */1 * * *"
  concurrencyPolicy: Forbid
  jobTemplate:
    spec:
      template:
        metadata:
          labels:
            app: freelibre-job
        spec:
          containers:
          - name: freelibre-upload-cron
            image: evanrich/freelibre2influx:latest
            imagePullPolicy: IfNotPresent
            envFrom:
            - secretRef:
                name: freestyle-secrets
          restartPolicy: OnFailure

All of the environmental variables (passwords, usernames, etc) are stored in DOPPLER, a free tier external secrets platform. I have external-secrets running in my home cluster and it automatically pulls in the credentials it needs with the following:

apiVersion: external-secrets.io/v1beta1
kind: ExternalSecret
metadata:
  name: freestyle-secrets
  namespace: default
spec:
  secretStoreRef:
    kind: ClusterSecretStore
    name: doppler
  target:
    name: freestyle-secrets
  data:
    - secretKey: INFLUXDB_HOST
      remoteRef:
        key: INFLUXDB_HOST
    - secretKey: INFLUXDB_PORT
      remoteRef:
        key: INFLUXDB_PORT
    - secretKey: INFLUXDB_USERNAME
      remoteRef:
        key: INFLUXDB_ADMIN_USER
    - secretKey: INFLUXDB_PASSWORD
      remoteRef:
        key: INFLUXDB_ADMIN_PASSWORD
    - secretKey: LIBRELINKUP_USERNAME
      remoteRef:
        key: LIBRELINKUP_USERNAME
    - secretKey: LIBRELINKUP_PASSWORD
      remoteRef:
        key: LIBRELINKUP_PASSWORD
    - secretKey: LIBRELINKUP_DATABASE
      remoteRef:
        key: LIBRELINKUP_DATABASE

This all then gets visualized in Grafana using a single panel dashboard.

I didn't fork your repo or anything, but, @c99koder if you're interested, I will and throw my files in so you can add them for others to use. The container/etc can easily be expanded upon for the other python scripts, I just had no use for them.

Thanks again!

c99koder commented 1 year ago

This is really great! I'm glad this project has been useful for you. Feel free to send a pull request with this documentation as a markdown file if you have some time, otherwise I'll copy/paste it from this issue over the weekend. Docker support has been requested a few times in the past, so it's nice to be able to give people a starting point on how to use it through Docker.

evanrich commented 1 year ago

Will do! will submit a PR later today with this info.