diff --git a/.github/workflows/rust.yml b/.github/workflows/build.yml similarity index 95% rename from .github/workflows/rust.yml rename to .github/workflows/build.yml index 792f737..ab7b305 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/build.yml @@ -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 @@ -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 @@ -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. @@ -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 \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index 9623b54..6a8f6b9 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,5 +1,8 @@ FROM debian:bookworm-slim COPY target/release/mqtt-gateway ./ +RUN chmod a+x mqtt-gateway + +VOLUME /config CMD ["./mqtt-gateway"] \ No newline at end of file diff --git a/config/config.yml b/config/config.yml new file mode 100644 index 0000000..057aed6 --- /dev/null +++ b/config/config.yml @@ -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" diff --git a/src/main.rs b/src/main.rs index 41b7652..34cbde3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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}; @@ -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"); @@ -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 = 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() +} +