Skip to content

Commit

Permalink
refactor: rewrite in go
Browse files Browse the repository at this point in the history
Signed-off-by: rare-magma <[email protected]>
  • Loading branch information
rare-magma committed Nov 1, 2024
1 parent 49fa65b commit 761c37b
Show file tree
Hide file tree
Showing 12 changed files with 497 additions and 236 deletions.
2 changes: 1 addition & 1 deletion .dockerignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
.github/
datadis_exporter.conf
datadis_exporter.json
*.png
*.json
*.service
Expand Down
35 changes: 33 additions & 2 deletions .github/workflows/docker.yml → .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Create and publish a container image
name: Create and publish artifacts

on:
push:
Expand All @@ -10,6 +10,10 @@ env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

jobs:
build-and-push-image:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -43,4 +47,31 @@ jobs:
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
labels: ${{ steps.meta.outputs.labels }}
labels: ${{ steps.meta.outputs.labels }}

releases-matrix:
permissions:
contents: write
name: Release Go Binaries
runs-on: ubuntu-latest
strategy:
matrix:
goos: [linux]
goarch: [amd64, arm64]
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
- name: Build
run: go build -ldflags="-s -w" -trimpath -o datadis_exporter main.go
env:
CGO_ENABLED: 0
GOOS: ${{ matrix.goos }}
GOARCH: ${{ matrix.goarch }}
- name: Compress
run: zip datadis_exporter-${{ matrix.goos }}-${{ matrix.goarch }}.zip datadis_exporter
- name: Create Release
id: create_release
uses: softprops/action-gh-release@v2
with:
files: datadis_exporter-${{ matrix.goos }}-${{ matrix.goarch }}.zip
tag_name: latest
18 changes: 9 additions & 9 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
FROM docker.io/library/alpine:latest
ENV RUNNING_IN_DOCKER=true
ENTRYPOINT ["/bin/bash"]
CMD ["/app/datadis_exporter.sh"]
COPY datadis_exporter.sh /app/datadis_exporter.sh
RUN addgroup -g 10001 user \
&& adduser -H -D -u 10000 -G user user
RUN apk add --quiet --no-cache bash coreutils curl jq
USER user:user
FROM docker.io/library/golang:alpine AS builder
WORKDIR /app
ENV CGO_ENABLED=0
COPY main.go go.mod ./
RUN go build -ldflags "-s -w" -trimpath -o app main.go

FROM cgr.dev/chainguard/static:latest
COPY --from=builder /app/app /usr/bin/app
ENTRYPOINT ["/usr/bin/app"]
16 changes: 10 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@
install:
@mkdir --parents $${HOME}/.local/bin \
&& mkdir --parents $${HOME}/.config/systemd/user \
&& cp datadis_exporter.sh $${HOME}/.local/bin/ \
&& chmod +x $${HOME}/.local/bin/datadis_exporter.sh \
&& cp --no-clobber datadis_exporter.conf $${HOME}/.config/datadis_exporter.conf \
&& chmod 400 $${HOME}/.config/datadis_exporter.conf \
&& cp datadis_exporter $${HOME}/.local/bin/ \
&& chmod +x $${HOME}/.local/bin/datadis_exporter \
&& cp --no-clobber datadis_exporter.json $${HOME}/.config/datadis_exporter.json \
&& chmod 400 $${HOME}/.config/datadis_exporter.json \
&& cp datadis-exporter.timer $${HOME}/.config/systemd/user/ \
&& cp datadis-exporter.service $${HOME}/.config/systemd/user/ \
&& systemctl --user enable --now datadis-exporter.timer

.PHONY: uninstall
uninstall:
@rm -f $${HOME}/.local/bin/datadis_exporter.sh \
&& rm -f $${HOME}/.config/datadis_exporter.conf \
@rm -f $${HOME}/.local/bin/datadis_exporter \
&& rm -f $${HOME}/.config/datadis_exporter.json \
&& systemctl --user disable --now datadis-exporter.timer \
&& rm -f $${HOME}/.config/.config/systemd/user/datadis-exporter.timer \
&& rm -f $${HOME}/.config/systemd/user/datadis-exporter.service

.PHONY: build
build:
@go build -ldflags="-s -w" -o datadis_exporter main.go
93 changes: 44 additions & 49 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
# datadis-exporter

Bash script that uploads the energy consumption and maximum power usage data from the DATADIS API to influxdb on a daily basis
CLI tool that uploads the energy consumption and maximum power usage data from the DATADIS API to influxdb on a daily basis

## Dependencies

- [awk](https://www.gnu.org/software/gawk/manual/gawk.html)
- [bash](https://www.gnu.org/software/bash/)
- [coreutils (date)](https://www.gnu.org/software/coreutils/)
- [curl](https://curl.se/)
- [gzip](https://www.gnu.org/software/gzip/)
- [go](https://go.dev/)
- [influxdb v2+](https://docs.influxdata.com/influxdb/v2.6/)
- [jq](https://stedolan.github.io/jq/)
- [systemd](https://systemd.io/)
- Optional:
- [make](https://www.gnu.org/software/make/) - for automatic installation support
- [docker](https://docs.docker.com/)
- [systemd](https://systemd.io/)

## Relevant documentation

Expand All @@ -29,7 +24,7 @@ Bash script that uploads the energy consumption and maximum power usage data fro

#### docker-compose

1. Configure `datadis_exporter.conf` (see the configuration section below).
1. Configure `datadis_exporter.json` (see the configuration section below).
1. Run it.

```bash
Expand All @@ -44,39 +39,46 @@ Bash script that uploads the energy consumption and maximum power usage data fro
docker build . --tag datadis-exporter
```

1. Configure `datadis_exporter.conf` (see the configuration section below).
1. Configure `datadis_exporter.json` (see the configuration section below).
1. Run it.

```bash
docker run --rm --init --tty --interactive --read-only --cap-drop ALL --security-opt no-new-privileges:true --cpus 2 -m 64m --pids-limit 16 --volume ./datadis_exporter.conf:/app/datadis_exporter.conf:ro ghcr.io/rare-magma/datadis-exporter:latest
docker run --rm --init --tty --interactive --read-only --cap-drop ALL --security-opt no-new-privileges:true --cpus 2 -m 64m --pids-limit 16 --volume ./datadis_exporter.json:/app/datadis_exporter.json:ro ghcr.io/rare-magma/datadis-exporter:latest
```

### With the Makefile

For convenience, you can install this exporter with the following command or follow the manual process described in the next paragraph.

```bash
make build
make install
$EDITOR $HOME/.config/datadis_exporter.conf
$EDITOR $HOME/.config/datadis_exporter.json
```

### Manually

1. Copy `datadis_exporter.sh` to `$HOME/.local/bin/` and make it executable.
1. Build `datadis_exporter` with:

2. Copy `datadis_exporter.conf` to `$HOME/.config/`, configure it (see the configuration section below) and make it read only.
```bash
go build -ldflags="-s -w" -o datadis_exporter main.go`
```

3. Copy the systemd unit and timer to `$HOME/.config/systemd/user/`:
2. Copy `datadis_exporter` to `$HOME/.local/bin/` and make it executable.

```bash
cp datadis-exporter.* $HOME/.config/systemd/user/
```
3. Copy `datadis_exporter.json` to `$HOME/.config/`, configure it (see the configuration section below) and make it read only.

4. and run the following command to activate the timer:
4. Copy the systemd unit and timer to `$HOME/.config/systemd/user/`:

```bash
systemctl --user enable --now datadis-exporter.timer
```
```bash
cp datadis-exporter.* $HOME/.config/systemd/user/
```

5. and run the following command to activate the timer:

```bash
systemctl --user enable --now datadis-exporter.timer
```

It's possible to trigger the execution by running manually:
Expand All @@ -88,25 +90,27 @@ systemctl --user start datadis-exporter.service
The config file has a few options:
```bash
INFLUXDB_HOST='influxdb.example.com'
INFLUXDB_API_TOKEN='ZXhhbXBsZXRva2VuZXhhcXdzZGFzZGptcW9kcXdvZGptcXdvZHF3b2RqbXF3ZHFhc2RhCg=='
ORG='home'
BUCKET='datadis'
DATADIS_USERNAME='username'
DATADIS_PASSWORD='password'
CUPS='ES0000000000000000XX0X'
DISTRIBUTOR_CODE='1'
```json
{
"InfluxDBHost": "influxdb.example.com",
"InfluxDBApiToken": "ZXhhbXBsZXRva2VuZXhhcXdzZGFzZGptcW9kcXdvZGptcXdvZHF3b2RqbXF3ZHFhc2RhCg==",
"Org": "home",
"Bucket": "datadis",
"DatadisUsername": "username",
"DatadisPassword": "password",
"Cups": "ES0000000000000000XX0X",
"DistributorCode": "1"
}
```
- `INFLUXDB_HOST` should be the FQDN of the influxdb server.
- `ORG` should be the name of the influxdb organization that contains the energy consumption data bucket defined below.
- `BUCKET` should be the name of the influxdb bucket that will hold the energy consumption data.
- `INFLUXDB_API_TOKEN` should be the influxdb API token value.
- `InfluxDBHost` should be the FQDN of the influxdb server.
- `Org` should be the name of the influxdb organization that contains the energy consumption data bucket defined below.
- `Bucket` should be the name of the influxdb bucket that will hold the energy consumption data.
- `InfluxDBApiToken` should be the influxdb API token value.
- This token should have write access to the `BUCKET` defined above.
- `DATADIS_USERNAME` and `DATADIS_PASSWORD`should be the credentials used to access the DATADIS website
- `CUPS` should be the Código Unificado de Punto de Suministro (CUPS)
- `DISTRIBUTOR_CODE` should be one of:
- `DatadisUsername` and `DATADIS_PASSWORD`should be the credentials used to access the DATADIS website
- `Cups` should be the Código Unificado de Punto de Suministro (CUPS)
- `DistributorCode` should be one of:
- 1: Viesgo,
- 2: E-distribución
- 3: E-redes
Expand All @@ -118,12 +122,6 @@ DISTRIBUTOR_CODE='1'
## Troubleshooting
Run the script manually with bash set to trace:
```bash
bash -x $HOME/.local/bin/datadis_exporter.sh
```
Check the systemd service logs and timer info with:
```bash
Expand Down Expand Up @@ -181,8 +179,8 @@ systemctl --user disable --now datadis-exporter.timer
Delete the following files:
```bash
~/.local/bin/datadis_exporter.sh
~/.config/datadis_exporter.conf
~/.local/bin/datadis_exporter
~/.config/datadis_exporter.json
~/.config/systemd/user/datadis-exporter.timer
~/.config/systemd/user/datadis-exporter.service
```
Expand All @@ -194,6 +192,3 @@ Delete the following files:
This project takes inspiration from the following:
- [MrMarble/datadis](https://github.com/MrMarble/datadis)
- [rare-magma/pbs-exporter](https://github.com/rare-magma/pbs-exporter)
- [mad-ady/prometheus-borg-exporter](https://github.com/mad-ady/prometheus-borg-exporter)
- [OVYA/prometheus-borg-exporter](https://github.com/OVYA/prometheus-borg-exporter)
4 changes: 2 additions & 2 deletions datadis-exporter.service
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ Description=datadis exporter
After=network-online.target

[Service]
ExecStart=/home/%u/.local/bin/datadis_exporter.sh
LoadCredential=creds:/home/%u/.config/datadis_exporter.conf
ExecStart=/home/%u/.local/bin/datadis_exporter
LoadCredential=creds:/home/%u/.config/datadis_exporter.json
Type=oneshot

# Security hardening options
Expand Down
8 changes: 0 additions & 8 deletions datadis_exporter.conf

This file was deleted.

10 changes: 10 additions & 0 deletions datadis_exporter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"InfluxDBHost": "",
"InfluxDBApiToken": "",
"Org": "",
"Bucket": "",
"DatadisUsername": "",
"DatadisPassword": "",
"Cups": "",
"DistributorCode": ""
}
Loading

0 comments on commit 761c37b

Please sign in to comment.