---
# System prepended metadata

title: Runbook - mar
tags: [Runbook]

---

# Runbook - mar

[toc]

## Account

| ID | 備註 | 
| -------- |--- |
| swae     | Ops account|


## Listening TCP Ports

* **==ss -ltp==**
![image](https://hackmd.io/_uploads/BJ2d9-kYke.png)

## Docker

:::info
**tips**
To avoid using sudo, we can add our user - swae to the docker group.
``sudo usermod -aG docker swae``
:::

* **==docker ps --no-trunc==**

| CID | IMAGE | COMMAND | PORTS | NAMES |
| -------- | -------- | -------- | -------- | -------- |
| -f83a | thingsboard/tb-postgres:3.8.1 | "start-tb.sh" | 0.0.0.0:1883->1883/tcp, :::1883->1883/tcp, 0.0.0.0:7070->7070/tcp, :::7070->7070/tcp, 0.0.0.0:5683-5688->5683-5688/udp, :::5683-5688->5683-5688/udp, 0.0.0.0:8080->9090/tcp, :::8080->9090/tcp | ce-spx-1 |
| -30bb | postgres:12 | "docker-entrypoint.sh postgres" | 0.0.0.0:5432->5432/tcp, :::5432->5432/tcp | ce-postgres-1 |
| -089a | 5giotlead/ds-center:1.0.0 | "java -jar /app.jar" | 0.0.0.0:8081->8081/tcp, :::8081->8081/tcp, 0.0.0.0:9898->9898/tcp, :::9898->9898/tcp | ce-ds-center-1 |

* **==cat /s3/spx/ce/docker-compose.yml==**

```yaml
services:
  ds-center:
    restart: always
    image: "5giotlead/ds-center:1.0.0"
    depends_on:
      - postgres
      - spx
    ports:
      - "8081:8081"
      - "9898:9898"
    environment:
      APPLICATION_WEB_PORT: 8081
      DATASOURCE_URL: jdbc:postgresql://postgres:5432/thingsboard
      SPRING_DATASOURCE_USERNAME: thingsboard
      TB_SECURE: false
      TB_HOST: localhost
      TB_WEB_PORT: 8080
      TB_MQTT_PORT: 1883
      SWAGGER_UI_ENABLE: true
      API_DOCS_ENABLE: true
      TB_MQTT_TELEMETRY_TOPIC: v1/devices/me/telemetry
      TB_MQTT_ATTRIBUTES_TOPIC: v1/devices/me/attributes
    volumes:
      - /usr/share/nginx/.spx-resource:/resource
      - ./.spx-backup:/backup
  spx:
    restart: always
    image: "thingsboard/tb-postgres:3.8.1"
    depends_on:
      - postgres
    ports:
      - "8080:9090"
      - "1883:1883"
      - "7070:7070"
      - "5683-5688:5683-5688/udp"
    environment:
      TB_QUEUE_TYPE: in-memory
      SPRING_DATASOURCE_URL: jdbc:postgresql://postgres:5432/thingsboard
      SPRING_DATASOURCE_USERNAME: thingsboard
      SPRING_DATASOURCE_PASSWORD: postgres
    volumes:
      - /s3/spx/ce/.spx-data:/data
      - /s3/spx/ce/.spx-logs:/var/log/thingsboard
      - /s3/spx/ce/.spx-extensions:/usr/share/thingsboard/extensions
  postgres:
    restart: always
    image: "postgres:12"
    ports:
      - "5432:5432"
    environment:
      POSTGRES_DB: thingsboard
      POSTGRES_PASSWORD: postgres
    volumes:
      - /s3/spx/ce/.spx-db:/var/lib/postgresql/data
```

### thingsboard/tb-postgres:3.8.1

* What is ==imageRepoDigests==? (驗證 tb-postgres:3.8.1 docker image 生產履歷)
```bash
swae@mar:/s3/spx$ docker image inspect thingsboard/tb-postgres:3.8.1
[
    {
        "Id": "sha256:40da1b617b34aa9c0a83cd9668070ffad843d54436064c68a70198743d90d68d",
        "RepoTags": [
            "thingsboard/tb-postgres:3.8.1"
        ],
        "RepoDigests": [
            "thingsboard/tb-postgres@sha256:110599d602e3fd59dd9bf9d1904e50869b7a1031b13b0dddaeecde24f5b25f7b"
        ],
```
![image](https://hackmd.io/_uploads/Bk3CFTWKkx.png)


### postgres:12

* 從 docker hub [here](https://hub.docker.com/layers/library/postgres/12/images/sha256-4bf4eb8e5932534db5fb9d3d91a212a91406aecf1fa626a60df4a9e2781d73ae) 上找不到對應的 imageRepoDigests?
```
swae@mar:/s3/spx/ce$ docker image inspect postgres:12
[
    {
        "Id": "sha256:0d56839ac0df4898dcfe760cb23e5f726dd62585d602385ed15bd16359f13089",
        "RepoTags": [
            "postgres:12"
        ],
        "RepoDigests": [
            "postgres@sha256:700c7af8b2275809b78a6e2f647b12f1ad0dee634cf48219fd31a3e4391742d9"
        ],
```

### 5giotlead/ds-center:1.0.0

* [GitHub](https://github.com/5giotlead/5s-ds-center)
  * How to generate ds-center:1.0.0 docker image from the source?
  * How to maintain source code?

### Upgrade (待確認 ...)

:::info
**tips**
``docker compose down`` 與 ``docker compose stop``，有何差別?
``docker compose stop``只會停止正在運行的 containers，但不會移除。反之，``docker compose down`` 會停止並且移除所有 docker-compose.yml 中 service 用到的container，且 container 用到的 network default network 也都一併會被移除。--- 此部分需再深入理解，避免錯用。
``docker compose down -v`` (連 volumes 也一併移除，例如想要清掉測試用 DB 掛載的資料。)
:::

* Working directory
```bash
swae@mar:/s3/spx/ce$ ls -al
total 438726
drwxr-xr-x  7 swae             swae         11 Feb  6 05:39 .
drwxr-xr-x  4 swae             swae          4 Dec 10 06:32 ..
-rw-r--r--  1 root             root 1891450880 Dec  4 06:31 backup_1204.tar
-rwxr-xr-x  1 swae             swae         35 Dec  4 06:30 backup.sh
-rw-r--r--  1 swae             swae       1519 Dec 10 06:34 docker-compose.yml
drwxr-xr-x  3 root             root          3 Dec 30 05:06 .spx-backup
drwxr-xr-x  3              799  799          6 Dec 10 06:40 .spx-data
drwx------ 19 systemd-coredump root         26 Dec 10 06:34 .spx-db
drwxr-xr-x  2              799  799          2 Nov 28  2023 .spx-extensions
drwxr-xr-x  2              799  799         45 Feb  6 00:00 .spx-logs
-rwxr-xr-x  1 swae             swae        154 Dec  4 06:32 upgrade.sh
```
* Backup all folders (.spx-backup, .spx-data, .spx-db, .spx-extensions, .spx-logs) into one tarball - **backup_1204.tar** 
```
./backup.sh 1204
```
* Update docker-compose.yml for tb-postgres image version (e.g. 3.8.1 > 3.9.0) ``./upgrade.sh 3.8.1``
```
docker compose stop
docker run -it -v /s3/spx/ce/.spx-data:/data --rm thingsboard/tb-postgres:$1 upgrade-tb.sh
docker compose rm spx
docker compose up -d
```
* Run lazydocker 觀察所有 container 運行是否一切正常無誤
```
lazydocker
```

## Nginx

* 靜態資源上傳後透過 Nginx host，預設圖片需手動複製到靜態資源存放路徑
```
sudo nano /etc/nginx/conf.d/ds_resource.conf
sudo systemctl restart nginx
sudo nano /etc/nginx/conf.d/ds_resource.conf
sudo systemctl restart nginx
```

## Simulator (/s3/spx/simulator)

* [Github](https://github.com/5giotlead/ws-fnb-simulator)







## References