#!/bin/bash # Use special param to make sure we're running under bash (in case user does "sh install.sh") [ "x$1" != "x--exec-self" ] && exec /bin/bash "$0" --exec-self "$@" if [ ! -d "/opt/dmsd" ] && [ ! -d "/srv/openslx/www/slx-admin" ]; then echo "This system doesn't look like a bwLehrpool Satellite Server" exit 1 fi TEST1=$(which systemctl) TEST2=$(lsb_release -sc) if [ -z "$TEST1" ] || [ "x$TEST2" != "xjessie" ]; then echo "This Version of the bwLehrpool Satellite Server is too old!" exit 1 fi unset TEST1 TEST2 unset TMPDIR [ -n "$TMPDIR" ] && exit 1 IGNORE_ERRORS= while [ $# -gt 0 ]; do [ "x$1" = "x--ignore-errors" ] && IGNORE_ERRORS=jup shift done readonly IGNORE_ERRORS declare -rg SELFPID=$$ perror () { if [ -n "$IGNORE_ERRORS" ]; then echo -n -e '\033[01;31m[ERROR]\033[00m ' echo "$@" return 0 fi echo -n -e '\033[01;31m[FATAL]\033[00m ' echo "$@" [ -n "$TMPDIR" ] && [ -d "$TMPDIR" ] && rm -rf -- "$TMPDIR" [ "$$" != "$SELFPID" ] && kill "$SELFPID" exit 1 } pwarning () { echo -n -e '\033[01;33m[WARNING]\033[00m ' echo "$@" } if [ "$UID" != "0" ]; then perror "Must be running as root" fi # ** Extract value from text file containing key=value pairs extractfield () { grep -m1 "^\s*$2\b" "$1" | awk -F '=' '{print $2}' | sed 's/\s//g' } # ** Wrap mysql command line client so we're always using the deb-sys-maint credentials mysql () { "$(which mysql)" --defaults-extra-file=/etc/mysql/debian.cnf --default-character-set=utf8 "$@" } # ** Restart given systemd service, warn if it fails but do not bail out restart_service () { if ! systemctl restart "$1"; then pwarning "Could not restart service $1 - !! YOU SHOULD REBOOT THE SERVER !!" systemctl status "$1" fi } # ** Recursively set permissions of given directory to 0755/0644 optionally setting owner/group fixperms () { [ -e "$1" ] || perror "normalize_perms: $1 does not exist" chmod -R a-rw,u+rwX,go+rX "$1" if [ -n "$2" ]; then chown -R "$2" "$1" || pwarning "Could not change owner of $1 to $2" fi } # ** Constants - to be patched by the packaging script declare -rg TARGET_WEBIF_VERSION="%TARGET_WEBIF_VERSION%" declare -rg TGZ_SLXADMIN="%TGZ_SLXADMIN%" declare -rg TGZ_DOZMOD="%TGZ_DOZMOD%" declare -rg TGZ_TASKMANAGER="%TGZ_TASKMANAGER%" declare -rg TGZ_TFTP="%TGZ_TFTP%" declare -rg TGZ_IPXE="%TGZ_IPXE%" declare -rg TGZ_LDADP="%TGZ_LDADP%" declare -rg PAYLOAD_OFFSET="%PAYLOAD_OFFSET%" # ** Constants - hardcoded or determined at runtime declare -rg PATH_SLXADMIN="/srv/openslx/www/slx-admin" declare -rg PATH_DOZMOD="/opt/dmsd" declare -rg PATH_TASKMANAGER="/opt/taskmanager" declare -rg PATH_TFTP="/srv/openslx/tftp" declare -rg PATH_IPXE="/opt/openslx/ipxe" declare -rg PATH_LDADP="/opt/ldadp" # ** Check if constants have been filled, bail out otherwise if [ -z "$TARGET_WEBIF_VERSION" ] || [[ "$TARGET_WEBIF_VERSION" == %*% ]]; then perror "Bad upgrader: TARGET_WEBIF_VERSION not set" fi if [ -z "$PAYLOAD_OFFSET" ] || [[ "$PAYLOAD_OFFSET" == %*% ]]; then perror "Bad upgrader: PAYLOAD_OFFSET not set" fi # ********************************************************** # Get current webif version declare -rg CURRENT_WEBIF_VERSION=$(mysql -e 'SELECT value FROM openslx.property WHERE name = "webif-version" LIMIT 1' | tail -n 1) [ -z "$CURRENT_WEBIF_VERSION" ] && perror "Could not determine current webif version" if [ "$TARGET_WEBIF_VERSION" = "missing" ] || [ "$CURRENT_WEBIF_VERSION" -le "$TARGET_WEBIF_VERSION" ]; then : # Negate so NaN triggers else branch else perror "This update seems to be older than the server version you're currently running" fi # ************** Extract payload *************************** declare -rg TMPDIR=$(mktemp -d) [ -z "$TMPDIR" ] && perror "Could not create temporary directory for installer" dd "bs=$PAYLOAD_OFFSET" "if=$0" skip=1 | tar -z -x -C "$TMPDIR" RET=$? [ "$RET" -ne 0 ] && perror "Extracting installer payload failed with exit code $RET" declare -rg FILEDIR="$TMPDIR/files" # ********************************************************** # ************************** IPXE ************************** if [ -n "$TGZ_IPXE" ]; then [ -e "$TMPDIR/$TGZ_IPXE" ] || perror "$TGZ_IPXE missing from payload" echo "* iPXE source code" echo "Extracting" rm -rf -- "$PATH_IPXE" mkdir -p "$PATH_IPXE" tar -x -C "$PATH_IPXE" -f "$TMPDIR/$TGZ_IPXE" || perror "Could not extract $TGZ_IPXE to $PATH_IPXE" fixperms "$PATH_IPXE" taskmanager:taskmanager echo "Resetting pxe menu" mysql -e 'UPDATE openslx.property SET value = "invalid" WHERE name = "server-ip"' || pwarning "Could not reset pxe menu status; manual regeneration of menu required" echo "iPXE upgrade complete" # Trigger default compile in the background so the next regen doesn't take too long { cd "$PATH_IPXE/src" && sudo -u taskmanager nice -n 10 make bin/undionly.kkkpxe > /dev/null 2>&1 } & fi # ************************** SLX-Admin ********************* if [ -n "$TGZ_SLXADMIN" ]; then [ -e "$TMPDIR/$TGZ_SLXADMIN" ] || perror "$TGZ_SLXADMIN missing from payload." echo "* SLX-Admin: $CURRENT_WEBIF_VERSION -> $TARGET_WEBIF_VERSION" # Cheap hack: extract, then delete, extract again to get rid of old unused files. If the extraction fails, we don't leave # the user with a broken webif, because we didn't remove the directories yet tar -x -C "$PATH_SLXADMIN" -f "$TMPDIR/$TGZ_SLXADMIN" || perror "Could not extract $TGZ_SLXADMIN to $PATH_SLXADMIN" rm -rf -- "$PATH_SLXADMIN/inc" "$PATH_SLXADMIN/apis" "$PATH_SLXADMIN/modules" "$PATH_SLXADMIN/templates" tar -x -C "$PATH_SLXADMIN" -f "$TMPDIR/$TGZ_SLXADMIN" rm -- "$PATH_SLXADMIN/config.php.example" # Remove translation feature sed -i '/