diff --git a/src/linux/scripts/afterRemoveRPM.sh b/src/linux/scripts/afterRemoveRPM.sh index f77beb60c..064824517 100644 --- a/src/linux/scripts/afterRemoveRPM.sh +++ b/src/linux/scripts/afterRemoveRPM.sh @@ -113,16 +113,17 @@ if [ "$1" = "0" ]; then remove purge elif [ "$1" = "1" ]; then - # Call the purge function echo "Package is being upgraded" # Check the version of the package and if it's inferior to 1.5.0, we need to copy the variables.env file - if [ "$2" <= "1.5.0" ]; then + VERSION=$(rpm -q --queryformat '%{VERSION}' bunkerweb) + if [ "$VERSION" <= "1.5.0" ]; then echo "ℹ️ Copy /etc/bunkerweb/variables.env to /var/tmp/bunkerweb/variables.env" do_and_check_cmd cp -f /opt/bunkerweb/variables.env /var/tmp/variables.env do_and_check_cmd cp -f /opt/bunkerweb/ui.env /var/tmp/ui.env fi cp -f /etc/bunkerweb/variables.env /var/tmp/variables.env cp -f /etc/bunkerweb/ui.env /var/tmp/ui.env + cp -f /var/lib/bunkerweb/db.sqlite3 /var/tmp/db.sqlite3 exit 0 fi else diff --git a/src/linux/scripts/beforeInstall.sh b/src/linux/scripts/beforeInstall.sh index 3467140f7..2b5bdfa57 100644 --- a/src/linux/scripts/beforeInstall.sh +++ b/src/linux/scripts/beforeInstall.sh @@ -15,12 +15,30 @@ function do_and_check_cmd() { return 0 } -# Get the version of the package -VERSION=$(dpkg-query -W -f='${Version}' bunkerweb) - -if dpkg --compare-versions "$VERSION" lt "1.5.0"; then - echo "ℹ️ Copy /var/tmp/bunkerweb/variables.env to /etc/bunkerweb/variables.env" - do_and_check_cmd cp -f /opt/bunkerweb/variables.env /var/tmp/variables.env - echo "ℹ️ Copy /var/tmp/bunkerweb/variables.env to /etc/bunkerweb/variables.env" - do_and_check_cmd cp -f /opt/bunkerweb/ui.env /var/tmp/ui.env +# Check the os running +if [ -f /etc/os-release ]; then + . /etc/os-release + OS=$NAME + if [[ "$OS" == "Ubuntu" || "$OS" == "Debian" ]]; then + # Get the version of the package + VERSION=$(dpkg-query -W -f='${Version}' bunkerweb) + if dpkg --compare-versions "$VERSION" lt "1.5.0"; then + echo "ℹ️ Copy /var/tmp/bunkerweb/variables.env to /etc/bunkerweb/variables.env" + do_and_check_cmd cp -f /opt/bunkerweb/variables.env /var/tmp/variables.env + echo "ℹ️ Copy /var/tmp/bunkerweb/variables.env to /etc/bunkerweb/variables.env" + do_and_check_cmd cp -f /opt/bunkerweb/ui.env /var/tmp/ui.env + fi + elif [[ "$OS" == "CentOS Linux" || "$OS" == "Fedora" ]]; then + # Get the version of the package + VERSION=$(rpm -q --queryformat '%{VERSION}' bunkerweb) + if [ "$(printf '%s\n' "$VERSION" "$(echo '1.5.0' | tr -d ' ')" | sort -V | head -n 1)" = "$VERSION" ]; then + echo "ℹ️ Copy /var/tmp/bunkerweb/variables.env to /etc/bunkerweb/variables.env" + do_and_check_cmd cp -f /opt/bunkerweb/variables.env /var/tmp/variables.env + echo "ℹ️ Copy /var/tmp/bunkerweb/variables.env to /etc/bunkerweb/variables.env" + do_and_check_cmd cp -f /opt/bunkerweb/ui.env /var/tmp/ui.env + fi + fi +else + echo "❌ Error: /etc/os-release not found" + exit 1 fi \ No newline at end of file diff --git a/src/linux/scripts/bunkerweb-ui.sh b/src/linux/scripts/bunkerweb-ui.sh index 6c67bcdd8..ebfeea4ee 100755 --- a/src/linux/scripts/bunkerweb-ui.sh +++ b/src/linux/scripts/bunkerweb-ui.sh @@ -27,7 +27,7 @@ stop() { echo "Stopping UI service..." if [ -f "/var/tmp/bunkerweb/ui.pid" ]; then pid=$(cat /var/tmp/bunkerweb/ui.pid) - kill -SIGINT $pid + kill -s TERM $pid else echo "UI service is not running or the pid file doesn't exist." fi diff --git a/src/linux/scripts/postinstall.sh b/src/linux/scripts/postinstall.sh index da29345e6..19174a65d 100644 --- a/src/linux/scripts/postinstall.sh +++ b/src/linux/scripts/postinstall.sh @@ -15,38 +15,66 @@ function do_and_check_cmd() { return 0 } -#Start the nginx service -echo "Starting nginx service..." -systemctl start nginx +#Start the nginx service if it is not already running +if ! systemctl is-active nginx; then + echo "Starting nginx service..." + do_and_check_cmd systemctl start nginx +fi #Give all the permissions to the nginx user echo "Setting ownership for all necessary directories to nginx user and group..." -chown -R nginx:nginx /usr/share/bunkerweb /var/cache/bunkerweb /var/lib/bunkerweb /etc/bunkerweb /var/tmp/bunkerweb +do_and_check_cmd chown -R nginx:nginx /usr/share/bunkerweb /var/cache/bunkerweb /var/lib/bunkerweb /etc/bunkerweb /var/tmp/bunkerweb #Start bunkerweb service as nginx user and enable it to start on boot echo "Enabling and starting bunkerweb service..." -systemctl enable bunkerweb -systemctl start bunkerweb +do_and_check_cmd systemctl enable bunkerweb +do_and_check_cmd systemctl start bunkerweb #Start and enable bunkerweb-ui service echo "Enabling and starting bunkerweb-ui service..." -systemctl enable bunkerweb-ui -systemctl start bunkerweb-ui +do_and_check_cmd systemctl enable bunkerweb-ui +do_and_check_cmd systemctl start bunkerweb-ui # Copy old line from environment file to new one -echo "Copying old line from environment file to new one..." -while read line; do - echo "$line" >> /etc/bunkerweb/variables.env -done < /var/tmp/variables.env +# Check if old environment file exists +if [ -f /var/tmp/variables.env ]; then + echo "Old environment file found!" + echo "Copying old line from environment file to new one..." + while read line; do + echo "$line" >> /etc/bunkerweb/variables.env + done < /var/tmp/variables.env + # Remove old environment files + echo "Removing old environment files..." + do_and_check_cmd rm -f /var/tmp/variables.env +else + echo "Old environment file not found!" + exit 0 +fi -echo "Copying old line from ui environment file to new one..." -while read line; do - echo "$line" >> /etc/bunkerweb/ui.env -done < /var/tmp/ui.env +# Copy old line from ui environment file to new one +# Check if old environment file exists +if [ -f /var/tmp/ui.env ]; then + echo "Old ui environment file found!" + echo "Copying old line from ui environment file to new one..." + while read line; do + echo "$line" >> /etc/bunkerweb/ui.env + done < /var/tmp/ui.env + # Remove old environment files + echo "Removing old environment files..." + do_and_check_cmd rm -f /var/tmp/ui.env +else + echo "Old ui environment file not found!" + exit 0 +fi -# Remove old environment files -echo "Removing old environment files..." -rm -f /var/tmp/variables.env -rm -f /var/tmp/ui.env +# Check if old db.sqlite3 file exists +if [ -f /var/tmp/bunkerweb/db.sqlite3 ]; then + echo "Old db.sqlite3 file found!" + do_and_check_cmd cp /var/tmp/bunkerweb/db.sqlite3 /var/lib/bunkerweb/db.sqlite3 + do_and_check_cmd rm -f /var/lib/bunkerweb/db.sqlite3 +else + echo "Old db.sqlite3 file not found!" + exit 0 +fi echo "All services started and enabled successfully!" \ No newline at end of file diff --git a/tests/Dockerfile-centos b/tests/Dockerfile-centos index bdb369b75..82f823cb2 100644 --- a/tests/Dockerfile-centos +++ b/tests/Dockerfile-centos @@ -13,6 +13,9 @@ rm -f /lib/systemd/system/anaconda.target.wants/*; RUN dnf -y install sudo +RUN echo -e "[nginx-stable]\nname=nginx stable repo\nbaseurl=http://nginx.org/packages/centos/\$releasever/\$basearch/\ngpgcheck=1\nenabled=1\ngpgkey=https://nginx.org/keys/nginx_signing.key\nmodule_hotfixes=true" > /etc/yum.repos.d/nginx.repo +RUN sudo dnf install nginx-1.20.2 -y && sudo dnf install epel-release -y + VOLUME [/sys/fs/cgroup] CMD ["/usr/sbin/init"] \ No newline at end of file diff --git a/tests/Upgrade.py b/tests/Upgrade.py index 6708baa9a..542ddecce 100644 --- a/tests/Upgrade.py +++ b/tests/Upgrade.py @@ -2,6 +2,7 @@ import subprocess import sys import tempfile import os +import time distro = sys.argv[1] if distro == "ubuntu": @@ -71,7 +72,12 @@ if distro == "ubuntu": f.write(bash_script) f.flush() subprocess.run( - ["docker", "cp", f.name, "systemd-ubuntu:/data/install_nginx.sh"] + [ + "docker", + "cp", + f.name, + "systemd-ubuntu:/data/install_nginx.sh" + ] ) result = subprocess.run( [ @@ -399,34 +405,24 @@ if distro == "ubuntu": subprocess.run( [ "docker", - "exec", - "-it", + "rm", + "-f", "systemd-ubuntu", - "bash", - "-c", - "sudo apt remove -y nginx", ] ) subprocess.run( [ "docker", - "exec", - "-it", - "systemd-ubuntu", - "bash", - "-c", - "sudo apt purge -y nginx", - ] - ) - subprocess.run( - [ - "docker", - "exec", - "-it", - "systemd-ubuntu", - "bash", - "-c", - "sudo apt autoremove -y", + "run", + "-d", + "--name", + "systemd-{}".format(distro), + "--privileged", + "-v", + "/sys/fs/cgroup:/sys/fs/cgroup", + "-v", + "deb:/data", + "jrei/systemd-ubuntu:22.04", ] ) subprocess.run( @@ -452,7 +448,15 @@ if distro == "ubuntu": ] ) subprocess.run( - ["docker", "exec", "-it", "systemd-ubuntu", "bash", "-c", "sudo apt update"] + [ + "docker", + "exec", + "-it", + "systemd-ubuntu", + "bash", + "-c", + "sudo apt update" + ] ) subprocess.run( [ @@ -1416,7 +1420,7 @@ elif distro == "fedora": "docker", "exec", "-it", - "systemd-centos", + "systemd-fedora", "bash", "-c", "[ -d /var/lib/bunkerweb ]", @@ -1433,7 +1437,7 @@ elif distro == "fedora": "docker", "exec", "-it", - "systemd-centos", + "systemd-fedora", "bash", "-c", "[ -d /etc/bunkerweb ]", @@ -1542,7 +1546,7 @@ elif distro == "fedora": "systemd-fedora", "bash", "-c", - "cat /usr/share/bunkerweb/VERSION", + "cat /opt/bunkerweb/VERSION", ], capture_output=True, ) @@ -1557,7 +1561,7 @@ elif distro == "fedora": "systemd-fedora", "bash", "-c", - "sudo dnf upgrade -y", + "sudo dnf upgrade --refresh -y", ] ) subprocess.run( @@ -1568,7 +1572,77 @@ elif distro == "fedora": "systemd-fedora", "bash", "-c", - "sudo dnf install -y /data/bunkerweb.deb", + "sudo dnf install dnf-plugin-system-upgrade -y", + ] + ) + subprocess.run( + [ + "docker", + "exec", + "-it", + "systemd-fedora", + "bash", + "-c", + "sudo dnf system-upgrade download --releasever=37 -y", + ] + ) + subprocess.run( + [ + "docker", + "exec", + "-it", + "systemd-fedora", + "bash", + "-c", + "sudo dnf system-upgrade reboot", + ] + ) + # Checking container is running + def start_container(): + subprocess.run(["docker", "start", "systemd-fedora"]) + + def check_container_status(): + result = subprocess.run(["docker", "inspect", "systemd-fedora"], stdout=subprocess.PIPE) + return "running" in str(result.stdout) + + while True: + start_container() + time.sleep(30) + if not check_container_status(): + continue + break + + subprocess.run( + [ + "docker", + "exec", + "-it", + "systemd-fedora", + "bash", + "-c", + "sudo dnf install -y curl gnupg2 ca-certificates redhat-lsb-core", + ] + ) + subprocess.run( + [ + "docker", + "exec", + "-it", + "systemd-fedora", + "bash", + "-c", + "sudo dnf install nginx-1.22.1 -y", + ] + ) + subprocess.run( + [ + "docker", + "exec", + "-it", + "systemd-fedora", + "bash", + "-c", + "sudo dnf upgrade -y /data/bunkerweb.rpm", ] ) # Checking version @@ -1976,27 +2050,40 @@ elif distro == "centos": # Upgrading test print("Upgrading bunkerweb...") - # Installing official package subprocess.run( [ "docker", - "exec", - "-it", + "rm", + "-f", "systemd-centos", - "bash", - "-c", - "sudo dnf remove -y nginx", ] ) subprocess.run( [ + "sudo", "docker", - "exec", - "-it", + "build", + "-t", + "systemd-centos", + "-f", + "tests/Dockerfile-centos", + ".", + ] + ) + subprocess.run( + [ + "sudo", + "docker", + "run", + "-d", + "--name", + "systemd-centos", + "--privileged", + "-v", + "/sys/fs/cgroup:/sys/fs/cgroup", + "-v", + "deb:/data", "systemd-centos", - "bash", - "-c", - "sudo dnf install -y nginx-1.20.2", ] ) subprocess.run( @@ -2049,6 +2136,28 @@ elif distro == "centos": print("Old version:", old_version.stdout.decode().strip()) # Upgrading package + subprocess.run( + [ + "docker", + "exec", + "-it", + "systemd-centos", + "bash", + "-c", + "sudo dnf remove -y nginx", + ] + ) + subprocess.run( + [ + "docker", + "exec", + "-it", + "systemd-centos", + "bash", + "-c", + "sudo dnf autoremove -y", + ] + ) subprocess.run( [ "docker",