diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 248c31abca9dff84dc0c1093945441c5e5de27a4..8583b52039f4d0288108cd0d410fdcaaf03c646a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -1,5 +1,9 @@ -# Taken and modified from https://docs.gitlab.com/ee/ci/docker/using_kaniko.html -# Licensed under CC BY-SA 4.0 (see LICENSE-3RD-PARTY.md) +# Taken and modified from the GitLab documentation +# Title of the page: "Building a Docker image with kaniko" +# Author: (c) 2011-present GitLab B.V. +# URL: https://docs.gitlab.com/ee/ci/docker/using_kaniko.html#building-a-docker-image-with-kaniko +# License: CC BY-SA 4.0 (https://creativecommons.org/licenses/by-sa/4.0) + image: name: gcr.io/kaniko-project/executor:debug entrypoint: [""] @@ -10,6 +14,6 @@ docker-build: - mkdir -p /kaniko/.docker - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_REGISTRY_USER\",\"password\":\"$CI_REGISTRY_PASSWORD\"}}}" > /kaniko/.docker/config.json script: - - "/kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_TAG --build-arg vcs-rev=$CI_COMMIT_SHORT_SHA" + - "/kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --destination $CI_REGISTRY_IMAGE:$CI_COMMIT_BRANCH --build-arg vcsrev=$CI_COMMIT_SHORT_SHA" only: - - master + - played-around \ No newline at end of file diff --git a/Dockerfile b/Dockerfile index d38586e27c64adcc7a4808bb3091640e9c06bf96..bbb57d7101ed381e338fe4a9bd29f1a1da82d339 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,38 +1,75 @@ -# JRE base +# Base image on OpenJDK 11 JRE FROM openjdk:11.0-jre-slim +LABEL org.label-schema.name="clspigot-docker" +LABEL org.label-schema.description="Docker Container for an Papermc Server" +LABEL org.label-schema.vcs-url="https://gitlab.cl.uni-heidelberg.de/tmueller/clspigot-docker/" +LABEL maintainer="mueller@tobistech.ovh" +LABEL org.label-schema.schema-version=1.0 + +# Execute all the following instructions in the container as user "root" USER root -ARG user=papermc +# User and group used to run the PaperMC server (build time argument, defaulting to "papermc") +ARG papermc_user=papermc +ARG papermc_group=papermc +ARG vcsref="" + +LABEL org.label-schema.vcs-ref=${vcsref} -# Environment variables +# Default environment variables +# The Java Options are Aikar's flags to optimize a few things, among them garbage collection: +# https://aikar.co/2018/07/02/tuning-the-jvm-g1gc-garbage-collector-flags-for-minecraft/ ENV MC_VERSION="latest" \ PAPER_BUILD="latest" \ MC_RAM="2G" \ - JAVA_OPTS="-XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true" - -COPY papermc.sh . -COPY backup.sh . -COPY restore.sh . -RUN apt-get update \ - && apt-get install wget curl jq webp screen cron -y \ - && rm -rf /var/lib/apt/lists/* \ - && mkdir /papermc \ - && mkdir /backup \ - && chmod +x papermc.sh \ - && chmod +x backup.sh \ - && chmod +x restore.sh -RUN groupadd -g 61000 papermc -RUN useradd -g 61000 -l -M -s /bin/false -u 61000 papermc -RUN chown -R ${user} /papermc -RUN chown -R ${user} /backup - -USER papermc - - -# Start script -CMD ["sh", "./papermc.sh"] - -# Container setup + JAVA_OPTS="-XX:+UseG1GC -XX:+ParallelRefProcEnabled -XX:MaxGCPauseMillis=200 -XX:+UnlockExperimentalVMOptions -XX:+DisableExplicitGC -XX:+AlwaysPreTouch -XX:G1NewSizePercent=30 -XX:G1MaxNewSizePercent=40 -XX:G1HeapRegionSize=8M -XX:G1ReservePercent=20 -XX:G1HeapWastePercent=5 -XX:G1MixedGCCountTarget=4 -XX:InitiatingHeapOccupancyPercent=15 -XX:G1MixedGCLiveThresholdPercent=90 -XX:G1RSetUpdatingPauseTimePercent=5 -XX:SurvivorRatio=32 -XX:+PerfDisableSharedMem -XX:MaxTenuringThreshold=1 -Dusing.aikars.flags=https://mcflags.emc.gs -Daikars.new.flags=true" \ +#Man kann nun auch die guid des eigenen benutzers passen und mus somit nichtmehr die rechte des hostsystems anpassen, bitte die GUID eines non-root users nehmen der an dem speicherort zugriff hat. + GUID=61000 \ + TZ=Europe/Berlin +# Copy over the three script files (entrypoint, backup and restore) +COPY papermc.sh / +COPY backup.sh / +COPY restore.sh / +COPY health.sh / +COPY test.sh / +# Set Timezone +RUN ln -snf /usr/share/zoneinfo/$TZ /etc/localtime && echo $TZ > /etc/timezone + +# Create /papermc and /backup directories +RUN mkdir /papermc /backup + +# Add papermc user and group, then change ownership of /papermc and /backup to this user +RUN groupadd -g ${GUID} ${papermc_group} +RUN useradd -g ${GUID} -l -M -s /bin/false -u ${GUID} ${papermc_user} +RUN chown -R ${papermc_user} /papermc +RUN chown -R ${papermc_user} /backup + + +# Get package lists, install needed packages, then remove the package lists again (to save space) +# "webp" is needed to generate images with better compression for the dynamic map (optional, needs further configuration in dynmap-plugin´s config file) +# "cron" is for making it possible to restart the server every day, week or soemthing similar (its optional) +# "curl" is required for getting the server initialised +# "screen" is required for getting the server running at all +RUN apt-get update && apt-get install -y \ + cron \ + curl \ + jq \ + screen \ + webp \ + && rm -rf /var/lib/apt/lists/* + +# Execute all following commands as the papermc user +USER $papermc_user + +# Start script and also entrypoint for the container +CMD ["sh", "/papermc.sh"] + +#Alle 10s testen ob exit code = 0 +HEALTHCHECK --interval=10s --timeout=10s --start-period=30s \ + CMD ["sh", "/health.sh"] + +# Container setup, specifies needed ports. May be adjusted or overwritten if plugins need other ports EXPOSE 25565/tcp EXPOSE 25565/udp +#Specifies the datastructure in the container VOLUME /papermc VOLUME /backup diff --git a/README-dev.md.txt b/README-dev.md.txt new file mode 100644 index 0000000000000000000000000000000000000000..a248204a9d2a191a604d0abbcf91db2a8e344f6b --- /dev/null +++ b/README-dev.md.txt @@ -0,0 +1,25 @@ +# TODOs (Next Merge request) + +- [ ] Automatisierung des testscripts, evtl in git so integrieren damit wenn alle essentiellen tests bestanden sind +und ein Approval gegeben ist automatisch in meinem branch played-around gemerged werden (if possible) +- [ ] + +# Future Plans +- [ ] Entwikclung einer Weboberfläche für einfache administration (Jedoch keine klassische Ready-to-buy-Lösung sondern eine minimalistische effiziente Variante +In erster Linie für Verwaltung von Spielinternen dingen wie z.B. Rechteverwaltung, Spielerliste, Banliste, evtl Aktuelle Auslastung etc +- [ ] Automatische Pluginupdates +- [ ] +- [ ] +- [ ] +- [ ] + + +# Grundlegende Tests die bestanden werden müssen + +- Healthcheck sollte ein healthy bzw exit code 0 zurückgeben +- latest.log sollte in der letzten Zeile Timings Reset beinhalten (testscript folgt noch, das ist aber eher ein milestone) + +# Optionale Tests, deren Priorität zweitrangig sind + +- Dynmap webserver files sind für webserver lesbar und .json files beschreibbar (Chatfunktion) +- `backup.sh` kann von `EssentialsX (Plugin)` gestartet werden. \ No newline at end of file diff --git a/backup.sh b/backup.sh old mode 100644 new mode 100755 diff --git a/health.sh b/health.sh new file mode 100644 index 0000000000000000000000000000000000000000..4e9b81b23c329b5e34b0373885b1fc7164890ab0 --- /dev/null +++ b/health.sh @@ -0,0 +1,8 @@ +#!/bin/bash +screen -S mc -X select . ; SCRUN=$? #Queryabfrage um rauszufindne ob screensession noch aktiv ist. Return: bool + if [ ${SCRUN} = 0 ] + then # Server läuft + exit 0 + else # Server läuft nicht + exit 1 + fi diff --git a/papermc.sh b/papermc.sh old mode 100644 new mode 100755 index 80b03f060c299fc16be4f339dba8cbc4713e8e46..088ea73295379e457f3522d236a6633064635844 --- a/papermc.sh +++ b/papermc.sh @@ -1,38 +1,51 @@ #!/bin/bash +# Configuration +PAPER_URL=https://papermc.io/api/v2/projects/paper +TM_BASE_AUTH=tmueller:supercooles_passwort_hier_einfuegen +TM_BASE_URL=https://tobistechblog.de/uni/initial.tar.gz + # Enter server directory -cd papermc +cd /papermc -# Perform initial setup -urlPrefix=https://papermc.io/api/v2/projects/paper +# Determine minecraft version (MC_VERSION) and build number (PAPER_BUILD). If they are set to +# "latest", use PaperMC's API to determine which version and build number is the latest. if [ ${MC_VERSION} = latest ] then # Get the latest MC version - MC_VERSION=$(wget -qO - $urlPrefix | jq -r '.versions[-1]') # "-r" is needed because the output has quotes otherwise + MC_VERSION=$(curl ${PAPER_URL} | jq -r '.versions[-1]') # "-r" is needed because the output has quotes otherwise fi -urlPrefix=${urlPrefix}/versions/${MC_VERSION} + +PAPER_URL=${PAPER_URL}/versions/${MC_VERSION} if [ ${PAPER_BUILD} = latest ] then - # Get the latest build - PAPER_BUILD=$(wget -qO - $urlPrefix | jq '.builds[-1]') + # Get the latest build + PAPER_BUILD=$(curl ${PAPER_URL} | jq '.builds[-1]') fi -JAR_NAME=papermc-${MC_VERSION}-${PAPER_BUILD}.jar +# Generate the name of the server JAR file we are about to download from +# the minecraft version and the paper build number we have determined above. +JAR_NAME=paper-${MC_VERSION}-${PAPER_BUILD}.jar +# If the jar is not yet downloaded, download it if [ ! -e ${JAR_NAME} ] then + # Cleanup older, left-over JAR files rm -f *.jar - wget ${urlPrefix}/builds/${PAPER_BUILD}/downloads/paper-${MC_VERSION}-${PAPER_BUILD}.jar -O ${JAR_NAME} + # Download the file + curl ${PAPER_URL}/builds/${PAPER_BUILD}/downloads/${JAR_NAME} --output ${JAR_NAME} + # Accept EULA if [ ! -e eula.txt ] - then - java -jar ${JAR_NAME} - sed -i 's/false/true/g' eula.txt + then + java -jar ${JAR_NAME} + sed -i 's/false/true/g' eula.txt fi fi -# minecraft base image laden + +# Download base plugins, worlds and configuration by Tobias Müller #if [ ! -d "plugins" ] # then -# curl -u https://tobistechblog.de/uni/initial.tar.gz --output initial.tar.gz +# curl -u ${TM_BASE_AUTH} ${TM_BASE_URL} --output initial.tar.gz # tar -xvzf initial.tar.gz #fi @@ -45,15 +58,16 @@ exec_stop () { } while true - do - screen -S mc -X select . ; SCRUN=$? #Queryabfrage um rauszufindne ob screensession noch aktiv ist. Return: bool - if [ ${SCRUN} = 0 ] - then # Server läuft, zeige letzte zeilen vom log an... - tail /papermc/logs/latest.log - trap exec_stop TERM INT # SIGTERM ^C etc abfangen und funktion für gracefull shutdown aufrufen - sleep 10 # CPU Zeit sparen da eine downtime von max 10s + Startzeit des javapplets vertretbar ist kann bei bedarf beliebig erhöht werden. - else # Server läuft nicht, abgestürzt, gestoppt über minecraft /stop - screen -dmS mc java -server -Xms${MC_RAM} -Xmx${MC_RAM} ${JAVA_OPTS} -jar ${JAR_NAME} nogui #Startupbefehl inkl. ENVs vom Dockerfile - screen -wipe # sollte der server mal abstürzen oder sont irgednwie ein fehler auftreten wird damit zombiesessions entfernt. - fi - done + do + screen -S mc -X select . ; SCRUN=$? #Queryabfrage um rauszufindne ob screensession noch aktiv ist. Return: bool + if [ ${SCRUN} = 0 ] + then # Server läuft, zeige letzte zeilen vom log an... + clear # Aber erstmal sauber machen ^^ + tail /papermc/logs/latest.log + trap exec_stop TERM INT # SIGTERM ^C etc abfangen und funktion für gracefull shutdown aufrufen + sleep 10 # CPU Zeit sparen da eine downtime von max 10s + Startzeit des javapplets vertretbar ist kann bei bedarf beliebig erhöht werden. + else # Server läuft nicht, abgestürzt, gestoppt über minecraft /stop + screen -dmS mc java -server -Xms${MC_RAM} -Xmx${MC_RAM} ${JAVA_OPTS} -jar ${JAR_NAME} nogui #Startupbefehl inkl. ENVs vom Dockerfile + screen -wipe # sollte der server mal abstürzen oder sont irgednwie ein fehler auftreten wird damit zombiesessions entfernt. + fi + done diff --git a/restore.sh b/restore.sh old mode 100644 new mode 100755 diff --git a/test.sh b/test.sh new file mode 100644 index 0000000000000000000000000000000000000000..0e85ab7bbffddef9c8db6b2a7228752eb2f1590f --- /dev/null +++ b/test.sh @@ -0,0 +1,27 @@ +#!/bin/bash +bash /health.sh +if [ $? = 1 ] + then + echo "Health: Test_Failed" + sleep 30 + exit 1 + else + echo "Health: Test_Passed" +fi + +screen -r mc -p 0 -X stuff "backup^M" +echo "Give it some time to start the backup process..." +sleep 10 +LEER="true" && ls /backup/* 2> /dev/null > /dev/null && LEER="false" +if [ $LEER = "true" ] + then + echo "Backup: Test_Failed" + else + echo "Backup: Test_Passed" +fi + +tail /papermc/logs/latest.log + +echo "Optionale Tests:" + +ls -lanR /papermc/plugins/dynmap/web/standalone \ No newline at end of file