From 400752615552ba660505f9cff6836ca2394b1d5e Mon Sep 17 00:00:00 2001 From: Andrii Landiak Date: Tue, 12 Sep 2023 15:39:57 +0300 Subject: [PATCH] Add init edge instructions for ubuntu and centos --- .../centos/instructions.md | 197 ++++++++++++++++++ .../docker/instructions.md | 4 +- .../ubuntu/instructions.md | 125 +++++++++++ .../server/controller/EdgeController.java | 6 +- .../DefaultEdgeInstallService.java | 50 ++++- .../edge/instructions/EdgeInstallService.java | 3 +- .../data/edge/EdgeInstallInstructions.java | 4 +- 7 files changed, 372 insertions(+), 17 deletions(-) create mode 100644 application/src/main/data/json/edge/install_instructions/centos/instructions.md create mode 100644 application/src/main/data/json/edge/install_instructions/ubuntu/instructions.md diff --git a/application/src/main/data/json/edge/install_instructions/centos/instructions.md b/application/src/main/data/json/edge/install_instructions/centos/instructions.md new file mode 100644 index 0000000000..d42ce4499e --- /dev/null +++ b/application/src/main/data/json/edge/install_instructions/centos/instructions.md @@ -0,0 +1,197 @@ +## Install ThingsBoard Edge on CentOS/RHEL Server + +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. + +### Prerequisites +Before continue to installation execute the following commands in order to install necessary tools: + +```bash +sudo yum install -y nano wget +sudo yum install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-7.noarch.rpm +``` + +### Install Java 11 (OpenJDK) +ThingsBoard service is running on Java 11. Follow this instructions to install OpenJDK 11: + +```bash +sudo yum install java-11-openjdk +``` +{: .copy-code} + +Please don't forget to configure your operating system to use OpenJDK 11 by default. +You can configure which version is the default using the following command: + +```bash +sudo update-alternatives --config java +``` +{: .copy-code} + +You can check the installation using the following command: + +```bash +java -version +``` +{: .copy-code} + +Expected command output is: + +```text +openjdk version "11.0.xx" +OpenJDK Runtime Environment (...) +OpenJDK 64-Bit Server VM (build ...) +``` + +### Configure PostgreSQL +ThingsBoard Edge uses PostgreSQL database as a local storage. +Instructions listed below will help you to install PostgreSQL. + +```bash +# Update your system +sudo yum update +``` +{: .copy-code} + +**For CentOS 7:** + +```bash +# Install the repository RPM (for CentOS 7): +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 +# Initialize your PostgreSQL DB +sudo /usr/pgsql-12/bin/postgresql-12-setup initdb +sudo systemctl start postgresql-12 +# Optional: Configure PostgreSQL to start on boot +sudo systemctl enable --now postgresql-12 + +``` +{: .copy-code} + +**For CentOS 8:** + +```bash +# Install the repository RPM (for CentOS 8): +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 +# Initialize your PostgreSQL DB +sudo /usr/pgsql-12/bin/postgresql-12-setup initdb +sudo systemctl start postgresql-12 +# Optional: Configure PostgreSQL to start on boot +sudo systemctl enable --now postgresql-12 + +``` +{: .copy-code} + +Once PostgreSQL is installed you may want to create a new user or set the password for the the main user. +The instructions below will help to set the password for main postgresql user + +```text +sudo su - postgres +psql +\password +\q +``` + +Then, press "Ctrl+D" to return to main user console. + +After configuring the password, edit the pg_hba.conf to use MD5 authentication with the postgres user. + +Edit pg_hba.conf file: + +```bash +sudo nano /var/lib/pgsql/12/data/pg_hba.conf +``` +{: .copy-code} + +Locate the following lines: + +```text +# IPv4 local connections: +host all all 127.0.0.1/32 ident +``` + +Replace `ident` with `md5`: + +```text +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 +``` +{: .copy-code} + +Connect to the database to create ThingsBoard Edge DB: + +```bash +psql -U postgres -d postgres -h 127.0.0.1 -W +``` +{: .copy-code} + +Execute create database statement + +```bash +CREATE DATABASE tb_edge; +\q +``` +{: .copy-code} + +### ThingsBoard Edge service installation +Download installation package. + +```bash +wget https://github.com/thingsboard/thingsboard-edge/releases/download/{{ site.release.edge_tag }}/tb-edge-{{ site.release.edge_ver }}.rpm +``` +{: .copy-code} + +Go to the download repository and install ThingsBoard Edge service + +```bash +sudo rpm -Uvh tb-edge-${TB_EDGE_VERSION}.rpm +``` +{: .copy-code} + +### Configure ThingsBoard Edge + + +#### [Optional] Update bind ports +If ThingsBoard Edge is going to be running on the same machine where ThingsBoard server (cloud) is running, you'll need to update docker compose port mapping to avoid port collision between ThingsBoard server and ThingsBoard Edge. + +Please update next lines of `docker-compose.yml` file: + +```bash +ports: + - "18080:8080" + - "11883:1883" + - "15683-15688:5683-5688/udp" +``` +Make sure that ports above (18080, 11883, 15683-15688) are not used by any other application. + +#### Run installation script +Once ThingsBoard Edge is installed and configured please execute the following install script: + +```bash +sudo /usr/share/tb-edge/bin/install/install.sh +``` +{: .copy-code} + +### Restart ThingsBoard Edge service + +```bash +sudo service tb-edge restart +``` + +#### Open ThingsBoard Edge UI + +Once started, you will be able to open **ThingsBoard Edge UI** using the following link http://localhost:8080. + +###### NOTE: Edge HTTP bind port update + +Use next **ThingsBoard Edge UI** link **http://localhost:18080** if you updated HTTP 8080 bind port to **18080**. + diff --git a/application/src/main/data/json/edge/install_instructions/docker/instructions.md b/application/src/main/data/json/edge/install_instructions/docker/instructions.md index 6b93447851..14307dbea7 100644 --- a/application/src/main/data/json/edge/install_instructions/docker/instructions.md +++ b/application/src/main/data/json/edge/install_instructions/docker/instructions.md @@ -1,6 +1,6 @@ -## Install ThingsBoard Edge and connect to cloud instructions +## Install ThingsBoard Edge using Docker -Here is the list of commands, that can be used to quickly install and connect ThingsBoard Edge to the cloud using docker compose. +Here is the list of commands, that can be used to quickly install ThingsBoard Edge using docker compose and connect to the cloud. ### Prerequisites diff --git a/application/src/main/data/json/edge/install_instructions/ubuntu/instructions.md b/application/src/main/data/json/edge/install_instructions/ubuntu/instructions.md new file mode 100644 index 0000000000..58df993bfa --- /dev/null +++ b/application/src/main/data/json/edge/install_instructions/ubuntu/instructions.md @@ -0,0 +1,125 @@ +## Install ThingsBoard Edge on Ubuntu Server + +Here is the list of commands, that can be used to quickly install ThingsBoard Edge on Ubuntu Server and connect to the cloud. + +### Install Java 11 (OpenJDK) +ThingsBoard service is running on Java 11. Follow this instructions to install OpenJDK 11: + +```bash +sudo apt update +sudo apt install openjdk-11-jdk +``` +{: .copy-code} + +Please don't forget to configure your operating system to use OpenJDK 11 by default. +You can configure which version is the default using the following command: + +```bash +sudo update-alternatives --config java +``` +{: .copy-code} + +You can check the installation using the following command: + +```bash +java -version +``` +{: .copy-code} + +Expected command output is: + +```text +openjdk version "11.0.xx" +OpenJDK Runtime Environment (...) +OpenJDK 64-Bit Server VM (build ...) +``` + +### Configure PostgreSQL +ThingsBoard Edge uses PostgreSQL database as a local storage. +Instructions listed below will help you to install PostgreSQL. + +```bash +# install **wget** if not already installed: +sudo apt install -y wget + +# import the repository signing key: +wget --quiet -O - https://www.postgresql.org/media/keys/ACCC4CF8.asc | sudo apt-key add - + +# add repository contents to your system: +RELEASE=$(lsb_release -cs) +echo "deb http://apt.postgresql.org/pub/repos/apt/ ${RELEASE}"-pgdg main | sudo tee /etc/apt/sources.list.d/pgdg.list + +# install and launch the postgresql service: +sudo apt update +sudo apt -y install postgresql-12 +sudo service postgresql start +``` +{: .copy-code} + +Once PostgreSQL is installed you may want to create a new user or set the password for the the main user. +The instructions below will help to set the password for main postgresql user + +```text +sudo su - postgres +psql +\password +\q +``` + +Then, press “Ctrl+D” to return to main user console and connect to the database to create ThingsBoard Edge DB: + +```text +psql -U postgres -d postgres -h 127.0.0.1 -W +CREATE DATABASE tb_edge; +\q +``` + +### Thingsboard Edge service installation +Download installation package. + +```bash +wget https://github.com/thingsboard/thingsboard-edge/releases/download/v${TB_EDGE_VERSION}/tb-edge-${TB_EDGE_VERSION}.deb +``` +{: .copy-code} + +Go to the download repository and install ThingsBoard Edge service + +```bash +sudo dpkg -i tb-edge-{TB_EDGE_VERSION}.deb +{:copy-code} +``` + +Edit ThingsBoard Edge configuration file + +```bash +sudo sh -c 'cat <> /etc/tb-edge/conf/tb-edge.conf +export HTTP_BIND_PORT=18080 +export MQTT_BIND_PORT=11883 +export COAP_BIND_PORT=15683 +export LWM2M_ENABLED=false +EOL' +{:copy-code} +``` + +#### Run installation script + +Once ThingsBoard Edge is installed and configured please execute the following install script: + +```bash +sudo /usr/share/tb-edge/bin/install/install.sh +``` + +#### Restart ThingsBoard Edge service + +```bash +sudo service tb-edge restart +``` + +#### Open ThingsBoard Edge UI + +Once started, you will be able to open **ThingsBoard Edge UI** using the following link http://localhost:8080. + +###### NOTE: Edge HTTP bind port update + +Use next **ThingsBoard Edge UI** link **http://localhost:18080** if you updated HTTP 8080 bind port to **18080**. + 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 a50f9c7658..2bc9c0e128 100644 --- a/application/src/main/java/org/thingsboard/server/controller/EdgeController.java +++ b/application/src/main/java/org/thingsboard/server/controller/EdgeController.java @@ -557,17 +557,19 @@ 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/{edgeId}", method = RequestMethod.GET) + @RequestMapping(value = "/edge/instructions/{edgeId}/{method}", method = RequestMethod.GET) @ResponseBody public EdgeInstallInstructions getEdgeDockerInstallInstructions( @ApiParam(value = EDGE_ID_PARAM_DESCRIPTION, required = true) @PathVariable("edgeId") String strEdgeId, + @ApiParam(value = "Installation method (e.g., 'docker', 'ubuntu', 'centos')") + @PathVariable("method") String installationMethod, HttpServletRequest request) throws ThingsboardException { if (isEdgesEnabled() && edgeInstallServiceOpt.isPresent()) { EdgeId edgeId = new EdgeId(toUUID(strEdgeId)); edgeId = checkNotNull(edgeId); Edge edge = checkEdgeId(edgeId, Operation.READ); - return checkNotNull(edgeInstallServiceOpt.get().getDockerInstallInstructions(getTenantId(), edge, request)); + return checkNotNull(edgeInstallServiceOpt.get().getInstallInstructions(getTenantId(), edge, installationMethod, request)); } 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 2c4a14635d..455b3e657a 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 @@ -28,7 +28,6 @@ import org.thingsboard.server.service.install.InstallScripts; import javax.servlet.http.HttpServletRequest; import java.io.IOException; -import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; @@ -56,7 +55,20 @@ public class DefaultEdgeInstallService implements EdgeInstallService { private String appVersion; @Override - public EdgeInstallInstructions getDockerInstallInstructions(TenantId tenantId, Edge edge, HttpServletRequest request) { + public EdgeInstallInstructions getInstallInstructions(TenantId tenantId, Edge edge, String installationMethod, HttpServletRequest request) { + switch (installationMethod.toLowerCase()) { + case "docker": + return getDockerInstallInstructions(edge, request); + case "ubuntu": + return getUbuntuInstallInstructions(edge, request); + case "centos": + return getCentosInstallInstructions(edge, request); + default: + throw new IllegalArgumentException("Unsupported installation method for Edge: " + installationMethod); + } + } + + private EdgeInstallInstructions 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")) { @@ -67,19 +79,39 @@ public class DefaultEdgeInstallService implements EdgeInstallService { dockerInstallInstructions = dockerInstallInstructions.replace("${LOCALHOST_WARNING}", ""); dockerInstallInstructions = dockerInstallInstructions.replace("${BASE_URL}", baseUrl); } + dockerInstallInstructions = replacePlaceholders(dockerInstallInstructions, edge); + return new EdgeInstallInstructions(dockerInstallInstructions); + } + + private EdgeInstallInstructions getUbuntuInstallInstructions(Edge edge, HttpServletRequest request) { + String ubuntuInstallInstructions = readFile(resolveFile("ubuntu", "instructions.md")); + ubuntuInstallInstructions = replacePlaceholders(ubuntuInstallInstructions, edge); + ubuntuInstallInstructions = ubuntuInstallInstructions.replace("${BASE_URL}", request.getServerName()); + return new EdgeInstallInstructions(ubuntuInstallInstructions); + } + + + private EdgeInstallInstructions getCentosInstallInstructions(Edge edge, HttpServletRequest request) { + String centosInstallInstructions = readFile(resolveFile("centos", "instructions.md")); + centosInstallInstructions = replacePlaceholders(centosInstallInstructions, edge); + centosInstallInstructions = centosInstallInstructions.replace("${BASE_URL}", request.getServerName()); + return new EdgeInstallInstructions(centosInstallInstructions); + } + + private String replacePlaceholders(String instructions, Edge edge) { String edgeVersion = appVersion + "EDGE"; edgeVersion = edgeVersion.replace("-SNAPSHOT", ""); - dockerInstallInstructions = dockerInstallInstructions.replace("${TB_EDGE_VERSION}", edgeVersion); - dockerInstallInstructions = dockerInstallInstructions.replace("${CLOUD_ROUTING_KEY}", edge.getRoutingKey()); - dockerInstallInstructions = dockerInstallInstructions.replace("${CLOUD_ROUTING_SECRET}", edge.getSecret()); - dockerInstallInstructions = dockerInstallInstructions.replace("${CLOUD_RPC_PORT}", Integer.toString(rpcPort)); - dockerInstallInstructions = dockerInstallInstructions.replace("${CLOUD_RPC_SSL_ENABLED}", Boolean.toString(sslEnabled)); - return new EdgeInstallInstructions(dockerInstallInstructions); + instructions = instructions.replace("${TB_EDGE_VERSION}", edgeVersion); + instructions = instructions.replace("${CLOUD_ROUTING_KEY}", edge.getRoutingKey()); + instructions = instructions.replace("${CLOUD_ROUTING_SECRET}", edge.getSecret()); + instructions = instructions.replace("${CLOUD_RPC_PORT}", Integer.toString(rpcPort)); + instructions = instructions.replace("${CLOUD_RPC_SSL_ENABLED}", Boolean.toString(sslEnabled)); + return instructions; } private String readFile(Path file) { try { - return new String(Files.readAllBytes(file), StandardCharsets.UTF_8); + return Files.readString(file); } catch (IOException e) { log.warn("Failed to read file: {}", file, e); throw new RuntimeException(e); 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 2a030fc4fa..20cac25e33 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 @@ -23,6 +23,5 @@ import javax.servlet.http.HttpServletRequest; public interface EdgeInstallService { - EdgeInstallInstructions getDockerInstallInstructions(TenantId tenantId, Edge edge, HttpServletRequest request); - + EdgeInstallInstructions getInstallInstructions(TenantId tenantId, Edge edge, String installationMethod, HttpServletRequest request); } 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/EdgeInstallInstructions.java index acfc458906..8343058250 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/EdgeInstallInstructions.java @@ -27,6 +27,6 @@ import lombok.NoArgsConstructor; @NoArgsConstructor public class EdgeInstallInstructions { - @ApiModelProperty(position = 1, value = "Markdown with docker install instructions") - private String dockerInstallInstructions; + @ApiModelProperty(position = 1, value = "Markdown with install instructions") + private String installInstructions; }