diff --git a/src/minecraft_be/Containerfile b/src/minecraft_be/Containerfile new file mode 100644 index 0000000..2e39bb6 --- /dev/null +++ b/src/minecraft_be/Containerfile @@ -0,0 +1,48 @@ +### +### Build Variables +### +FROM localhost/debian:latest + +# deploy options +# -p (port) and -v (volume) both go host:container +LABEL deployopts="\ +-p 19132:19132/udp \ +-p 19133:19133/udp \ +-v /srv/vol/minecraft_be/worlds:/vol/worlds" + +# Build variables +# uid that the files owner user should have +ARG FILESUID=5000 + +### +### General Setup +### + +# install packages we want +RUN apt update -y && apt install -y libcurl4 + +# create minecraft server user with file owner UID +RUN addgroup --gid $FILESUID mcadmin && \ + adduser mcadmin --ingroup mcadmin --uid $FILESUID --disabled-password --gecos "Minecraft Server Admin" --shell /usr/sbin/nologin && \ + rm /home/mcadmin/.bashrc + +# Copy custom scripts +COPY assets/bin/ /usr/local/bin/ + +### +### Minecraft +### +WORKDIR /home/mcadmin + +# download Minecraft Bedrock dedicated server +RUN url=$(wget -q https://www.minecraft.net/en-us/download/server/bedrock/ -O - | grep -Eo 'https://[^ ]+bin-linux/bedrock-server-[^ ]+\.zip' | head -n 1) && \ + wget $url && \ + unzip $(basename $url) && \ + rm $(basename $url) && \ + chown -R mcadmin:mcadmin ./ + +# copy units to systemd +COPY assets/systemd/ /etc/systemd/system/ + +# enable systemd units +RUN systemctl enable mcbe.service mcbe-backup.timer diff --git a/src/minecraft_be/assets/bin/mcbe-backup b/src/minecraft_be/assets/bin/mcbe-backup new file mode 100755 index 0000000..7a8c638 --- /dev/null +++ b/src/minecraft_be/assets/bin/mcbe-backup @@ -0,0 +1,43 @@ +#!/bin/bash + +do_backup() { + mcbe-stop $1 "Shutting down server for scheduled daily backup." + rsyn /home/mcadmin/worlds/ /vol/worlds/ + systemctl start mcbe + rm /home/mcadmin/backup.lock +} + +# Check if server is started +status=$(systemctl show "$service" -p ActiveState --value) +# if service is not active, do backup immediately and exit +if [[ $status == inactive ]]; then + rsyn /home/mcadmin/worlds/ /vol/worlds/ + rm /home/mcadmin/backup.lock + exit 0 +fi + +# Check if any players present +present=$(mcbe-exec list | cut -d ' ' -f 3 | cut -d '/' -f 1) +# if no players are present, do backup immediately and exit +if [[ $present == 0 ]]; then + do_backup 0 + exit 0 +fi + +cd /home/mcadmin + +# create backup.attempts file if it doesn't exist +if [[ ! -e backup.attempts ]]; then + echo "0" > backup.attempts +fi + +# read backup.wait and add one to it +attempts=$(( $(cat backup.attempts) + 1 )) + +# if 12 or more attempts have already been made, go ahead and force a shutdown anyways +if [[ $attempts -ge 12 ]]; then + do_backup 10 +# otherwise, schedule another attempt in an hour +else + systemd-run --on-active=1h $0 +fi diff --git a/src/minecraft_be/assets/bin/mcbe-exec b/src/minecraft_be/assets/bin/mcbe-exec new file mode 100755 index 0000000..38a9107 --- /dev/null +++ b/src/minecraft_be/assets/bin/mcbe-exec @@ -0,0 +1,5 @@ +#!/bin/bash +today=$(date "+%Y-%m-%d %H:%M:%S") +echo "$*" > /run/mcbe +sleep 0.1 +journalctl -u mcbe -S "$today" -o cat diff --git a/src/minecraft_be/assets/bin/mcbe-stop b/src/minecraft_be/assets/bin/mcbe-stop new file mode 100755 index 0000000..e56192b --- /dev/null +++ b/src/minecraft_be/assets/bin/mcbe-stop @@ -0,0 +1,36 @@ +#!/bin/bash + +if [[ -z $1 ]]; then + # default warning time is 10 seconds + time=10 +elif [[ $1 -ge 0 && $1 -le 60 ]]; then + time=$1 +else + echo "Invalid value for time-to-shutdown: only values between 0 and 60 seconds are accepted." + exit 2 +fi + +# if 2nd arg is given (and shutdown is not immediate), say it before shutting down +if [[ -z $2 ]]; then + mcbe-exec say "$2" +fi + +echo "Shutting down server in $time seconds..." +count=$time + +while [[ $count -gt 0 ]]; do + # print warning every 10 seconds + if [[ $(( $count % 10 )) -eq 0 ]]; then + mcbe-exec say "Server shutting down in ${count} seconds!" + fi + + # print warning every second for last 5 seconds + if [[ $count -le 5 ]]; then + mcbe-exec say "Shutting down in ${count}s..." + fi + + count=$((count-1)) + sleep 1 +done + +mcbe-exec stop diff --git a/src/minecraft_be/assets/systemd/mcbe-backup.service b/src/minecraft_be/assets/systemd/mcbe-backup.service new file mode 100644 index 0000000..de120f2 --- /dev/null +++ b/src/minecraft_be/assets/systemd/mcbe-backup.service @@ -0,0 +1,9 @@ +[Unit] +After=mcbe.service +Description=Back up Minecraft Bedrock Edition server +Requisite=mcbe.service + +[Service] +ExecStart=/usr/local/bin/mcbe-backup +KillMode=none +Type=oneshot \ No newline at end of file diff --git a/src/minecraft_be/assets/systemd/mcbe-backup.timer b/src/minecraft_be/assets/systemd/mcbe-backup.timer new file mode 100644 index 0000000..11f007b --- /dev/null +++ b/src/minecraft_be/assets/systemd/mcbe-backup.timer @@ -0,0 +1,9 @@ +[Unit] +Description=Back up Minecraft Bedrock Edition server daily at 4:05 AM + +[Timer] +# Every 2 hours: OnCalendar=0/2:00:00 +OnCalendar=*-*-* 04:05 + +[Install] +WantedBy=timers.target diff --git a/src/minecraft_be/assets/systemd/mcbe.service b/src/minecraft_be/assets/systemd/mcbe.service new file mode 100644 index 0000000..1cca06d --- /dev/null +++ b/src/minecraft_be/assets/systemd/mcbe.service @@ -0,0 +1,27 @@ +[Unit] +# Implicit needs are explicitly needed to survive shutdown till stop finishes +After=network.target +BindsTo=mcbe.socket +Description=Minecraft Bedrock Edition server + +[Service] +ExecReload=/usr/local/bin/mcbe-exec reload +ExecStop=/usr/local/bin/mcbe-stop +ExecStart=/bin/bash -c 'LD_LIBRARY_PATH=. ./bedrock_server' +User=mcadmin +WorkingDirectory=/home/mcadmin +Restart=on-failure +StandardInput=socket +StandardOutput=journal +StandardError=journal +Type=simple +# Thanks for the security agowa338 +PrivateUsers=true +ProtectHome=true +ProtectControlGroups=true +ProtectKernelModules=true +ProtectKernelTunables=true +ProtectSystem=full + +[Install] +WantedBy=multi-user.target diff --git a/src/minecraft_be/assets/systemd/mcbe.socket b/src/minecraft_be/assets/systemd/mcbe.socket new file mode 100644 index 0000000..6a951df --- /dev/null +++ b/src/minecraft_be/assets/systemd/mcbe.socket @@ -0,0 +1,8 @@ +[Unit] +BindsTo=mcbe.service + +[Socket] +ListenFIFO=/run/mcbe +RemoveOnStop=true +SocketMode=600 +SocketUser=mcadmin diff --git a/src/minecraft_bedrock/docs/resources.md b/src/minecraft_be/docs/resources.md similarity index 100% rename from src/minecraft_bedrock/docs/resources.md rename to src/minecraft_be/docs/resources.md diff --git a/src/minecraft_bedrock/Containerfile b/src/minecraft_bedrock/Containerfile deleted file mode 100644 index fbed82f..0000000 --- a/src/minecraft_bedrock/Containerfile +++ /dev/null @@ -1,77 +0,0 @@ -### -### Build Variables -### -FROM localhost/debian:latest - -# deploy options -# -p (port) and -v (volume) both go host:container -LABEL deployopts="\ --p 19132:19132/udp \ --p 19133:19133/udp \ --v /srv/vol/minecraft_bedrock/backup:/vol/backup" - -# Build variables -# uid that the files owner user should have -ARG FILESUID=5000 - -### -### General Setup -### - -# install packages we want -RUN apt update -y && apt install -y libcurl4 - -# create minecraft server user with file owner UID -RUN addgroup --gid $FILESUID mcadmin && \ - adduser mcadmin --ingroup mcadmin --uid $FILESUID --disabled-password --gecos "Minecraft Server Admin" --shell /usr/sbin/nologin && \ - rm /home/mcadmin/.bashrc - -### -### Minecraft -### - -# download Minecraft Bedrock dedicated server -RUN url=$(wget -q https://www.minecraft.net/en-us/download/server/bedrock/ -O - | grep -Eo 'https://[^ ]+bin-linux/bedrock-server-[^ ]+\.zip' | head -n 1) && \ - wget $url && \ - unzip $(basename $url) && \ - rm $(basename $url) - -### -### Gitea -### - -# dowload gitea -RUN wget https://dl.gitea.io/gitea/${giteav}/gitea-${giteav}-linux-amd64 && \ - mv gitea /usr/local/bin/gitea && \ - chmod +x /usr/local/bin/gitea - -# make directories gitea needs -RUN mkdir -p /var/lib/gitea/ && \ - cd /var/lib/gitea/ && \ - mkdir custom data log && \ - chown -R gitea:gitea /var/lib/gitea/ && \ - chmod -R 750 /var/lib/gitea/ - -# copy gitea config template -COPY assets/app.ini.esh /etc/gitea/ -# template config file -RUN cd /etc/gitea/ && \ - esh app.ini.esh > app.ini && \ - rm app.ini.esh && \ - chmod -R +r /etc/gitea/ - -COPY assets/gitea.service /etc/systemd/system/ - -### -### Crontab -### -COPY assets/crontab /root/ -RUN crontab -u gitea /root/crontab - -### -### Bugfix -### - -# execute command to workaround bug in cron -COPY bugfix/cronfix /root/ -RUN chmod +x /root/cronfix && /root/cronfix