Skip to content

Commit

Permalink
Add config volume to docker image and allow reading from it (#5)
Browse files Browse the repository at this point in the history
* allow config file in config subfolder

* allow config file in config subfolder

* only push docker image from main branch

* fix condition

* skip attestation if not uploaded

* rename

* fix permission
  • Loading branch information
wuan authored Nov 9, 2024
1 parent d7ad018 commit 33e874c
Show file tree
Hide file tree
Showing 4 changed files with 78 additions and 8 deletions.
14 changes: 8 additions & 6 deletions .github/workflows/rust.yml → .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ jobs:
- name: Build Release
run: cargo build --release

- uses: actions/upload-artifact@v4
- name: Upload Release
uses: actions/upload-artifact@v4
with:
name: mqtt-gateway
path: target/release/mqtt-gateway
Expand All @@ -75,8 +76,9 @@ jobs:
# timeout-minutes: 5
# env:
# SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
build-and-push-image:
docker-image:
runs-on: ubuntu-latest
if:
needs: build
env:
REGISTRY: ghcr.io
Expand Down Expand Up @@ -108,6 +110,7 @@ jobs:
uses: docker/metadata-action@9ec57ed1fcdbf14dcef7dfbe97b2010124a938b7
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
tags: latest
# This step uses the `docker/build-push-action` action to build the image, based on your repository's `Dockerfile`. If the build succeeds, it pushes the image to GitHub Packages.
# It uses the `context` parameter to define the build's context as the set of files located in the specified path. For more information, see "[Usage](https://github.com/docker/build-push-action#usage)" in the README of the `docker/build-push-action` repository.
# It uses the `tags` and `labels` parameters to tag and label the image with the output from the "meta" step.
Expand All @@ -116,16 +119,15 @@ jobs:
uses: docker/build-push-action@f2a1d5e99d037542a71f64918e516c093c6f3fc4
with:
context: .
push: true
push: ${{ github.ref == 'refs/heads/main' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}

# This step generates an artifact attestation for the image, which is an unforgeable statement about where and how it was built. It increases supply chain security for people who consume the image. For more information, see "[AUTOTITLE](/actions/security-guides/using-artifact-attestations-to-establish-provenance-for-builds)."
- name: Generate artifact attestation
uses: actions/attest-build-provenance@v1
if: github.ref == 'refs/heads/main'
with:
subject-name: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME}}
subject-digest: ${{ steps.push.outputs.digest }}
push-to-registry: true


push-to-registry: true
3 changes: 3 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
FROM debian:bookworm-slim

COPY target/release/mqtt-gateway ./
RUN chmod a+x mqtt-gateway

VOLUME /config

CMD ["./mqtt-gateway"]
39 changes: 39 additions & 0 deletions config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
mqttUrl: "mqtt://mqtt.home.wuerl.net:1883"
mqttClientId: "sensors_gateway_dev"
sources:
- name: "Sensor data"
type: "sensor"
prefix: "sensors"
targets:
# - type: "influxdb"
# host: "influx.home.wuerl.net"
# port: 8086
# database: "sensors"
# - type: "postgresql"
# host: "atlas.home.wuerl.net"
# port: 5433
# user: "postgres"
# password: "iUtHPTBprMp7wBMqiUHgZROG"
# database: "sensors"
- name: "Shelly data"
type: "shelly"
prefix: "shellies"
targets:
# - type: "influxdb"
# host: "influx.home.wuerl.net"
# port: 8086
# database: "shelly"
# - type: "postgresql"
# host: "atlas.home.wuerl.net"
# port: 5433
# user: "postgres"
# password: "iUtHPTBprMp7wBMqiUHgZROG"
# database: "shelly"
- name: "PV data"
type: "opendtu"
prefix: "solar"
targets:
# - type: "influxdb"
# host: "influx.home.wuerl.net"
# port: 8086
# database: "solar"
30 changes: 28 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::fmt::Debug;
use std::sync::{Arc, Mutex};
use std::thread::JoinHandle;
use std::{fs, time::Duration};

use std::path::Path;
use std::process::exit;
use crate::config::SourceType;
use crate::data::CheckMessage;
use chrono::{DateTime, Utc};
Expand Down Expand Up @@ -34,7 +35,9 @@ fn main() {
// Initialize the logger from the environment
env_logger::init();

let config_string = fs::read_to_string("config.yml").expect("failed to read config file");
let config_file_path = determine_config_file_path();

let config_string = fs::read_to_string(config_file_path).expect("failed to read config file");
let config: config::Config =
serde_yml::from_str(&config_string).expect("failed to parse config file");

Expand Down Expand Up @@ -112,3 +115,26 @@ fn main() {
}
}

fn determine_config_file_path() -> String {
let config_file_name = "config.yml";
let config_locations = ["./", "./config"];

let mut config_file_path: Option<String> = None;

for config_location in config_locations {
let path = Path::new(config_location);
let tmp_config_file_path = path.join(Path::new(config_file_name));
if tmp_config_file_path.exists() && tmp_config_file_path.is_file() {
config_file_path = Some(String::from(tmp_config_file_path.to_str().unwrap()));
break;
}
}

if config_file_path.is_none() {
println!("ERROR: no configuration file found");
exit(10);
}

config_file_path.unwrap()
}

0 comments on commit 33e874c

Please sign in to comment.