From 0f7ede5eed8f17745e5a90bb0ab6ce81813a7cff Mon Sep 17 00:00:00 2001 From: sh <37271604+shumvgolove@users.noreply.github.com> Date: Fri, 12 May 2023 12:45:04 +0300 Subject: [PATCH] docker+scripts: XFTP servers installation, update docs, install scripts (#750) * docker: major overhaul with xftp support * github: update docker actions * install: add installation scripts * readme: update readme to reflect latest changes --- .github/workflows/docker-image.yml | 12 +- build.Dockerfile => Dockerfile | 38 +++-- README.md | 108 ++++++++---- download.Dockerfile | 22 --- install.sh | 141 +++++++++++++++ .../{entrypoint => entrypoint-smp-server} | 21 ++- scripts/docker/entrypoint-xftp-server | 35 ++++ scripts/main/simplex-servers-uninstall | 18 ++ scripts/main/simplex-servers-update | 160 ++++++++++++++++++ scripts/main/smp-server.service | 15 ++ scripts/main/xftp-server.service | 16 ++ 11 files changed, 505 insertions(+), 81 deletions(-) rename build.Dockerfile => Dockerfile (53%) delete mode 100644 download.Dockerfile create mode 100755 install.sh rename scripts/docker/{entrypoint => entrypoint-smp-server} (59%) create mode 100755 scripts/docker/entrypoint-xftp-server create mode 100755 scripts/main/simplex-servers-uninstall create mode 100755 scripts/main/simplex-servers-update create mode 100644 scripts/main/smp-server.service create mode 100644 scripts/main/xftp-server.service diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index a17839407..e1f4c87f1 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -13,8 +13,10 @@ jobs: fail-fast: false matrix: include: - - application: smp-server - dockerfile: build.Dockerfile + - app: smp-server + app_port: 5223 + - app: xftp-server + app_port: 443 steps: - name: Clone project uses: actions/checkout@v3 @@ -29,7 +31,7 @@ jobs: id: meta uses: docker/metadata-action@v4 with: - images: ${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.application }} + images: ${{ secrets.DOCKERHUB_USERNAME }}/${{ matrix.app }} flavor: | latest=auto tags: | @@ -41,6 +43,8 @@ jobs: uses: docker/build-push-action@v4 with: push: true - file: ${{ matrix.dockerfile }} + build-args: | + APP=${{ matrix.app }} + APP_PORT=${{ matrix.app_port }} tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} diff --git a/build.Dockerfile b/Dockerfile similarity index 53% rename from build.Dockerfile rename to Dockerfile index ff996ca0f..1a2aabf88 100644 --- a/build.Dockerfile +++ b/Dockerfile @@ -4,7 +4,7 @@ FROM ubuntu:${TAG} AS build ### Build stage -# Install curl and git and smp-related dependencies +# Install curl and git and simplexmq dependencies RUN apt-get update && apt-get install -y curl git build-essential libgmp3-dev zlib1g-dev llvm-12 llvm-12-dev libnuma-dev # Specify bootstrap Haskell versions @@ -24,33 +24,39 @@ RUN ghcup set ghc "${BOOTSTRAP_HASKELL_GHC_VERSION}" && \ COPY . /project WORKDIR /project -# Compile smp-server +ARG APP +ARG APP_PORT +RUN if [ -z "$APP" ] || [ -z "$APP_PORT" ]; then printf "Please spcify \$APP and \$APP_PORT build-arg.\n"; exit 1; fi + +# Compile app RUN cabal update -RUN cabal build exe:smp-server +RUN cabal build exe:$APP + +# Create new path containing all files needed +RUN mkdir /final +WORKDIR /final # Strip the binary from debug symbols to reduce size -RUN smp=$(find ./dist-newstyle -name "smp-server" -type f -executable) && \ - mv "$smp" ./ && \ - strip ./smp-server +RUN bin=$(find /project/dist-newstyle -name "$APP" -type f -executable) && \ + mv "$bin" ./ && \ + strip ./"$APP" &&\ + mv /project/scripts/docker/entrypoint-"$APP" ./entrypoint ### Final stage - FROM ubuntu:${TAG} # Install OpenSSL dependency RUN apt-get update && apt-get install -y openssl libnuma-dev -# Copy compiled smp-server from build stage -COPY --from=build /project/smp-server /usr/bin/smp-server +# Copy compiled app from build stage +COPY --from=build /final /usr/local/bin/ -# Copy our helper script -COPY ./scripts/docker/entrypoint /usr/bin/entrypoint +# Open app listening port +ARG APP_PORT +EXPOSE $APP_PORT -# Open smp-server listening port -EXPOSE 5223 - -# SimpleX requires using SIGINT to correctly preserve undelivered messages and restore them on restart +# simplexmq requires using SIGINT to correctly preserve undelivered messages and restore them on restart STOPSIGNAL SIGINT # Finally, execute helper script -ENTRYPOINT [ "/usr/bin/entrypoint" ] +ENTRYPOINT [ "/usr/local/bin/entrypoint" ] diff --git a/README.md b/README.md index ec40182de..eb850aaf1 100644 --- a/README.md +++ b/README.md @@ -90,11 +90,11 @@ You can either run your own SMP server locally or deploy using [Linode StackScri It's the easiest to try SMP agent via a prototype [simplex-chat](https://github.com/simplex-chat/simplex-chat) terminal UI. -## Deploy SMP server on Linux +## Deploy SMP/XFTP servers on Linux -You can run your SMP server as a Linux process, optionally using a service manager for booting and restarts. +You can run your SMP/XFTP server as a Linux process, optionally using a service manager for booting and restarts. -Notice that `smp-server` requires `openssl` as run-time dependency (it is used to generate server certificates during initialization). Install it with your packet manager: +Notice that `smp-server` and `xftp-server` requires `openssl` as run-time dependency (it is used to generate server certificates during initialization). Install it with your packet manager: ```sh # For Ubuntu @@ -105,28 +105,53 @@ apt update && apt install openssl #### Using Docker -On Linux, you can deploy smp server using Docker. This will download image from [Docker Hub](https://hub.docker.com/r/simplexchat/smp-server). +On Linux, you can deploy smp and xftp server using Docker. This will download image from [Docker Hub](https://hub.docker.com/r/simplexchat). -1. Create `config` and `logs` directories: +1. Create directories for persistent Docker configuration: ```sh - mkdir -p ~/simplex/{config,logs} + mkdir -p $HOME/simplex/{xftp,smp}/{config,logs} && mkdir -p $HOME/simplex/xftp/files ``` -2. Run your Docker container. You must change **your_ip_or_domain**. `-e "pass=password"` is optional variable to password-protect your `smp` server: - ```sh - docker run -d \ - -e "addr=your_ip_or_domain" \ - -e "pass=password" \ - -p 5223:5223 \ - -v $HOME/simplex/config:/etc/opt/simplex:z \ - -v $HOME/simplex/logs:/var/opt/simplex:z \ - simplexchat/smp-server:latest - ``` +2. Run your Docker container. -#### Ubuntu + - `smp-server` + + You must change **your_ip_or_domain**. `-e "pass=password"` is optional variable to password-protect your `smp` server: + ```sh + docker run -d \ + -e "ADDR=your_ip_or_domain" \ + -e "PASS=password" \ + -p 5223:5223 \ + -v $HOME/simplex/smp/config:/etc/opt/simplex:z \ + -v $HOME/simplex/smp/logs:/var/opt/simplex:z \ + simplexchat/smp-server:latest + ``` -For Ubuntu you can download a binary from [the latest release](https://github.com/simplex-chat/simplexmq/releases). + - `xftp-server` + + You must change **your_ip_or_domain** and **maximum_storage**. + ```sh + docker run -d \ + -e "ADDR=your_ip_or_domain" \ + -e "QUOTA=maximum_storage" \ + -p 443:443 \ + -v $HOME/simplex/xftp/config:/etc/opt/simplex-xftp:z \ + -v $HOME/simplex/xftp/logs:/var/opt/simplex-xftp:z \ + -v $HOME/simplex/xftp/files:/srv/xftp:z \ + simplexchat/xftp-server:latest + ``` + +#### Using installation script + +**Please note** that currently, only Ubuntu distribution is supported. + +You can install and setup servers automatically using our script: + +```sh +curl --proto '=https' --tlsv1.2 -sSf https://raw.githubusercontent.com/simplex-chat/simplexmq/stable/install.sh -o simplex-server-install.sh \ +&& if echo '1268d605e90bca1a8c7ef476038a8bd9aa9b1a28f79ea0a1485669ccf8fc23cd simplex-server-install.sh' | sha256sum -c; then chmod +x ./simplex-server-install.sh && ./simplex-server-install.sh; rm /simplex-server-install.sh; else echo "SHA-256 checksum is incorrect!" && rm ./simplex-server-install.sh; fi +``` ### Build from source @@ -136,31 +161,50 @@ For Ubuntu you can download a binary from [the latest release](https://github.co On Linux, you can build smp server using Docker. -1. Build your `smp-server` image: +1. Build your images: ```sh git clone https://github.com/simplex-chat/simplexmq cd simplexmq git checkout stable - DOCKER_BUILDKIT=1 docker build -t smp-server -f ./build.Dockerfile . + DOCKER_BUILDKIT=1 docker build -t local/smp-server --build-arg APP="smp-server" --build-arg APP_PORT="5223" . # For xmp-server + DOCKER_BUILDKIT=1 docker build -t local/xftp-server --build-arg APP="xftp-server" --build-arg APP_PORT="443" . # For xftp-server ``` -2. Create `config` and `logs` directories: +2. Create directories for persistent Docker configuration: ```sh - mkdir -p ~/simplex/{config,logs} + mkdir -p $HOME/simplex/{xftp,smp}/{config,logs} && mkdir -p $HOME/simplex/xftp/files ``` -3. Run your Docker container. You must change **your_ip_or_domain**. `-e pass="password"` is optional variable to password-protect your `smp` server:: - ```sh - docker run -d \ - -e "addr=your_ip_or_domain" \ - -e "pass=password" \ - -p 5223:5223 \ - -v $HOME/simplex/config:/etc/opt/simplex:z \ - -v $HOME/simplex/logs:/var/opt/simplex:z \ - smp-server - ``` +3. Run your Docker container. + + - `smp-server` + + You must change **your_ip_or_domain**. `-e "pass=password"` is optional variable to password-protect your `smp` server: + ```sh + docker run -d \ + -e "ADDR=your_ip_or_domain" \ + -e "PASS=password" \ + -p 5223:5223 \ + -v $HOME/simplex/smp/config:/etc/opt/simplex:z \ + -v $HOME/simplex/smp/logs:/var/opt/simplex:z \ + simplexchat/smp-server:latest + ``` + + - `xftp-server` + + You must change **your_ip_or_domain** and **maximum_storage**. + ```sh + docker run -d \ + -e "ADDR=your_ip_or_domain" \ + -e "QUOTA=maximum_storage" \ + -p 443:443 \ + -v $HOME/simplex/xftp/config:/etc/opt/simplex-xftp:z \ + -v $HOME/simplex/xftp/logs:/var/opt/simplex-xftp:z \ + -v $HOME/simplex/xftp/files:/srv/xftp:z \ + simplexchat/xftp-server:latest + ``` #### Using your distribution diff --git a/download.Dockerfile b/download.Dockerfile deleted file mode 100644 index 55d79c29f..000000000 --- a/download.Dockerfile +++ /dev/null @@ -1,22 +0,0 @@ -FROM ubuntu:focal - -# Install curl -RUN apt-get update && apt-get install -y curl - -ARG version=undefined - -# Download latest smp-server release and assign executable permission -RUN curl -L https://github.com/simplex-chat/simplexmq/releases/latest/download/smp-server-ubuntu-20_04-x86-64 -o /usr/bin/smp-server && \ - chmod +x /usr/bin/smp-server - -# Copy our helper script -COPY ./scripts/docker/entrypoint /usr/bin/entrypoint - -# Open smp-server listening port -EXPOSE 5223 - -# SimpleX requires using SIGINT to correctly preserve undelivered messages and restore them on restart -STOPSIGNAL SIGINT - -# Finally, execute helper script -ENTRYPOINT [ "/usr/bin/entrypoint" ] diff --git a/install.sh b/install.sh new file mode 100755 index 000000000..3bcac3f49 --- /dev/null +++ b/install.sh @@ -0,0 +1,141 @@ +#!/usr/bin/env sh +set -eu + +# Links to scripts/configs +bin="https://github.com/simplex-chat/simplexmq/releases/latest/download" +bin_smp="$bin/smp-server-ubuntu-20_04-x86-64" +bin_xftp="$bin/xftp-server-ubuntu-20_04-x86-64" + +scripts="https://raw.githubusercontent.com/simplex-chat/simplexmq/stable/scripts/main" +scripts_systemd_smp="$scripts/smp-server.service" +scripts_systemd_xftp="$scripts/xftp-server.service" +scripts_update="$scripts/simplex-servers-update" +scripts_uninstall="$scripts/simplex-servers-uninstall" + +# Default installation paths +path_bin="/usr/local/bin" +path_bin_smp="$path_bin/smp-server" +path_bin_xftp="$path_bin/xftp-server" +path_bin_update="$path_bin/simplex-servers-update" +path_bin_uninstall="$path_bin/simplex-servers-uninstall" + +path_conf_etc="/etc/opt" +path_conf_var="/var/opt" +path_conf_smp="$path_conf_etc/simplex $path_conf_var/simplex" +path_conf_xftp="$path_conf_etc/simplex-xftp $path_conf_var/simplex-xftp /srv/xftp" + +path_systemd="/etc/systemd/system" +path_systemd_smp="$path_systemd/smp-server.service" +path_systemd_xftp="$path_systemd/xftp-server.service" + +# Defaut users +user_smp="smp" +user_xftp="xftp" + +GRN='\033[0;32m' +BLU='\033[1;34m' +YLW='\033[1;33m' +RED='\033[0;31m' +NC='\033[0m' + +logo=' + ____ _ _ __ __ +/ ___|(_)_ __ ___ _ __ | | ___\ \/ / +\___ \| | '"'"'_ ` _ \| '"'"'_ \| |/ _ \\ / + ___) | | | | | | | |_) | | __// \ +|____/|_|_| |_| |_| .__/|_|\___/_/\_\ + |_| +' + +welcome="Welcome to SMP/XFTP installation script! Here's what we're going to do: +${GRN}1.${NC} Install latest binaries from GitHub releases: + - smp: ${YLW}${path_bin_smp}${NC} + - xftp: ${YLW}${path_bin_xftp}${NC} +${GRN}2.${NC} Create server directories: + - smp: ${YLW}${path_conf_smp}${NC} + - xftp: ${YLW}${path_conf_xftp}${NC} +${GRN}3.${NC} Setup user for each server: + - xmp: ${YLW}${user_smp}${NC} + - xftp: ${YLW}${user_xftp}${NC} +${GRN}4.${NC} Create systemd services: + - smp: ${YLW}${path_systemd_smp}${NC} + - xftp: ${YLW}${path_systemd_xftp}${NC} +${GRN}5.${NC} Install update and uninstallation script: + - all: ${YLW}${path_bin_update}${NC}, ${YLW}${path_bin_uninstall}${NC} + +Press ${GRN}ENTER${NC} to continue or ${RED}Ctrl+C${NC} to cancel installation" + +end="Installtion is complete! + +Please checkout our server guides: +- smp: ${GRN}https://simplex.chat/docs/server.html${NC} +- xftp: ${GRN}https://simplex.chat/docs/xftp-server.html${NC} + +To uninstall with full clean-up, simply run: ${YLW}sudo /usr/local/bin/simplex-servers-uninstall${NC} +" + +setup_bins() { + curl --proto '=https' --tlsv1.2 -sSf -L "$bin_smp" -o "$path_bin_smp" && chmod +x "$path_bin_smp" + curl --proto '=https' --tlsv1.2 -sSf -L "$bin_xftp" -o "$path_bin_xftp" && chmod +x "$path_bin_xftp" +} + +setup_users() { + useradd -M "$user_smp" 2> /dev/null || true + useradd -M "$user_xftp" 2> /dev/null || true +} + +setup_dirs() { + # Unquoted varibles, so field splitting can occur + mkdir -p $path_conf_smp + chown "$user_smp":"$user_smp" "$path_conf_smp" + mkdir -p $path_conf_xftp + chown "$user_xftp":"$user_xftp" "$path_conf_xftp" +} + +setup_systemd() { + curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_systemd_smp" -o "$path_systemd_smp" + curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_systemd_xftp" -o "$path_systemd_xftp" +} + +setup_scripts() { + curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_update" -o "$path_bin_update" && chmod +x "$path_bin_update" + curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_uninstall" -o "$path_bin_uninstall" && chmod +x "$path_bin_uninstall" + } + +checks() { + if [ "$(id -u)" -ne 0 ]; then + printf "This script is intended to be run with root privileges. Please re-run script using sudo." + exit 1 + fi +} + +main() { + checks + + printf "%b\n%b\n" "${BLU}$logo${NC}" "$welcome" + read ans + + printf "Installing binaries..." + setup_bins + printf "${GRN} Done!${NC}\n" + + printf "Creating users..." + setup_users + printf "${GRN} Done!${NC}\n" + + printf "Creating directories..." + setup_dirs + printf "${GRN} Done!${NC}\n" + + printf "Creating systemd services..." + setup_systemd + printf "${GRN} Done!${NC}\n" + + printf "Installing update and uninstallation script..." + setup_scripts + printf "${GRN} Done!${NC}\n" + + printf "%b" "$end" +} + +main diff --git a/scripts/docker/entrypoint b/scripts/docker/entrypoint-smp-server similarity index 59% rename from scripts/docker/entrypoint rename to scripts/docker/entrypoint-smp-server index ba172c2a1..8958874f8 100755 --- a/scripts/docker/entrypoint +++ b/scripts/docker/entrypoint-smp-server @@ -5,21 +5,28 @@ logd="/var/opt/simplex/" # Check if server has been initialized if [ ! -f "$confd/smp-server.ini" ]; then # If not, determine ip or domain - case $addr in - '') printf "Please specify \$addr environment variable.\n"; exit 1 ;; - *[a-zA-Z]*) set -- -n $addr ;; - *) set -- --ip $addr ;; + case "$ADDR" in + '') printf "Please specify \$ADDR environment variable.\n"; exit 1 ;; + *[a-zA-Z]*) + case "$ADDR" in + *:*) set -- --ip "$ADDR" ;; + *) set -- -n "$ADDR" ;; + esac + ;; + *) set -- --ip "$ADDR" ;; esac - case $pass in + # Optionally, set password + case "$PASS" in '') set -- "$@" --no-password ;; - *) set -- "$@" --password $pass ;; + *) set -- "$@" --password "$PASS" ;; esac + # And init certificates and configs smp-server init -y -l "$@" fi -# backup store log +# Backup store log just in case [ -f "$logd/smp-server-store.log" ] && cp "$logd"/smp-server-store.log "$logd"/smp-server-store.log."$(date +'%FT%T')" # Finally, run smp-sever. Notice that "exec" here is important: diff --git a/scripts/docker/entrypoint-xftp-server b/scripts/docker/entrypoint-xftp-server new file mode 100755 index 000000000..c23262670 --- /dev/null +++ b/scripts/docker/entrypoint-xftp-server @@ -0,0 +1,35 @@ +#!/usr/bin/env sh +confd="/etc/opt/simplex-xftp" +logd="/var/opt/simplex-xftp" + +# Check if server has been initialized +if [ ! -f "$confd/file-server.ini" ]; then + # If not, determine ip or domain + case "$ADDR" in + '') printf "Please specify \$ADDR environment variable.\n"; exit 1 ;; + *[a-zA-Z]*) + case "$ADDR" in + *:*) set -- --ip "$ADDR" ;; + *) set -- -n "$ADDR" ;; + esac + ;; + *) set -- --ip "$ADDR" ;; + esac + + # Set quota + case "$QUOTA" in + '') printf "Please specify \$QUOTA environment variable.\n"; exit 1 ;; + *) set -- "$@" --quota "$QUOTA" ;; + esac + + # Init the certificates and configs + xftp-server init -l -p /srv/xftp "$@" +fi + +# Backup store log just in case +[ -f "$logd/file-server-store.log" ] && cp "$logd"/file-server-store.log "$logd"/file-server-store.log."$(date +'%FT%T')" + +# Finally, run xftp-sever. Notice that "exec" here is important: +# smp-server replaces our helper script, so that it can catch INT signal +exec xftp-server start +RTS -N -RTS + diff --git a/scripts/main/simplex-servers-uninstall b/scripts/main/simplex-servers-uninstall new file mode 100755 index 000000000..f08fc0efc --- /dev/null +++ b/scripts/main/simplex-servers-uninstall @@ -0,0 +1,18 @@ +#!/usr/bin/env sh +set -eu + +GRN='\033[0;32m' +RED='\033[0;31m' +NC='\033[0m' + +if [ "$(id -u)" -ne 0 ]; then + printf "This script is intended to be run with root privileges. Please re-run script using sudo." + exit 1 +fi + +printf "${RED}This action will permanently remove all configs, directories, binaries from Installation Script. Please backup any relevant configs if they are needed.${NC}\n\nPress ${GRN}ENTER${NC} to continue or ${RED}Ctrl+C${NC} to cancel installation" +read ans + +rm -rf /var/opt/simplex /etc/opt/simplex /var/opt/simplex-xftp /etc/opt/simplex-xftp /srv/xftp /etc/systemd/system/smp-server.service /etc/systemd/system/xftp-server.service /usr/local/bin/smp-server /usr/local/bin/xftp-server /usr/local/bin/simplex-servers-update /usr/local/bin/simplex-servers-uninstall && userdel smp && userdel xftp + +printf "Uninstallation is complete! Thanks for trying out SimpleX!\n" diff --git a/scripts/main/simplex-servers-update b/scripts/main/simplex-servers-update new file mode 100755 index 000000000..450665e09 --- /dev/null +++ b/scripts/main/simplex-servers-update @@ -0,0 +1,160 @@ +#!/usr/bin/env sh +set -eu + +# Links to scripts/configs +bin="https://github.com/simplex-chat/simplexmq/releases/latest/download" +bin_smp="$bin/smp-server-ubuntu-20_04-x86-64" +bin_xftp="$bin/xftp-server-ubuntu-20_04-x86-64" + +scripts="https://raw.githubusercontent.com/simplex-chat/simplexmq/stable/scripts/main" +scripts_systemd_smp="$scripts/smp-server.service" +scripts_systemd_xftp="$scripts/xftp-server.service" +scripts_update="$scripts/simplex-servers-update" +scripts_uninstall="$scripts/simplex-servers-uninstall" + +# Default installation paths +path_bin="/usr/local/bin" +path_bin_smp="$path_bin/smp-server" +path_bin_xftp="$path_bin/xftp-server" +path_bin_update="$path_bin/simplex-servers-update" +path_bin_uninstall="$path_bin/simplex-servers-uninstall" + +path_systemd="/etc/systemd/system" +path_systemd_smp="$path_systemd/smp-server.service" +path_systemd_xftp="$path_systemd/xftp-server.service" + +# Temporary paths +path_tmp_bin="$(mktemp -d)" +path_tmp_bin_update="$path_tmp_bin/simplex-servers-update" +path_tmp_bin_uninstall="$path_tmp_bin/simplex-servers-uninstall" +path_tmp_systemd_smp="$path_tmp_bin/smp-server.service" +path_tmp_systemd_xftp="$path_tmp_bin/xftp-server.service" + +GRN='\033[0;32m' +BLU='\033[1;36m' +YLW='\033[1;33m' +RED='\033[0;31m' +NC='\033[0m' + +# Currently, XFTP default to v0.1.0, so it doesn't make sense to check its version +local_version="$($path_bin_smp -v | awk '{print $3}')" +remote_version="$(curl --proto '=https' --tlsv1.2 -sSf -L https://api.github.com/repos/simplex-chat/simplexmq/releases/latest | grep -i "tag_name" | awk -F \" '{print $4}')" + +update_scripts() { + curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_update" -o "$path_tmp_bin_update" && chmod +x "$path_tmp_bin_update" + curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_uninstall" -o "$path_tmp_bin_uninstall" && chmod +x "$path_tmp_bin_uninstall" + + if diff -q "$path_bin_uninstall" "$path_tmp_bin_uninstall" > /dev/null; then + printf -- "- ${YLW}Uninstall script is up-to-date${NC}.\n" + rm "$path_tmp_bin_uninstall" + else + printf -- "- Updating uninstall script..." + mv "$path_tmp_bin_uninstall" "$path_bin_uninstall" + printf "${GRN}Done!${NC}\n" + fi + if diff -q "$path_bin_update" "$path_tmp_bin_update" > /dev/null; then + printf -- "- ${YLW}Update script is up-to-date${NC}.\n" + rm "$path_tmp_bin_update" + else + printf -- "- Updating update script..." + mv "$path_tmp_bin_update" "$path_bin_update" + printf "${GRN}Done!${NC}\n" + printf "Re-executing Update script with latest updates..." + exec sh "$path_bin_update" "continue" + fi +} + +update_systemd() { + curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_systemd_smp" -o "$path_tmp_systemd_smp" + curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_systemd_xftp" -o "$path_tmp_systemd_xftp" + + if diff -q "$path_systemd_smp" "$path_tmp_systemd_smp" > /dev/null; then + printf -- "- ${YLW}smp-server service is up-to-date${NC}.\n" + rm "$path_tmp_systemd_smp" + else + printf -- "- Updating smp-server service..." + mv "$path_tmp_systemd_smp" "$path_systemd_smp" + systemctl daemon-reload + printf "${GRN}Done!${NC}\n" + fi + if diff -q "$path_systemd_xftp" "$path_tmp_systemd_xftp" > /dev/null; then + printf -- "- ${YLW}xftp-server service is up-to-date${NC}.\n" + rm "$path_tmp_systemd_xftp" + else + printf -- "- Updating xftp-server service..." + mv "$path_tmp_systemd_xftp" "$path_systemd_xftp" + systemctl daemon-reload + printf "${GRN}Done!${NC}\n" + fi +} + +update_bins() { + if [ "$local_version" != "$remote_version" ]; then + if systemctl is-active --quiet smp-server; then + printf -- "- Stopping smp-server service..." + systemctl stop smp-server + printf "${GRN}Done!${NC}\n" + + print -- "- Updating smp-server bin to %s..." "$remote_version" + curl --proto '=https' --tlsv1.2 -sSf -L "$bin_smp" -o "$bin_path_smp" && chmod +x "$bin_path_smp" + printf "${GRN}Done!${NC}\n" + + printf -- "- Starting smp-server service..." + systemctl stop smp-server + printf "${GRN}Done!${NC}\n" + else + print -- "- Updating smp-server bin..." + curl --proto '=https' --tlsv1.2 -sSf -L "$bin_smp" -o "$bin_path_smp" && chmod +x "$bin_path_smp" + printf "${GRN}Done!${NC}\n" + fi + + if systemctl is-active --quiet xftp-server; then + printf -- "- Stopping xftp-server service..." + systemctl stop xftp-server + printf "${GRN}Done!${NC}\n" + + print -- "- Updating xftp-server bin to %s..." "$remote_version" + curl --proto '=https' --tlsv1.2 -sSf -L "$bin_xftp" -o "$bin_path_xftp" && chmod +x "$bin_path_xftp" + printf "${GRN}Done!${NC}\n" + + printf -- "- Starting xftp-server service..." + systemctl stop xftp-server + printf "${GRN}Done!${NC}\n" + else + print -- "- Updating xftp-server bin..." + curl --proto '=https' --tlsv1.2 -sSf -L "$bin_smp" -o "$bin_path_xftp" && chmod +x "$bin_path_xftp" + printf "${GRN}Done!${NC}\n" + fi + else + printf -- "- ${YLW}smp-server and xftp-server binaries is up-to-date${NC}.\n" + fi +} + +checks() { + if [ "$(id -u)" -ne 0 ]; then + printf "This script is intended to be run with root privileges. Please re-run script using sudo.\n" + exit 1 + fi +} + +main() { + checks + + set +u + if [ "$1" != "continue" ]; then + set -u + printf "Updating scripts...\n" + update_scripts + else + set -u + printf "${GRN}Done!${NC}\n" + fi + + printf "Updating systemd services...\n" + update_systemd + + printf "Updating simplex server binaries...\n" + update_bins +} + +main "$@" diff --git a/scripts/main/smp-server.service b/scripts/main/smp-server.service new file mode 100644 index 000000000..db3da7d9c --- /dev/null +++ b/scripts/main/smp-server.service @@ -0,0 +1,15 @@ +[Unit] +Description=SMP server + +[Service] +User=smp +Group=smp +Type=simple +ExecStart=/usr/local/bin/smp-server start +RTS -N -RTS +ExecStopPost=/usr/bin/env sh -c '[ -e "/var/opt/simplex/smp-server-store.log" ] && cp "/var/opt/simplex/smp-server-store.log" "/var/opt/simplex/smp-server-store.log.$(date +%FT%T)' +LimitNOFILE=65535 +KillSignal=SIGINT +TimeoutStopSec=infinity + +[Install] +WantedBy=multi-user.target diff --git a/scripts/main/xftp-server.service b/scripts/main/xftp-server.service new file mode 100644 index 000000000..80bb2bb08 --- /dev/null +++ b/scripts/main/xftp-server.service @@ -0,0 +1,16 @@ +[Unit] +Description=XFTP server + +[Service] +User=xftp +Group=xftp +Type=simple +ExecStart=/usr/local/bin/xftp-server start +RTS -N -RTS +ExecStopPost=/usr/bin/env sh -c '[ -e "/var/opt/simplex-xftp/file-server-store.log" ] && cp "/var/opt/simplex-xftp/file-server-store.log" "/var/opt/simplex-xftp/file-server-store.log.$(date +%FT%T)' +LimitNOFILE=65535 +KillSignal=SIGINT +TimeoutStopSec=infinity +AmbientCapabilities=CAP_NET_BIND_SERVICE + +[Install] +WantedBy=multi-user.target