From 78f244bb4b67eead4690f6f0b2a3dde137314c7e Mon Sep 17 00:00:00 2001 From: Anton Seledets Date: Thu, 8 Feb 2024 02:58:47 +0300 Subject: [PATCH] Added docker configuration --- .gitignore | 5 ++- Dockerfile | 12 +++++ README.md | 110 ++++++++++++++++++++++++++++++++++++--------- config-docker | 21 +++++++++ docker-compose.yml | 54 ++++++++++++++++++++++ 5 files changed, 179 insertions(+), 23 deletions(-) create mode 100644 Dockerfile create mode 100644 config-docker create mode 100644 docker-compose.yml diff --git a/.gitignore b/.gitignore index 52cb90e..3274478 100644 --- a/.gitignore +++ b/.gitignore @@ -31,4 +31,7 @@ logic.html # JSON topics.JSON values.JSON -logic.JSON \ No newline at end of file +logic.JSON + +.vscode +.DS_Store diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..ca5484d --- /dev/null +++ b/Dockerfile @@ -0,0 +1,12 @@ +FROM golang:1.21-alpine3.18 as builder +WORKDIR /app +ADD . /app +ADD ./config-docker ./config.go +RUN go mod download +RUN go build -o controller . + +FROM alpine:3.18 +WORKDIR /app +COPY --from=builder /app/controller /app/ +CMD ["/app/controller"] + diff --git a/README.md b/README.md index 5259d53..554fcbc 100644 --- a/README.md +++ b/README.md @@ -1,46 +1,112 @@ # OS-EDGEC +## Description + Open Steering edge controller is written in Go Lang, and can be run or built for many operating systems. A Publish/Subscribe system, where sensors publish data on unique topics to the subscribing edge controller, which in turn publishes control commands to relays, motors, actuators and more. -# System Setup +## System Setup -Edge controller relies on MQTT for telemetry. You may use your MQTT server of choice. We suggest EMQX with username/password, jailed users, and ssl/tls. EMQX is open source and free for on premise installation. Edge controller will also work with many MQTT free to use services online. We do however suggest you run your services on premise. +Edge controller relies on MQTT for telemetry. You may use your MQTT server of choice. We suggest [EMQX](https://www.emqx.io/) with username/password, jailed users, and ssl/tls. [EMQX](https://www.emqx.io/) is open source and free for on premise installation. Edge controller will also work with many MQTT free to use services online. We do however suggest you run your services on premise. -Edge controller also relies on InfluxDB for time based data store. +Edge controller also relies on InfluxDB for time based data store. -Edit config.go to match your mqtt and influxdb server details. +Edit `config.go` to match your mqtt and influxdb server details. -Setup and deploy sensors or control. You can find SDI-12, RS485, and power control examples in our repository. You should configure each sensor mcu with a unique mqtt/device id, and a unique topic or "zone name" for orginizational purposes. +Setup and deploy sensors or control. You can find SDI-12, RS485, and power control examples in our repository. You should configure each sensor mcu with a unique mqtt/device id, and a unique topic or "zone name" for organizational purposes. Create a ca.pem file in the same directory as your edgec_server.go (or binary) run or build the edge controller +```bash go run . +``` +```bash go build +``` + +## Using Docker + +Prerequisites + +- Docker [installed](https://docs.docker.com/engine/install/) + +### Building + +```bash + +docker build -t os-edgec:latest . + +``` + +### Running + +Please make sure that MQTT and Influxdb variables are set properly in ./config-docker if you running both servers locally. + +[Build](#building) service in docker, then run it. + +```bash + +docker run -p 8081:8081 os-edgec:latest + +``` + +### Running using docker compose + +`docker-compose.yml` contains InfluxDB, EMQX and edge controller. + +```bash +docker compose build +``` + +```bash +docker compose up -d +``` + +### Check logs + +```bash + +docker compose logs -f controller + +``` + +### Cleaning up +```bash +docker compose down +``` +To free up docker rubbish (optional). +```bash +docker system prune +``` +```bash +docker image prune -a -f +``` -Example of ca.pem: +Example of `ca.pem`: ------BEGIN CERTIFICATE----- -MIIM9DCCC9wCAQAwXDEVMBMGA1UEAwwMKi5nb29nbGUuY29tMQswCQYDVQQGEwJD -QTEiMCAGA1UECAwZTmV3Zm91bmRsYW5kIGFuZCBMYWJyYWRvcjESMBAGA1UEBwwJ -U3QuIEpvaG5zMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAvKILbKB+ -9qUzK3Vr98hnG4T4UVZGKh1Q9jkrBOU9M2vDeWTMwQt+Eu9HMUe4E2vj+iBxSDes -6pi6LwlUDWqHBckj/SOzMQblUD3r+cyaP30hONfGxdynJwp25PGTuYUgk9fHm5GA -I9BHhM6f+Uh+tTsQESJgAmGCUJj9FEToM2/CRTRG6SDb8SR3bdOWzlun3S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TCCCk0GCSqGSIb3DQEJDjGCCj4wggo6MA4GA1Ud +DwEB/wQEAwIHgDATBgNVHSUEDDAKBggrBgEFBQcDATAMBgNVHRMBAf8EAjAAMIIK +AwYDVR0RBIIJ+jCCCfaCDCouZ29vZ2xlLmNvbYIWKi5hcHBlbmdpbmUuZ29vZ2xl +LmNvbYIJKi5iZG4uZGV2ghUqLm9yaWdpbi10ZXN0LmJkbi5kZXaCEiouY2xvdWQu +Z29vZ2xlLmNvbYIYKi5jcm93ZHNvdXJjZS5nb29nbGUuY29tghgqLmRhdGFjb21w +zAlloJswHfU= +-----END CERTIFICATE----- +``` diff --git a/config-docker b/config-docker new file mode 100644 index 0000000..223ca25 --- /dev/null +++ b/config-docker @@ -0,0 +1,21 @@ +package main + +/** MQTT Info */ + +var MQTT_BROKER = "mqtt" +var MQTT_ID = "EdgeC" +var MQTT_USER = "" +var MQTT_PASSWORD = "" +var MQTT_CONFIG = MQTT_USER + "/" + MQTT_ID + "/config" +var MQTT_STATUS = MQTT_USER + "/" + MQTT_ID + "/status" +var MQTT_PORT = 8883 + +/** InfluxDB Info */ +var INFLUX_URL = "http://influxdb:8086" +var INFLUX_TOKEN = "my-super-secret-auth-token" +var INFLUX_ORG = "oss-org" +var INFLUX_BUCKET = "oss-bucket" +var INFLUX_MEASUREMENT = "" + +/** Debug Messages */ +var DEBUG = true diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..690d720 --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,54 @@ +version: '3' +services: + controller: + build: . + ports: + - "8081:8081" + depends_on: + mqtt: + condition: service_started + influxdb: + condition: service_healthy + + mqtt: + image: emqx/emqx:5.5.0 + ports: + - "1883:1883" + - "8083:8083" + - "8084:8084" + - "8883:8883" + - "18083:18083" + environment: + foo: bar + volumes: + - emqx-data:/opt/emqx/data + + influxdb: + image: influxdb:2.0 + privileged: true + ports: + - "8086:8086" + environment: + - DOCKER_INFLUXDB_INIT_MODE=setup + - DOCKER_INFLUXDB_INIT_USERNAME=oss-user + - DOCKER_INFLUXDB_INIT_PASSWORD=oss-password + - DOCKER_INFLUXDB_INIT_ORG=oss-org + - DOCKER_INFLUXDB_INIT_BUCKET=oss-bucket + - DOCKER_INFLUXDB_INIT_RETENTION=1w + - DOCKER_INFLUXDB_INIT_ADMIN_TOKEN=my-super-secret-auth-token + volumes: + - influxdb-data:/var/lib/influxdb2 + - influxdb-config:/etc/influxdb2 + healthcheck: + test: "curl -f http://localhost:8086/ping" + interval: 5s + timeout: 10s + retries: 5 + +volumes: + emqx-data: + influxdb-data: + influxdb-config: + + +