From 2c0056aa101fa172ed4c2384343be416df55717a Mon Sep 17 00:00:00 2001
From: Dmitry Puzyrkov <dmitry.puzyrkov@demlabs.net>
Date: Thu, 18 May 2023 13:58:31 +0000
Subject: [PATCH] Feature updater service

---
 .gitlab-ci.yml                             |  9 +++++
 CMakeLists.txt                             | 16 +++++++-
 dist.linux/share/cellframe-updater.service | 14 +++++++
 dist.linux/share/cellframe-updater.timer   |  4 ++
 dist.linux/share/update.sh                 | 47 ++++++++++++++++++++++
 os/debian/postinst                         | 18 ++++++++-
 os/debian/postrm                           |  3 ++
 version.mk                                 |  2 +-
 8 files changed, 109 insertions(+), 4 deletions(-)
 create mode 100644 dist.linux/share/cellframe-updater.service
 create mode 100644 dist.linux/share/cellframe-updater.timer
 create mode 100755 dist.linux/share/update.sh

diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml
index d0637a534..e6b8632d3 100644
--- a/.gitlab-ci.yml
+++ b/.gitlab-ci.yml
@@ -74,6 +74,14 @@ linux-amd64-debian-buster-rwd:
       - ./prod_build/build.sh --target linux rwd -DBUILD_WITH_PYTHON_ENV=ON -DBUILD_DIAGTOOL=ON
       - ./prod_build/pack.sh --target linux rwd
 
+linux-amd64-debian-buster-updtr:
+    extends: .build
+    image: demlabs/amd64/debian-buster:linuxbuilder
+    before_script: /opt/buildtools/prepare_environment.sh amd64-linux
+    script:
+      - ./prod_build/build.sh --target linux release -DBUILD_WITH_PYTHON_ENV=ON -DBUILD_DIAGTOOL=ON -DADD_UPDATER=ON
+      - ./prod_build/pack.sh --target linux release
+
 linux-amd64-debian-buster:
     extends: .build
     image: demlabs/amd64/debian-buster:linuxbuilder
@@ -108,6 +116,7 @@ publish:
     - linux-armhf-debian-bullseye 
     - linux-arm64-debian-bullseye 
     - linux-amd64-debian-buster-rwd
+    - linux-amd64-debian-buster-updtr
     - macos-amd64
    
   script: 
diff --git a/CMakeLists.txt b/CMakeLists.txt
index 2a1d256bb..cef610dae 100644
--- a/CMakeLists.txt
+++ b/CMakeLists.txt
@@ -42,7 +42,7 @@ set(BUILD_WITH_GDB_DRIVER_MDBX ON)
 
 set(BUILD_WITH_ZIP OFF)
 option(SUPPORT_PYTHON_PLUGINS DEFAULT OFF)
-
+option(ADD_UPDATER OFF)
 #if(MACOS)
 #    if(SUPPORT_PYTHON_PLUGINS)
 #        set(BUILD_WITH_PYTHON_ENV ON)
@@ -134,6 +134,12 @@ if(UNIX)
     if(CMAKE_BUILD_TYPE MATCHES RelWithDebInfo)
         SET( CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}-rwd" )
     endif()
+
+    if(ADD_UPDATER)
+        SET( CPACK_PACKAGE_VERSION "${CPACK_PACKAGE_VERSION}-updtr" )
+    endif()
+
+
     SET( CPACK_DEBIAN_PACKAGE_MAINTAINER "Dmitriy Gerasimov <naeper@demlabs.net>" )
     SET( CPACK_DEBIAN_PACKAGE_DESCRIPTION_SUMMARY "CellFrame Node software https://cellframe.net" )
     SET( CPACK_DEBIAN_PACKAGE_DESCRIPTION   "CellFrame Node" )
@@ -322,7 +328,13 @@ else()
 
     INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dist/ DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*"  PATTERN "*")
     if(LINUX)
-	    INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dist.linux/ DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*"  PATTERN "*")
+	    
+
+        if(NOT ADD_UPDATER)
+            INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dist.linux/ DESTINATION ${CMAKE_INSTALL_PREFIX} PATTERN "*cellframe-updater*" EXCLUDE )
+        else()
+            INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dist.linux/ DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*"  PATTERN "*")
+        endif()
     elseif(BSD)
 	    INSTALL(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR}/dist.bsd/ DESTINATION ${CMAKE_INSTALL_PREFIX} FILES_MATCHING PATTERN "*"  PATTERN "*")
     endif()
diff --git a/dist.linux/share/cellframe-updater.service b/dist.linux/share/cellframe-updater.service
new file mode 100644
index 000000000..a1f119ebe
--- /dev/null
+++ b/dist.linux/share/cellframe-updater.service
@@ -0,0 +1,14 @@
+[Unit]
+Description=Cellframe Updater
+After=network.target
+
+[Service]
+WorkingDirectory=/opt/cellframe-node/
+ExecStart=/bin/bash /opt/cellframe-node/share/update.sh &
+
+User=root
+Group=root
+LogNamespace=cellframe
+
+[Install]
+WantedBy=timers.target
\ No newline at end of file
diff --git a/dist.linux/share/cellframe-updater.timer b/dist.linux/share/cellframe-updater.timer
new file mode 100644
index 000000000..c30ec758e
--- /dev/null
+++ b/dist.linux/share/cellframe-updater.timer
@@ -0,0 +1,4 @@
+[Timer]
+OnCalendar=0/2:00:00
+Persistent=true
+unit=cellframe-updater
\ No newline at end of file
diff --git a/dist.linux/share/update.sh b/dist.linux/share/update.sh
new file mode 100755
index 000000000..6c49a0d87
--- /dev/null
+++ b/dist.linux/share/update.sh
@@ -0,0 +1,47 @@
+ #! /bin/bash
+set -e
+STORAGE_URL=https://pub.cellframe.net/linux/cellframe-node/release-5.2/updates
+REGEXP="href=\"cellframe-node-([0-9].[0-9]-[0-9]+)-updtr-amd64.deb" 
+INSTALLED_VERSION=$(dpkg -l | awk '$2=="cellframe-node" { print $3 }')
+
+echo "Cellframe-node installed: $INSTALLED_VERSION"
+
+
+AVAILABLE_VERSIONS=()
+for l in $(wget -qO- $STORAGE_URL)
+do
+    if [[ $l =~ $REGEXP ]]
+    then
+        AVAILABLE_VERSIONS+=(${BASH_REMATCH[1]})
+    fi
+done
+
+echo "Available versions: ${AVAILABLE_VERSIONS[@]}"
+
+REBUILDS=()
+for l in ${AVAILABLE_VERSIONS[@]}
+do
+    REBUILDS+=($(echo $l ))
+done
+
+MAX_REBUILD=( $( printf "%s\n"  "${AVAILABLE_VERSIONS[@]}" | cut -c 5-10 | sort -nr ) )
+CURRENT_REBUILD=$(echo $INSTALLED_VERSION | grep -Po "[0-9]([0-9]+)")
+
+echo "Available patch: $MAX_REBUILD | Current patch: $CURRENT_REBUILD"
+
+if (( MAX_REBUILD > CURRENT_REBUILD )); then
+    echo "Need update cellframe-node to 5.2-$MAX_REBUILD..."
+else
+    echo "No need to update cellframe-node"
+    exit 0
+fi
+
+PACKAGE_NAME="cellframe-node-5.2-$MAX_REBUILD-updtr-amd64.deb"
+echo "wget"
+mkdir -p /tmp/cfupd/
+wget $STORAGE_URL/$PACKAGE_NAME -O /tmp/cfupd/$PACKAGE_NAME
+echo "wgot"
+
+service cellframe-node stop
+dpkg -i /tmp/cfupd/$PACKAGE_NAME
+service cellframe-node start
\ No newline at end of file
diff --git a/os/debian/postinst b/os/debian/postinst
index 244613ab2..ec1c99723 100755
--- a/os/debian/postinst
+++ b/os/debian/postinst
@@ -260,7 +260,23 @@ else
             systemctl --system enable $DAP_PREFIX/share/cellframe-diagtool.service || true
         fi
     fi
-
+    #updater service
+    if [ -f "$DAP_PREFIX/share/update.sh" ]; then
+        if [ -e /etc/systemd/system/cellframe-updater.service ]; then
+            echo "[*] Restarting cellframe-updater service"
+            systemctl --system stop cellframe-updater  >> /dev/null|| true
+            echo "[*] Stopped cellframe-updater"
+            systemctl daemon-reload || true
+            systemctl --system start cellframe-updater || true
+            echo "[*] Started cellframe-diagtool"
+        else
+            echo "[!] Installing cellframe-updater as systemd service"
+            ln -sf $DAP_PREFIX/share/cellframe-updater.service /etc/systemd/system/cellframe-updater.service || true
+            ln -sf $DAP_PREFIX/share/cellframe-updater.timer /etc/systemd/system/cellframe-updater.timer || true
+            systemctl --system enable $DAP_PREFIX/share/cellframe-updater.service || true
+            systemctl start cellframe-updater.timer
+        fi
+    fi
     chmod 0666 $DAP_CFG
     chmod 0666 $DAP_CFG_TPL
     chmod 0774 $DAP_PREFIX/bin/* || true
diff --git a/os/debian/postrm b/os/debian/postrm
index d95f25eb2..fd929100a 100755
--- a/os/debian/postrm
+++ b/os/debian/postrm
@@ -8,4 +8,7 @@ GROUP_RM="cellframe-node"
 systemctl stop cellframe-node || true
 systemctl disable cellframe-node || true
 
+rm  /etc/systemd/system/cellframe-updater.service || true
+rm  /etc/systemd/system/cellframe-updater.timer || true
+        
 #this file will be merged to dashboard postinst, be aware of this
diff --git a/version.mk b/version.mk
index c4ef53f46..d8712e8a4 100644
--- a/version.mk
+++ b/version.mk
@@ -1,3 +1,3 @@
 VERSION_MAJOR=5
 VERSION_MINOR=2
-VERSION_PATCH=131
+VERSION_PATCH=133
-- 
GitLab