From 868c645f8195b197bd03e7bfcf04fd4483cc99b9 Mon Sep 17 00:00:00 2001 From: Hasan Eroglu Date: Fri, 12 Jul 2024 11:24:21 +0200 Subject: [PATCH 1/5] add parameterized hostnames and ports Signed-off-by: Hasan Eroglu --- .env | 11 +++++++++++ docker-compose.yml | 18 +++++++++--------- .../js/coap-content-negotiation-calculator.js | 4 ++-- .../coap/js/coap-simple-calculator.js | 4 ++-- .../http-content-negotiation-calculator.js | 4 ++-- .../http/express/http-simple-calculator.js | 4 ++-- things/calculator/http/flask/main.py | 6 ++++++ things/calculator/mqtt/js/main.js | 4 ++-- things/elevator/modbus/js/main.js | 4 ++-- 9 files changed, 38 insertions(+), 21 deletions(-) create mode 100644 .env diff --git a/.env b/.env new file mode 100644 index 0000000..c193ccf --- /dev/null +++ b/.env @@ -0,0 +1,11 @@ +WEB_PORT_IN=80 +MODBUS_ELEVATOR_PORT_IN=3179 +COAP_SIMPLE_PORT_IN=5683 +COAP_NEGOTIATION_PORT_IN=5684 +TRAEFIK_DASHBOARD_PORT_IN=8080 + +WEB_PORT_OUT=80 +MODBUS_ELEVATOR_PORT_OUT=3179 +COAP_SIMPLE_PORT_OUT=5683 +COAP_NEGOTIATION_PORT_OUT=5684 +TRAEFIK_DASHBOARD_PORT_OUT=8080 diff --git a/docker-compose.yml b/docker-compose.yml index c54c674..8203b14 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -47,16 +47,16 @@ services: - "--log.level=DEBUG" - "--api.insecure=true" - "--providers.docker=true" - - "--entrypoints.web.address=:80" - - "--entrypoints.modbus-elevator.address=:3179" - - "--entrypoints.coap-calculator-simple.address=:5683/udp" - - "--entrypoints.coap-calculator-content-negotiation.address=:5684/udp" + - "--entrypoints.web.address=:${WEB_PORT_IN}" + - "--entrypoints.modbus-elevator.address=:${MODBUS_ELEVATOR_PORT_IN}" + - "--entrypoints.coap-calculator-simple.address=:${COAP_SIMPLE_PORT_IN}/udp" + - "--entrypoints.coap-calculator-content-negotiation.address=:${COAP_NEGOTIATION_PORT_IN}/udp" ports: - - "80:80" - - "3179:3179" - - "5683:5683/udp" - - "5684:5684/udp" - - "8080:8080" + - "${WEB_PORT_OUT}:${WEB_PORT_IN}" + - "${MODBUS_ELEVATOR_PORT_OUT}:${MODBUS_ELEVATOR_PORT_IN}" + - "${COAP_SIMPLE_PORT_OUT}:${COAP_SIMPLE_PORT_IN}/udp" + - "${COAP_NEGOTIATION_PORT_OUT}:${COAP_NEGOTIATION_PORT_IN}/udp" + - "${TRAEFIK_DASHBOARD_PORT_OUT}:${TRAEFIK_DASHBOARD_PORT_IN}" volumes: # So that Traefik can listen to the Docker events - /var/run/docker.sock:/var/run/docker.sock diff --git a/things/calculator/coap/js/coap-content-negotiation-calculator.js b/things/calculator/coap/js/coap-content-negotiation-calculator.js index c5e5138..6f95935 100644 --- a/things/calculator/coap/js/coap-content-negotiation-calculator.js +++ b/things/calculator/coap/js/coap-content-negotiation-calculator.js @@ -7,8 +7,8 @@ const cbor = require('cbor') require('dotenv').config() const server = coap.createServer() -const hostname = 'localhost' -let portNumber = 5684 +const hostname = process.env.HOSTNAME ?? 'localhost' +let portNumber = process.env.PORT ?? 5684 const thingName = 'coap-calculator-content-negotiation' const { values: { port } } = parseArgs({ diff --git a/things/calculator/coap/js/coap-simple-calculator.js b/things/calculator/coap/js/coap-simple-calculator.js index 802888d..d347803 100644 --- a/things/calculator/coap/js/coap-simple-calculator.js +++ b/things/calculator/coap/js/coap-simple-calculator.js @@ -6,8 +6,8 @@ const { JsonPlaceholderReplacer } = require('json-placeholder-replacer') require('dotenv').config() const server = coap.createServer() -const hostname = 'localhost' -let portNumber = 5683 +const hostname = process.env.HOSTNAME ?? 'localhost' +let portNumber = process.env.PORT ?? 5683 const thingName = 'coap-calculator-simple' const { values: { port } } = parseArgs({ diff --git a/things/calculator/http/express/http-content-negotiation-calculator.js b/things/calculator/http/express/http-content-negotiation-calculator.js index bbc15b0..e51644d 100644 --- a/things/calculator/http/express/http-content-negotiation-calculator.js +++ b/things/calculator/http/express/http-content-negotiation-calculator.js @@ -10,8 +10,8 @@ require('dotenv').config() const app = express() app.use(express.json({ strict: false })); -const hostname = 'localhost' -let portNumber = 3001 +const hostname = process.env.HOSTNAME ?? 'localhost' +let portNumber = process.env.PORT ?? 3001 const thingName = 'http-express-calculator-content-negotiation' const TDEndPoint = `/${thingName}`, diff --git a/things/calculator/http/express/http-simple-calculator.js b/things/calculator/http/express/http-simple-calculator.js index 8f414be..84763e2 100644 --- a/things/calculator/http/express/http-simple-calculator.js +++ b/things/calculator/http/express/http-simple-calculator.js @@ -9,8 +9,8 @@ require("dotenv").config(); const app = express(); app.use(express.json({ strict: false })); -const hostname = "localhost"; -let portNumber = 3000; +const hostname = process.env.HOSTNAME ?? "localhost"; +let portNumber = process.env.PORT ?? 3000; const thingName = "http-express-calculator-simple"; const TDEndPoint = `/${thingName}`, diff --git a/things/calculator/http/flask/main.py b/things/calculator/http/flask/main.py index 7fd2eec..6a348ab 100644 --- a/things/calculator/http/flask/main.py +++ b/things/calculator/http/flask/main.py @@ -25,6 +25,12 @@ hostname = "0.0.0.0" portNumber = 5000 +if "HOSTNAME" in os.environ: + hostname = os.environ["HOSTNAME"] + +if "PORT" in os.environ: + portNumber = os.environ["PORT"] + thingName = "http-flask-calculator" PROPERTIES = "properties" ACTIONS = "actions" diff --git a/things/calculator/mqtt/js/main.js b/things/calculator/mqtt/js/main.js index 2c6ff73..d67e7df 100644 --- a/things/calculator/mqtt/js/main.js +++ b/things/calculator/mqtt/js/main.js @@ -5,8 +5,8 @@ const path = require('path') const { JsonPlaceholderReplacer } = require('json-placeholder-replacer') require('dotenv').config() -const hostname = 'test.mosquitto.org' -let portNumber = 1883 +const hostname = process.env.HOSTNAME ?? 'test.mosquitto.org' +let portNumber = process.env.PORT ?? 1883 const { values: { port } } = parseArgs({ options: { diff --git a/things/elevator/modbus/js/main.js b/things/elevator/modbus/js/main.js index 378beff..d2ac77c 100644 --- a/things/elevator/modbus/js/main.js +++ b/things/elevator/modbus/js/main.js @@ -6,8 +6,8 @@ const { parseArgs } = require('node:util') require('dotenv').config() const thingName = "modbus-elevator" -const hostname = "0.0.0.0" -let portNumber = "8502" +const hostname = process.env.HOSTNAME ?? "0.0.0.0" +let portNumber = process.env.PORT ?? "8502" const thingUnitID = 1 const { values: { port } } = parseArgs({ From dcde949dbe2f5f04d0e3624bcad5d69a4db92963 Mon Sep 17 00:00:00 2001 From: Hasan Eroglu Date: Thu, 25 Jul 2024 17:46:18 +0200 Subject: [PATCH 2/5] add hostname as env variable and pass it to the things Signed-off-by: Hasan Eroglu --- .env | 5 ++++ docker-compose.yml | 24 +++++++++++++++++++ package-lock.json | 6 ++--- .../calculator/coap/js/Dockerfile-contentneg | 4 +++- things/calculator/coap/js/Dockerfile-simple | 4 +++- .../js/coap-content-negotiation-calculator.js | 1 + .../http/express/Dockerfile-contentneg | 4 +++- .../calculator/http/express/Dockerfile-simple | 4 +++- things/calculator/http/flask/Dockerfile | 4 +++- things/calculator/mqtt/js/Dockerfile | 2 -- things/calculator/mqtt/js/main.js | 2 +- things/elevator/modbus/js/Dockerfile | 5 +++- 12 files changed, 53 insertions(+), 12 deletions(-) diff --git a/.env b/.env index c193ccf..7eb8cc3 100644 --- a/.env +++ b/.env @@ -1,11 +1,16 @@ +# IN PORTS WEB_PORT_IN=80 MODBUS_ELEVATOR_PORT_IN=3179 COAP_SIMPLE_PORT_IN=5683 COAP_NEGOTIATION_PORT_IN=5684 TRAEFIK_DASHBOARD_PORT_IN=8080 +# OUT PORTS WEB_PORT_OUT=80 MODBUS_ELEVATOR_PORT_OUT=3179 COAP_SIMPLE_PORT_OUT=5683 COAP_NEGOTIATION_PORT_OUT=5684 TRAEFIK_DASHBOARD_PORT_OUT=8080 + +HOSTNAME="localhost" +BROKER_URI="test.mosquitto.org" \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml index 8203b14..4a4621b 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -6,30 +6,50 @@ services: build: context: ./things/calculator/ dockerfile: ./coap/js/Dockerfile-simple + args: + - PORT_ARG=${COAP_SIMPLE_PORT_OUT} + environment: + - HOSTNAME=${HOSTNAME} coap-calculator-content-negotiation: labels: - traefik.udp.routers.coap-calculator-content-negotiation.entrypoints=coap-calculator-content-negotiation build: context: ./things/calculator/ dockerfile: ./coap/js/Dockerfile-contentneg + args: + - PORT_ARG=${COAP_NEGOTIATION_PORT_OUT} + environment: + - HOSTNAME=${HOSTNAME} http-express-calculator-simple: labels: - traefik.http.routers.http-express-calculator-simple.rule=PathPrefix(`/http-express-calculator-simple`) build: context: ./things/calculator/ dockerfile: ./http/express/Dockerfile-simple + args: + - PORT_ARG=${WEB_PORT_OUT} + environment: + - HOSTNAME=${HOSTNAME} http-express-calculator-content-negotiation: labels: - traefik.http.routers.http-express-calculator-content-negotiation.rule=PathPrefix(`/http-express-calculator-content-negotiation`) build: context: ./things/calculator/ dockerfile: ./http/express/Dockerfile-contentneg + args: + - PORT_ARG=${WEB_PORT_OUT} + environment: + - HOSTNAME=${HOSTNAME} http-flask-calculator: labels: - traefik.http.routers.http-flask-calculator.rule=PathPrefix(`/http-flask-calculator`) build: context: ./things/calculator/ dockerfile: ./http/flask/Dockerfile + args: + - PORT_ARG=${WEB_PORT_OUT} + environment: + - HOSTNAME=${HOSTNAME} mqtt-calculator: build: context: ./things/calculator/ @@ -38,9 +58,13 @@ services: labels: - traefik.tcp.routers.modbus-elevator.entrypoints=modbus-elevator - traefik.tcp.routers.modbus-elevator.rule=HostSNI(`*`) + environment: + - BROKER_URI=${BROKER_URI} build: context: ./things/elevator/ dockerfile: ./modbus/js/Dockerfile + args: + - PORT_ARG=${MODBUS_ELEVATOR_PORT_OUT} reverse-proxy: image: traefik:v3.0 command: diff --git a/package-lock.json b/package-lock.json index 54df27f..7dc5f8e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -10,9 +10,9 @@ "./things/*/*/*" ], "dependencies": { - "@node-wot/binding-coap": "^0.8.13", - "@node-wot/binding-http": "^0.8.13", - "@node-wot/core": "^0.8.13" + "@node-wot/binding-coap": "0.8.13", + "@node-wot/binding-http": "0.8.13", + "@node-wot/core": "0.8.13" }, "devDependencies": { "@types/express": "^4.17.17", diff --git a/things/calculator/coap/js/Dockerfile-contentneg b/things/calculator/coap/js/Dockerfile-contentneg index 70f1c8e..bf7437a 100644 --- a/things/calculator/coap/js/Dockerfile-contentneg +++ b/things/calculator/coap/js/Dockerfile-contentneg @@ -6,8 +6,10 @@ COPY ./coap/js . RUN npm install +ARG PORT_ARG=5684 +ENV PORT=${PORT_ARG} ENV TM_PATH="./calculator.tm.json" CMD ["node", "coap-content-negotiation-calculator.js"] -EXPOSE 5684/udp \ No newline at end of file +EXPOSE ${PORT}/udp \ No newline at end of file diff --git a/things/calculator/coap/js/Dockerfile-simple b/things/calculator/coap/js/Dockerfile-simple index 2cb265c..e04b669 100644 --- a/things/calculator/coap/js/Dockerfile-simple +++ b/things/calculator/coap/js/Dockerfile-simple @@ -6,8 +6,10 @@ COPY ./coap/js . RUN npm install +ARG PORT_ARG=5683 +ENV PORT=${PORT_ARG} ENV TM_PATH="./calculator.tm.json" CMD ["node", "coap-simple-calculator.js"] -EXPOSE 5683/udp \ No newline at end of file +EXPOSE ${PORT}/udp \ No newline at end of file diff --git a/things/calculator/coap/js/coap-content-negotiation-calculator.js b/things/calculator/coap/js/coap-content-negotiation-calculator.js index 6f95935..a16b668 100644 --- a/things/calculator/coap/js/coap-content-negotiation-calculator.js +++ b/things/calculator/coap/js/coap-content-negotiation-calculator.js @@ -217,6 +217,7 @@ server.on('request', (req, res) => { } else { if (!segments[2]) { if (req.method === 'GET') { + //FIXME: No null check for acceptHeaders if (acceptHeaders.includes('application/json') || acceptHeaders.includes('application/td+json') || acceptHeaders.includes('application/*') || acceptHeaders === '*/*') { res.setOption('Content-Format', 'application/json') res.end(JSON.stringify(thingDescription)) diff --git a/things/calculator/http/express/Dockerfile-contentneg b/things/calculator/http/express/Dockerfile-contentneg index 4c304ad..0ff6da9 100644 --- a/things/calculator/http/express/Dockerfile-contentneg +++ b/things/calculator/http/express/Dockerfile-contentneg @@ -6,8 +6,10 @@ COPY ./http/express . RUN npm install +ARG PORT_ARG=3001 +ENV PORT=${PORT_ARG} ENV TM_PATH="./calculator.tm.json" CMD ["node", "http-content-negotiation-calculator.js"] -EXPOSE 3001 \ No newline at end of file +EXPOSE ${PORT} \ No newline at end of file diff --git a/things/calculator/http/express/Dockerfile-simple b/things/calculator/http/express/Dockerfile-simple index 8382623..b194e7e 100644 --- a/things/calculator/http/express/Dockerfile-simple +++ b/things/calculator/http/express/Dockerfile-simple @@ -6,8 +6,10 @@ COPY ./http/express . RUN npm install +ARG PORT_ARG=3000 +ENV PORT=${PORT_ARG} ENV TM_PATH="./calculator.tm.json" CMD ["node", "http-simple-calculator.js"] -EXPOSE 3000 \ No newline at end of file +EXPOSE ${PORT} \ No newline at end of file diff --git a/things/calculator/http/flask/Dockerfile b/things/calculator/http/flask/Dockerfile index fecd32c..00866c7 100644 --- a/things/calculator/http/flask/Dockerfile +++ b/things/calculator/http/flask/Dockerfile @@ -7,10 +7,12 @@ WORKDIR /app COPY ./calculator.tm.json . COPY ./http/flask . +ARG PORT_ARG=5000 +ENV PORT=${PORT_ARG} ENV TM_PATH=./calculator.tm.json RUN poetry install CMD [ "poetry", "run", "python", "main.py" ] -EXPOSE 5000 \ No newline at end of file +EXPOSE ${PORT} \ No newline at end of file diff --git a/things/calculator/mqtt/js/Dockerfile b/things/calculator/mqtt/js/Dockerfile index 6862080..09e09b6 100644 --- a/things/calculator/mqtt/js/Dockerfile +++ b/things/calculator/mqtt/js/Dockerfile @@ -9,5 +9,3 @@ RUN npm install ENV TM_PATH="./calculator.tm.json" CMD ["node", "main.js"] - -EXPOSE 1883 \ No newline at end of file diff --git a/things/calculator/mqtt/js/main.js b/things/calculator/mqtt/js/main.js index d67e7df..155c921 100644 --- a/things/calculator/mqtt/js/main.js +++ b/things/calculator/mqtt/js/main.js @@ -5,7 +5,7 @@ const path = require('path') const { JsonPlaceholderReplacer } = require('json-placeholder-replacer') require('dotenv').config() -const hostname = process.env.HOSTNAME ?? 'test.mosquitto.org' +const hostname = process.env.BROKER_URI ?? 'test.mosquitto.org' let portNumber = process.env.PORT ?? 1883 const { values: { port } } = parseArgs({ diff --git a/things/elevator/modbus/js/Dockerfile b/things/elevator/modbus/js/Dockerfile index 7cf9f84..6bb1698 100644 --- a/things/elevator/modbus/js/Dockerfile +++ b/things/elevator/modbus/js/Dockerfile @@ -6,7 +6,10 @@ COPY ./modbus/js . RUN npm install +ARG PORT_ARG=8502 +ENV PORT=${PORT_ARG} ENV TM_PATH="./elevator.tm.json" CMD ["node", "main.js"] -EXPOSE 8502 + +EXPOSE ${PORT} From ae041b8cbebef12422195a559665b9dbc9df1872 Mon Sep 17 00:00:00 2001 From: Hasan Eroglu Date: Fri, 26 Jul 2024 11:36:29 +0200 Subject: [PATCH 3/5] change variable name Signed-off-by: Hasan Eroglu --- things/calculator/mqtt/js/main.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/things/calculator/mqtt/js/main.js b/things/calculator/mqtt/js/main.js index 155c921..9d86cd0 100644 --- a/things/calculator/mqtt/js/main.js +++ b/things/calculator/mqtt/js/main.js @@ -5,7 +5,7 @@ const path = require('path') const { JsonPlaceholderReplacer } = require('json-placeholder-replacer') require('dotenv').config() -const hostname = process.env.BROKER_URI ?? 'test.mosquitto.org' +const brokerURI = process.env.BROKER_URI ?? 'test.mosquitto.org' let portNumber = process.env.PORT ?? 1883 const { values: { port } } = parseArgs({ @@ -26,7 +26,7 @@ const PROPERTIES = 'properties' const ACTIONS = 'actions' const EVENTS = 'events' -const broker = mqtt.connect(`mqtt://${hostname}`, { port: portNumber }) +const broker = mqtt.connect(`mqtt://${brokerURI}`, { port: portNumber }) const tmPath = process.env.TM_PATH @@ -43,7 +43,7 @@ placeholderReplacer.addVariableMap({ PROPERTIES, ACTIONS, EVENTS, - HOSTNAME: hostname, + HOSTNAME: brokerURI, PORT_NUMBER: portNumber, RESULT_OBSERVABLE: true, LAST_CHANGE_OBSERVABLE: true From b3414185f3bc43c186703f4dc51a0b3cd370587b Mon Sep 17 00:00:00 2001 From: Hasan Eroglu Date: Fri, 26 Jul 2024 14:42:52 +0200 Subject: [PATCH 4/5] add conditional for hostname env variable Signed-off-by: Hasan Eroglu --- things/elevator/modbus/js/main.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/things/elevator/modbus/js/main.js b/things/elevator/modbus/js/main.js index d2ac77c..2de0fe6 100644 --- a/things/elevator/modbus/js/main.js +++ b/things/elevator/modbus/js/main.js @@ -6,7 +6,9 @@ const { parseArgs } = require('node:util') require('dotenv').config() const thingName = "modbus-elevator" -const hostname = process.env.HOSTNAME ?? "0.0.0.0" +const hostname = process.env.HOSTNAME + ? process.env.HOSTNAME === "localhost" ? "0.0.0.0" : process.env.HOSTNAME + : "0.0.0.0" let portNumber = process.env.PORT ?? "8502" const thingUnitID = 1 From e8cbe44ed6cb73e3457cbd1d7c2c2c27282d2303 Mon Sep 17 00:00:00 2001 From: Ege Korkan Date: Fri, 26 Jul 2024 14:52:37 +0200 Subject: [PATCH 5/5] Update main.js --- things/elevator/modbus/js/main.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/things/elevator/modbus/js/main.js b/things/elevator/modbus/js/main.js index 2de0fe6..c3f8647 100644 --- a/things/elevator/modbus/js/main.js +++ b/things/elevator/modbus/js/main.js @@ -6,6 +6,7 @@ const { parseArgs } = require('node:util') require('dotenv').config() const thingName = "modbus-elevator" +// The following is needed since the modbus library we use does not support localhost but does support 0.0.0.0 const hostname = process.env.HOSTNAME ? process.env.HOSTNAME === "localhost" ? "0.0.0.0" : process.env.HOSTNAME : "0.0.0.0" @@ -191,4 +192,4 @@ const serverTCP = new ServerTCP(vector, { host: hostname, port: portNumber, debu serverTCP.on("socketError", function(err){ // Handle socket error if needed, can be ignored console.error(err) -}); \ No newline at end of file +});