From deeb0208a9590f59973ee1ea5e0b60b1bb79faa1 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Tue, 21 Nov 2023 16:51:27 +0200 Subject: [PATCH 01/18] Introduce edge upgrade documentation --- .../install}/centos/instructions.md | 2 +- .../install}/docker/instructions.md | 2 +- .../install}/docker/localhost_warning.md | 0 .../install}/ubuntu/instructions.md | 2 +- .../upgrade/centos/instructions.md | 30 +++ .../upgrade/docker/instructions.md | 15 ++ .../upgrade/docker/start_service.md | 7 + .../upgrade/docker/stop_service.md | 11 + .../instructions/upgrade/docker/upgrade_db.md | 53 +++++ .../upgrade/docker/upgrade_preparing.md | 26 +++ .../instructions/upgrade/start_service.md | 6 + .../edge/instructions/upgrade/stop_service.md | 6 + .../upgrade/ubuntu/instructions.md | 19 ++ .../edge/instructions/upgrade/upgrade_db.md | 8 + .../instructions/upgrade/upgrade_preparing.md | 37 ++++ .../server/controller/EdgeController.java | 35 +++- .../DefaultEdgeInstallService.java | 22 +- .../DefaultEdgeUpgradeService.java | 190 ++++++++++++++++++ .../edge/instructions/EdgeInstallService.java | 4 +- .../edge/instructions/EdgeUpgradeService.java | 25 +++ .../service/edge/rpc/EdgeGrpcSession.java | 9 +- ...nstructions.java => EdgeInstructions.java} | 6 +- .../thingsboard/rest/client/RestClient.java | 8 +- 23 files changed, 494 insertions(+), 29 deletions(-) rename application/src/main/data/json/edge/{install_instructions => instructions/install}/centos/instructions.md (98%) rename application/src/main/data/json/edge/{install_instructions => instructions/install}/docker/instructions.md (98%) rename application/src/main/data/json/edge/{install_instructions => instructions/install}/docker/localhost_warning.md (100%) rename application/src/main/data/json/edge/{install_instructions => instructions/install}/ubuntu/instructions.md (98%) create mode 100644 application/src/main/data/json/edge/instructions/upgrade/centos/instructions.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/docker/instructions.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/docker/stop_service.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/start_service.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/stop_service.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/ubuntu/instructions.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/upgrade_db.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/upgrade_preparing.md create mode 100644 application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java create mode 100644 application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java rename common/data/src/main/java/org/thingsboard/server/common/data/edge/{EdgeInstallInstructions.java => EdgeInstructions.java} (90%) diff --git a/application/src/main/data/json/edge/install_instructions/centos/instructions.md b/application/src/main/data/json/edge/instructions/install/centos/instructions.md similarity index 98% rename from application/src/main/data/json/edge/install_instructions/centos/instructions.md rename to application/src/main/data/json/edge/instructions/install/centos/instructions.md index 126c79b641..9ad882c387 100644 --- a/application/src/main/data/json/edge/install_instructions/centos/instructions.md +++ b/application/src/main/data/json/edge/instructions/install/centos/instructions.md @@ -1,4 +1,4 @@ -Here is the list of commands, that can be used to quickly install ThingsBoard Edge on RHEL/CentOS 7/8 and connect to the cloud. +Here is the list of commands, that can be used to quickly install ThingsBoard Edge on RHEL/CentOS 7/8 and connect to the server. #### Prerequisites Before continue to installation execute the following commands in order to install necessary tools: diff --git a/application/src/main/data/json/edge/install_instructions/docker/instructions.md b/application/src/main/data/json/edge/instructions/install/docker/instructions.md similarity index 98% rename from application/src/main/data/json/edge/install_instructions/docker/instructions.md rename to application/src/main/data/json/edge/instructions/install/docker/instructions.md index e308a38076..cbfe5f8c4c 100644 --- a/application/src/main/data/json/edge/install_instructions/docker/instructions.md +++ b/application/src/main/data/json/edge/instructions/install/docker/instructions.md @@ -1,4 +1,4 @@ -Here is the list of commands, that can be used to quickly install ThingsBoard Edge using docker compose and connect to the cloud. +Here is the list of commands, that can be used to quickly install ThingsBoard Edge using docker compose and connect to the server. #### Prerequisites diff --git a/application/src/main/data/json/edge/install_instructions/docker/localhost_warning.md b/application/src/main/data/json/edge/instructions/install/docker/localhost_warning.md similarity index 100% rename from application/src/main/data/json/edge/install_instructions/docker/localhost_warning.md rename to application/src/main/data/json/edge/instructions/install/docker/localhost_warning.md diff --git a/application/src/main/data/json/edge/install_instructions/ubuntu/instructions.md b/application/src/main/data/json/edge/instructions/install/ubuntu/instructions.md similarity index 98% rename from application/src/main/data/json/edge/install_instructions/ubuntu/instructions.md rename to application/src/main/data/json/edge/instructions/install/ubuntu/instructions.md index 425c08662b..2a6d9c0bc3 100644 --- a/application/src/main/data/json/edge/install_instructions/ubuntu/instructions.md +++ b/application/src/main/data/json/edge/instructions/install/ubuntu/instructions.md @@ -1,4 +1,4 @@ -Here is the list of commands, that can be used to quickly install ThingsBoard Edge on Ubuntu Server and connect to the cloud. +Here is the list of commands, that can be used to quickly install ThingsBoard Edge on Ubuntu Server and connect to the server. #### Install Java 11 (OpenJDK) ThingsBoard service is running on Java 11. Follow these instructions to install OpenJDK 11: diff --git a/application/src/main/data/json/edge/instructions/upgrade/centos/instructions.md b/application/src/main/data/json/edge/instructions/upgrade/centos/instructions.md new file mode 100644 index 0000000000..d0b2a1895d --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/centos/instructions.md @@ -0,0 +1,30 @@ +#### Upgrading to ${TB_EDGE_VERSION_TITLE} +**NOTE**:These steps are applicable for ThingsBoard Edge ${CURRENT_TB_EDGE_VERSION} version. + +**ThingsBoard Edge package download** +```bash +wget https://github.com/thingsboard/thingsboard-edge/releases/download/v${TB_EDGE_TAG}/tb-edge-${TB_EDGE_TAG}.rpm +{:copy-code} +``` + +#### ThingsBoard Edge service upgrade + +Stop ThingsBoard Edge service if it is running: + +```bash +sudo service tb-edge stop +{:copy-code} +``` + +```bash +sudo rpm -Uvh tb-edge-${TB_EDGE_TAG}.rpm +{:copy-code} +``` + +${UPGRADE_DB} + +Start the service +```bash +sudo service tb-edge start +{:copy-code} +``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/instructions.md b/application/src/main/data/json/edge/instructions/upgrade/docker/instructions.md new file mode 100644 index 0000000000..ad7f4b2c46 --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/instructions.md @@ -0,0 +1,15 @@ +#### Upgrading to ${TB_EDGE_VERSION} + +**NOTE**:These steps are applicable for ThingsBoard Edge ${CURRENT_TB_EDGE_VERSION} version. +Execute the following command to pull **${TB_EDGE_VERSION}** image: + +```bash +docker pull thingsboard/tb-edge:${TB_EDGE_VERSION} +{:copy-code} +``` + +${STOP_SERVICE} + +${UPGRADE_DB} + +Make sure your image is the set to tb-edge-${TB_EDGE_VERSION}. diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md b/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md new file mode 100644 index 0000000000..cfdd9a66f2 --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md @@ -0,0 +1,7 @@ +Execute the following commands to up this docker compose directly: + +```bash +docker compose up -d +docker compose logs -f mytbedge +{:copy-code} +``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/stop_service.md b/application/src/main/data/json/edge/instructions/upgrade/docker/stop_service.md new file mode 100644 index 0000000000..8f4aedb117 --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/stop_service.md @@ -0,0 +1,11 @@ +Set the terminal in the directory which contains the `docker-compose.yml` file and execute the following command to stop +and remove currently running TB Edge container (if it’s still running): + +```bash +make docker-compose.yml + +```bash +docker compose stop +docker compose rm mytbedge +{:copy-code} +``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md new file mode 100644 index 0000000000..ae6181bf08 --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md @@ -0,0 +1,53 @@ +Create docker compose file for ThingsBoard Edge upgrade process: + +```bash +nano docker-compose-upgrade.yml +{:copy-code} +``` + +Add the following lines to the yml file: + +```bash +version: '3.0' +services: + mytbedge: + restart: on-failure + image: "thingsboard/tb-edge:${TB_EDGE_VERSION}" + environment: + SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/tb-edge + volumes: + - ~/.mytb-edge-data:/data + - ~/.mytb-edge-logs:/var/log/tb-edge + entrypoint: upgrade-tb-edge.sh + postgres: + restart: always + image: "postgres:12" + ports: + - "5432" + environment: + POSTGRES_DB: tb-edge + POSTGRES_PASSWORD: postgres + volumes: + - ~/.mytb-edge-data/db:/var/lib/postgresql/data +{:copy-code} +``` + +Execute the following command to start upgrade process: + +```bash +docker compose -f docker-compose-upgrade.yml up +{:copy-code} +``` + +Once upgrade process successfully completed, exit from the docker-compose shell by this combination: + +```text +Ctrl + C +``` + +Execute the following command to stop TB Edge upgrade container: + +```bash +docker compose -f docker-compose-upgrade.yml stop +{:copy-code} +``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md new file mode 100644 index 0000000000..c82daaee26 --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md @@ -0,0 +1,26 @@ +Here is the list of commands, that can be used to quickly upgrade ThingsBoard Edge on Docker (Linux or MacOS). + +#### Prepare for upgrading ThingsBoard Edge +Set the terminal in the directory which contains the `docker-compose.yml` file and execute the following command +to stop and remove currently running TB Edge container: + +```bash +docker compose stop +docker compose rm mytbedge +{:copy-code} +``` + +If you still rely on Docker Compose as docker-compose (with a hyphen) here is the list of the above commands: +docker-compose stop +docker-compose rm mytbedge + +#### Backup Database +Make a copy of the database folder before upgrading: + +```bash +sudo cp -r ~/.mytb-edge-data/db ~/.mytb-edge-db-BACKUP +{:copy-code} +``` + +```bash +make docker-compose.yml diff --git a/application/src/main/data/json/edge/instructions/upgrade/start_service.md b/application/src/main/data/json/edge/instructions/upgrade/start_service.md new file mode 100644 index 0000000000..0ce6a0b222 --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/start_service.md @@ -0,0 +1,6 @@ +Start the service + +```bash +sudo service tb-edge start +{:copy-code} +``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/stop_service.md b/application/src/main/data/json/edge/instructions/upgrade/stop_service.md new file mode 100644 index 0000000000..615d0f8a40 --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/stop_service.md @@ -0,0 +1,6 @@ +Stop ThingsBoard Edge service if it is running: + +```bash +sudo service tb-edge stop +{:copy-code} +``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/ubuntu/instructions.md b/application/src/main/data/json/edge/instructions/upgrade/ubuntu/instructions.md new file mode 100644 index 0000000000..5824875f47 --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/ubuntu/instructions.md @@ -0,0 +1,19 @@ +#### Upgrading to ${TB_EDGE_VERSION} +**NOTE**:These steps are applicable for ThingsBoard Edge ${CURRENT_TB_EDGE_VERSION} version. + +**ThingsBoard Edge package download** +```bash +wget https://github.com/thingsboard/thingsboard-edge/releases/download/v${TB_EDGE_TAG}/tb-edge-${TB_EDGE_TAG}.deb +{:copy-code} +``` + +#### ThingsBoard Edge service upgrade + +${STOP_SERVICE} + +```bash +sudo dpkg -i tb-edge-${TB_EDGE_TAG}.deb +{:copy-code} +``` + +${UPGRADE_DB} diff --git a/application/src/main/data/json/edge/instructions/upgrade/upgrade_db.md b/application/src/main/data/json/edge/instructions/upgrade/upgrade_db.md new file mode 100644 index 0000000000..d3a28fb20b --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/upgrade_db.md @@ -0,0 +1,8 @@ +**NOTE**: Package installer may ask you to merge your tb-edge configuration. It is preferred to use **merge option** to make sure that all your previous parameters will not be overwritten. + +Execute regular upgrade script: + +```bash +sudo /usr/share/tb-edge/bin/install/upgrade.sh --fromVersion=${CURRENT_TB_EDGE_VERSION} +{:copy-code} +``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/upgrade_preparing.md b/application/src/main/data/json/edge/instructions/upgrade/upgrade_preparing.md new file mode 100644 index 0000000000..105a94b778 --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/upgrade_preparing.md @@ -0,0 +1,37 @@ +Here is the list of commands, that can be used to quickly upgrade ThingsBoard Edge on RHEL/CentOS 7/8. + +#### Prepare for upgrading ThingsBoard Edge + +Stop ThingsBoard Edge service: + +```bash +sudo systemctl stop tb-edge +{:copy-code} +``` + +#### Backup Database +Make a backup of the database before upgrading. +**Make sure you have enough space to place a backup of the database.** + +Check database size: + +```bash +sudo -u postgres psql -c "SELECT pg_size_pretty( pg_database_size('tb_edge') );" +{:copy-code} +``` + +Check free space: + +```bash +df -h / +{:copy-code} +``` + +If there is enough free space - make a backup: + +```bash +sudo -Hiu postgres pg_dump tb_edge > tb_edge.sql.bak +{:copy-code} +``` + +Check backup file created successfully. diff --git a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java index fa16fbff81..c621dc777f 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java @@ -39,7 +39,7 @@ import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeInfo; -import org.thingsboard.server.common.data.edge.EdgeInstallInstructions; +import org.thingsboard.server.common.data.edge.EdgeInstructions; import org.thingsboard.server.common.data.edge.EdgeSearchQuery; import org.thingsboard.server.common.data.exception.ThingsboardErrorCode; import org.thingsboard.server.common.data.exception.ThingsboardException; @@ -60,6 +60,7 @@ import org.thingsboard.server.dao.model.ModelConstants; import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.edge.EdgeBulkImportService; import org.thingsboard.server.service.edge.instructions.EdgeInstallService; +import org.thingsboard.server.service.edge.instructions.EdgeUpgradeService; import org.thingsboard.server.service.edge.rpc.EdgeRpcService; import org.thingsboard.server.service.entitiy.edge.TbEdgeService; import org.thingsboard.server.service.security.model.SecurityUser; @@ -102,6 +103,7 @@ public class EdgeController extends BaseController { private final TbEdgeService tbEdgeService; private final Optional edgeRpcServiceOpt; private final Optional edgeInstallServiceOpt; + private final Optional edgeUpgradeServiceOpt; public static final String EDGE_ID = "edgeId"; public static final String EDGE_SECURITY_CHECK = "If the user has the authority of 'Tenant Administrator', the server checks that the edge is owned by the same tenant. " + @@ -553,16 +555,16 @@ public class EdgeController extends BaseController { return edgeBulkImportService.processBulkImport(request, user); } - @ApiOperation(value = "Get Edge Docker Install Instructions (getEdgeDockerInstallInstructions)", + @ApiOperation(value = "Get Edge Install Instructions (getEdgeInstallInstructions)", notes = "Get a docker install instructions for provided edge id." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE) @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") - @RequestMapping(value = "/edge/instructions/{edgeId}/{method}", method = RequestMethod.GET) + @RequestMapping(value = "/edge/instructions/install/{edgeId}/{method}", method = RequestMethod.GET) @ResponseBody - public EdgeInstallInstructions getEdgeDockerInstallInstructions( + public EdgeInstructions getEdgeInstallInstructions( @ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) @PathVariable("edgeId") String strEdgeId, - @ApiParam(value = "Installation method ('docker', 'ubuntu' or 'centos')") + @ApiParam(value = "Installation method ('docker', 'ubuntu' or 'centos')", allowableValues = "docker,ubuntu,centos") @PathVariable("method") String installationMethod, HttpServletRequest request) throws ThingsboardException { if (isEdgesEnabled() && edgeInstallServiceOpt.isPresent()) { @@ -574,4 +576,27 @@ public class EdgeController extends BaseController { throw new ThingsboardException("Edges support disabled", ThingsboardErrorCode.GENERAL); } } + + @ApiOperation(value = "Get Edge Upgrade Instructions (getEdgeUpgradeInstructions)", + notes = "Get a docker install instructions for provided edge id." + TENANT_AUTHORITY_PARAGRAPH, + produces = MediaType.APPLICATION_JSON_VALUE) + @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") + @RequestMapping(value = "/edge/instructions/upgrade/{edgeId}/{edgeVersion}/{method}", method = RequestMethod.GET) + @ResponseBody + public EdgeInstructions getEdgeUpgradeInstructions( + @ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) + @PathVariable("edgeId") String strEdgeId, + @ApiParam(value = "Edge version", required = true) + @PathVariable("edgeVersion") String edgeVersion, + @ApiParam(value = "Installation method ('docker', 'ubuntu' or 'centos')", allowableValues = "docker,ubuntu,centos") + @PathVariable("method") String method) throws Exception { + if (isEdgesEnabled() && edgeUpgradeServiceOpt.isPresent()) { + EdgeId edgeId = new EdgeId(toUUID(strEdgeId)); + edgeId = checkNotNull(edgeId); + Edge edge = checkEdgeId(edgeId, Operation.READ); + return checkNotNull(edgeUpgradeServiceOpt.get().getUpgradeInstructions(getTenantId(), edge, edgeVersion, method)); + } else { + throw new ThingsboardException("Edges support disabled", ThingsboardErrorCode.GENERAL); + } + } } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java index dd17f83317..24340f7a97 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java @@ -21,7 +21,7 @@ import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; import org.thingsboard.server.common.data.edge.Edge; -import org.thingsboard.server.common.data.edge.EdgeInstallInstructions; +import org.thingsboard.server.common.data.edge.EdgeInstructions; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.install.InstallScripts; @@ -40,8 +40,8 @@ import java.nio.file.Paths; public class DefaultEdgeInstallService implements EdgeInstallService { private static final String EDGE_DIR = "edge"; - - private static final String EDGE_INSTALL_INSTRUCTIONS_DIR = "install_instructions"; + private static final String INSTRUCTIONS_DIR = "instructions"; + private static final String INSTALL_DIR = "install"; private final InstallScripts installScripts; @@ -55,7 +55,7 @@ public class DefaultEdgeInstallService implements EdgeInstallService { private String appVersion; @Override - public EdgeInstallInstructions getInstallInstructions(TenantId tenantId, Edge edge, String installationMethod, HttpServletRequest request) { + public EdgeInstructions getInstallInstructions(TenantId tenantId, Edge edge, String installationMethod, HttpServletRequest request) { switch (installationMethod.toLowerCase()) { case "docker": return getDockerInstallInstructions(edge, request); @@ -68,7 +68,7 @@ public class DefaultEdgeInstallService implements EdgeInstallService { } } - private EdgeInstallInstructions getDockerInstallInstructions(Edge edge, HttpServletRequest request) { + private EdgeInstructions getDockerInstallInstructions(Edge edge, HttpServletRequest request) { String dockerInstallInstructions = readFile(resolveFile("docker", "instructions.md")); String baseUrl = request.getServerName(); if (baseUrl.contains("localhost") || baseUrl.contains("127.0.0.1")) { @@ -83,26 +83,26 @@ public class DefaultEdgeInstallService implements EdgeInstallService { edgeVersion = edgeVersion.replace("-SNAPSHOT", ""); dockerInstallInstructions = dockerInstallInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); dockerInstallInstructions = replacePlaceholders(dockerInstallInstructions, edge); - return new EdgeInstallInstructions(dockerInstallInstructions); + return new EdgeInstructions(dockerInstallInstructions); } - private EdgeInstallInstructions getUbuntuInstallInstructions(Edge edge, HttpServletRequest request) { + private EdgeInstructions getUbuntuInstallInstructions(Edge edge, HttpServletRequest request) { String ubuntuInstallInstructions = readFile(resolveFile("ubuntu", "instructions.md")); ubuntuInstallInstructions = replacePlaceholders(ubuntuInstallInstructions, edge); ubuntuInstallInstructions = ubuntuInstallInstructions.replace("${BASE_URL}", request.getServerName()); String edgeVersion = appVersion.replace("-SNAPSHOT", ""); ubuntuInstallInstructions = ubuntuInstallInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); - return new EdgeInstallInstructions(ubuntuInstallInstructions); + return new EdgeInstructions(ubuntuInstallInstructions); } - private EdgeInstallInstructions getCentosInstallInstructions(Edge edge, HttpServletRequest request) { + private EdgeInstructions getCentosInstallInstructions(Edge edge, HttpServletRequest request) { String centosInstallInstructions = readFile(resolveFile("centos", "instructions.md")); centosInstallInstructions = replacePlaceholders(centosInstallInstructions, edge); centosInstallInstructions = centosInstallInstructions.replace("${BASE_URL}", request.getServerName()); String edgeVersion = appVersion.replace("-SNAPSHOT", ""); centosInstallInstructions = centosInstallInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); - return new EdgeInstallInstructions(centosInstallInstructions); + return new EdgeInstructions(centosInstallInstructions); } private String replacePlaceholders(String instructions, Edge edge) { @@ -127,6 +127,6 @@ public class DefaultEdgeInstallService implements EdgeInstallService { } private Path getEdgeInstallInstructionsDir() { - return Paths.get(installScripts.getDataDir(), InstallScripts.JSON_DIR, EDGE_DIR, EDGE_INSTALL_INSTRUCTIONS_DIR); + return Paths.get(installScripts.getDataDir(), InstallScripts.JSON_DIR, EDGE_DIR, INSTRUCTIONS_DIR, INSTALL_DIR); } } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java new file mode 100644 index 0000000000..a1d737757d --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java @@ -0,0 +1,190 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.service.edge.instructions; + +import lombok.AllArgsConstructor; +import lombok.Data; +import lombok.RequiredArgsConstructor; +import lombok.extern.slf4j.Slf4j; +import org.springframework.beans.factory.annotation.Value; +import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; +import org.springframework.stereotype.Service; +import org.thingsboard.server.common.data.DataConstants; +import org.thingsboard.server.common.data.edge.Edge; +import org.thingsboard.server.common.data.edge.EdgeInstructions; +import org.thingsboard.server.common.data.id.EdgeId; +import org.thingsboard.server.common.data.id.TenantId; +import org.thingsboard.server.common.data.kv.AttributeKvEntry; +import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; +import org.thingsboard.server.common.data.kv.StringDataEntry; +import org.thingsboard.server.dao.attributes.AttributesService; +import org.thingsboard.server.queue.util.TbCoreComponent; +import org.thingsboard.server.service.install.InstallScripts; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.HashMap; + +@Service +@Slf4j +@RequiredArgsConstructor +@ConditionalOnProperty(prefix = "edges", value = "enabled", havingValue = "true") +@TbCoreComponent +public class DefaultEdgeUpgradeService implements EdgeUpgradeService { + + private static final HashMap upgradeVersionHashMap; + + static { + upgradeVersionHashMap = new HashMap<>(); + upgradeVersionHashMap.put("3.6.0", new UpgradeInfo(true, "3.6.1")); + upgradeVersionHashMap.put("3.6.1", new UpgradeInfo(true, "3.6.2")); + upgradeVersionHashMap.put("3.6.2", new UpgradeInfo(true, null)); + } + + private static final String EDGE_DIR = "edge"; + private static final String INSTRUCTIONS_DIR = "instructions"; + private static final String UPGRADE_DIR = "upgrade"; + + private final InstallScripts installScripts; + private final AttributesService attributesService; + + @Value("${app.version:unknown}") + private String appVersion; + + @Override + public EdgeInstructions getUpgradeInstructions(TenantId tenantId, Edge edge, String edgeVersion, String upgradeMethod) { + String tbVersion = appVersion.replace("-SNAPSHOT", ""); + String currentEdgeVersion = convertEdgeVersionToDocsFormat(edgeVersion); + switch (upgradeMethod.toLowerCase()) { + case "docker": + return getDockerUpgradeInstructions(tenantId, edge.getId(), tbVersion, currentEdgeVersion); + case "ubuntu": + case "centos": + return getLinuxUpgradeInstructions(tenantId, edge.getId(), tbVersion, currentEdgeVersion, upgradeMethod.toLowerCase()); + default: + throw new IllegalArgumentException("Unsupported upgrade method for Edge: " + upgradeMethod); + } + } + + private EdgeInstructions getDockerUpgradeInstructions(TenantId tenantId, EdgeId edgeId, String tbVersion, String currentEdgeVersion) { + UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); + if (upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { + return null; + } + boolean stoppedService = false; + StringBuilder result = new StringBuilder(readFile(resolveFile("docker", "upgrade_preparing.md"))); + while (upgradeInfo.getNextVersion() != null || !tbVersion.equals(currentEdgeVersion)) { + String edgeVersion = upgradeInfo.getNextVersion(); + String ubuntuUpgradeInstructions = readFile(resolveFile("docker", "instructions.md")); + if (upgradeInfo.isUpgradeDb()) { + String upgradeDb = readFile(resolveFile("docker", "upgrade_db.md")); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); + } + if (!stoppedService) { + stoppedService = true; + String stopService = readFile(resolveFile("docker", "stop_service.md")); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${STOP_SERVICE}", stopService); + } else { + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${STOP_SERVICE}", ""); + } + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion + "EDGE"); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CURRENT_TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); + currentEdgeVersion = edgeVersion; + upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextVersion()); + result.append(ubuntuUpgradeInstructions); + } + String startService = readFile(resolveFile("docker", "start_service.md")); + result.append(startService); + AttributeKvEntry attributeKvEntry = new BaseAttributeKvEntry(new StringDataEntry("edgeVersion", currentEdgeVersion), System.currentTimeMillis()); + attributesService.save(tenantId, edgeId, DataConstants.SERVER_SCOPE, attributeKvEntry); + return new EdgeInstructions(result.toString()); + } + + private EdgeInstructions getLinuxUpgradeInstructions(TenantId tenantId, EdgeId edgeId, String tbVersion, String currentEdgeVersion, String os) { + UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); + if (upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { + return null; + } + boolean stoppedService = false; + StringBuilder result = new StringBuilder(readFile(resolveFile("upgrade_preparing.md"))); + while (upgradeInfo.getNextVersion() != null || !tbVersion.equals(currentEdgeVersion)) { + String edgeVersion = upgradeInfo.getNextVersion(); + String ubuntuUpgradeInstructions = readFile(resolveFile(os, "instructions.md")); + if (upgradeInfo.isUpgradeDb()) { + String upgradeDb = readFile(resolveFile("upgrade_db.md")); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); + } + if (!stoppedService) { + stoppedService = true; + String stopService = readFile(resolveFile("stop_service.md")); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${STOP_SERVICE}", stopService); + } else { + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${STOP_SERVICE}", ""); + } + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_TAG}", getTagVersion(edgeVersion)); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CURRENT_TB_EDGE_TAG}", getTagVersion(currentEdgeVersion)); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CURRENT_TB_EDGE_VERSION}", currentEdgeVersion); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION_TITLE}", edgeVersion + "EDGE"); + currentEdgeVersion = edgeVersion; + upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextVersion()); + result.append(ubuntuUpgradeInstructions); + } + String startService = readFile(resolveFile("start_service.md")); + result.append(startService); + AttributeKvEntry attributeKvEntry = new BaseAttributeKvEntry(new StringDataEntry("edgeVersion", convertDocsFormatToEdgeVersion(currentEdgeVersion)), System.currentTimeMillis()); + attributesService.save(tenantId, edgeId, DataConstants.SERVER_SCOPE, attributeKvEntry); + return new EdgeInstructions(result.toString()); + } + + private String getTagVersion(String version) { + return version.endsWith(".0") ? version.substring(0, version.length() - 2) : version; + } + + private String convertEdgeVersionToDocsFormat(String edgeVersion) { + return edgeVersion.replace("_", ".").substring(2); + } + + private String convertDocsFormatToEdgeVersion(String edgeVersion) { + return "V_" + edgeVersion.replace(".", "_"); + } + + private String readFile(Path file) { + try { + return Files.readString(file); + } catch (IOException e) { + log.warn("Failed to read file: {}", file, e); + throw new RuntimeException(e); + } + } + + private Path resolveFile(String subDir, String... subDirs) { + return getEdgeInstallInstructionsDir().resolve(Paths.get(subDir, subDirs)); + } + + private Path getEdgeInstallInstructionsDir() { + return Paths.get(installScripts.getDataDir(), InstallScripts.JSON_DIR, EDGE_DIR, INSTRUCTIONS_DIR, UPGRADE_DIR); + } + + @AllArgsConstructor + @Data + public static class UpgradeInfo { + private boolean upgradeDb; + private String nextVersion; + } +} diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java index 20cac25e33..cba8703920 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java @@ -16,12 +16,12 @@ package org.thingsboard.server.service.edge.instructions; import org.thingsboard.server.common.data.edge.Edge; -import org.thingsboard.server.common.data.edge.EdgeInstallInstructions; +import org.thingsboard.server.common.data.edge.EdgeInstructions; import org.thingsboard.server.common.data.id.TenantId; import javax.servlet.http.HttpServletRequest; public interface EdgeInstallService { - EdgeInstallInstructions getInstallInstructions(TenantId tenantId, Edge edge, String installationMethod, HttpServletRequest request); + EdgeInstructions getInstallInstructions(TenantId tenantId, Edge edge, String installationMethod, HttpServletRequest request); } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java new file mode 100644 index 0000000000..0f4394ace9 --- /dev/null +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java @@ -0,0 +1,25 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.service.edge.instructions; + +import org.thingsboard.server.common.data.edge.Edge; +import org.thingsboard.server.common.data.edge.EdgeInstructions; +import org.thingsboard.server.common.data.id.TenantId; + +public interface EdgeUpgradeService { + + EdgeInstructions getUpgradeInstructions(TenantId tenantId, Edge edge, String edgeVersion, String upgradeMethod) throws Exception; +} diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java index 7318b1eb24..32d648c045 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java @@ -34,6 +34,7 @@ import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.kv.AttributeKvEntry; import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; import org.thingsboard.server.common.data.kv.LongDataEntry; +import org.thingsboard.server.common.data.kv.StringDataEntry; import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.SortOrder; @@ -774,7 +775,7 @@ public final class EdgeGrpcSession implements Closeable { try { if (edge.getSecret().equals(request.getEdgeSecret())) { sessionOpenListener.accept(edge.getId(), this); - this.edgeVersion = request.getEdgeVersion(); + this.edgeVersion = processGetAndSaveEdgeVersion(request.getEdgeVersion()); return ConnectResponseMsg.newBuilder() .setResponseCode(ConnectResponseCode.ACCEPTED) .setErrorMsg("") @@ -800,6 +801,12 @@ public final class EdgeGrpcSession implements Closeable { .setConfiguration(EdgeConfiguration.getDefaultInstance()).build(); } + private EdgeVersion processGetAndSaveEdgeVersion(EdgeVersion edgeVersion) { + AttributeKvEntry attributeKvEntry = new BaseAttributeKvEntry(new StringDataEntry("edgeVersion", edgeVersion.name()), System.currentTimeMillis()); + ctx.getAttributesService().save(this.tenantId, this.edge.getId(), DataConstants.SERVER_SCOPE, attributeKvEntry); + return edgeVersion; + } + @Override public void close() { log.debug("[{}][{}] Closing session", this.tenantId, sessionId); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/edge/EdgeInstallInstructions.java b/common/data/src/main/java/org/thingsboard/server/common/data/edge/EdgeInstructions.java similarity index 90% rename from common/data/src/main/java/org/thingsboard/server/common/data/edge/EdgeInstallInstructions.java rename to common/data/src/main/java/org/thingsboard/server/common/data/edge/EdgeInstructions.java index 8343058250..2c3a66c002 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/edge/EdgeInstallInstructions.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/edge/EdgeInstructions.java @@ -25,8 +25,8 @@ import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor -public class EdgeInstallInstructions { +public class EdgeInstructions { - @ApiModelProperty(position = 1, value = "Markdown with install instructions") - private String installInstructions; + @ApiModelProperty(position = 1, value = "Markdown with install/upgrade instructions") + private String instructions; } diff --git a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java index fb878a33b6..d90a58fde8 100644 --- a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java +++ b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java @@ -84,7 +84,7 @@ import org.thingsboard.server.common.data.device.DeviceSearchQuery; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeEvent; import org.thingsboard.server.common.data.edge.EdgeInfo; -import org.thingsboard.server.common.data.edge.EdgeInstallInstructions; +import org.thingsboard.server.common.data.edge.EdgeInstructions; import org.thingsboard.server.common.data.edge.EdgeSearchQuery; import org.thingsboard.server.common.data.entityview.EntityViewSearchQuery; import org.thingsboard.server.common.data.id.AlarmCommentId; @@ -3241,9 +3241,9 @@ public class RestClient implements Closeable { }).getBody(); } - public Optional getEdgeDockerInstallInstructions(EdgeId edgeId) { - ResponseEntity edgeInstallInstructionsResult = - restTemplate.getForEntity(baseURL + "/api/edge/instructions/{edgeId}", EdgeInstallInstructions.class, edgeId.getId()); + public Optional getEdgeDockerInstallInstructions(EdgeId edgeId, String method) { + ResponseEntity edgeInstallInstructionsResult = + restTemplate.getForEntity(baseURL + "/api/edge/instructions/install/{edgeId}/{method}", EdgeInstructions.class, edgeId.getId(), method); return Optional.ofNullable(edgeInstallInstructionsResult.getBody()); } From 855b9c054b2680973491ed1e616d416e9faf79e7 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Wed, 22 Nov 2023 14:01:10 +0200 Subject: [PATCH 02/18] Remove edge from upgradeEdgeController. We modify edgeVersion only on edgeConnect --- .../server/controller/EdgeController.java | 9 ++------ .../DefaultEdgeUpgradeService.java | 22 +++++-------------- .../edge/instructions/EdgeUpgradeService.java | 3 +-- 3 files changed, 8 insertions(+), 26 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java index c621dc777f..26a7938dfb 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java @@ -581,20 +581,15 @@ public class EdgeController extends BaseController { notes = "Get a docker install instructions for provided edge id." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE) @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") - @RequestMapping(value = "/edge/instructions/upgrade/{edgeId}/{edgeVersion}/{method}", method = RequestMethod.GET) + @RequestMapping(value = "/edge/instructions/upgrade/{edgeVersion}/{method}", method = RequestMethod.GET) @ResponseBody public EdgeInstructions getEdgeUpgradeInstructions( - @ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) - @PathVariable("edgeId") String strEdgeId, @ApiParam(value = "Edge version", required = true) @PathVariable("edgeVersion") String edgeVersion, @ApiParam(value = "Installation method ('docker', 'ubuntu' or 'centos')", allowableValues = "docker,ubuntu,centos") @PathVariable("method") String method) throws Exception { if (isEdgesEnabled() && edgeUpgradeServiceOpt.isPresent()) { - EdgeId edgeId = new EdgeId(toUUID(strEdgeId)); - edgeId = checkNotNull(edgeId); - Edge edge = checkEdgeId(edgeId, Operation.READ); - return checkNotNull(edgeUpgradeServiceOpt.get().getUpgradeInstructions(getTenantId(), edge, edgeVersion, method)); + return checkNotNull(edgeUpgradeServiceOpt.get().getUpgradeInstructions(getTenantId(), edgeVersion, method)); } else { throw new ThingsboardException("Edges support disabled", ThingsboardErrorCode.GENERAL); } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java index a1d737757d..dbd08faef8 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java @@ -22,15 +22,8 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; -import org.thingsboard.server.common.data.DataConstants; -import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeInstructions; -import org.thingsboard.server.common.data.id.EdgeId; import org.thingsboard.server.common.data.id.TenantId; -import org.thingsboard.server.common.data.kv.AttributeKvEntry; -import org.thingsboard.server.common.data.kv.BaseAttributeKvEntry; -import org.thingsboard.server.common.data.kv.StringDataEntry; -import org.thingsboard.server.dao.attributes.AttributesService; import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.install.InstallScripts; @@ -61,27 +54,26 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { private static final String UPGRADE_DIR = "upgrade"; private final InstallScripts installScripts; - private final AttributesService attributesService; @Value("${app.version:unknown}") private String appVersion; @Override - public EdgeInstructions getUpgradeInstructions(TenantId tenantId, Edge edge, String edgeVersion, String upgradeMethod) { + public EdgeInstructions getUpgradeInstructions(TenantId tenantId, String edgeVersion, String upgradeMethod) { String tbVersion = appVersion.replace("-SNAPSHOT", ""); String currentEdgeVersion = convertEdgeVersionToDocsFormat(edgeVersion); switch (upgradeMethod.toLowerCase()) { case "docker": - return getDockerUpgradeInstructions(tenantId, edge.getId(), tbVersion, currentEdgeVersion); + return getDockerUpgradeInstructions(tenantId, tbVersion, currentEdgeVersion); case "ubuntu": case "centos": - return getLinuxUpgradeInstructions(tenantId, edge.getId(), tbVersion, currentEdgeVersion, upgradeMethod.toLowerCase()); + return getLinuxUpgradeInstructions(tenantId, tbVersion, currentEdgeVersion, upgradeMethod.toLowerCase()); default: throw new IllegalArgumentException("Unsupported upgrade method for Edge: " + upgradeMethod); } } - private EdgeInstructions getDockerUpgradeInstructions(TenantId tenantId, EdgeId edgeId, String tbVersion, String currentEdgeVersion) { + private EdgeInstructions getDockerUpgradeInstructions(TenantId tenantId, String tbVersion, String currentEdgeVersion) { UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); if (upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { return null; @@ -110,12 +102,10 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { } String startService = readFile(resolveFile("docker", "start_service.md")); result.append(startService); - AttributeKvEntry attributeKvEntry = new BaseAttributeKvEntry(new StringDataEntry("edgeVersion", currentEdgeVersion), System.currentTimeMillis()); - attributesService.save(tenantId, edgeId, DataConstants.SERVER_SCOPE, attributeKvEntry); return new EdgeInstructions(result.toString()); } - private EdgeInstructions getLinuxUpgradeInstructions(TenantId tenantId, EdgeId edgeId, String tbVersion, String currentEdgeVersion, String os) { + private EdgeInstructions getLinuxUpgradeInstructions(TenantId tenantId, String tbVersion, String currentEdgeVersion, String os) { UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); if (upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { return null; @@ -147,8 +137,6 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { } String startService = readFile(resolveFile("start_service.md")); result.append(startService); - AttributeKvEntry attributeKvEntry = new BaseAttributeKvEntry(new StringDataEntry("edgeVersion", convertDocsFormatToEdgeVersion(currentEdgeVersion)), System.currentTimeMillis()); - attributesService.save(tenantId, edgeId, DataConstants.SERVER_SCOPE, attributeKvEntry); return new EdgeInstructions(result.toString()); } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java index 0f4394ace9..878e38a468 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java @@ -15,11 +15,10 @@ */ package org.thingsboard.server.service.edge.instructions; -import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeInstructions; import org.thingsboard.server.common.data.id.TenantId; public interface EdgeUpgradeService { - EdgeInstructions getUpgradeInstructions(TenantId tenantId, Edge edge, String edgeVersion, String upgradeMethod) throws Exception; + EdgeInstructions getUpgradeInstructions(TenantId tenantId, String edgeVersion, String upgradeMethod) throws Exception; } From 7fad29e05971c0ba8233aac3be2fefbcabbd42ea Mon Sep 17 00:00:00 2001 From: deaflynx Date: Wed, 22 Nov 2023 16:59:21 +0200 Subject: [PATCH 03/18] edge upgrade instructions ui implementation --- common/edge-api/src/main/proto/edge.proto | 1 + ui-ngx/src/app/core/http/edge.service.ts | 10 ++++-- .../edge-instructions-dialog.component.ts | 34 +++++++++++++++---- .../home/pages/edge/edge.component.html | 27 +++++++++++---- .../modules/home/pages/edge/edge.component.ts | 29 +++++++++++++++- .../pages/edge/edges-table-config.resolver.ts | 10 ++++-- ui-ngx/src/app/shared/models/edge.models.ts | 6 ++-- .../assets/locale/locale.constant-en_US.json | 1 + 8 files changed, 95 insertions(+), 23 deletions(-) diff --git a/common/edge-api/src/main/proto/edge.proto b/common/edge-api/src/main/proto/edge.proto index 71e84304ed..854ce29935 100644 --- a/common/edge-api/src/main/proto/edge.proto +++ b/common/edge-api/src/main/proto/edge.proto @@ -36,6 +36,7 @@ enum EdgeVersion { V_3_4_0 = 2; V_3_6_0 = 3; V_3_6_1 = 4; + V_3_6_2 = 5; } /** diff --git a/ui-ngx/src/app/core/http/edge.service.ts b/ui-ngx/src/app/core/http/edge.service.ts index aeb4f7d16b..6eb1e2f029 100644 --- a/ui-ngx/src/app/core/http/edge.service.ts +++ b/ui-ngx/src/app/core/http/edge.service.ts @@ -21,7 +21,7 @@ import { HttpClient } from '@angular/common/http'; import { PageLink, TimePageLink } from '@shared/models/page/page-link'; import { PageData } from '@shared/models/page/page-data'; import { EntitySubtype } from '@app/shared/models/entity-type.models'; -import { Edge, EdgeEvent, EdgeInfo, EdgeInstallInstructions, EdgeSearchQuery } from '@shared/models/edge.models'; +import { Edge, EdgeEvent, EdgeInfo, EdgeInstructions, EdgeSearchQuery } from '@shared/models/edge.models'; import { EntityId } from '@shared/models/id/entity-id'; import { BulkImportRequest, BulkImportResult } from '@home/components/import-export/import-export.models'; @@ -114,7 +114,11 @@ export class EdgeService { return this.http.post('/api/edge/bulk_import', entitiesData, defaultHttpOptionsFromConfig(config)); } - public getEdgeInstallInstructions(edgeId: string, method: string = 'ubuntu', config?: RequestConfig): Observable { - return this.http.get(`/api/edge/instructions/${edgeId}/${method}`, defaultHttpOptionsFromConfig(config)); + public getEdgeInstallInstructions(edgeId: string, method: string = 'ubuntu', config?: RequestConfig): Observable { + return this.http.get(`/api/edge/instructions/install/${edgeId}/${method}`, defaultHttpOptionsFromConfig(config)); + } + + public getEdgeUpgradeInstructions(edgeVersion: string, method: string = 'ubuntu', config?: RequestConfig): Observable { + return this.http.get(`/api/edge/instructions/upgrade/${edgeVersion}/${method}`, defaultHttpOptionsFromConfig(config)); } } diff --git a/ui-ngx/src/app/modules/home/pages/edge/edge-instructions-dialog.component.ts b/ui-ngx/src/app/modules/home/pages/edge/edge-instructions-dialog.component.ts index 99da1dfb14..c618df96bb 100644 --- a/ui-ngx/src/app/modules/home/pages/edge/edge-instructions-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/pages/edge/edge-instructions-dialog.component.ts @@ -21,12 +21,21 @@ import { AppState } from '@core/core.state'; import { Router } from '@angular/router'; import { MAT_DIALOG_DATA, MatDialogRef } from '@angular/material/dialog'; import { ActionPreferencesPutUserSettings } from '@core/auth/auth.actions'; -import { EdgeInfo, EdgeInstructionsMethod } from '@shared/models/edge.models'; +import { + EdgeInfo, + EdgeInstructions, + EdgeInstructionsMethod, + edgeVersionAttributeKey +} from '@shared/models/edge.models'; import { EdgeService } from '@core/http/edge.service'; +import { AttributeService } from '@core/http/attribute.service'; +import { AttributeScope } from '@shared/models/telemetry/telemetry.models'; +import { mergeMap, Observable } from 'rxjs'; export interface EdgeInstructionsDialogData { edge: EdgeInfo; afterAdd: boolean; + upgradeAvailable: boolean; } @Component({ @@ -49,6 +58,7 @@ export class EdgeInstructionsDialogComponent extends DialogComponent, + private attributeService: AttributeService, private edgeService: EdgeService) { super(store, router, dialogRef); @@ -85,12 +95,22 @@ export class EdgeInstructionsDialogComponent extends DialogComponent { - this.contentData[method] = res.installInstructions; - this.loadedInstructions = true; - } - ); + let edgeInstructions$: Observable; + if (this.data.upgradeAvailable) { + edgeInstructions$ = this.attributeService.getEntityAttributes(this.data.edge.id, AttributeScope.SERVER_SCOPE, [edgeVersionAttributeKey]) + .pipe(mergeMap(attributes => { + if (attributes.length) { + const edgeVersion = attributes[0].value; + return this.edgeService.getEdgeUpgradeInstructions(edgeVersion, method); + } + })); + } else { + edgeInstructions$ = this.edgeService.getEdgeInstallInstructions(this.data.edge.id.id, method); + } + edgeInstructions$.subscribe(res => { + this.contentData[method] = res.instructions; + this.loadedInstructions = true; + }); } } } diff --git a/ui-ngx/src/app/modules/home/pages/edge/edge.component.html b/ui-ngx/src/app/modules/home/pages/edge/edge.component.html index 9c9a78f91a..3d0baad650 100644 --- a/ui-ngx/src/app/modules/home/pages/edge/edge.component.html +++ b/ui-ngx/src/app/modules/home/pages/edge/edge.component.html @@ -114,13 +114,26 @@
- +
+ + + + + + +
diff --git a/ui-ngx/src/app/modules/home/pages/edge/edge.component.ts b/ui-ngx/src/app/modules/home/pages/edge/edge.component.ts index 78c8df6c08..b7cb2b1752 100644 --- a/ui-ngx/src/app/modules/home/pages/edge/edge.component.ts +++ b/ui-ngx/src/app/modules/home/pages/edge/edge.component.ts @@ -20,12 +20,15 @@ import { AppState } from '@core/core.state'; import { EntityComponent } from '@home/components/entity/entity.component'; import { UntypedFormBuilder, UntypedFormGroup, Validators } from '@angular/forms'; import { EntityType } from '@shared/models/entity-type.models'; -import { EdgeInfo } from '@shared/models/edge.models'; +import { EdgeInfo, edgeVersionAttributeKey } from '@shared/models/edge.models'; import { TranslateService } from '@ngx-translate/core'; import { NULL_UUID } from '@shared/models/id/has-uuid'; import { ActionNotificationShow } from '@core/notification/notification.actions'; import { generateSecret, guid } from '@core/utils'; import { EntityTableConfig } from '@home/models/entity/entities-table-config.models'; +import { environment as env } from '@env/environment'; +import { AttributeService } from '@core/http/attribute.service'; +import { AttributeScope } from '@shared/models/telemetry/telemetry.models'; @Component({ selector: 'tb-edge', @@ -37,9 +40,11 @@ export class EdgeComponent extends EntityComponent { entityType = EntityType; edgeScope: 'tenant' | 'customer' | 'customer_user'; + upgradeAvailable: boolean = false; constructor(protected store: Store, protected translate: TranslateService, + private attributeService: AttributeService, @Inject('entity') protected entityValue: EdgeInfo, @Inject('entitiesTableConfig') protected entitiesTableConfigValue: EntityTableConfig, public fb: UntypedFormBuilder, @@ -95,6 +100,7 @@ export class EdgeComponent extends EntityComponent { } }); this.generateRoutingKeyAndSecret(entity, this.entityForm); + this.checkEdgeVersion(); } updateFormState() { @@ -133,4 +139,25 @@ export class EdgeComponent extends EntityComponent { form.get('secret').patchValue(generateSecret(20), {emitEvent: false}); } } + + checkEdgeVersion() { + this.attributeService.getEntityAttributes(this.entity.id, AttributeScope.SERVER_SCOPE, [edgeVersionAttributeKey]) + .subscribe(attributes => { + if (attributes?.length) { + const edgeVersion = attributes[0].value; + const tbVersion = 'V_' + env.tbVersion.replaceAll('.', '_'); + this.upgradeAvailable = this.versionUpgradeSupported(edgeVersion) && (edgeVersion !== tbVersion); + } else { + this.upgradeAvailable = false; + } + } + ); + } + + private versionUpgradeSupported(edgeVersion: string): boolean { + const edgeVersionArray = edgeVersion.split('_'); + const major = parseInt(edgeVersionArray[1]); + const minor = parseInt(edgeVersionArray[2]); + return major >= 3 && minor >= 6; + } } diff --git a/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts b/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts index 6a80388b52..fa08c0daa0 100644 --- a/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts +++ b/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts @@ -558,7 +558,7 @@ export class EdgesTableConfigResolver implements Resolve { if (afterAdd) { @@ -610,9 +611,12 @@ export class EdgesTableConfigResolver implements Resolve { body: string; } -export interface EdgeInstallInstructions { - installInstructions: string; +export interface EdgeInstructions { + instructions: string; } export enum EdgeInstructionsMethod { @@ -187,3 +187,5 @@ export enum EdgeInstructionsMethod { centos, docker } + +export const edgeVersionAttributeKey = 'edgeVersion'; diff --git a/ui-ngx/src/assets/locale/locale.constant-en_US.json b/ui-ngx/src/assets/locale/locale.constant-en_US.json index 37bc5d449f..4bd0361c36 100644 --- a/ui-ngx/src/assets/locale/locale.constant-en_US.json +++ b/ui-ngx/src/assets/locale/locale.constant-en_US.json @@ -2017,6 +2017,7 @@ "sync-process-started-successfully": "Sync process started successfully!", "missing-related-rule-chains-title": "Edge has missing related rule chain(s)", "missing-related-rule-chains-text": "Assigned to edge rule chain(s) use rule nodes that forward message(s) to rule chain(s) that are not assigned to this edge.

List of missing rule chain(s):
{{missingRuleChains}}", + "upgrade-instructions": "Upgrade Instructions", "widget-datasource-error": "This widget supports only EDGE entity datasource" }, "edge-event": { From 6a1e5f315f966f6c6de5de36616af8b1384fc5a8 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Wed, 22 Nov 2023 17:01:33 +0200 Subject: [PATCH 04/18] Remove unused parameters in service --- .../server/controller/EdgeController.java | 4 ++-- .../DefaultEdgeInstallService.java | 3 +-- .../DefaultEdgeUpgradeService.java | 19 +++++++------------ .../edge/instructions/EdgeInstallService.java | 3 +-- .../edge/instructions/EdgeUpgradeService.java | 3 +-- 5 files changed, 12 insertions(+), 20 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java index 26a7938dfb..f85529dbfe 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java @@ -571,7 +571,7 @@ public class EdgeController extends BaseController { EdgeId edgeId = new EdgeId(toUUID(strEdgeId)); edgeId = checkNotNull(edgeId); Edge edge = checkEdgeId(edgeId, Operation.READ); - return checkNotNull(edgeInstallServiceOpt.get().getInstallInstructions(getTenantId(), edge, installationMethod, request)); + return checkNotNull(edgeInstallServiceOpt.get().getInstallInstructions(edge, installationMethod, request)); } else { throw new ThingsboardException("Edges support disabled", ThingsboardErrorCode.GENERAL); } @@ -589,7 +589,7 @@ public class EdgeController extends BaseController { @ApiParam(value = "Installation method ('docker', 'ubuntu' or 'centos')", allowableValues = "docker,ubuntu,centos") @PathVariable("method") String method) throws Exception { if (isEdgesEnabled() && edgeUpgradeServiceOpt.isPresent()) { - return checkNotNull(edgeUpgradeServiceOpt.get().getUpgradeInstructions(getTenantId(), edgeVersion, method)); + return checkNotNull(edgeUpgradeServiceOpt.get().getUpgradeInstructions(edgeVersion, method)); } else { throw new ThingsboardException("Edges support disabled", ThingsboardErrorCode.GENERAL); } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java index 24340f7a97..8ebc5322c7 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java @@ -22,7 +22,6 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeInstructions; -import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.install.InstallScripts; @@ -55,7 +54,7 @@ public class DefaultEdgeInstallService implements EdgeInstallService { private String appVersion; @Override - public EdgeInstructions getInstallInstructions(TenantId tenantId, Edge edge, String installationMethod, HttpServletRequest request) { + public EdgeInstructions getInstallInstructions(Edge edge, String installationMethod, HttpServletRequest request) { switch (installationMethod.toLowerCase()) { case "docker": return getDockerInstallInstructions(edge, request); diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java index dbd08faef8..338c1fb8b7 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java @@ -16,14 +16,13 @@ package org.thingsboard.server.service.edge.instructions; import lombok.AllArgsConstructor; -import lombok.Data; +import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; import org.thingsboard.server.common.data.edge.EdgeInstructions; -import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.install.InstallScripts; @@ -59,21 +58,21 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { private String appVersion; @Override - public EdgeInstructions getUpgradeInstructions(TenantId tenantId, String edgeVersion, String upgradeMethod) { + public EdgeInstructions getUpgradeInstructions(String edgeVersion, String upgradeMethod) { String tbVersion = appVersion.replace("-SNAPSHOT", ""); String currentEdgeVersion = convertEdgeVersionToDocsFormat(edgeVersion); switch (upgradeMethod.toLowerCase()) { case "docker": - return getDockerUpgradeInstructions(tenantId, tbVersion, currentEdgeVersion); + return getDockerUpgradeInstructions(tbVersion, currentEdgeVersion); case "ubuntu": case "centos": - return getLinuxUpgradeInstructions(tenantId, tbVersion, currentEdgeVersion, upgradeMethod.toLowerCase()); + return getLinuxUpgradeInstructions(tbVersion, currentEdgeVersion, upgradeMethod.toLowerCase()); default: throw new IllegalArgumentException("Unsupported upgrade method for Edge: " + upgradeMethod); } } - private EdgeInstructions getDockerUpgradeInstructions(TenantId tenantId, String tbVersion, String currentEdgeVersion) { + private EdgeInstructions getDockerUpgradeInstructions(String tbVersion, String currentEdgeVersion) { UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); if (upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { return null; @@ -105,7 +104,7 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { return new EdgeInstructions(result.toString()); } - private EdgeInstructions getLinuxUpgradeInstructions(TenantId tenantId, String tbVersion, String currentEdgeVersion, String os) { + private EdgeInstructions getLinuxUpgradeInstructions(String tbVersion, String currentEdgeVersion, String os) { UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); if (upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { return null; @@ -148,10 +147,6 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { return edgeVersion.replace("_", ".").substring(2); } - private String convertDocsFormatToEdgeVersion(String edgeVersion) { - return "V_" + edgeVersion.replace(".", "_"); - } - private String readFile(Path file) { try { return Files.readString(file); @@ -170,7 +165,7 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { } @AllArgsConstructor - @Data + @Getter public static class UpgradeInfo { private boolean upgradeDb; private String nextVersion; diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java index cba8703920..996e14f16e 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java @@ -17,11 +17,10 @@ package org.thingsboard.server.service.edge.instructions; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeInstructions; -import org.thingsboard.server.common.data.id.TenantId; import javax.servlet.http.HttpServletRequest; public interface EdgeInstallService { - EdgeInstructions getInstallInstructions(TenantId tenantId, Edge edge, String installationMethod, HttpServletRequest request); + EdgeInstructions getInstallInstructions(Edge edge, String installationMethod, HttpServletRequest request); } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java index 878e38a468..b7747655ef 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java @@ -16,9 +16,8 @@ package org.thingsboard.server.service.edge.instructions; import org.thingsboard.server.common.data.edge.EdgeInstructions; -import org.thingsboard.server.common.data.id.TenantId; public interface EdgeUpgradeService { - EdgeInstructions getUpgradeInstructions(TenantId tenantId, String edgeVersion, String upgradeMethod) throws Exception; + EdgeInstructions getUpgradeInstructions(String edgeVersion, String upgradeMethod); } From 60fb14a708304430647038c9b262afc0d052baf8 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Thu, 23 Nov 2023 14:42:22 +0200 Subject: [PATCH 05/18] Improve logic for docker upgrade --- .../upgrade/centos/instructions.md | 23 ++--------- .../upgrade/docker/instructions.md | 5 --- .../upgrade/docker/start_service.md | 16 ++++++++ .../upgrade/docker/stop_service.md | 11 ------ .../instructions/upgrade/docker/upgrade_db.md | 2 + .../upgrade/docker/upgrade_preparing.md | 10 ++--- .../instructions/upgrade/docker/upgrade_rm.md | 5 +++ .../instructions/upgrade/start_service.md | 2 +- .../edge/instructions/upgrade/stop_service.md | 6 --- .../upgrade/ubuntu/instructions.md | 12 ++---- .../instructions/upgrade/upgrade_preparing.md | 7 ++-- .../DefaultEdgeUpgradeService.java | 38 +++++++++---------- 12 files changed, 57 insertions(+), 80 deletions(-) delete mode 100644 application/src/main/data/json/edge/instructions/upgrade/docker/stop_service.md create mode 100644 application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_rm.md delete mode 100644 application/src/main/data/json/edge/instructions/upgrade/stop_service.md diff --git a/application/src/main/data/json/edge/instructions/upgrade/centos/instructions.md b/application/src/main/data/json/edge/instructions/upgrade/centos/instructions.md index d0b2a1895d..c0e721ace2 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/centos/instructions.md +++ b/application/src/main/data/json/edge/instructions/upgrade/centos/instructions.md @@ -1,30 +1,15 @@ -#### Upgrading to ${TB_EDGE_VERSION_TITLE} -**NOTE**:These steps are applicable for ThingsBoard Edge ${CURRENT_TB_EDGE_VERSION} version. +#### Upgrading to ${TB_EDGE_VERSION}EDGE -**ThingsBoard Edge package download** +**ThingsBoard Edge package download:** ```bash wget https://github.com/thingsboard/thingsboard-edge/releases/download/v${TB_EDGE_TAG}/tb-edge-${TB_EDGE_TAG}.rpm {:copy-code} ``` +##### ThingsBoard Edge service upgrade -#### ThingsBoard Edge service upgrade - -Stop ThingsBoard Edge service if it is running: - -```bash -sudo service tb-edge stop -{:copy-code} -``` - +Install package: ```bash sudo rpm -Uvh tb-edge-${TB_EDGE_TAG}.rpm {:copy-code} ``` - ${UPGRADE_DB} - -Start the service -```bash -sudo service tb-edge start -{:copy-code} -``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/instructions.md b/application/src/main/data/json/edge/instructions/upgrade/docker/instructions.md index ad7f4b2c46..f1b9931e8b 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/docker/instructions.md +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/instructions.md @@ -1,6 +1,5 @@ #### Upgrading to ${TB_EDGE_VERSION} -**NOTE**:These steps are applicable for ThingsBoard Edge ${CURRENT_TB_EDGE_VERSION} version. Execute the following command to pull **${TB_EDGE_VERSION}** image: ```bash @@ -8,8 +7,4 @@ docker pull thingsboard/tb-edge:${TB_EDGE_VERSION} {:copy-code} ``` -${STOP_SERVICE} - ${UPGRADE_DB} - -Make sure your image is the set to tb-edge-${TB_EDGE_VERSION}. diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md b/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md index cfdd9a66f2..b9e5786211 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md @@ -1,3 +1,19 @@ +Modify ‘main’ docker compose (`docker-compose.yml`) file for ThingsBoard Edge and update version of the image: +```bash +nano docker-compose.yml +{:copy-code} +``` + +```text +version: '3.0' +services: +mytbedge: +restart: always +image: "thingsboard/tb-edge:${TB_EDGE_VERSION}" +... +``` + +Make sure your image is the set to tb-edge-${TB_EDGE_VERSION}. Execute the following commands to up this docker compose directly: ```bash diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/stop_service.md b/application/src/main/data/json/edge/instructions/upgrade/docker/stop_service.md deleted file mode 100644 index 8f4aedb117..0000000000 --- a/application/src/main/data/json/edge/instructions/upgrade/docker/stop_service.md +++ /dev/null @@ -1,11 +0,0 @@ -Set the terminal in the directory which contains the `docker-compose.yml` file and execute the following command to stop -and remove currently running TB Edge container (if it’s still running): - -```bash -make docker-compose.yml - -```bash -docker compose stop -docker compose rm mytbedge -{:copy-code} -``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md index ae6181bf08..ac38031517 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md @@ -1,3 +1,5 @@ +${CLEAR_DOCKER_UPGRADE} + Create docker compose file for ThingsBoard Edge upgrade process: ```bash diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md index c82daaee26..04034dea5e 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md @@ -10,17 +10,15 @@ docker compose rm mytbedge {:copy-code} ``` -If you still rely on Docker Compose as docker-compose (with a hyphen) here is the list of the above commands: +**OPTIONAL:** If you still rely on Docker Compose as docker-compose (with a hyphen) here is the list of the above commands: +```text docker-compose stop docker-compose rm mytbedge - -#### Backup Database +``` +##### Backup Database Make a copy of the database folder before upgrading: ```bash sudo cp -r ~/.mytb-edge-data/db ~/.mytb-edge-db-BACKUP {:copy-code} ``` - -```bash -make docker-compose.yml diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_rm.md b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_rm.md new file mode 100644 index 0000000000..4957f9333f --- /dev/null +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_rm.md @@ -0,0 +1,5 @@ +Delete docker compose file, if already exists: +```bash +rm docker-compose-upgrade.yml +{:copy-code} +``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/start_service.md b/application/src/main/data/json/edge/instructions/upgrade/start_service.md index 0ce6a0b222..c26c183154 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/start_service.md +++ b/application/src/main/data/json/edge/instructions/upgrade/start_service.md @@ -1,6 +1,6 @@ Start the service ```bash -sudo service tb-edge start +sudo systemctl tb-edge start {:copy-code} ``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/stop_service.md b/application/src/main/data/json/edge/instructions/upgrade/stop_service.md deleted file mode 100644 index 615d0f8a40..0000000000 --- a/application/src/main/data/json/edge/instructions/upgrade/stop_service.md +++ /dev/null @@ -1,6 +0,0 @@ -Stop ThingsBoard Edge service if it is running: - -```bash -sudo service tb-edge stop -{:copy-code} -``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/ubuntu/instructions.md b/application/src/main/data/json/edge/instructions/upgrade/ubuntu/instructions.md index 5824875f47..59d243c63c 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/ubuntu/instructions.md +++ b/application/src/main/data/json/edge/instructions/upgrade/ubuntu/instructions.md @@ -1,19 +1,15 @@ -#### Upgrading to ${TB_EDGE_VERSION} -**NOTE**:These steps are applicable for ThingsBoard Edge ${CURRENT_TB_EDGE_VERSION} version. +#### Upgrading to ${TB_EDGE_VERSION}EDGE -**ThingsBoard Edge package download** +**ThingsBoard Edge package download:** ```bash wget https://github.com/thingsboard/thingsboard-edge/releases/download/v${TB_EDGE_TAG}/tb-edge-${TB_EDGE_TAG}.deb {:copy-code} ``` +##### ThingsBoard Edge service upgrade -#### ThingsBoard Edge service upgrade - -${STOP_SERVICE} - +Install package: ```bash sudo dpkg -i tb-edge-${TB_EDGE_TAG}.deb {:copy-code} ``` - ${UPGRADE_DB} diff --git a/application/src/main/data/json/edge/instructions/upgrade/upgrade_preparing.md b/application/src/main/data/json/edge/instructions/upgrade/upgrade_preparing.md index 105a94b778..bdd6c4ea18 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/upgrade_preparing.md +++ b/application/src/main/data/json/edge/instructions/upgrade/upgrade_preparing.md @@ -1,4 +1,4 @@ -Here is the list of commands, that can be used to quickly upgrade ThingsBoard Edge on RHEL/CentOS 7/8. +Here is the list of commands, that can be used to quickly upgrade ThingsBoard Edge on ${OS} #### Prepare for upgrading ThingsBoard Edge @@ -9,9 +9,8 @@ sudo systemctl stop tb-edge {:copy-code} ``` -#### Backup Database -Make a backup of the database before upgrading. -**Make sure you have enough space to place a backup of the database.** +##### Backup Database +Make a backup of the database before upgrading. **Make sure you have enough space to place a backup of the database.** Check database size: diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java index 338c1fb8b7..8586ad5b4d 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java @@ -74,10 +74,10 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { private EdgeInstructions getDockerUpgradeInstructions(String tbVersion, String currentEdgeVersion) { UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); - if (upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { - return null; + if (upgradeInfo == null || upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { + return new EdgeInstructions("Edge upgrade instruction for " + currentEdgeVersion + "EDGE is not available."); } - boolean stoppedService = false; + boolean rmUpgradeCompose = false; StringBuilder result = new StringBuilder(readFile(resolveFile("docker", "upgrade_preparing.md"))); while (upgradeInfo.getNextVersion() != null || !tbVersion.equals(currentEdgeVersion)) { String edgeVersion = upgradeInfo.getNextVersion(); @@ -85,13 +85,15 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { if (upgradeInfo.isUpgradeDb()) { String upgradeDb = readFile(resolveFile("docker", "upgrade_db.md")); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); - } - if (!stoppedService) { - stoppedService = true; - String stopService = readFile(resolveFile("docker", "stop_service.md")); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${STOP_SERVICE}", stopService); } else { - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${STOP_SERVICE}", ""); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", ""); + } + if (!rmUpgradeCompose) { + rmUpgradeCompose = true; + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CLEAR_DOCKER_UPGRADE}", ""); + } else { + String rmUpgrade = readFile(resolveFile("docker", "upgrade_rm.md")); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CLEAR_DOCKER_UPGRADE}", rmUpgrade); } ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion + "EDGE"); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CURRENT_TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); @@ -100,36 +102,32 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { result.append(ubuntuUpgradeInstructions); } String startService = readFile(resolveFile("docker", "start_service.md")); + startService = startService.replace("${TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); result.append(startService); return new EdgeInstructions(result.toString()); } private EdgeInstructions getLinuxUpgradeInstructions(String tbVersion, String currentEdgeVersion, String os) { UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); - if (upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { - return null; + if (upgradeInfo == null || upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { + return new EdgeInstructions("Edge upgrade instruction for " + currentEdgeVersion + "EDGE is not available."); } - boolean stoppedService = false; - StringBuilder result = new StringBuilder(readFile(resolveFile("upgrade_preparing.md"))); + String upgrade_preparing = readFile(resolveFile("upgrade_preparing.md")); + upgrade_preparing = upgrade_preparing.replace("${OS}", os.equals("centos") ? "RHEL/CentOS 7/8" : "Ubuntu"); + StringBuilder result = new StringBuilder(upgrade_preparing); while (upgradeInfo.getNextVersion() != null || !tbVersion.equals(currentEdgeVersion)) { String edgeVersion = upgradeInfo.getNextVersion(); String ubuntuUpgradeInstructions = readFile(resolveFile(os, "instructions.md")); if (upgradeInfo.isUpgradeDb()) { String upgradeDb = readFile(resolveFile("upgrade_db.md")); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); - } - if (!stoppedService) { - stoppedService = true; - String stopService = readFile(resolveFile("stop_service.md")); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${STOP_SERVICE}", stopService); } else { - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${STOP_SERVICE}", ""); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", ""); } ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_TAG}", getTagVersion(edgeVersion)); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CURRENT_TB_EDGE_TAG}", getTagVersion(currentEdgeVersion)); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CURRENT_TB_EDGE_VERSION}", currentEdgeVersion); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION_TITLE}", edgeVersion + "EDGE"); currentEdgeVersion = edgeVersion; upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextVersion()); result.append(ubuntuUpgradeInstructions); From 7817816f8e075ea3a46dcb67f96f09c2524d0579 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Thu, 23 Nov 2023 14:55:53 +0200 Subject: [PATCH 06/18] Fix ApiOperation text to be correct --- .../org/thingsboard/server/controller/EdgeController.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java index f85529dbfe..a1291d9440 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java @@ -556,7 +556,7 @@ public class EdgeController extends BaseController { } @ApiOperation(value = "Get Edge Install Instructions (getEdgeInstallInstructions)", - notes = "Get a docker install instructions for provided edge id." + TENANT_AUTHORITY_PARAGRAPH, + notes = "Get an install instructions for provided edge id." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE) @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") @RequestMapping(value = "/edge/instructions/install/{edgeId}/{method}", method = RequestMethod.GET) @@ -578,7 +578,7 @@ public class EdgeController extends BaseController { } @ApiOperation(value = "Get Edge Upgrade Instructions (getEdgeUpgradeInstructions)", - notes = "Get a docker install instructions for provided edge id." + TENANT_AUTHORITY_PARAGRAPH, + notes = "Get an upgrade instructions for provided edge id." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE) @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") @RequestMapping(value = "/edge/instructions/upgrade/{edgeVersion}/{method}", method = RequestMethod.GET) From aa769b4f372ad99c935cc16022d784cf7c6043e9 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Fri, 24 Nov 2023 12:10:55 +0200 Subject: [PATCH 07/18] edge upgrade instructions minor fixes --- .../modules/home/pages/edge/edges-table-config.resolver.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts b/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts index fa08c0daa0..4429515705 100644 --- a/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts +++ b/ui-ngx/src/app/modules/home/pages/edge/edges-table-config.resolver.ts @@ -558,7 +558,7 @@ export class EdgesTableConfigResolver implements Resolve Date: Fri, 24 Nov 2023 12:22:01 +0200 Subject: [PATCH 08/18] Fix edge install test --- .../org/thingsboard/server/controller/EdgeControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java index 379dafef94..c41d835251 100644 --- a/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java @@ -1151,7 +1151,7 @@ public class EdgeControllerTest extends AbstractControllerTest { public void testGetEdgeInstallInstructions() throws Exception { Edge edge = constructEdge(tenantId, "Edge for Test Docker Install Instructions", "default", "7390c3a6-69b0-9910-d155-b90aca4b772e", "l7q4zsjplzwhk16geqxy"); Edge savedEdge = doPost("/api/edge", edge, Edge.class); - String installInstructions = doGet("/api/edge/instructions/" + savedEdge.getId().getId().toString() + "/docker", String.class); + String installInstructions = doGet("/api/edge/instructions/install/" + savedEdge.getId().getId().toString() + "/docker", String.class); Assert.assertTrue(installInstructions.contains("l7q4zsjplzwhk16geqxy")); Assert.assertTrue(installInstructions.contains("7390c3a6-69b0-9910-d155-b90aca4b772e")); } From 109305f66a41a1f0ecefa055373e595ea9f5af43 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Mon, 27 Nov 2023 11:54:22 +0200 Subject: [PATCH 09/18] Refactor EdgeInstructions: renaming, improve logic, add test --- .../json/edge/instructions/upgrade/upgrade_db.md | 2 +- .../server/controller/EdgeController.java | 14 +++++++------- ... => DefaultEdgeInstallInstructionsService.java} | 2 +- .../instructions/DefaultEdgeUpgradeService.java | 8 ++++---- ...ce.java => EdgeInstallInstructionsService.java} | 2 +- ...ce.java => EdgeUpgradeInstructionsService.java} | 2 +- .../server/service/edge/rpc/EdgeGrpcSession.java | 8 ++++---- .../server/controller/EdgeControllerTest.java | 14 +++++++++++++- .../org/thingsboard/edge/rpc/EdgeGrpcClient.java | 2 +- .../org/thingsboard/rest/client/RestClient.java | 8 +++++++- 10 files changed, 40 insertions(+), 22 deletions(-) rename application/src/main/java/org/thingsboard/server/service/edge/instructions/{DefaultEdgeInstallService.java => DefaultEdgeInstallInstructionsService.java} (98%) rename application/src/main/java/org/thingsboard/server/service/edge/instructions/{EdgeInstallService.java => EdgeInstallInstructionsService.java} (95%) rename application/src/main/java/org/thingsboard/server/service/edge/instructions/{EdgeUpgradeService.java => EdgeUpgradeInstructionsService.java} (94%) diff --git a/application/src/main/data/json/edge/instructions/upgrade/upgrade_db.md b/application/src/main/data/json/edge/instructions/upgrade/upgrade_db.md index d3a28fb20b..6e7beb1425 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/upgrade_db.md +++ b/application/src/main/data/json/edge/instructions/upgrade/upgrade_db.md @@ -3,6 +3,6 @@ Execute regular upgrade script: ```bash -sudo /usr/share/tb-edge/bin/install/upgrade.sh --fromVersion=${CURRENT_TB_EDGE_VERSION} +sudo /usr/share/tb-edge/bin/install/upgrade.sh --fromVersion=${FROM_TB_EDGE_VERSION} {:copy-code} ``` diff --git a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java index a1291d9440..7556ab589b 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java @@ -59,8 +59,8 @@ import org.thingsboard.server.dao.exception.IncorrectParameterException; import org.thingsboard.server.dao.model.ModelConstants; import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.edge.EdgeBulkImportService; -import org.thingsboard.server.service.edge.instructions.EdgeInstallService; -import org.thingsboard.server.service.edge.instructions.EdgeUpgradeService; +import org.thingsboard.server.service.edge.instructions.EdgeInstallInstructionsService; +import org.thingsboard.server.service.edge.instructions.EdgeUpgradeInstructionsService; import org.thingsboard.server.service.edge.rpc.EdgeRpcService; import org.thingsboard.server.service.entitiy.edge.TbEdgeService; import org.thingsboard.server.service.security.model.SecurityUser; @@ -102,8 +102,8 @@ public class EdgeController extends BaseController { private final EdgeBulkImportService edgeBulkImportService; private final TbEdgeService tbEdgeService; private final Optional edgeRpcServiceOpt; - private final Optional edgeInstallServiceOpt; - private final Optional edgeUpgradeServiceOpt; + private final Optional edgeInstallServiceOpt; + private final Optional edgeUpgradeServiceOpt; public static final String EDGE_ID = "edgeId"; public static final String EDGE_SECURITY_CHECK = "If the user has the authority of 'Tenant Administrator', the server checks that the edge is owned by the same tenant. " + @@ -564,7 +564,7 @@ public class EdgeController extends BaseController { public EdgeInstructions getEdgeInstallInstructions( @ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) @PathVariable("edgeId") String strEdgeId, - @ApiParam(value = "Installation method ('docker', 'ubuntu' or 'centos')", allowableValues = "docker,ubuntu,centos") + @ApiParam(value = "Installation method ('docker', 'ubuntu' or 'centos')", allowableValues = "docker, ubuntu, centos") @PathVariable("method") String installationMethod, HttpServletRequest request) throws ThingsboardException { if (isEdgesEnabled() && edgeInstallServiceOpt.isPresent()) { @@ -578,7 +578,7 @@ public class EdgeController extends BaseController { } @ApiOperation(value = "Get Edge Upgrade Instructions (getEdgeUpgradeInstructions)", - notes = "Get an upgrade instructions for provided edge id." + TENANT_AUTHORITY_PARAGRAPH, + notes = "Get an upgrade instructions for provided edge version." + TENANT_AUTHORITY_PARAGRAPH, produces = MediaType.APPLICATION_JSON_VALUE) @PreAuthorize("hasAnyAuthority('TENANT_ADMIN')") @RequestMapping(value = "/edge/instructions/upgrade/{edgeVersion}/{method}", method = RequestMethod.GET) @@ -586,7 +586,7 @@ public class EdgeController extends BaseController { public EdgeInstructions getEdgeUpgradeInstructions( @ApiParam(value = "Edge version", required = true) @PathVariable("edgeVersion") String edgeVersion, - @ApiParam(value = "Installation method ('docker', 'ubuntu' or 'centos')", allowableValues = "docker,ubuntu,centos") + @ApiParam(value = "Upgrade method ('docker', 'ubuntu' or 'centos')", allowableValues = "docker, ubuntu, centos") @PathVariable("method") String method) throws Exception { if (isEdgesEnabled() && edgeUpgradeServiceOpt.isPresent()) { return checkNotNull(edgeUpgradeServiceOpt.get().getUpgradeInstructions(edgeVersion, method)); diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java similarity index 98% rename from application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java rename to application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java index 8ebc5322c7..9da2b2abe8 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java @@ -36,7 +36,7 @@ import java.nio.file.Paths; @RequiredArgsConstructor @ConditionalOnProperty(prefix = "edges", value = "enabled", havingValue = "true") @TbCoreComponent -public class DefaultEdgeInstallService implements EdgeInstallService { +public class DefaultEdgeInstallInstructionsService implements EdgeInstallInstructionsService { private static final String EDGE_DIR = "edge"; private static final String INSTRUCTIONS_DIR = "instructions"; diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java index 8586ad5b4d..7ad9e94977 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java @@ -37,7 +37,7 @@ import java.util.HashMap; @RequiredArgsConstructor @ConditionalOnProperty(prefix = "edges", value = "enabled", havingValue = "true") @TbCoreComponent -public class DefaultEdgeUpgradeService implements EdgeUpgradeService { +public class DefaultEdgeUpgradeService implements EdgeUpgradeInstructionsService { private static final HashMap upgradeVersionHashMap; @@ -96,7 +96,7 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CLEAR_DOCKER_UPGRADE}", rmUpgrade); } ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion + "EDGE"); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CURRENT_TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); currentEdgeVersion = edgeVersion; upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextVersion()); result.append(ubuntuUpgradeInstructions); @@ -125,9 +125,9 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeService { ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", ""); } ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_TAG}", getTagVersion(edgeVersion)); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CURRENT_TB_EDGE_TAG}", getTagVersion(currentEdgeVersion)); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_TAG}", getTagVersion(currentEdgeVersion)); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CURRENT_TB_EDGE_VERSION}", currentEdgeVersion); + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion); currentEdgeVersion = edgeVersion; upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextVersion()); result.append(ubuntuUpgradeInstructions); diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java similarity index 95% rename from application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java rename to application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java index 996e14f16e..af97d7a393 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java @@ -20,7 +20,7 @@ import org.thingsboard.server.common.data.edge.EdgeInstructions; import javax.servlet.http.HttpServletRequest; -public interface EdgeInstallService { +public interface EdgeInstallInstructionsService { EdgeInstructions getInstallInstructions(Edge edge, String installationMethod, HttpServletRequest request); } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java similarity index 94% rename from application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java rename to application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java index b7747655ef..61abd113dc 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java @@ -17,7 +17,7 @@ package org.thingsboard.server.service.edge.instructions; import org.thingsboard.server.common.data.edge.EdgeInstructions; -public interface EdgeUpgradeService { +public interface EdgeUpgradeInstructionsService { EdgeInstructions getUpgradeInstructions(String edgeVersion, String upgradeMethod); } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java b/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java index 32d648c045..4593384594 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/rpc/EdgeGrpcSession.java @@ -775,7 +775,8 @@ public final class EdgeGrpcSession implements Closeable { try { if (edge.getSecret().equals(request.getEdgeSecret())) { sessionOpenListener.accept(edge.getId(), this); - this.edgeVersion = processGetAndSaveEdgeVersion(request.getEdgeVersion()); + this.edgeVersion = request.getEdgeVersion(); + processSaveEdgeVersionAsAttribute(request.getEdgeVersion().name()); return ConnectResponseMsg.newBuilder() .setResponseCode(ConnectResponseCode.ACCEPTED) .setErrorMsg("") @@ -801,10 +802,9 @@ public final class EdgeGrpcSession implements Closeable { .setConfiguration(EdgeConfiguration.getDefaultInstance()).build(); } - private EdgeVersion processGetAndSaveEdgeVersion(EdgeVersion edgeVersion) { - AttributeKvEntry attributeKvEntry = new BaseAttributeKvEntry(new StringDataEntry("edgeVersion", edgeVersion.name()), System.currentTimeMillis()); + private void processSaveEdgeVersionAsAttribute(String edgeVersion) { + AttributeKvEntry attributeKvEntry = new BaseAttributeKvEntry(new StringDataEntry("edgeVersion", edgeVersion), System.currentTimeMillis()); ctx.getAttributesService().save(this.tenantId, this.edge.getId(), DataConstants.SERVER_SCOPE, attributeKvEntry); - return edgeVersion; } @Override diff --git a/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java index c41d835251..836b5903a5 100644 --- a/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java @@ -68,6 +68,7 @@ import org.thingsboard.server.gen.edge.v1.AssetUpdateMsg; import org.thingsboard.server.gen.edge.v1.CustomerUpdateMsg; import org.thingsboard.server.gen.edge.v1.DeviceProfileUpdateMsg; import org.thingsboard.server.gen.edge.v1.DeviceUpdateMsg; +import org.thingsboard.server.gen.edge.v1.EdgeVersion; import org.thingsboard.server.gen.edge.v1.QueueUpdateMsg; import org.thingsboard.server.gen.edge.v1.RuleChainUpdateMsg; import org.thingsboard.server.gen.edge.v1.SyncCompletedMsg; @@ -125,7 +126,7 @@ public class EdgeControllerTest extends AbstractControllerTest { } @After - public void teardownEdgeTest() throws Exception { + public void teardownEdgeTest() { executor.shutdownNow(); } @@ -1155,4 +1156,15 @@ public class EdgeControllerTest extends AbstractControllerTest { Assert.assertTrue(installInstructions.contains("l7q4zsjplzwhk16geqxy")); Assert.assertTrue(installInstructions.contains("7390c3a6-69b0-9910-d155-b90aca4b772e")); } + + @Test + public void testGetEdgeUpgradeInstructions() throws Exception { + Edge edge = constructEdge("Edge for Test Docker Upgrade Instructions", "default"); + Edge savedEdge = doPost("/api/edge", edge, Edge.class); + String body = "{\"edgeVersion\": \"V_3_6_0\"}"; + doPostAsync("/api/plugins/telemetry/EDGE/" + savedEdge.getId().getId() + "/attributes/SERVER_SCOPE", body, String.class, status().isOk()); + String upgradeInstructions = doGet("/api/edge/instructions/upgrade/" + EdgeVersion.V_3_6_0.name() + "/docker", String.class); + Assert.assertTrue(upgradeInstructions.contains("Upgrading to 3.6.1EDGE")); + Assert.assertTrue(upgradeInstructions.contains("Upgrading to 3.6.2EDGE")); + } } diff --git a/common/edge-api/src/main/java/org/thingsboard/edge/rpc/EdgeGrpcClient.java b/common/edge-api/src/main/java/org/thingsboard/edge/rpc/EdgeGrpcClient.java index a9b12865a2..973c50c1d2 100644 --- a/common/edge-api/src/main/java/org/thingsboard/edge/rpc/EdgeGrpcClient.java +++ b/common/edge-api/src/main/java/org/thingsboard/edge/rpc/EdgeGrpcClient.java @@ -111,7 +111,7 @@ public class EdgeGrpcClient implements EdgeRpcClient { .setConnectRequestMsg(ConnectRequestMsg.newBuilder() .setEdgeRoutingKey(edgeKey) .setEdgeSecret(edgeSecret) - .setEdgeVersion(EdgeVersion.V_3_6_1) + .setEdgeVersion(EdgeVersion.V_3_6_2) .setMaxInboundMessageSize(maxInboundMessageSize) .build()) .build()); diff --git a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java index d90a58fde8..de35623a4e 100644 --- a/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java +++ b/rest-client/src/main/java/org/thingsboard/rest/client/RestClient.java @@ -3241,12 +3241,18 @@ public class RestClient implements Closeable { }).getBody(); } - public Optional getEdgeDockerInstallInstructions(EdgeId edgeId, String method) { + public Optional getEdgeInstallInstructions(EdgeId edgeId, String method) { ResponseEntity edgeInstallInstructionsResult = restTemplate.getForEntity(baseURL + "/api/edge/instructions/install/{edgeId}/{method}", EdgeInstructions.class, edgeId.getId(), method); return Optional.ofNullable(edgeInstallInstructionsResult.getBody()); } + public Optional getEdgeUpgradeInstructions(String edgeVersion, String method) { + ResponseEntity edgeUpgradeInstructionsResult = + restTemplate.getForEntity(baseURL + "/api/edge/instructions/upgrade/{edgeVersion}/{method}", EdgeInstructions.class, edgeVersion, method); + return Optional.ofNullable(edgeUpgradeInstructionsResult.getBody()); + } + public UUID saveEntitiesVersion(VersionCreateRequest request) { return restTemplate.postForEntity(baseURL + "/api/entities/vc/version", request, UUID.class).getBody(); } From 2aa4d5bcf15fb45ea874343ffca8780e220743f7 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Fri, 1 Dec 2023 14:53:46 +0200 Subject: [PATCH 10/18] Improve code using dynamic approach of updating Install or Update EdgeInstructions --- ...DefaultEdgeInstallInstructionsService.java | 5 ++ ...efaultEdgeUpgradeInstructionsService.java} | 47 ++++++++++--------- .../EdgeInstallInstructionsService.java | 2 + .../EdgeUpgradeInstructionsService.java | 7 +++ .../service/update/DefaultUpdateService.java | 24 ++++++++++ ...ientSparkplugBAttributesInProfileTest.java | 2 +- .../MqttV5ClientSparkplugBAttributesTest.java | 2 +- .../common/data/EdgeUpgradeMessage.java | 33 +++++++++++++ .../server/common/data/UpgradeInfo.java | 11 +++++ 9 files changed, 110 insertions(+), 23 deletions(-) rename application/src/main/java/org/thingsboard/server/service/edge/instructions/{DefaultEdgeUpgradeService.java => DefaultEdgeUpgradeInstructionsService.java} (85%) create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeMessage.java create mode 100644 common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java index 9da2b2abe8..49f24ac454 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java @@ -67,6 +67,11 @@ public class DefaultEdgeInstallInstructionsService implements EdgeInstallInstruc } } + @Override + public void updateApplicationVersion(String version) { + appVersion = version; + } + private EdgeInstructions getDockerInstallInstructions(Edge edge, HttpServletRequest request) { String dockerInstallInstructions = readFile(resolveFile("docker", "instructions.md")); String baseUrl = request.getServerName(); diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java similarity index 85% rename from application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java rename to application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java index 7ad9e94977..66f12a7ab1 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java @@ -15,13 +15,12 @@ */ package org.thingsboard.server.service.edge.instructions; -import lombok.AllArgsConstructor; -import lombok.Getter; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; +import org.thingsboard.server.common.data.UpgradeInfo; import org.thingsboard.server.common.data.edge.EdgeInstructions; import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.install.InstallScripts; @@ -31,15 +30,16 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; import java.util.HashMap; +import java.util.Map; @Service @Slf4j @RequiredArgsConstructor @ConditionalOnProperty(prefix = "edges", value = "enabled", havingValue = "true") @TbCoreComponent -public class DefaultEdgeUpgradeService implements EdgeUpgradeInstructionsService { +public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstructionsService { - private static final HashMap upgradeVersionHashMap; + private static final Map upgradeVersionHashMap; static { upgradeVersionHashMap = new HashMap<>(); @@ -72,17 +72,29 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeInstructionsService } } + @Override + public void updateApplicationVersion(String version) { + appVersion = version; + } + + @Override + public void updateInstructionMap(Map map) { + for (String key : map.keySet()) { + upgradeVersionHashMap.put(key, map.get(key)); + } + } + private EdgeInstructions getDockerUpgradeInstructions(String tbVersion, String currentEdgeVersion) { UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); - if (upgradeInfo == null || upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { + if (upgradeInfo == null || upgradeInfo.getNextEdgeVersion() == null || tbVersion.equals(currentEdgeVersion)) { return new EdgeInstructions("Edge upgrade instruction for " + currentEdgeVersion + "EDGE is not available."); } boolean rmUpgradeCompose = false; StringBuilder result = new StringBuilder(readFile(resolveFile("docker", "upgrade_preparing.md"))); - while (upgradeInfo.getNextVersion() != null || !tbVersion.equals(currentEdgeVersion)) { - String edgeVersion = upgradeInfo.getNextVersion(); + while (upgradeInfo.getNextEdgeVersion() != null || !tbVersion.equals(currentEdgeVersion)) { + String edgeVersion = upgradeInfo.getNextEdgeVersion(); String ubuntuUpgradeInstructions = readFile(resolveFile("docker", "instructions.md")); - if (upgradeInfo.isUpgradeDb()) { + if (upgradeInfo.isRequiresUpdateDb()) { String upgradeDb = readFile(resolveFile("docker", "upgrade_db.md")); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); } else { @@ -98,7 +110,7 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeInstructionsService ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion + "EDGE"); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); currentEdgeVersion = edgeVersion; - upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextVersion()); + upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextEdgeVersion()); result.append(ubuntuUpgradeInstructions); } String startService = readFile(resolveFile("docker", "start_service.md")); @@ -109,16 +121,16 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeInstructionsService private EdgeInstructions getLinuxUpgradeInstructions(String tbVersion, String currentEdgeVersion, String os) { UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); - if (upgradeInfo == null || upgradeInfo.getNextVersion() == null || tbVersion.equals(currentEdgeVersion)) { + if (upgradeInfo == null || upgradeInfo.getNextEdgeVersion() == null || tbVersion.equals(currentEdgeVersion)) { return new EdgeInstructions("Edge upgrade instruction for " + currentEdgeVersion + "EDGE is not available."); } String upgrade_preparing = readFile(resolveFile("upgrade_preparing.md")); upgrade_preparing = upgrade_preparing.replace("${OS}", os.equals("centos") ? "RHEL/CentOS 7/8" : "Ubuntu"); StringBuilder result = new StringBuilder(upgrade_preparing); - while (upgradeInfo.getNextVersion() != null || !tbVersion.equals(currentEdgeVersion)) { - String edgeVersion = upgradeInfo.getNextVersion(); + while (upgradeInfo.getNextEdgeVersion() != null || !tbVersion.equals(currentEdgeVersion)) { + String edgeVersion = upgradeInfo.getNextEdgeVersion(); String ubuntuUpgradeInstructions = readFile(resolveFile(os, "instructions.md")); - if (upgradeInfo.isUpgradeDb()) { + if (upgradeInfo.isRequiresUpdateDb()) { String upgradeDb = readFile(resolveFile("upgrade_db.md")); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); } else { @@ -129,7 +141,7 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeInstructionsService ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion); currentEdgeVersion = edgeVersion; - upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextVersion()); + upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextEdgeVersion()); result.append(ubuntuUpgradeInstructions); } String startService = readFile(resolveFile("start_service.md")); @@ -161,11 +173,4 @@ public class DefaultEdgeUpgradeService implements EdgeUpgradeInstructionsService private Path getEdgeInstallInstructionsDir() { return Paths.get(installScripts.getDataDir(), InstallScripts.JSON_DIR, EDGE_DIR, INSTRUCTIONS_DIR, UPGRADE_DIR); } - - @AllArgsConstructor - @Getter - public static class UpgradeInfo { - private boolean upgradeDb; - private String nextVersion; - } } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java index af97d7a393..2c1ef4fe87 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java @@ -23,4 +23,6 @@ import javax.servlet.http.HttpServletRequest; public interface EdgeInstallInstructionsService { EdgeInstructions getInstallInstructions(Edge edge, String installationMethod, HttpServletRequest request); + + void updateApplicationVersion(String version); } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java index 61abd113dc..4c67c1ab9c 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java @@ -15,9 +15,16 @@ */ package org.thingsboard.server.service.edge.instructions; +import org.thingsboard.server.common.data.UpgradeInfo; import org.thingsboard.server.common.data.edge.EdgeInstructions; +import java.util.Map; + public interface EdgeUpgradeInstructionsService { EdgeInstructions getUpgradeInstructions(String edgeVersion, String upgradeMethod); + + void updateInstructionMap(Map upgradeVersions); + + void updateApplicationVersion(String version); } diff --git a/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java b/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java index f5e3daf05d..7ddc4b0e9d 100644 --- a/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java +++ b/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java @@ -27,17 +27,21 @@ import org.springframework.stereotype.Service; import org.springframework.web.client.RestTemplate; import org.thingsboard.common.util.JacksonUtil; import org.thingsboard.common.util.ThingsBoardThreadFactory; +import org.thingsboard.server.common.data.EdgeUpgradeMessage; import org.thingsboard.server.common.data.UpdateMessage; import org.thingsboard.server.common.data.notification.rule.trigger.NewPlatformVersionTrigger; import org.thingsboard.server.common.msg.notification.NotificationRuleProcessor; import org.thingsboard.server.queue.util.AfterStartUp; import org.thingsboard.server.queue.util.TbCoreComponent; +import org.thingsboard.server.service.edge.instructions.EdgeInstallInstructionsService; +import org.thingsboard.server.service.edge.instructions.EdgeUpgradeInstructionsService; import javax.annotation.PreDestroy; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.HashMap; import java.util.UUID; import java.util.concurrent.Executors; import java.util.concurrent.ScheduledExecutorService; @@ -65,12 +69,20 @@ public class DefaultUpdateService implements UpdateService { @Autowired private NotificationRuleProcessor notificationRuleProcessor; + @Autowired + private EdgeInstallInstructionsService edgeInstallInstructionsService; + + @Autowired + private EdgeUpgradeInstructionsService edgeUpgradeInstructionsService; + private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, ThingsBoardThreadFactory.forName("tb-update-service")); private ScheduledFuture checkUpdatesFuture = null; private final RestTemplate restClient = new RestTemplate(); private UpdateMessage updateMessage; + private EdgeUpgradeMessage edgeUpgradeMessage; + private String edgeInstallVersion; private String platform; private String version; @@ -82,6 +94,7 @@ public class DefaultUpdateService implements UpdateService { updateMessage = new UpdateMessage(false, version, "", "", "https://thingsboard.io/docs/reference/releases", "https://thingsboard.io/docs/reference/releases"); + edgeUpgradeMessage = new EdgeUpgradeMessage(new HashMap<>()); if (updatesEnabled) { try { platform = System.getProperty("platform", "unknown"); @@ -141,6 +154,17 @@ public class DefaultUpdateService implements UpdateService { .updateInfo(updateMessage) .build()); } + String prevEdgeInstallVersion = edgeInstallVersion; + edgeInstallVersion = restClient.postForObject(UPDATE_SERVER_BASE_URL + "/api/v1/edge/install", new HttpEntity<>(request.toString(), headers), String.class); + if (edgeInstallVersion != null && !edgeInstallVersion.equals(prevEdgeInstallVersion)) { + edgeInstallInstructionsService.updateApplicationVersion(edgeInstallVersion); + edgeUpgradeInstructionsService.updateApplicationVersion(edgeInstallVersion); + } + EdgeUpgradeMessage prevEdgeUpgradeMessage = edgeUpgradeMessage; + edgeUpgradeMessage = restClient.postForObject(UPDATE_SERVER_BASE_URL + "/api/v1/edge/upgrades", new HttpEntity<>(request.toString(), headers), EdgeUpgradeMessage.class); + if (edgeUpgradeMessage != null && !edgeUpgradeMessage.equals(prevEdgeUpgradeMessage)) { + edgeUpgradeInstructionsService.updateInstructionMap(edgeUpgradeMessage.getEdgeVersions()); + } } catch (Exception e) { log.trace(e.getMessage()); } diff --git a/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/MqttV5ClientSparkplugBAttributesInProfileTest.java b/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/MqttV5ClientSparkplugBAttributesInProfileTest.java index 1d5a2c127f..faa29ad35d 100644 --- a/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/MqttV5ClientSparkplugBAttributesInProfileTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/MqttV5ClientSparkplugBAttributesInProfileTest.java @@ -52,4 +52,4 @@ public class MqttV5ClientSparkplugBAttributesInProfileTest extends AbstractMqttV processClientDeviceWithCorrectAccessTokenPublish_AttributesInProfileContainsKeyAttributes(); } -} \ No newline at end of file +} diff --git a/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/MqttV5ClientSparkplugBAttributesTest.java b/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/MqttV5ClientSparkplugBAttributesTest.java index f5d42046b7..abe1810887 100644 --- a/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/MqttV5ClientSparkplugBAttributesTest.java +++ b/application/src/test/java/org/thingsboard/server/transport/mqtt/sparkplug/attributes/MqttV5ClientSparkplugBAttributesTest.java @@ -78,4 +78,4 @@ public class MqttV5ClientSparkplugBAttributesTest extends AbstractMqttV5ClientSp processClientDeviceWithCorrectAccessTokenPublishWithBirth_SharedAttributes_LongType_IfMetricFailedTypeCheck_SendValueOk(); } -} \ No newline at end of file +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeMessage.java b/common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeMessage.java new file mode 100644 index 0000000000..67cbce56cb --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeMessage.java @@ -0,0 +1,33 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.thingsboard.server.common.data; + +import io.swagger.annotations.ApiModel; +import io.swagger.annotations.ApiModelProperty; +import lombok.Data; + +import java.io.Serializable; +import java.util.Map; + +@Data +@ApiModel +public class EdgeUpgradeMessage implements Serializable { + + private static final long serialVersionUID = 2872965507642822989L; + + @ApiModelProperty(position = 1, value = "Mapping for upgrade versions and upgrade strategy (next ver).") + private final Map edgeVersions; +} diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java new file mode 100644 index 0000000000..d9101bd971 --- /dev/null +++ b/common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java @@ -0,0 +1,11 @@ +package org.thingsboard.server.common.data; + +import lombok.AllArgsConstructor; +import lombok.Getter; + +@AllArgsConstructor +@Getter +public class UpgradeInfo { + private boolean requiresUpdateDb; + private String nextEdgeVersion; +} From 713915cfa18ef5a8bd130aba6e26a26ab81c585a Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Fri, 1 Dec 2023 15:02:29 +0200 Subject: [PATCH 11/18] Fix license-format: Add header to new file --- .../server/common/data/UpgradeInfo.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java index d9101bd971..8dd8f4d9b8 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java @@ -1,3 +1,18 @@ +/** + * Copyright © 2016-2023 The Thingsboard Authors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ package org.thingsboard.server.common.data; import lombok.AllArgsConstructor; From 1a23673df86162825cbdbcdbde5a50ff711dc03d Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Fri, 1 Dec 2023 15:21:06 +0200 Subject: [PATCH 12/18] Refactor upgradeVersionHashMap to use only Update service for update, do not hardcode --- .../DefaultEdgeUpgradeInstructionsService.java | 9 +-------- 1 file changed, 1 insertion(+), 8 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java index 66f12a7ab1..138fae70f2 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java @@ -39,14 +39,7 @@ import java.util.Map; @TbCoreComponent public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstructionsService { - private static final Map upgradeVersionHashMap; - - static { - upgradeVersionHashMap = new HashMap<>(); - upgradeVersionHashMap.put("3.6.0", new UpgradeInfo(true, "3.6.1")); - upgradeVersionHashMap.put("3.6.1", new UpgradeInfo(true, "3.6.2")); - upgradeVersionHashMap.put("3.6.2", new UpgradeInfo(true, null)); - } + private static final Map upgradeVersionHashMap = new HashMap<>(); private static final String EDGE_DIR = "edge"; private static final String INSTRUCTIONS_DIR = "instructions"; From fef62e2693a551f245c6e7b136a310334519afc6 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Mon, 4 Dec 2023 13:24:57 +0200 Subject: [PATCH 13/18] Update psql to use 15 version --- .../install/centos/instructions.md | 22 +++++++++---------- .../install/ubuntu/instructions.md | 2 +- .../instructions/upgrade/docker/upgrade_db.md | 2 +- .../service/update/DefaultUpdateService.java | 4 ++-- 4 files changed, 15 insertions(+), 15 deletions(-) diff --git a/application/src/main/data/json/edge/instructions/install/centos/instructions.md b/application/src/main/data/json/edge/instructions/install/centos/instructions.md index 9ad882c387..22bb3f63a0 100644 --- a/application/src/main/data/json/edge/instructions/install/centos/instructions.md +++ b/application/src/main/data/json/edge/instructions/install/centos/instructions.md @@ -56,13 +56,13 @@ sudo yum update sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-7-x86_64/pgdg-redhat-repo-latest.noarch.rpm # Install packages sudo yum -y install epel-release yum-utils -sudo yum-config-manager --enable pgdg12 -sudo yum install postgresql12-server postgresql12 +sudo yum-config-manager --enable pgdg15 +sudo yum install postgresql15-server postgresql15 # Initialize your PostgreSQL DB -sudo /usr/pgsql-12/bin/postgresql-12-setup initdb -sudo systemctl start postgresql-12 +sudo /usr/pgsql-15/bin/postgresql-15-setup initdb +sudo systemctl start postgresql-15 # Optional: Configure PostgreSQL to start on boot -sudo systemctl enable --now postgresql-12 +sudo systemctl enable --now postgresql-15 {:copy-code} ``` @@ -74,12 +74,12 @@ sudo systemctl enable --now postgresql-12 sudo yum -y install https://download.postgresql.org/pub/repos/yum/reporpms/EL-8-x86_64/pgdg-redhat-repo-latest.noarch.rpm # Install packages sudo dnf -qy module disable postgresql -sudo dnf -y install postgresql12 postgresql12-server +sudo dnf -y install postgresql15 postgresql15-server # Initialize your PostgreSQL DB -sudo /usr/pgsql-12/bin/postgresql-12-setup initdb -sudo systemctl start postgresql-12 +sudo /usr/pgsql-15/bin/postgresql-15-setup initdb +sudo systemctl start postgresql-15 # Optional: Configure PostgreSQL to start on boot -sudo systemctl enable --now postgresql-12 +sudo systemctl enable --now postgresql-15 {:copy-code} ``` @@ -101,7 +101,7 @@ After configuring the password, edit the pg_hba.conf to use MD5 authentication w Edit pg_hba.conf file: ```bash -sudo nano /var/lib/pgsql/12/data/pg_hba.conf +sudo nano /var/lib/pgsql/15/data/pg_hba.conf {:copy-code} ``` @@ -121,7 +121,7 @@ host all all 127.0.0.1/32 md5 Finally, you should restart the PostgreSQL service to initialize the new configuration: ```bash -sudo systemctl restart postgresql-12.service +sudo systemctl restart postgresql-15.service {:copy-code} ``` diff --git a/application/src/main/data/json/edge/instructions/install/ubuntu/instructions.md b/application/src/main/data/json/edge/instructions/install/ubuntu/instructions.md index 2a6d9c0bc3..6fcdcd50f2 100644 --- a/application/src/main/data/json/edge/instructions/install/ubuntu/instructions.md +++ b/application/src/main/data/json/edge/instructions/install/ubuntu/instructions.md @@ -49,7 +49,7 @@ echo "deb http://apt.postgresql.org/pub/repos/apt/ ${RELEASE}"-pgdg main | sudo # install and launch the postgresql service: sudo apt update -sudo apt -y install postgresql-12 +sudo apt -y install postgresql-15 sudo service postgresql start {:copy-code} ``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md index ac38031517..8c218ab41d 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md @@ -23,7 +23,7 @@ services: entrypoint: upgrade-tb-edge.sh postgres: restart: always - image: "postgres:12" + image: "postgres:15" ports: - "5432" environment: diff --git a/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java b/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java index 7ddc4b0e9d..cf31464ba9 100644 --- a/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java +++ b/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java @@ -69,10 +69,10 @@ public class DefaultUpdateService implements UpdateService { @Autowired private NotificationRuleProcessor notificationRuleProcessor; - @Autowired + @Autowired(required = false) private EdgeInstallInstructionsService edgeInstallInstructionsService; - @Autowired + @Autowired(required = false) private EdgeUpgradeInstructionsService edgeUpgradeInstructionsService; private final ScheduledExecutorService scheduler = Executors.newScheduledThreadPool(1, ThingsBoardThreadFactory.forName("tb-update-service")); From 36629fddb38070a854b3376eae305db55629a3a2 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Mon, 4 Dec 2023 14:51:31 +0200 Subject: [PATCH 14/18] Fix test and change upgrade instructions title --- .../server/controller/EdgeControllerTest.java | 12 ++++++++++++ .../pages/edge/edge-instructions-dialog.component.ts | 3 +++ 2 files changed, 15 insertions(+) diff --git a/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java index cacc5b86c8..fe5cceb206 100644 --- a/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java @@ -48,6 +48,7 @@ import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.TenantProfile; +import org.thingsboard.server.common.data.UpgradeInfo; import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.asset.AssetProfile; @@ -83,9 +84,11 @@ import org.thingsboard.server.gen.edge.v1.TenantUpdateMsg; import org.thingsboard.server.gen.edge.v1.UpdateMsgType; import org.thingsboard.server.gen.edge.v1.UserCredentialsUpdateMsg; import org.thingsboard.server.gen.edge.v1.UserUpdateMsg; +import org.thingsboard.server.service.edge.instructions.EdgeUpgradeInstructionsService; import java.util.ArrayList; import java.util.Collections; +import java.util.HashMap; import java.util.List; import java.util.Map; import java.util.Optional; @@ -116,6 +119,9 @@ public class EdgeControllerTest extends AbstractControllerTest { @Autowired private EdgeDao edgeDao; + @Autowired + private EdgeUpgradeInstructionsService edgeUpgradeInstructionsService; + static class Config { @Bean @Primary @@ -1180,6 +1186,12 @@ public class EdgeControllerTest extends AbstractControllerTest { @Test public void testGetEdgeUpgradeInstructions() throws Exception { + // UpdateInfo config is updating from Thingsboard Update server + HashMap upgradeInfoHashMap = new HashMap<>(); + upgradeInfoHashMap.put("3.6.0", new UpgradeInfo(true, "3.6.1")); + upgradeInfoHashMap.put("3.6.1", new UpgradeInfo(true, "3.6.2")); + upgradeInfoHashMap.put("3.6.2", new UpgradeInfo(true, null)); + edgeUpgradeInstructionsService.updateInstructionMap(upgradeInfoHashMap); Edge edge = constructEdge("Edge for Test Docker Upgrade Instructions", "default"); Edge savedEdge = doPost("/api/edge", edge, Edge.class); String body = "{\"edgeVersion\": \"V_3_6_0\"}"; diff --git a/ui-ngx/src/app/modules/home/pages/edge/edge-instructions-dialog.component.ts b/ui-ngx/src/app/modules/home/pages/edge/edge-instructions-dialog.component.ts index c618df96bb..6c9511d709 100644 --- a/ui-ngx/src/app/modules/home/pages/edge/edge-instructions-dialog.component.ts +++ b/ui-ngx/src/app/modules/home/pages/edge/edge-instructions-dialog.component.ts @@ -65,6 +65,9 @@ export class EdgeInstructionsDialogComponent extends DialogComponent Date: Tue, 5 Dec 2023 10:04:14 +0200 Subject: [PATCH 15/18] Minor refactoring. Provide better naming and refactor due to changes in thingsboard-updates --- ...DefaultEdgeUpgradeInstructionsService.java | 30 +++++++++---------- .../EdgeUpgradeInstructionsService.java | 4 +-- .../service/update/DefaultUpdateService.java | 5 ++-- .../server/controller/EdgeControllerTest.java | 10 +++---- ...{UpgradeInfo.java => EdgeUpgradeInfo.java} | 2 +- .../common/data/EdgeUpgradeMessage.java | 2 +- 6 files changed, 27 insertions(+), 26 deletions(-) rename common/data/src/main/java/org/thingsboard/server/common/data/{UpgradeInfo.java => EdgeUpgradeInfo.java} (96%) diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java index 138fae70f2..0401521a1c 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java @@ -20,7 +20,7 @@ import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; -import org.thingsboard.server.common.data.UpgradeInfo; +import org.thingsboard.server.common.data.EdgeUpgradeInfo; import org.thingsboard.server.common.data.edge.EdgeInstructions; import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.install.InstallScripts; @@ -39,7 +39,7 @@ import java.util.Map; @TbCoreComponent public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstructionsService { - private static final Map upgradeVersionHashMap = new HashMap<>(); + private static final Map upgradeVersionHashMap = new HashMap<>(); private static final String EDGE_DIR = "edge"; private static final String INSTRUCTIONS_DIR = "instructions"; @@ -71,23 +71,23 @@ public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstruc } @Override - public void updateInstructionMap(Map map) { + public void updateInstructionMap(Map map) { for (String key : map.keySet()) { upgradeVersionHashMap.put(key, map.get(key)); } } private EdgeInstructions getDockerUpgradeInstructions(String tbVersion, String currentEdgeVersion) { - UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); - if (upgradeInfo == null || upgradeInfo.getNextEdgeVersion() == null || tbVersion.equals(currentEdgeVersion)) { + EdgeUpgradeInfo edgeUpgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); + if (edgeUpgradeInfo == null || edgeUpgradeInfo.getNextEdgeVersion() == null || tbVersion.equals(currentEdgeVersion)) { return new EdgeInstructions("Edge upgrade instruction for " + currentEdgeVersion + "EDGE is not available."); } boolean rmUpgradeCompose = false; StringBuilder result = new StringBuilder(readFile(resolveFile("docker", "upgrade_preparing.md"))); - while (upgradeInfo.getNextEdgeVersion() != null || !tbVersion.equals(currentEdgeVersion)) { - String edgeVersion = upgradeInfo.getNextEdgeVersion(); + while (edgeUpgradeInfo.getNextEdgeVersion() != null || !tbVersion.equals(currentEdgeVersion)) { + String edgeVersion = edgeUpgradeInfo.getNextEdgeVersion(); String ubuntuUpgradeInstructions = readFile(resolveFile("docker", "instructions.md")); - if (upgradeInfo.isRequiresUpdateDb()) { + if (edgeUpgradeInfo.isRequiresUpdateDb()) { String upgradeDb = readFile(resolveFile("docker", "upgrade_db.md")); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); } else { @@ -103,7 +103,7 @@ public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstruc ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion + "EDGE"); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); currentEdgeVersion = edgeVersion; - upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextEdgeVersion()); + edgeUpgradeInfo = upgradeVersionHashMap.get(edgeUpgradeInfo.getNextEdgeVersion()); result.append(ubuntuUpgradeInstructions); } String startService = readFile(resolveFile("docker", "start_service.md")); @@ -113,17 +113,17 @@ public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstruc } private EdgeInstructions getLinuxUpgradeInstructions(String tbVersion, String currentEdgeVersion, String os) { - UpgradeInfo upgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); - if (upgradeInfo == null || upgradeInfo.getNextEdgeVersion() == null || tbVersion.equals(currentEdgeVersion)) { + EdgeUpgradeInfo edgeUpgradeInfo = upgradeVersionHashMap.get(currentEdgeVersion); + if (edgeUpgradeInfo == null || edgeUpgradeInfo.getNextEdgeVersion() == null || tbVersion.equals(currentEdgeVersion)) { return new EdgeInstructions("Edge upgrade instruction for " + currentEdgeVersion + "EDGE is not available."); } String upgrade_preparing = readFile(resolveFile("upgrade_preparing.md")); upgrade_preparing = upgrade_preparing.replace("${OS}", os.equals("centos") ? "RHEL/CentOS 7/8" : "Ubuntu"); StringBuilder result = new StringBuilder(upgrade_preparing); - while (upgradeInfo.getNextEdgeVersion() != null || !tbVersion.equals(currentEdgeVersion)) { - String edgeVersion = upgradeInfo.getNextEdgeVersion(); + while (edgeUpgradeInfo.getNextEdgeVersion() != null || !tbVersion.equals(currentEdgeVersion)) { + String edgeVersion = edgeUpgradeInfo.getNextEdgeVersion(); String ubuntuUpgradeInstructions = readFile(resolveFile(os, "instructions.md")); - if (upgradeInfo.isRequiresUpdateDb()) { + if (edgeUpgradeInfo.isRequiresUpdateDb()) { String upgradeDb = readFile(resolveFile("upgrade_db.md")); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); } else { @@ -134,7 +134,7 @@ public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstruc ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion); currentEdgeVersion = edgeVersion; - upgradeInfo = upgradeVersionHashMap.get(upgradeInfo.getNextEdgeVersion()); + edgeUpgradeInfo = upgradeVersionHashMap.get(edgeUpgradeInfo.getNextEdgeVersion()); result.append(ubuntuUpgradeInstructions); } String startService = readFile(resolveFile("start_service.md")); diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java index 4c67c1ab9c..e6f5cb0753 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java @@ -15,7 +15,7 @@ */ package org.thingsboard.server.service.edge.instructions; -import org.thingsboard.server.common.data.UpgradeInfo; +import org.thingsboard.server.common.data.EdgeUpgradeInfo; import org.thingsboard.server.common.data.edge.EdgeInstructions; import java.util.Map; @@ -24,7 +24,7 @@ public interface EdgeUpgradeInstructionsService { EdgeInstructions getUpgradeInstructions(String edgeVersion, String upgradeMethod); - void updateInstructionMap(Map upgradeVersions); + void updateInstructionMap(Map upgradeVersions); void updateApplicationVersion(String version); } diff --git a/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java b/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java index cf31464ba9..6a86d022df 100644 --- a/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java +++ b/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java @@ -154,14 +154,15 @@ public class DefaultUpdateService implements UpdateService { .updateInfo(updateMessage) .build()); } + ObjectNode edgeRequest = JacksonUtil.newObjectNode().put(VERSION_PARAM, version); String prevEdgeInstallVersion = edgeInstallVersion; - edgeInstallVersion = restClient.postForObject(UPDATE_SERVER_BASE_URL + "/api/v1/edge/install", new HttpEntity<>(request.toString(), headers), String.class); + edgeInstallVersion = restClient.postForObject(UPDATE_SERVER_BASE_URL + "/api/v1/edge/installMapping", new HttpEntity<>(edgeRequest.toString(), headers), String.class); if (edgeInstallVersion != null && !edgeInstallVersion.equals(prevEdgeInstallVersion)) { edgeInstallInstructionsService.updateApplicationVersion(edgeInstallVersion); edgeUpgradeInstructionsService.updateApplicationVersion(edgeInstallVersion); } EdgeUpgradeMessage prevEdgeUpgradeMessage = edgeUpgradeMessage; - edgeUpgradeMessage = restClient.postForObject(UPDATE_SERVER_BASE_URL + "/api/v1/edge/upgrades", new HttpEntity<>(request.toString(), headers), EdgeUpgradeMessage.class); + edgeUpgradeMessage = restClient.postForObject(UPDATE_SERVER_BASE_URL + "/api/v1/edge/upgradeMapping", new HttpEntity<>(edgeRequest.toString(), headers), EdgeUpgradeMessage.class); if (edgeUpgradeMessage != null && !edgeUpgradeMessage.equals(prevEdgeUpgradeMessage)) { edgeUpgradeInstructionsService.updateInstructionMap(edgeUpgradeMessage.getEdgeVersions()); } diff --git a/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java b/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java index fe5cceb206..9ecabf06f3 100644 --- a/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java +++ b/application/src/test/java/org/thingsboard/server/controller/EdgeControllerTest.java @@ -48,7 +48,7 @@ import org.thingsboard.server.common.data.EntitySubtype; import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.Tenant; import org.thingsboard.server.common.data.TenantProfile; -import org.thingsboard.server.common.data.UpgradeInfo; +import org.thingsboard.server.common.data.EdgeUpgradeInfo; import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.asset.Asset; import org.thingsboard.server.common.data.asset.AssetProfile; @@ -1187,10 +1187,10 @@ public class EdgeControllerTest extends AbstractControllerTest { @Test public void testGetEdgeUpgradeInstructions() throws Exception { // UpdateInfo config is updating from Thingsboard Update server - HashMap upgradeInfoHashMap = new HashMap<>(); - upgradeInfoHashMap.put("3.6.0", new UpgradeInfo(true, "3.6.1")); - upgradeInfoHashMap.put("3.6.1", new UpgradeInfo(true, "3.6.2")); - upgradeInfoHashMap.put("3.6.2", new UpgradeInfo(true, null)); + HashMap upgradeInfoHashMap = new HashMap<>(); + upgradeInfoHashMap.put("3.6.0", new EdgeUpgradeInfo(true, "3.6.1")); + upgradeInfoHashMap.put("3.6.1", new EdgeUpgradeInfo(true, "3.6.2")); + upgradeInfoHashMap.put("3.6.2", new EdgeUpgradeInfo(true, null)); edgeUpgradeInstructionsService.updateInstructionMap(upgradeInfoHashMap); Edge edge = constructEdge("Edge for Test Docker Upgrade Instructions", "default"); Edge savedEdge = doPost("/api/edge", edge, Edge.class); diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java b/common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeInfo.java similarity index 96% rename from common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java rename to common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeInfo.java index 8dd8f4d9b8..cadd589b49 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/UpgradeInfo.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeInfo.java @@ -20,7 +20,7 @@ import lombok.Getter; @AllArgsConstructor @Getter -public class UpgradeInfo { +public class EdgeUpgradeInfo { private boolean requiresUpdateDb; private String nextEdgeVersion; } diff --git a/common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeMessage.java b/common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeMessage.java index 67cbce56cb..bb17479607 100644 --- a/common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeMessage.java +++ b/common/data/src/main/java/org/thingsboard/server/common/data/EdgeUpgradeMessage.java @@ -29,5 +29,5 @@ public class EdgeUpgradeMessage implements Serializable { private static final long serialVersionUID = 2872965507642822989L; @ApiModelProperty(position = 1, value = "Mapping for upgrade versions and upgrade strategy (next ver).") - private final Map edgeVersions; + private final Map edgeVersions; } From 88d9128b965cee0475a078f114bc8be938594f95 Mon Sep 17 00:00:00 2001 From: Volodymyr Babak Date: Fri, 8 Dec 2023 13:34:36 +0200 Subject: [PATCH 16/18] Use docker volume instead of bind mount --- .../install/docker/instructions.md | 31 ++++----- .../install/docker/localhost_warning.md | 3 - .../upgrade/docker/start_service.md | 10 +-- .../instructions/upgrade/docker/upgrade_db.md | 20 ++++-- .../upgrade/docker/upgrade_preparing.md | 65 ++++++++++++++++++- .../instructions/upgrade/docker/upgrade_rm.md | 5 -- ...DefaultEdgeInstallInstructionsService.java | 11 ++-- ...DefaultEdgeUpgradeInstructionsService.java | 8 --- .../dao/util/DeviceConnectivityUtil.java | 2 +- 9 files changed, 100 insertions(+), 55 deletions(-) delete mode 100644 application/src/main/data/json/edge/instructions/install/docker/localhost_warning.md delete mode 100644 application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_rm.md diff --git a/application/src/main/data/json/edge/instructions/install/docker/instructions.md b/application/src/main/data/json/edge/instructions/install/docker/instructions.md index b07b128062..9910124b3e 100644 --- a/application/src/main/data/json/edge/instructions/install/docker/instructions.md +++ b/application/src/main/data/json/edge/instructions/install/docker/instructions.md @@ -4,22 +4,8 @@ Here is the list of commands, that can be used to quickly install ThingsBoard Ed Install Docker CE and Docker Compose. -#### Create data and logs folders - -Run following commands, before starting docker container(s), to create folders for storing data and logs. -These commands additionally will change owner of newly created folders to docker container user. -To do this (to change user) **chown** command is used, and this command requires *sudo* permissions (command will request password for a *sudo* access): - -```bash -mkdir -p ~/.mytb-edge-data && sudo chown -R 799:799 ~/.mytb-edge-data -mkdir -p ~/.mytb-edge-logs && sudo chown -R 799:799 ~/.mytb-edge-logs -{:copy-code} -``` - #### Running ThingsBoard Edge as docker service -${LOCALHOST_WARNING} - Create docker compose file for ThingsBoard Edge service: ```bash @@ -30,7 +16,7 @@ nano docker-compose.yml Add the following lines to the yml file: ```bash -version: '3.0' +version: '3.8' services: mytbedge: restart: always @@ -47,8 +33,9 @@ services: CLOUD_RPC_PORT: ${CLOUD_RPC_PORT} CLOUD_RPC_SSL_ENABLED: ${CLOUD_RPC_SSL_ENABLED} volumes: - - ~/.mytb-edge-data:/data - - ~/.mytb-edge-logs:/var/log/tb-edge + - tb-edge-data:/data + - tb-edge-logs:/var/log/tb-edge + ${EXTRA_HOSTS} postgres: restart: always image: "postgres:15" @@ -58,7 +45,15 @@ services: POSTGRES_DB: tb-edge POSTGRES_PASSWORD: postgres volumes: - - ~/.mytb-edge-data/db:/var/lib/postgresql/data + - tb-edge-postgres-data:/var/lib/postgresql/data + +volumes: + tb-edge-data: + name: tb-edge-data + tb-edge-logs: + name: tb-edge-logs + tb-edge-postgres-data: + name: tb-edge-postgres-data {:copy-code} ``` diff --git a/application/src/main/data/json/edge/instructions/install/docker/localhost_warning.md b/application/src/main/data/json/edge/instructions/install/docker/localhost_warning.md deleted file mode 100644 index d2a5bad26b..0000000000 --- a/application/src/main/data/json/edge/instructions/install/docker/localhost_warning.md +++ /dev/null @@ -1,3 +0,0 @@ -###### WARNING NOTE: 'localhost' can not be used as CLOUD_RPC_HOST - -Please note that your ThingsBoard base URL is **'localhost'** at the moment. **'localhost'** cannot be used for docker containers - please update **CLOUD_RPC_HOST** environment variable below to the IP address of your machine (*docker **host** machine*). IP address must be `192.168.1.XX` or similar format. In other case - ThingsBoard Edge service, that is running in docker container, will not be able to connect to the cloud. diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md b/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md index b9e5786211..a6c9c6731c 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/start_service.md @@ -5,15 +5,15 @@ nano docker-compose.yml ``` ```text -version: '3.0' +version: '3.8' services: -mytbedge: -restart: always -image: "thingsboard/tb-edge:${TB_EDGE_VERSION}" + mytbedge: + restart: always + image: "thingsboard/tb-edge:${TB_EDGE_VERSION}" ... ``` -Make sure your image is the set to tb-edge-${TB_EDGE_VERSION}. +Make sure your image is the set to **tb-edge-${TB_EDGE_VERSION}**. Execute the following commands to up this docker compose directly: ```bash diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md index 8c218ab41d..d922fa9155 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_db.md @@ -1,16 +1,14 @@ -${CLEAR_DOCKER_UPGRADE} - Create docker compose file for ThingsBoard Edge upgrade process: ```bash -nano docker-compose-upgrade.yml +> docker-compose-upgrade.yml && nano docker-compose-upgrade.yml {:copy-code} ``` Add the following lines to the yml file: ```bash -version: '3.0' +version: '3.8' services: mytbedge: restart: on-failure @@ -18,8 +16,8 @@ services: environment: SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/tb-edge volumes: - - ~/.mytb-edge-data:/data - - ~/.mytb-edge-logs:/var/log/tb-edge + - tb-edge-data:/data + - tb-edge-logs:/var/log/tb-edge entrypoint: upgrade-tb-edge.sh postgres: restart: always @@ -30,7 +28,15 @@ services: POSTGRES_DB: tb-edge POSTGRES_PASSWORD: postgres volumes: - - ~/.mytb-edge-data/db:/var/lib/postgresql/data + - tb-edge-postgres-data:/var/lib/postgresql/data + +volumes: + tb-edge-data: + name: tb-edge-data + tb-edge-logs: + name: tb-edge-logs + tb-edge-postgres-data: + name: tb-edge-postgres-data {:copy-code} ``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md index 04034dea5e..bb3ce268b8 100644 --- a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md +++ b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_preparing.md @@ -15,10 +15,69 @@ docker compose rm mytbedge docker-compose stop docker-compose rm mytbedge ``` -##### Backup Database -Make a copy of the database folder before upgrading: + +##### Migrating Data from Docker Bind Mount Folders to Docker Volumes +Starting with the **3.6.2** release, the ThingsBoard team has transitioned from using Docker bind mount folders to Docker volumes. +This change aims to enhance security and efficiency in storing data for Docker containers and to mitigate permission issues across various environments. + +To migrate from Docker bind mounts to Docker volumes, please execute the following commands: ```bash -sudo cp -r ~/.mytb-edge-data/db ~/.mytb-edge-db-BACKUP +docker run --rm -v tb-edge-data:/volume -v ~/.mytb-edge-data:/backup busybox sh -c "cp -a /backup/. /volume" +docker run --rm -v tb-edge-logs:/volume -v ~/.mytb-edge-logs:/backup busybox sh -c "cp -a /backup/. /volume" +docker run --rm -v tb-edge-postgres-data:/volume -v ~/.mytb-edge-data/db:/backup busybox sh -c "cp -a /backup/. /volume" +{:copy-code} +``` + +After completing the data migration to the newly created Docker volumes, you'll need to update the volume mounts in your Docker Compose configuration. +Modify the `docker-compose.yml` file for ThingsBoard Edge to update the volume settings. + +First, please update docker compose file version. Find next snippet: +```text +version: '3.0' +... +``` + +And replace it with: +```text +version: '3.8' +... +``` + +Then update volume mounts. Locate the following snippet: +```text + volumes: + - ~/.mytb-edge-data:/data + - ~/.mytb-edge-logs:/var/log/tb-edge +... +``` + +And replace it with: +```text + volumes: + - tb-edge-data:/data + - tb-edge-logs:/var/log/tb-edge +... +``` + +Apply a similar update for the PostgreSQL service. Find the section: +```text + volumes: + - ~/.mytb-edge-data/db:/var/lib/postgresql/data +... +``` + +And replace it with: +```text + volumes: + - tb-edge-postgres-data/:/var/lib/postgresql/data +... +``` + +##### Backup Database +Make a copy of the database volume before upgrading: + +```bash +docker run --rm -v tb-edge-postgres-data:/source -v tb-edge-postgres-data-backup:/backup busybox sh -c "cp -a /source/. /backup" {:copy-code} ``` diff --git a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_rm.md b/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_rm.md deleted file mode 100644 index 4957f9333f..0000000000 --- a/application/src/main/data/json/edge/instructions/upgrade/docker/upgrade_rm.md +++ /dev/null @@ -1,5 +0,0 @@ -Delete docker compose file, if already exists: -```bash -rm docker-compose-upgrade.yml -{:copy-code} -``` diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java index 49f24ac454..fef3c2644a 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java @@ -22,6 +22,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.stereotype.Service; import org.thingsboard.server.common.data.edge.Edge; import org.thingsboard.server.common.data.edge.EdgeInstructions; +import org.thingsboard.server.dao.util.DeviceConnectivityUtil; import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.service.install.InstallScripts; @@ -75,12 +76,12 @@ public class DefaultEdgeInstallInstructionsService implements EdgeInstallInstruc private EdgeInstructions getDockerInstallInstructions(Edge edge, HttpServletRequest request) { String dockerInstallInstructions = readFile(resolveFile("docker", "instructions.md")); String baseUrl = request.getServerName(); - if (baseUrl.contains("localhost") || baseUrl.contains("127.0.0.1")) { - String localhostWarning = readFile(resolveFile("docker", "localhost_warning.md")); - dockerInstallInstructions = dockerInstallInstructions.replace("${LOCALHOST_WARNING}", localhostWarning); - dockerInstallInstructions = dockerInstallInstructions.replace("${BASE_URL}", "!!!REPLACE_ME_TO_HOST_IP_ADDRESS!!!"); + + if (DeviceConnectivityUtil.isLocalhost(baseUrl)) { + dockerInstallInstructions = dockerInstallInstructions.replace("${EXTRA_HOSTS}", "extra_hosts:\n - \"host.docker.internal:host-gateway\"\n"); + dockerInstallInstructions = dockerInstallInstructions.replace("${BASE_URL}", "host.docker.internal"); } else { - dockerInstallInstructions = dockerInstallInstructions.replace("${LOCALHOST_WARNING}", ""); + dockerInstallInstructions = dockerInstallInstructions.replace("${EXTRA_HOSTS}", ""); dockerInstallInstructions = dockerInstallInstructions.replace("${BASE_URL}", baseUrl); } String edgeVersion = appVersion + "EDGE"; diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java index 0401521a1c..4f233babf1 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java @@ -82,7 +82,6 @@ public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstruc if (edgeUpgradeInfo == null || edgeUpgradeInfo.getNextEdgeVersion() == null || tbVersion.equals(currentEdgeVersion)) { return new EdgeInstructions("Edge upgrade instruction for " + currentEdgeVersion + "EDGE is not available."); } - boolean rmUpgradeCompose = false; StringBuilder result = new StringBuilder(readFile(resolveFile("docker", "upgrade_preparing.md"))); while (edgeUpgradeInfo.getNextEdgeVersion() != null || !tbVersion.equals(currentEdgeVersion)) { String edgeVersion = edgeUpgradeInfo.getNextEdgeVersion(); @@ -93,13 +92,6 @@ public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstruc } else { ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", ""); } - if (!rmUpgradeCompose) { - rmUpgradeCompose = true; - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CLEAR_DOCKER_UPGRADE}", ""); - } else { - String rmUpgrade = readFile(resolveFile("docker", "upgrade_rm.md")); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CLEAR_DOCKER_UPGRADE}", rmUpgrade); - } ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion + "EDGE"); ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); currentEdgeVersion = edgeVersion; diff --git a/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java b/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java index c5708ce34e..63ddba1c14 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java +++ b/dao/src/main/java/org/thingsboard/server/dao/util/DeviceConnectivityUtil.java @@ -217,7 +217,7 @@ public class DeviceConnectivityUtil { return host; } - private static boolean isLocalhost(String host) { + public static boolean isLocalhost(String host) { try { InetAddress inetAddress = InetAddress.getByName(host); return inetAddress.isLoopbackAddress(); From 5a6ca7e5b63e03b438fb9bc4c47483f21cc78851 Mon Sep 17 00:00:00 2001 From: Volodymyr Babak Date: Fri, 8 Dec 2023 13:39:49 +0200 Subject: [PATCH 17/18] Rename local env to make it fix method name --- ...DefaultEdgeUpgradeInstructionsService.java | 28 +++++++++---------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java index 4f233babf1..212fe781de 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java @@ -85,18 +85,18 @@ public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstruc StringBuilder result = new StringBuilder(readFile(resolveFile("docker", "upgrade_preparing.md"))); while (edgeUpgradeInfo.getNextEdgeVersion() != null || !tbVersion.equals(currentEdgeVersion)) { String edgeVersion = edgeUpgradeInfo.getNextEdgeVersion(); - String ubuntuUpgradeInstructions = readFile(resolveFile("docker", "instructions.md")); + String dockerUpgradeInstructions = readFile(resolveFile("docker", "instructions.md")); if (edgeUpgradeInfo.isRequiresUpdateDb()) { String upgradeDb = readFile(resolveFile("docker", "upgrade_db.md")); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); + dockerUpgradeInstructions = dockerUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); } else { - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", ""); + dockerUpgradeInstructions = dockerUpgradeInstructions.replace("${UPGRADE_DB}", ""); } - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion + "EDGE"); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); + dockerUpgradeInstructions = dockerUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion + "EDGE"); + dockerUpgradeInstructions = dockerUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); currentEdgeVersion = edgeVersion; edgeUpgradeInfo = upgradeVersionHashMap.get(edgeUpgradeInfo.getNextEdgeVersion()); - result.append(ubuntuUpgradeInstructions); + result.append(dockerUpgradeInstructions); } String startService = readFile(resolveFile("docker", "start_service.md")); startService = startService.replace("${TB_EDGE_VERSION}", currentEdgeVersion + "EDGE"); @@ -114,20 +114,20 @@ public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstruc StringBuilder result = new StringBuilder(upgrade_preparing); while (edgeUpgradeInfo.getNextEdgeVersion() != null || !tbVersion.equals(currentEdgeVersion)) { String edgeVersion = edgeUpgradeInfo.getNextEdgeVersion(); - String ubuntuUpgradeInstructions = readFile(resolveFile(os, "instructions.md")); + String linuxUpgradeInstructions = readFile(resolveFile(os, "instructions.md")); if (edgeUpgradeInfo.isRequiresUpdateDb()) { String upgradeDb = readFile(resolveFile("upgrade_db.md")); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); + linuxUpgradeInstructions = linuxUpgradeInstructions.replace("${UPGRADE_DB}", upgradeDb); } else { - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", ""); + linuxUpgradeInstructions = linuxUpgradeInstructions.replace("${UPGRADE_DB}", ""); } - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_TAG}", getTagVersion(edgeVersion)); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_TAG}", getTagVersion(currentEdgeVersion)); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion); + linuxUpgradeInstructions = linuxUpgradeInstructions.replace("${TB_EDGE_TAG}", getTagVersion(edgeVersion)); + linuxUpgradeInstructions = linuxUpgradeInstructions.replace("${FROM_TB_EDGE_TAG}", getTagVersion(currentEdgeVersion)); + linuxUpgradeInstructions = linuxUpgradeInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); + linuxUpgradeInstructions = linuxUpgradeInstructions.replace("${FROM_TB_EDGE_VERSION}", currentEdgeVersion); currentEdgeVersion = edgeVersion; edgeUpgradeInfo = upgradeVersionHashMap.get(edgeUpgradeInfo.getNextEdgeVersion()); - result.append(ubuntuUpgradeInstructions); + result.append(linuxUpgradeInstructions); } String startService = readFile(resolveFile("start_service.md")); result.append(startService); From 9543cfd353fa99bf727cf19612a7e948e7166995 Mon Sep 17 00:00:00 2001 From: Volodymyr Babak Date: Fri, 8 Dec 2023 14:26:40 +0200 Subject: [PATCH 18/18] Refactoring - renaming --- .../DefaultEdgeInstallInstructionsService.java | 7 ++----- .../DefaultEdgeUpgradeInstructionsService.java | 7 ++----- .../EdgeInstallInstructionsService.java | 2 +- .../EdgeUpgradeInstructionsService.java | 2 +- .../service/update/DefaultUpdateService.java | 14 ++++++-------- 5 files changed, 12 insertions(+), 20 deletions(-) diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java index fef3c2644a..ba2cd06ee1 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeInstallInstructionsService.java @@ -16,6 +16,7 @@ package org.thingsboard.server.service.edge.instructions; import lombok.RequiredArgsConstructor; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -52,6 +53,7 @@ public class DefaultEdgeInstallInstructionsService implements EdgeInstallInstruc private boolean sslEnabled; @Value("${app.version:unknown}") + @Setter private String appVersion; @Override @@ -68,11 +70,6 @@ public class DefaultEdgeInstallInstructionsService implements EdgeInstallInstruc } } - @Override - public void updateApplicationVersion(String version) { - appVersion = version; - } - private EdgeInstructions getDockerInstallInstructions(Edge edge, HttpServletRequest request) { String dockerInstallInstructions = readFile(resolveFile("docker", "instructions.md")); String baseUrl = request.getServerName(); diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java index 212fe781de..940a813fc6 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/DefaultEdgeUpgradeInstructionsService.java @@ -16,6 +16,7 @@ package org.thingsboard.server.service.edge.instructions; import lombok.RequiredArgsConstructor; +import lombok.Setter; import lombok.extern.slf4j.Slf4j; import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; @@ -48,6 +49,7 @@ public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstruc private final InstallScripts installScripts; @Value("${app.version:unknown}") + @Setter private String appVersion; @Override @@ -65,11 +67,6 @@ public class DefaultEdgeUpgradeInstructionsService implements EdgeUpgradeInstruc } } - @Override - public void updateApplicationVersion(String version) { - appVersion = version; - } - @Override public void updateInstructionMap(Map map) { for (String key : map.keySet()) { diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java index 2c1ef4fe87..9df1f86e86 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeInstallInstructionsService.java @@ -24,5 +24,5 @@ public interface EdgeInstallInstructionsService { EdgeInstructions getInstallInstructions(Edge edge, String installationMethod, HttpServletRequest request); - void updateApplicationVersion(String version); + void setAppVersion(String version); } diff --git a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java index e6f5cb0753..be40bc6c89 100644 --- a/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java +++ b/application/src/main/java/org/thingsboard/server/service/edge/instructions/EdgeUpgradeInstructionsService.java @@ -26,5 +26,5 @@ public interface EdgeUpgradeInstructionsService { void updateInstructionMap(Map upgradeVersions); - void updateApplicationVersion(String version); + void setAppVersion(String version); } diff --git a/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java b/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java index 6a86d022df..48a4187efc 100644 --- a/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java +++ b/application/src/main/java/org/thingsboard/server/service/update/DefaultUpdateService.java @@ -155,15 +155,13 @@ public class DefaultUpdateService implements UpdateService { .build()); } ObjectNode edgeRequest = JacksonUtil.newObjectNode().put(VERSION_PARAM, version); - String prevEdgeInstallVersion = edgeInstallVersion; - edgeInstallVersion = restClient.postForObject(UPDATE_SERVER_BASE_URL + "/api/v1/edge/installMapping", new HttpEntity<>(edgeRequest.toString(), headers), String.class); - if (edgeInstallVersion != null && !edgeInstallVersion.equals(prevEdgeInstallVersion)) { - edgeInstallInstructionsService.updateApplicationVersion(edgeInstallVersion); - edgeUpgradeInstructionsService.updateApplicationVersion(edgeInstallVersion); + String edgeInstallVersion = restClient.postForObject(UPDATE_SERVER_BASE_URL + "/api/v1/edge/installMapping", new HttpEntity<>(edgeRequest.toString(), headers), String.class); + if (edgeInstallVersion != null) { + edgeInstallInstructionsService.setAppVersion(edgeInstallVersion); + edgeUpgradeInstructionsService.setAppVersion(edgeInstallVersion); } - EdgeUpgradeMessage prevEdgeUpgradeMessage = edgeUpgradeMessage; - edgeUpgradeMessage = restClient.postForObject(UPDATE_SERVER_BASE_URL + "/api/v1/edge/upgradeMapping", new HttpEntity<>(edgeRequest.toString(), headers), EdgeUpgradeMessage.class); - if (edgeUpgradeMessage != null && !edgeUpgradeMessage.equals(prevEdgeUpgradeMessage)) { + EdgeUpgradeMessage edgeUpgradeMessage = restClient.postForObject(UPDATE_SERVER_BASE_URL + "/api/v1/edge/upgradeMapping", new HttpEntity<>(edgeRequest.toString(), headers), EdgeUpgradeMessage.class); + if (edgeUpgradeMessage != null) { edgeUpgradeInstructionsService.updateInstructionMap(edgeUpgradeMessage.getEdgeVersions()); } } catch (Exception e) {