Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

scripts/simplex-servers-update: refactor #1445

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
84a5ac3
scripts/simplex-servers-update: refactor
shumvgolove Jan 30, 2025
089dcc2
simplex-servers-update: don't check empty variable
shumvgolove Jan 31, 2025
b1778ec
simplex-servers-update: minor decoration change
shumvgolove Jan 31, 2025
07ac7fc
simplex-servers-update: add new line when reexecuting script
shumvgolove Jan 31, 2025
19e734f
simplex-servers-update: refactor menus
shumvgolove Jan 31, 2025
ff005f3
simplex-servers-update: fix re-executing
shumvgolove Jan 31, 2025
191696f
simplex-servers-update: fix menus
shumvgolove Jan 31, 2025
9bb317c
simplex-servers-update: do not rerended menu every time
shumvgolove Jan 31, 2025
4a51ad3
simplex-servers-update: update menus logic
shumvgolove Jan 31, 2025
04c505d
simplex-servers-update: ask permission to update only if versions differ
shumvgolove Jan 31, 2025
e3051b6
simplex-servers-update: skip empty variable
shumvgolove Jan 31, 2025
0af3db6
simplex-servers-update: include while loop
shumvgolove Jan 31, 2025
3d9e2e9
simplex-servers-update: update re-executing skip logic
shumvgolove Jan 31, 2025
30652d4
simplex-servers-update: ignore empty
shumvgolove Jan 31, 2025
91da5df
simplex-servers-update: s/while/if
shumvgolove Jan 31, 2025
99c02bc
simplex-servers-update: ignore empty variable
shumvgolove Jan 31, 2025
64d0ca8
simplex-servers-update: update skipping scripts
shumvgolove Jan 31, 2025
8cd1146
simplex-servers-update: minor fixes
shumvgolove Jan 31, 2025
79118fe
simplex-servers-update: add spaces
shumvgolove Jan 31, 2025
7ed782f
simplex-servers-update: safeguard files and add cleanup logic
shumvgolove Jan 31, 2025
6edba27
simplex-servers-update: update menus
shumvgolove Jan 31, 2025
83687f4
simplex-servers-update: fix binary check
shumvgolove Jan 31, 2025
f4bc021
simplex-servers-update: adjust erasing lines
shumvgolove Jan 31, 2025
aadc780
simplex-servers-update: simplify bin update logic
shumvgolove Jan 31, 2025
e36a000
simplex-servers-update: fix binary path
shumvgolove Jan 31, 2025
00d4893
simplex-servers-update: format errors
shumvgolove Jan 31, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
285 changes: 244 additions & 41 deletions scripts/main/simplex-servers-update
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,47 @@ YLW='\033[1;33m'
RED='\033[0;31m'
NC='\033[0m'

menu="Select option:
- ${GRN}1${NC}) Update everything: ${YLW}systemd services${NC}, ${YLW}update/backup/install scripts${NC}, ${YLW}binaries${NC}
- ${GRN}2${NC}) Update ${YLW}only binaries${NC}
- ${RED}3${NC}) Exit the update script

For non-interactive usage rerun the script the following way:
${BLU}CHOOSE=1${NC} simplex-servers-update

To specify version:
${BLU}VER=vX.X.X${NC} CHOOSE=1 simplex-servers-update

Selection: "

menu_one="This will:
- ${YLW}Update update/backup/uninstall scripts${NC}
- ${YLW}Update systemd services and reload the daemon${NC}
- ${YLW}Stop the systemd service if it's running${NC}
- ${YLW}Update the binary${NC}
- ${YLW}Restart the service if it was stopped by the script${NC}

Proceed? (${GRN}yes${NC}/${RED}no${NC}): "

menu_two="This will:
- ${YLW}Stop the systemd service if it's running${NC}
- ${YLW}Update the binary${NC}
- ${YLW}Restart the service if it was stopped by the script${NC}

Proceed? (${GRN}yes${NC}/${RED}no${NC}): "

# Set CHOOSe globally and only once
choose="${CHOOSE:-}"

# Cleanup temp files
cleanup() {
rm -rf -- "$path_tmp_bin"
}

trap '(exit 130)' INT
trap '(exit 143)' TERM
trap 'rc=$?; cleanup; exit $rc' EXIT

# Currently, XFTP default to v0.1.0, so it doesn't make sense to check its version

os_test() {
Expand All @@ -61,39 +102,94 @@ os_test() {
bin_xftp="$bin/xftp-server-ubuntu-${version}-${arch}"
}

installed_test() {
set +u
for i in $path_conf_etc/*; do
if [ -d "$i" ]; then
case "$i" in
*simplex) apps="smp $apps" ;;
*simplex-xftp) apps="xftp $apps" ;;
esac
fi
done
set -u
}

set_version() {
ver="${VER:-latest}"

case "$ver" in
latest)
bin="https://github.com/simplex-chat/simplexmq/releases/latest/download"
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}')"
remote_version="$(curl --proto '=https' --tlsv1.2 -sSf -L https://api.github.com/repos/simplex-chat/simplexmq/releases/latest 2>/dev/null | grep -i "tag_name" | awk -F \" '{print $4}')"

if [ -z "$remote_version" ]; then
printf "${RED}Something went wrong when ${YLW}resolving the lastest version${NC}: either you don't have connection to Github or you're rate-limited.\n"
exit 1
fi
;;
*)
bin="https://github.com/simplex-chat/simplexmq/releases/download/${ver}"
remote_version="${ver}"
;;
esac

set +u
for i in smp xftp; do
if command -v "$i-server" >/dev/null; then
apps="$i $apps"
eval local_version_$i="$(${i}-server -v | awk '{print $NF}')"
fi
done
set -u
}

erase_lines() {
for i in $(seq 1 "${1}"); do
printf '\e[A\e[K'
done
}

check_sanity() {
path="$1"
criteria="$2"

case "$criteria" in
string:*)
pattern="$(printf '%s' "$criteria" | awk '{print $2}')"

if grep -q "$pattern" "$path"; then
sane=0
else
sane=1
fi
;;
file:*)
pattern="$(printf '%s' "$criteria" | awk '{print $2}')"

if file "$path" | grep -q "$pattern"; then
sane=0
else
sane=1
fi
;;
*) printf 'Unknown criteria.\n'; sane=1 ;;
esac

unset path string

return "$sane"
}

update_scripts() {
curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_update" -o "$path_tmp_bin_update" && chmod +x "$path_tmp_bin_update"

if ! check_sanity "$path_tmp_bin_update" 'string: /usr/bin/env'; then
printf "${RED}Something went wrong when downloading ${YLW}Update script${NC}: either you don't have connection to Github or you're rate-limited.\n"
exit 1
fi

curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_uninstall" -o "$path_tmp_bin_uninstall" && chmod +x "$path_tmp_bin_uninstall"

if ! check_sanity "$path_tmp_bin_uninstall" 'string: /usr/bin/env'; then
printf "${RED}Something went wrong when downloading ${YLW}Uninstall script${NC}: either you don't have connection to Github or you're rate-limited.\n"
exit 1
fi

curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_stopscript" -o "$path_tmp_bin_stopscript" && chmod +x "$path_tmp_bin_stopscript"

if ! check_sanity "$path_tmp_bin_stopscript" 'string: /usr/bin/env'; then
printf "${RED}Something went wrong when downloading ${YLW}Update script${NC}: either you don't have connection to Github or you're rate-limited.\n"
exit 1
fi

if diff -q "$path_bin_uninstall" "$path_tmp_bin_uninstall" > /dev/null 2>&1; then
printf -- "- ${YLW}Uninstall script is up-to-date${NC}.\n"
rm "$path_tmp_bin_uninstall"
Expand All @@ -119,8 +215,8 @@ update_scripts() {
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"
printf -- "- Re-executing Update script..."
exec env CHOOSE="$choose" SCRIPTS_DONE=1 "$path_bin_update"
fi
}

Expand All @@ -132,6 +228,11 @@ update_systemd() {

curl --proto '=https' --tlsv1.2 -sSf -L "$scripts_systemd" -o "$path_tmp_systemd"

if ! check_sanity "$path_tmp_systemd" 'string: [Service]'; then
printf "${RED}Something went wrong when downloading ${YLW}Systemd service${NC}: either you don't have connection to Github or you're rate-limited.\n"
exit 1
fi

if diff -q "$path_systemd" "$path_tmp_systemd" > /dev/null 2>&1; then
printf -- "- ${YLW}%s service is up-to-date${NC}.\n" "$service"
rm "$path_tmp_systemd"
Expand Down Expand Up @@ -170,23 +271,47 @@ update_bins() {
set_ver "$1"
fi

update_submenu() {
printf "This will update ${YLW}%s${NC} from ${BLU}%s${NC} to ${BLU}%s${NC}. Proceed? (${GRN}yes${NC}/${RED}no${NC}): " "$service" "$local_version" "$remote_version"
read proceed

case "$proceed" in
yes) : ;;
no) printf '\n'; exit 0 ;;
*) unset proceed; erase_lines 1; update_submenu ;;
esac
}

update_bin() {
printf -- "- Updating %s to %s..." "$service" "$remote_version"

curl --proto '=https' --tlsv1.2 -sSf -L "$bin" -o "$path_bin.new" && chmod +x "$path_bin.new"
if ! check_sanity "$path_bin.new" 'file: ELF'; then
printf "\n${RED}Something went wrong when downloading ${YLW}%s binary${NC}: either you don't have connection to Github or you're rate-limited.\n" "$service"
rm -f "$path_bin.new"
exit 1
else
mv "$path_bin.new" "$path_bin"
fi

printf "${GRN}Done!${NC}\n"
}

if [ "$local_version" != "$remote_version" ]; then
update_submenu

if systemctl is-active --quiet "$service"; then
printf -- "- Stopping %s service..." "$service"
systemctl stop "$service"
printf "${GRN}Done!${NC}\n"

printf -- "- Updating %s to %s..." "$service" "$remote_version"
curl --proto '=https' --tlsv1.2 -sSf -L "$bin" -o "$path_bin" && chmod +x "$path_bin"
printf "${GRN}Done!${NC}\n"
update_bin

printf -- "- Starting %s service..." "$service"
systemctl start "$service"
printf "${GRN}Done!${NC}\n"
else
printf -- "- Updating %s to %s..." "$service" "$remote_version"
curl --proto '=https' --tlsv1.2 -sSf -L "$bin" -o "$path_bin" && chmod +x "$path_bin"
printf "${GRN}Done!${NC}\n"
update_bin
fi
else
printf -- "- ${YLW}%s is up-to-date${NC}.\n" "$service"
Expand All @@ -205,35 +330,113 @@ checks() {

set_version
os_test
installed_test

if [ -z "$apps" ]; then
printf 'No simplex servers installed! Aborting...\n'
exit 1
fi

mkdir -p $path_conf_info
}

main() {
checks
menu_one() {
set +u
if [ -z "$choose_one" ]; then
printf '%b' "$menu_one"
read choose_one
fi
set -u

case "$choose_one" in
yes) : ;;
no) printf '\n'; exit 0 ;;
*) unset choose_one; erase_lines 8; menu_one ;;
esac
}

menu_two() {
set +u
if [ "$1" != "continue" ]; then
set -u
printf "Updating scripts...\n"
update_scripts
else
set -u
printf "${GRN}Done!${NC}\n"
if [ -z "$choose_two" ]; then
printf '%b' "$menu_two"
read choose_two
fi
set -u

printf "Updating systemd services...\n"
for i in $apps; do
update_systemd "$i"
done
case "$choose_two" in
yes) : ;;
no) printf '\n'; exit 0 ;;
*) unset choose_two; erase_lines 6; menu_two ;;
esac
}

main() {
set +u
if [ -z "$choose" ]; then
printf '%b' "$menu"
read choose
fi
set -u

case "$choose" in
1)
set +u
case "$CHOOSE" in
1) choose_one=yes ;;
*) : ;;
esac
set -u

menu_one

set +u
if [ -z "$SCRIPTS_DONE" ]; then
set -u

printf "Updating scripts...\n"
update_scripts
else
printf "${GRN}Done!${NC}\n"
fi
set -u

printf "Updating systemd services...\n"
for i in $apps; do
update_systemd "$i"
done

printf "Updating simplex servers...\n"
for i in $apps; do
update_bins "$i"
done
printf "Updating simplex servers...\n"
for i in $apps; do
update_bins "$i"
done
;;

2)
set +u
case "$CHOOSE" in
2) choose_two=yes ;;
*) : ;;
esac
set -u

rm -rf "$path_tmp_bin"
menu_two

printf "Updating simplex servers...\n"
for i in $apps; do
update_bins "$i"
done
;;

3)
printf '\n'
exit 0
;;
*)
unset choose
erase_lines 12
main "$@"
;;
esac
}

checks
main "$@"
Loading