From 4a7343446292a1f57fb3e273e35a8c5820f478cb Mon Sep 17 00:00:00 2001 From: rodneyosodo Date: Fri, 19 Jan 2024 17:32:52 +0300 Subject: [PATCH] Simplify docker deployment Signed-off-by: rodneyosodo --- Makefile | 56 +---------------- README.md | 2 +- docker/README.md | 101 ++++++++++++++++++++++++++++++ docker/brokers/README.md | 34 ---------- docker/brokers/docker-compose.yml | 51 --------------- docker/brokers/nats.yml | 16 ----- docker/brokers/profiles/README.md | 24 ------- docker/brokers/profiles/nats.yml | 19 ------ docker/brokers/rabbitmq.yml | 14 ----- docker/docker-compose.yml | 26 +++++--- docker/es/docker-compose.yml | 17 ----- docker/policy/model.conf | 17 ----- 12 files changed, 123 insertions(+), 254 deletions(-) delete mode 100644 docker/brokers/README.md delete mode 100644 docker/brokers/docker-compose.yml delete mode 100644 docker/brokers/nats.yml delete mode 100644 docker/brokers/profiles/README.md delete mode 100644 docker/brokers/profiles/nats.yml delete mode 100644 docker/brokers/rabbitmq.yml delete mode 100644 docker/es/docker-compose.yml delete mode 100644 docker/policy/model.conf diff --git a/Makefile b/Makefile index d95a67906..d1de47292 100644 --- a/Makefile +++ b/Makefile @@ -21,7 +21,6 @@ DOCKER_PROJECT ?= $(shell echo $(subst $(space),,$(USER_REPO)) | tr -c -s '[:aln DOCKER_COMPOSE_COMMANDS_SUPPORTED := up down config DEFAULT_DOCKER_COMPOSE_COMMAND := up GRPC_MTLS_CERT_FILES_EXISTS = 0 -DOCKER_PROFILE ?= $(MG_MQTT_BROKER_TYPE)_$(MG_MESSAGE_BROKER_TYPE) MOCKERY_VERSION=v2.38.0 ifneq ($(MG_MESSAGE_BROKER_TYPE),) MG_MESSAGE_BROKER_TYPE := $(MG_MESSAGE_BROKER_TYPE) @@ -29,12 +28,6 @@ else MG_MESSAGE_BROKER_TYPE=nats endif -ifneq ($(MG_MQTT_BROKER_TYPE),) - MG_MQTT_BROKER_TYPE := $(MG_MQTT_BROKER_TYPE) -else - MG_MQTT_BROKER_TYPE=nats -endif - ifneq ($(MG_ES_TYPE),) MG_ES_TYPE := $(MG_ES_TYPE) else @@ -111,7 +104,7 @@ clean: cleandocker: # Stops containers and removes containers, networks, volumes, and images created by up - docker compose -f docker/docker-compose.yml --profile $(DOCKER_PROFILE) -p $(DOCKER_PROJECT) down --rmi all -v --remove-orphans + docker compose -f docker/docker-compose.yml -p $(DOCKER_PROJECT) down --rmi all -v --remove-orphans ifdef pv # Remove unused volumes @@ -208,53 +201,10 @@ endif endif endif -define edit_docker_config - sed -i "s/MG_MQTT_BROKER_TYPE=.*/MG_MQTT_BROKER_TYPE=$(1)/" docker/.env - sed -i "s/MG_MQTT_BROKER_HEALTH_CHECK=.*/MG_MQTT_BROKER_HEALTH_CHECK=$$\{MG_$(shell echo ${MG_MQTT_BROKER_TYPE} | tr 'a-z' 'A-Z')_HEALTH_CHECK}/" docker/.env - sed -i "s/MG_MQTT_ADAPTER_WS_TARGET_PATH=.*/MG_MQTT_ADAPTER_WS_TARGET_PATH=$$\{MG_$(shell echo ${MG_MQTT_BROKER_TYPE} | tr 'a-z' 'A-Z')_WS_TARGET_PATH}/" docker/.env - sed -i "s/MG_MESSAGE_BROKER_TYPE=.*/MG_MESSAGE_BROKER_TYPE=$(2)/" docker/.env - sed -i "s,file: .*.yml,file: brokers/$(2).yml," docker/brokers/docker-compose.yml - sed -i "s,MG_MESSAGE_BROKER_URL=.*,MG_MESSAGE_BROKER_URL=$$\{MG_$(shell echo ${MG_MESSAGE_BROKER_TYPE} | tr 'a-z' 'A-Z')_URL\}," docker/.env - sed -i "s,MG_MQTT_ADAPTER_MQTT_QOS=.*,MG_MQTT_ADAPTER_MQTT_QOS=$$\{MG_$(shell echo ${MG_MQTT_BROKER_TYPE} | tr 'a-z' 'A-Z')_MQTT_QOS\}," docker/.env -endef - -change_config: -ifeq ($(DOCKER_PROFILE),nats_nats) - sed -i "s/- broker/- nats/g" docker/docker-compose.yml - sed -i "s/- rabbitmq/- nats/g" docker/docker-compose.yml - sed -i "s,MG_NATS_URL=.*,MG_NATS_URL=nats://nats:$$\{MG_NATS_PORT}," docker/.env - $(call edit_docker_config,nats,nats) -else ifeq ($(DOCKER_PROFILE),nats_rabbitmq) - sed -i "s/nats/broker/g" docker/docker-compose.yml - sed -i "s,MG_NATS_URL=.*,MG_NATS_URL=nats://nats:$$\{MG_NATS_PORT}," docker/.env - sed -i "s/rabbitmq/broker/g" docker/docker-compose.yml - $(call edit_docker_config,nats,rabbitmq) -else ifeq ($(DOCKER_PROFILE),vernemq_nats) - sed -i "s/nats/broker/g" docker/docker-compose.yml - sed -i "s/rabbitmq/broker/g" docker/docker-compose.yml - sed -i "s,MG_NATS_URL=.*,MG_NATS_URL=nats://broker:$$\{MG_NATS_PORT}," docker/.env - $(call edit_docker_config,vernemq,nats) -else ifeq ($(DOCKER_PROFILE),vernemq_rabbitmq) - sed -i "s/nats/broker/g" docker/docker-compose.yml - sed -i "s/rabbitmq/broker/g" docker/docker-compose.yml - $(call edit_docker_config,vernemq,rabbitmq) -else - $(error Invalid DOCKER_PROFILE $(DOCKER_PROFILE)) -endif - -run: check_certs change_config -ifeq ($(MG_ES_TYPE), redis) - sed -i "s/MG_ES_TYPE=.*/MG_ES_TYPE=redis/" docker/.env - sed -i "s/MG_ES_URL=.*/MG_ES_URL=$$\{MG_REDIS_URL}/" docker/.env - docker compose -f docker/docker-compose.yml --profile $(DOCKER_PROFILE) --profile redis -p $(DOCKER_PROJECT) $(DOCKER_COMPOSE_COMMAND) $(args) -else - sed -i "s,MG_ES_TYPE=.*,MG_ES_TYPE=$$\{MG_MESSAGE_BROKER_TYPE}," docker/.env - sed -i "s,MG_ES_URL=.*,MG_ES_URL=$$\{MG_$(shell echo ${MG_MESSAGE_BROKER_TYPE} | tr 'a-z' 'A-Z')_URL\}," docker/.env - docker compose -f docker/docker-compose.yml --env-file docker/.env --profile $(DOCKER_PROFILE) -p $(DOCKER_PROJECT) $(DOCKER_COMPOSE_COMMAND) $(args) -endif +run: check_certs + docker compose -f docker/docker-compose.yml --env-file docker/.env -p $(DOCKER_PROJECT) $(DOCKER_COMPOSE_COMMAND) $(args) run_addons: check_certs - $(call change_config) $(foreach SVC,$(RUN_ADDON_ARGS),$(if $(filter $(SVC),$(ADDON_SERVICES) $(EXTERNAL_SERVICES)),,$(error Invalid Service $(SVC)))) @for SVC in $(RUN_ADDON_ARGS); do \ MG_ADDONS_CERTS_PATH_PREFIX="../." docker compose -f docker/addons/$$SVC/docker-compose.yml -p $(DOCKER_PROJECT) --env-file ./docker/.env $(DOCKER_COMPOSE_COMMAND) $(args) & \ diff --git a/README.md b/README.md index 806b2dfda..499b1669b 100644 --- a/README.md +++ b/README.md @@ -51,7 +51,7 @@ Developing Magistrala will also require: Once the prerequisites are installed, execute the following commands from the project's root: ```bash -docker compose -f docker/docker-compose.yml --env-file docker/.env --profile nats_nats -p git_github_com_absmach_magistrala_git_ up +docker compose -f docker/docker-compose.yml --env-file docker/.env -p git_github_com_absmach_magistrala_git_ up ``` This will bring up the Magistrala docker services and interconnect them. This command can also be executed using the project's included Makefile: diff --git a/docker/README.md b/docker/README.md index 1f8b2c179..bc4f51a30 100644 --- a/docker/README.md +++ b/docker/README.md @@ -21,3 +21,104 @@ docker-compose -f docker/addons//docker-compose.yml up ``` To pull docker images from a specific release you need to change the value of `MG_RELEASE_TAG` in `.env` before running these commands. + +## Broker Configuration + +Magistrala supports configurable MQTT broker and Message broker, which also acts as events store. Magistrala uses two types of brokers: + +1. MQTT_BROKER: Handles MQTT communication between MQTT adapters and message broker. This can either be 'vernemq' or 'nats'. +2. MESSAGE_BROKER: Manages communication between adapters and Magistrala writer services. This can either be 'nats' or 'rabbitmq' or 'redis'. This is used to store messages for distributed processing. + +Events store: This is the same as MESSAGE_BROKER. This can either be 'nats' or 'rabbitmq' or 'redis'. This is used by Magistrala services to store events for distributed processing. If redis is used as an events store, then rabbitmq or nats is used as a message broker. + +Since nats is used as both MQTT_BROKER and MESSAGE_BROKER, it is not possible to run nats as an MQTT_BROKER and nats as a MESSAGE_BROKER at the same time, this is the current depolyment strategy for Magistrala in `docker/docker-compose.yml`. + +. Therefore, the following combinations are possible: + +- MQTT_BROKER: vernemq, MESSAGE_BROKER: nats, EVENTS_STORE: nats +- MQTT_BROKER: vernemq, MESSAGE_BROKER: nats, EVENTS_STORE: redis +- MQTT_BROKER: vernemq, MESSAGE_BROKER: rabbitmq, EVENTS_STORE: rabbitmq +- MQTT_BROKER: vernemq, MESSAGE_BROKER: rabbitmq, EVENTS_STORE: redis +- MQTT_BROKER: nats, MESSAGE_BROKER: rabbitmq, EVENTS_STORE: rabbitmq +- MQTT_BROKER: nats, MESSAGE_BROKER: rabbitmq, EVENTS_STORE: redis +- MQTT_BROKER: nats, MESSAGE_BROKER: nats, EVENTS_STORE: nats +- MQTT_BROKER: nats, MESSAGE_BROKER: nats, EVENTS_STORE: redis + +For Message brokers other than nats, you would need to change the `docker/.env`. For example, to use rabbitmq as a message broker: + +```env +MG_MESSAGE_BROKER_TYPE=rabbitmq +MG_MESSAGE_BROKER_URL=${MG_RABBITMQ_URL} +``` + +For redis as an events store, you would need to run rabbitmq or nats as a message broker. For example, to use redis as an events store with rabbitmq as a message broker: + +```env +MG_MESSAGE_BROKER_TYPE=rabbitmq +MG_MESSAGE_BROKER_URL=${MG_RABBITMQ_URL} +MG_ES_TYPE=redis +MG_ES_URL=${MG_REDIS_URL} +``` + +For MQTT brokers other than nats, you would need to change the `docker/.env`. For example, to use vernemq as a MQTT broker: + +```env +MG_MQTT_BROKER_TYPE=vernemq +MG_MQTT_BROKER_HEALTH_CHECK=${MG_VERNEMQ_HEALTH_CHECK} +MG_MQTT_ADAPTER_MQTT_QOS=${MG_VERNEMQ_MQTT_QOS} +MG_MQTT_ADAPTER_MQTT_TARGET_HOST=${MG_MQTT_BROKER_TYPE} +MG_MQTT_ADAPTER_MQTT_TARGET_PORT=1883 +MG_MQTT_ADAPTER_MQTT_TARGET_HEALTH_CHECK=${MG_MQTT_BROKER_HEALTH_CHECK} +MG_MQTT_ADAPTER_WS_TARGET_HOST=${MG_MQTT_BROKER_TYPE} +MG_MQTT_ADAPTER_WS_TARGET_PORT=8080 +MG_MQTT_ADAPTER_WS_TARGET_PATH=${MG_VERNEMQ_WS_TARGET_PATH} +``` + +### VerneMQ configuration + +```yaml +services: + vernemq: + image: magistrala/vernemq:${MG_RELEASE_TAG} + container_name: magistrala-vernemq + restart: on-failure + environment: + DOCKER_VERNEMQ_ALLOW_ANONYMOUS: ${MG_DOCKER_VERNEMQ_ALLOW_ANONYMOUS} + DOCKER_VERNEMQ_LOG__CONSOLE__LEVEL: ${MG_DOCKER_VERNEMQ_LOG__CONSOLE__LEVEL} + networks: + - magistrala-base-net + volumes: + - magistrala-broker-volume:/var/lib/vernemq +``` + +### RabbitMQ configuration + +```yaml +services: + rabbitmq: + image: rabbitmq:3.9.20-management-alpine + environment: + RABBITMQ_ERLANG_COOKIE: ${MG_RABBITMQ_COOKIE} + RABBITMQ_DEFAULT_USER: ${MG_RABBITMQ_USER} + RABBITMQ_DEFAULT_PASS: ${MG_RABBITMQ_PASS} + RABBITMQ_DEFAULT_VHOST: ${MG_RABBITMQ_VHOST} + ports: + - ${MG_RABBITMQ_PORT}:${MG_RABBITMQ_PORT} + - ${MG_RABBITMQ_HTTP_PORT}:${MG_RABBITMQ_HTTP_PORT} + networks: + - magistrala-base-net +``` + +### Redis configuration + +```yaml +services: + redis: + image: redis:7.2.0-alpine + container_name: magistrala-es-redis + restart: on-failure + networks: + - magistrala-base-net + volumes: + - magistrala-broker-volume:/data +``` diff --git a/docker/brokers/README.md b/docker/brokers/README.md deleted file mode 100644 index f2f6314c3..000000000 --- a/docker/brokers/README.md +++ /dev/null @@ -1,34 +0,0 @@ -# Brokers Docker Compose - -Magistrala supports configurable MQTT broker and Message broker. - -## MQTT Broker - -Magistrala supports VerneMQ and Nats as an MQTT broker. - -## Message Broker - -Magistrala supports NATS and RabbitMQ as a message broker. - -## Profiles - -This directory contains 4 docker-compose profiles for running Magistrala with different combinations of MQTT and message brokers. - -The profiles are: - -- `vernemq_nats` - VerneMQ as an MQTT broker and Nats as a message broker -- `vernemq_rabbitmq` - VerneMQ as an MQTT broker and RabbitMQ as a message broker -- `nats_nats` - Nats as an MQTT broker and Nats as a message broker -- `nats_rabbitmq` - Nats as an MQTT broker and RabbitMQ as a message broker - -The following command will run VerneMQ as an MQTT broker and Nats as a message broker: - -```bash -MG_MQTT_BROKER_TYPE=vernemq MG_MESSAGE_BROKER_TYPE=nats make run -``` - -The following command will run VerneMQ as an MQTT broker and RabbitMQ as a message broker: - -```bash -MG_MQTT_BROKER_TYPE=vernemq MG_MESSAGE_BROKER_TYPE=rabbitmq make run -``` diff --git a/docker/brokers/docker-compose.yml b/docker/brokers/docker-compose.yml deleted file mode 100644 index 419b3b681..000000000 --- a/docker/brokers/docker-compose.yml +++ /dev/null @@ -1,51 +0,0 @@ -# Copyright (c) Abstract Machines -# SPDX-License-Identifier: Apache-2.0 - -# This file configures Magistrala brokers. Magistrala uses two types of brokers: -# 1. MQTT_BROKER: Handles MQTT communication between MQTT adapters and message broker. -# 2. MESSAGE_BROKER: Manages communication between adapters and Magistrala writer services. -# -# MQTT_BROKER can be either 'vernemq' or 'nats'. -# MESSAGE_BROKER can be either 'nats' or 'rabbitmq'. -# -# Each broker has a unique profile for configuration. The available profiles are: -# - vernemq_nats: Uses 'vernemq' as MQTT_BROKER and 'nats' as MESSAGE_BROKER. -# - vernemq_rabbitmq: Uses 'vernemq' as MQTT_BROKER and 'rabbitmq' as MESSAGE_BROKER. -# - nats_nats: Uses 'nats' as both MQTT_BROKER and MESSAGE_BROKER. -# - nats_rabbitmq: Uses 'nats' as MQTT_BROKER and 'rabbitmq' as MESSAGE_BROKER. -# - -include: - - path: brokers/profiles/nats.yml - env_file: docker/.env - -services: - vernemq: - image: magistrala/vernemq:${MG_RELEASE_TAG} - container_name: magistrala-vernemq - restart: on-failure - environment: - DOCKER_VERNEMQ_ALLOW_ANONYMOUS: ${MG_DOCKER_VERNEMQ_ALLOW_ANONYMOUS} - DOCKER_VERNEMQ_LOG__CONSOLE__LEVEL: ${MG_DOCKER_VERNEMQ_LOG__CONSOLE__LEVEL} - networks: - - magistrala-base-net - volumes: - - magistrala-mqtt-broker-volume:/var/lib/vernemq - profiles: - - vernemq_nats - - vernemq_rabbitmq - - broker: - extends: - file: brokers/nats.yml - service: broker - container_name: magistrala-broker - restart: on-failure - networks: - - magistrala-base-net - volumes: - - magistrala-broker-volume:/data - profiles: - - vernemq_nats - - vernemq_rabbitmq - - nats_rabbitmq diff --git a/docker/brokers/nats.yml b/docker/brokers/nats.yml deleted file mode 100644 index 6a33b2cb6..000000000 --- a/docker/brokers/nats.yml +++ /dev/null @@ -1,16 +0,0 @@ -# Copyright (c) Abstract Machines -# SPDX-License-Identifier: Apache-2.0 - -services: - broker: - image: nats:2.9.21-alpine - command: "--config=/etc/nats/nats.conf" - volumes: - - ./../nats/:/etc/nats - environment: - - MG_NATS_PORT=${MG_NATS_PORT} - - MG_NATS_HTTP_PORT=${MG_NATS_HTTP_PORT} - - MG_NATS_JETSTREAM_KEY=${MG_NATS_JETSTREAM_KEY} - ports: - - ${MG_NATS_PORT}:${MG_NATS_PORT} - - ${MG_NATS_HTTP_PORT}:${MG_NATS_HTTP_PORT} diff --git a/docker/brokers/profiles/README.md b/docker/brokers/profiles/README.md deleted file mode 100644 index 502e22ce2..000000000 --- a/docker/brokers/profiles/README.md +++ /dev/null @@ -1,24 +0,0 @@ -# Nats Docker Profiles - -This directory contains the docker-compose profiles for running Nats as an MQTT broker. It is separated from the main profile at `../docker-compose.yml` because of name conflicts with the Nats message broker. - -The configuration is the same as for the main profile, except that the MQTT broker is set to `nats` instead of `vernemq`. - -The profiles are: - -- `nats_nats.yml` - Nats as an MQTT broker and Nats as a message broker -- `nats_rabbit.yml` - Nats as an MQTT broker and RabbitMQ as a message broker - -They are automatically included in the main profile, so you can run them depending on the profile you want to use: - -The following command will run Nats as an MQTT broker and Nats as a message broker: - -```bash -MG_MQTT_BROKER_TYPE=nats MG_MESSAGE_BROKER_TYPE=nats make run -``` - -The following command will run Nats as an MQTT broker and RabbitMQ as a message broker: - -```bash -MG_MQTT_BROKER_TYPE=nats MG_MESSAGE_BROKER_TYPE=rabbit make run -``` diff --git a/docker/brokers/profiles/nats.yml b/docker/brokers/profiles/nats.yml deleted file mode 100644 index 3f747cfcd..000000000 --- a/docker/brokers/profiles/nats.yml +++ /dev/null @@ -1,19 +0,0 @@ -# Copyright (c) Abstract Machines -# SPDX-License-Identifier: Apache-2.0 - -# This file is used to configure NATS broker. -# It used when running nats both as an MQTT and Message broker. -services: - nats: - extends: - file: brokers/nats.yml - service: broker - container_name: magistrala-nats - restart: on-failure - networks: - - magistrala-base-net - volumes: - - magistrala-broker-volume:/data - profiles: - - nats_nats - - nats_rabbitmq diff --git a/docker/brokers/rabbitmq.yml b/docker/brokers/rabbitmq.yml deleted file mode 100644 index 13fe1b87c..000000000 --- a/docker/brokers/rabbitmq.yml +++ /dev/null @@ -1,14 +0,0 @@ -# Copyright (c) Abstract Machines -# SPDX-License-Identifier: Apache-2.0 - -services: - broker: - image: rabbitmq:3.9.20-management-alpine - environment: - RABBITMQ_ERLANG_COOKIE: ${MG_RABBITMQ_COOKIE} - RABBITMQ_DEFAULT_USER: ${MG_RABBITMQ_USER} - RABBITMQ_DEFAULT_PASS: ${MG_RABBITMQ_PASS} - RABBITMQ_DEFAULT_VHOST: ${MG_RABBITMQ_VHOST} - ports: - - ${MG_RABBITMQ_PORT}:${MG_RABBITMQ_PORT} - - ${MG_RABBITMQ_HTTP_PORT}:${MG_RABBITMQ_HTTP_PORT} diff --git a/docker/docker-compose.yml b/docker/docker-compose.yml index 417c72b3f..b2b27cee6 100644 --- a/docker/docker-compose.yml +++ b/docker/docker-compose.yml @@ -12,19 +12,11 @@ volumes: magistrala-users-db-volume: magistrala-things-db-volume: magistrala-things-redis-volume: - magistrala-mqtt-broker-volume: magistrala-broker-volume: - magistrala-es-volume: magistrala-spicedb-db-volume: magistrala-auth-db-volume: magistrala-invitations-db-volume: -include: - - path: brokers/docker-compose.yml - env_file: docker/.env - - path: es/docker-compose.yml - env_file: docker/.env - services: spicedb: image: "authzed/spicedb" @@ -660,6 +652,24 @@ services: bind: create_host_path: true + nats: + image: nats:2.9.21-alpine + container_name: magistrala-nats + restart: on-failure + command: "--config=/etc/nats/nats.conf" + environment: + - MG_NATS_PORT=${MG_NATS_PORT} + - MG_NATS_HTTP_PORT=${MG_NATS_HTTP_PORT} + - MG_NATS_JETSTREAM_KEY=${MG_NATS_JETSTREAM_KEY} + ports: + - ${MG_NATS_PORT}:${MG_NATS_PORT} + - ${MG_NATS_HTTP_PORT}:${MG_NATS_HTTP_PORT} + volumes: + - magistrala-broker-volume:/data + - ./nats:/etc/nats + networks: + - magistrala-base-net + ui: image: magistrala/ui:${MG_RELEASE_TAG} container_name: magistrala-ui diff --git a/docker/es/docker-compose.yml b/docker/es/docker-compose.yml deleted file mode 100644 index 897b0493a..000000000 --- a/docker/es/docker-compose.yml +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright (c) Abstract Machines -# SPDX-License-Identifier: Apache-2.0 - -volumes: - magistrala-es-redis-volume: - -services: - es-redis: - image: redis:7.2.0-alpine - container_name: magistrala-es-redis - restart: on-failure - networks: - - magistrala-base-net - volumes: - - magistrala-es-volume:/data - profiles: - - redis diff --git a/docker/policy/model.conf b/docker/policy/model.conf deleted file mode 100644 index 3d2ce5897..000000000 --- a/docker/policy/model.conf +++ /dev/null @@ -1,17 +0,0 @@ -# Copyright (c) Abstract Machines -# SPDX-License-Identifier: Apache-2.0 - -[request_definition] -r = sub, obj, act - -[policy_definition] -p = sub, obj, act - -[role_definition] -g = _, _ - -[policy_effect] -e = some(where (p.eft == allow)) - -[matchers] -m = ( g(r.sub, p.sub) && keyMatch(r.obj, p.obj) && regexMatch(r.act, p.act) ) || r.sub == 'admin@example.com'