Skip to content

Commit 916607b

Browse files
authored
Merge pull request #2 from spaceapi-community/dockerize
Add Dockerfile and publish image on Docker Hub.
2 parents 501da9d + 860698b commit 916607b

6 files changed

Lines changed: 128 additions & 1 deletion

File tree

.dockerignore

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Dockerfile
2+
.dockerignore
3+
venv/
4+
.git/
5+
.mypy_cache/
6+
config.json

.github/workflows/ci.yml

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
on:
2+
push:
3+
schedule:
4+
- cron: '30 3 * * 2'
5+
6+
name: CI
7+
8+
jobs:
9+
10+
docker-build:
11+
name: Build Docker image
12+
runs-on: ubuntu-latest
13+
if: github.ref != 'refs/heads/master'
14+
steps:
15+
- uses: actions/checkout@v2
16+
- name: Build Docker image
17+
run: |
18+
docker build -t spaceapi/influxdb-sensor-logger:latest .
19+
20+
docker-publish:
21+
name: Publish Docker image
22+
runs-on: ubuntu-latest
23+
if: github.ref == 'refs/heads/master'
24+
steps:
25+
- uses: actions/checkout@v2
26+
- name: Build Docker image
27+
run: |
28+
docker build \
29+
--no-cache \
30+
-t spaceapi/influxdb-sensor-logger:latest \
31+
-t spaceapi/influxdb-sensor-logger:v1 \
32+
.
33+
- name: Push Docker image
34+
run: |
35+
docker login -u "${{ secrets.DOCKER_USERNAME }}" -p "${{ secrets.DOCKER_PASSWORD }}" && \
36+
docker push -a spaceapi/influxdb-sensor-logger

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
*.pyc
33
venv/
44
config.json
5+
.mypy_cache/

Dockerfile

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
FROM python:3.10-alpine3.14
2+
3+
# Add user (UID 4333, chosen randomly)
4+
RUN addgroup -g 4333 -S spaceapi \
5+
&& adduser -u 4333 -S -G spaceapi spaceapi
6+
7+
# Install system dependencies
8+
RUN apk update && apk add bash dumb-init
9+
10+
# Install project dependencies
11+
COPY requirements.txt /code/requirements.txt
12+
RUN pip install -r /code/requirements.txt
13+
14+
# Add code
15+
COPY . /code
16+
WORKDIR /code
17+
RUN chown -R spaceapi:spaceapi /code/
18+
19+
# Default config
20+
ENV RELAY_INTERVAL_SECONDS=60
21+
22+
# Switch to non-privileged user
23+
USER spaceapi
24+
25+
# Specify entrypoint
26+
ENTRYPOINT ["/usr/bin/dumb-init", "--"]
27+
CMD ["bash", "entrypoint.sh"]

README.md

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1-
# SpaceAPI Sensor Logger
1+
# SpaceAPI InfluxDB Sensor Logger
2+
3+
[![Docker Image Size (tag)](https://img.shields.io/docker/image-size/spaceapi/influxdb-sensor-logger/latest)](https://hub.docker.com/r/spaceapi/influxdb-sensor-logger)
24

35
A Python 3 script to relay sensor values from a SpaceAPI endpoint to an
46
InfluxDB instance so it can be viewed in Grafana.
@@ -26,6 +28,22 @@ After installing the requirements from `requirements.txt`, just run `python3
2628
relay.py <endpoint-url>` regularly :)
2729

2830

31+
## Docker
32+
33+
The repository provides a `Dockerfile` that calls `relay.py` in a loop.
34+
Configure it using the following env vars:
35+
36+
- `INFLUXDB_HOST`
37+
- `INFLUXDB_PORT`
38+
- `INFLUXDB_USER`
39+
- `INFLUXDB_PASS`
40+
- `INFLUXDB_DB`
41+
- `SPACEAPI_ENDPOINT`
42+
- `RELAY_INTERVAL_SECONDS`
43+
44+
The image is published at [docker.io/spaceapi/influxdb-sensor-logger](https://hub.docker.com/r/spaceapi/influxdb-sensor-logger).
45+
46+
2947
## License
3048

3149
Licensed under either of

entrypoint.sh

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
#!/usr/bin/env bash
2+
set -euo pipefail
3+
4+
# Ensure that the specified variable is defined and non-empty.
5+
# Otherwise, abort.
6+
function ensure_var() {
7+
var=$1
8+
if [[ ${!var:-} == "" ]]; then
9+
1>&2 echo "Error: Variable $var not defined!"
10+
exit 1
11+
fi
12+
}
13+
14+
# Required vars
15+
ensure_var "INFLUXDB_HOST"
16+
ensure_var "INFLUXDB_PORT"
17+
ensure_var "INFLUXDB_USER"
18+
ensure_var "INFLUXDB_PASS"
19+
ensure_var "INFLUXDB_DB"
20+
ensure_var "SPACEAPI_ENDPOINT"
21+
ensure_var "RELAY_INTERVAL_SECONDS"
22+
23+
# Write config file
24+
{
25+
echo "{"
26+
echo " \"influxdb_host\": \"$INFLUXDB_HOST\","
27+
echo " \"influxdb_port\": $INFLUXDB_PORT,"
28+
echo " \"influxdb_user\": \"$INFLUXDB_USER\","
29+
echo " \"influxdb_pass\": \"$INFLUXDB_PASS\","
30+
echo " \"influxdb_db\": \"$INFLUXDB_DB\""
31+
echo "}"
32+
} > config.json
33+
34+
# Run every $RELAY_INTERVAL_SECONDS seconds
35+
while true; do
36+
echo -n "$(date) "
37+
python3 relay.py "$SPACEAPI_ENDPOINT"
38+
sleep "$RELAY_INTERVAL_SECONDS"
39+
done

0 commit comments

Comments
 (0)