From 60fb14a708304430647038c9b262afc0d052baf8 Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Thu, 23 Nov 2023 14:42:22 +0200 Subject: [PATCH] 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 | 36 +++++++++---------- 12 files changed, 56 insertions(+), 79 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); + } else { + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${UPGRADE_DB}", ""); } - if (!stoppedService) { - stoppedService = true; - String stopService = readFile(resolveFile("docker", "stop_service.md")); - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${STOP_SERVICE}", stopService); + if (!rmUpgradeCompose) { + rmUpgradeCompose = true; + ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${CLEAR_DOCKER_UPGRADE}", ""); } else { - ubuntuUpgradeInstructions = ubuntuUpgradeInstructions.replace("${STOP_SERVICE}", ""); + 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);