From b51b19b3c056f9c4aaa9e4d9baaa13bb62040b92 Mon Sep 17 00:00:00 2001 From: Michael Reichenbach Date: Sat, 23 Mar 2019 14:12:58 +0100 Subject: [PATCH 1/4] feat(minecraft-server): replace environment variables in configs Add additional setup script that replaces environment variables inside config files. Closes #298 --- minecraft-server/Dockerfile | 29 ++++++++++--------- .../start-finalSetup04ServerProperties | 2 +- .../start-finalSetup05EnvVariables | 15 ++++++++++ 3 files changed, 31 insertions(+), 15 deletions(-) create mode 100644 minecraft-server/start-finalSetup05EnvVariables diff --git a/minecraft-server/Dockerfile b/minecraft-server/Dockerfile index 5f5cfc6d..dc02b3af 100644 --- a/minecraft-server/Dockerfile +++ b/minecraft-server/Dockerfile @@ -3,19 +3,19 @@ FROM openjdk:8u171-jre-alpine LABEL maintainer "itzg" RUN apk add --no-cache -U \ - openssl \ - imagemagick \ - lsof \ - su-exec \ - shadow \ - bash \ - curl iputils wget \ - git \ - jq \ - mysql-client \ - tzdata \ - rsync \ - python python-dev py2-pip + openssl \ + imagemagick \ + lsof \ + su-exec \ + shadow \ + bash \ + curl iputils wget \ + git \ + jq \ + mysql-client \ + tzdata \ + rsync \ + python python-dev py2-pip RUN pip install mcstatus @@ -71,7 +71,8 @@ ENV UID=1000 GID=1000 \ JVM_XX_OPTS="-XX:+UseG1GC" MEMORY="1G" \ TYPE=VANILLA VERSION=LATEST FORGEVERSION=RECOMMENDED SPONGEBRANCH=STABLE SPONGEVERSION= LEVEL=world \ PVP=true DIFFICULTY=easy ENABLE_RCON=true RCON_PORT=25575 RCON_PASSWORD=minecraft \ - LEVEL_TYPE=DEFAULT GENERATOR_SETTINGS= WORLD= MODPACK= MODS= SERVER_PORT=25565 ONLINE_MODE=TRUE CONSOLE=true SERVER_NAME="Dedicated Server" + LEVEL_TYPE=DEFAULT GENERATOR_SETTINGS= WORLD= MODPACK= MODS= SERVER_PORT=25565 ONLINE_MODE=TRUE CONSOLE=true SERVER_NAME="Dedicated Server" \ + REPLACE_ENV_VARIABLES="FALSE" ENV_VARIABLE_PREFIX="CFG_" COPY start* / RUN dos2unix /start* && chmod +x /start* diff --git a/minecraft-server/start-finalSetup04ServerProperties b/minecraft-server/start-finalSetup04ServerProperties index 17cd52ab..a137fb00 100755 --- a/minecraft-server/start-finalSetup04ServerProperties +++ b/minecraft-server/start-finalSetup04ServerProperties @@ -132,4 +132,4 @@ else echo "server.properties already created, skipping" fi -exec /start-minecraftFinalSetup $@ +exec /start-finalSetup05EnvVariables $@ diff --git a/minecraft-server/start-finalSetup05EnvVariables b/minecraft-server/start-finalSetup05EnvVariables new file mode 100644 index 00000000..b209034e --- /dev/null +++ b/minecraft-server/start-finalSetup05EnvVariables @@ -0,0 +1,15 @@ +#!/bin/bash + +if [ "$REPLACE_ENV_VARIABLES" = "TRUE" ]; then + echo "Replacing env variables in configs that match the prefix $ENV_VARIABLE_PREFIX..." + while IFS='=' read -r name value ; do + # check if name of env variable matches the prefix + # sanity check environment variables to avoid code injections + if [[ "$name" = $ENV_VARIABLE_PREFIX* ]] && [[ $value =~ ^[0-9a-zA-Z_\-:/=?.+]*$ ]] && [[ $name =~ ^[0-9a-zA-Z_\-]*$ ]]; then + echo "$name = $value" + find /data/ -type f -exec sed -i 's#${'"$name"'}#'"$value"'#g' {} \; + fi + done < <(env) +fi + +exec /start-minecraftFinalSetup $@ \ No newline at end of file From 67cf80d70f211ab44b43d8f436497b1e0d7f9c4b Mon Sep 17 00:00:00 2001 From: Michael Reichenbach Date: Sat, 23 Mar 2019 14:26:48 +0100 Subject: [PATCH 2/4] docs(minecraft-server): for dynamic env variable replacement Add documentation and examples for dynamic replacement of env variables. Closes #298 --- minecraft-server/README.md | 79 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 79 insertions(+) diff --git a/minecraft-server/README.md b/minecraft-server/README.md index 49f87f25..f7675676 100644 --- a/minecraft-server/README.md +++ b/minecraft-server/README.md @@ -202,6 +202,85 @@ This works well if you want to have a common set of modules in a separate location, but still have multiple worlds with different server requirements in either persistent volumes or a downloadable archive. +### Replacing variables inside configs + +Sometimes you have mods or plugins that require configuration information that is only available at runtime. +For example if you need to configure a plugin to connect to a database, +you don't want to include this information in your Git repository or Docker image. +Or maybe you have some runtime information like the server name that needs to be set +in your config files after the container starts. + +For those cases there is the option to replace defined variables inside your configs +with environment variables defined at container runtime. + +If you set the enviroment variable `REPLACE_ENV_VARIABLES` to `TRUE` the startup script +will go thru all files inside your `/data` volume and replace variables that match your +defined environment variables. Variables that you want to replace need to be wrapped +inside `${YOUR_VARIABLE}` curly brackets and prefixed with a dollar sign. This is the regular +syntax for enviromment variables inside strings or config files. + +Optionally you can also define a prefix to only match predefined enviroment variables. + +`ENV_VARIABLE_PREFIX="CFG_"` <-- this is the default prefix + +There are some limitations to what characters you can use. + +| Type | Allowed Characters | +| ----- | ------------------- | +| Name | `0-9a-zA-Z_-` | +| Value | `0-9a-zA-Z_-:/=?.+` | + +Here is a full example where we want to replace values inside a `database.yml`. + +```yml +... +database: + host: ${CFG_DB_HOST} + name: ${CFG_DB_NAME} + password: ${CFG_DB_PASSWORD} +``` + +This is how your `docker-compose.yml` file could look like: + +```yml +version: '3' +# Other docker-compose examples in /examples + +services: + minecraft: + image: itzg/minecraft-server + ports: + - "25565:25565" + volumes: + - "mc:/data" + environment: + EULA: "TRUE" + CONSOLE: "false" + ENABLE_RCON: "true" + RCON_PASSWORD: "testing" + RCON_PORT: 28016 + # enable env variable replacement + REPLACE_ENV_VARIABLES: "TRUE" + # define an optional prefix for your env variables you want to replace + ENV_VARIABLE_PREFIX: "CFG_" + # and here are the actual variables + CFG_DB_HOST: "http://localhost:3306" + CFG_DB_NAME: "minecraft" + CFG_DB_PASSWORD: "ug23u3bg39o-ogADSs" + restart: always + rcon: + image: itzg/rcon + ports: + - "4326:4326" + - "4327:4327" + volumes: + - "rcon:/opt/rcon-web-admin/db" + +volumes: + mc: + rcon: +``` + ## Running a Bukkit/Spigot server Enable Bukkit/Spigot server mode by adding a `-e TYPE=BUKKIT -e VERSION=1.8` or `-e TYPE=SPIGOT -e VERSION=1.8` to your command-line. From 73046d7499ed9e7f13a27a0957e8f52cd43b1cf2 Mon Sep 17 00:00:00 2001 From: Michael Reichenbach Date: Mon, 25 Mar 2019 18:10:45 +0100 Subject: [PATCH 3/4] fix(minecraft-server): env variable value check not working The check for the value of the env variable now works correctly. #298 --- minecraft-server/start-finalSetup05EnvVariables | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/minecraft-server/start-finalSetup05EnvVariables b/minecraft-server/start-finalSetup05EnvVariables index b209034e..7146955a 100644 --- a/minecraft-server/start-finalSetup05EnvVariables +++ b/minecraft-server/start-finalSetup05EnvVariables @@ -5,7 +5,7 @@ if [ "$REPLACE_ENV_VARIABLES" = "TRUE" ]; then while IFS='=' read -r name value ; do # check if name of env variable matches the prefix # sanity check environment variables to avoid code injections - if [[ "$name" = $ENV_VARIABLE_PREFIX* ]] && [[ $value =~ ^[0-9a-zA-Z_\-:/=?.+]*$ ]] && [[ $name =~ ^[0-9a-zA-Z_\-]*$ ]]; then + if [[ "$name" = $ENV_VARIABLE_PREFIX* ]] && [[ $value =~ ^[0-9a-zA-Z_:/=?.+\-]*$ ]] && [[ $name =~ ^[0-9a-zA-Z_\-]*$ ]]; then echo "$name = $value" find /data/ -type f -exec sed -i 's#${'"$name"'}#'"$value"'#g' {} \; fi From a316bd8f9553dbdb1ca5d9f75bdc251db36cc8a0 Mon Sep 17 00:00:00 2001 From: Michael Reichenbach Date: Fri, 29 Mar 2019 12:11:42 +0100 Subject: [PATCH 4/4] fix(minecraft-server): filter env replacement file Environment variable replacement now only affects matching file types. Closes #299 --- build | 0 elasticsearch/Dockerfile | 0 elasticsearch/README.md | 0 elasticsearch/start | 0 gitblit/start.sh | 0 jekyll-github-pages/start.sh | 0 jenkins/download-and-start.sh | 0 kibana/Dockerfile | 0 kibana/start.sh | 0 logstash/Dockerfile | 0 minecraft-server/start | 0 minecraft-server/start-configuration | 0 minecraft-server/start-deployBukkitSpigot | 0 minecraft-server/start-deployFTB | 0 minecraft-server/start-deployForge | 0 minecraft-server/start-deployPaper | 0 minecraft-server/start-deploySpongeVanilla | 0 minecraft-server/start-deployVanilla | 0 minecraft-server/start-finalSetup01World | 0 minecraft-server/start-finalSetup02Modpack | 0 minecraft-server/start-finalSetup03Modconfig | 0 minecraft-server/start-finalSetup04ServerProperties | 0 minecraft-server/start-finalSetup05EnvVariables | 4 ++-- minecraft-server/start-minecraftFinalSetup | 0 titan-gremlin/Dockerfile | 0 titan-gremlin/start-gremlin.sh | 0 titandb/gremlin | 0 titandb/rexster | 0 ubuntu-openjdk-7/Dockerfile | 0 29 files changed, 2 insertions(+), 2 deletions(-) mode change 100755 => 100644 build mode change 100755 => 100644 elasticsearch/Dockerfile mode change 100755 => 100644 elasticsearch/README.md mode change 100755 => 100644 elasticsearch/start mode change 100755 => 100644 gitblit/start.sh mode change 100755 => 100644 jekyll-github-pages/start.sh mode change 100755 => 100644 jenkins/download-and-start.sh mode change 100755 => 100644 kibana/Dockerfile mode change 100755 => 100644 kibana/start.sh mode change 100755 => 100644 logstash/Dockerfile mode change 100755 => 100644 minecraft-server/start mode change 100755 => 100644 minecraft-server/start-configuration mode change 100755 => 100644 minecraft-server/start-deployBukkitSpigot mode change 100755 => 100644 minecraft-server/start-deployFTB mode change 100755 => 100644 minecraft-server/start-deployForge mode change 100755 => 100644 minecraft-server/start-deployPaper mode change 100755 => 100644 minecraft-server/start-deploySpongeVanilla mode change 100755 => 100644 minecraft-server/start-deployVanilla mode change 100755 => 100644 minecraft-server/start-finalSetup01World mode change 100755 => 100644 minecraft-server/start-finalSetup02Modpack mode change 100755 => 100644 minecraft-server/start-finalSetup03Modconfig mode change 100755 => 100644 minecraft-server/start-finalSetup04ServerProperties mode change 100755 => 100644 minecraft-server/start-minecraftFinalSetup mode change 100755 => 100644 titan-gremlin/Dockerfile mode change 100755 => 100644 titan-gremlin/start-gremlin.sh mode change 100755 => 100644 titandb/gremlin mode change 100755 => 100644 titandb/rexster mode change 100755 => 100644 ubuntu-openjdk-7/Dockerfile diff --git a/build b/build old mode 100755 new mode 100644 diff --git a/elasticsearch/Dockerfile b/elasticsearch/Dockerfile old mode 100755 new mode 100644 diff --git a/elasticsearch/README.md b/elasticsearch/README.md old mode 100755 new mode 100644 diff --git a/elasticsearch/start b/elasticsearch/start old mode 100755 new mode 100644 diff --git a/gitblit/start.sh b/gitblit/start.sh old mode 100755 new mode 100644 diff --git a/jekyll-github-pages/start.sh b/jekyll-github-pages/start.sh old mode 100755 new mode 100644 diff --git a/jenkins/download-and-start.sh b/jenkins/download-and-start.sh old mode 100755 new mode 100644 diff --git a/kibana/Dockerfile b/kibana/Dockerfile old mode 100755 new mode 100644 diff --git a/kibana/start.sh b/kibana/start.sh old mode 100755 new mode 100644 diff --git a/logstash/Dockerfile b/logstash/Dockerfile old mode 100755 new mode 100644 diff --git a/minecraft-server/start b/minecraft-server/start old mode 100755 new mode 100644 diff --git a/minecraft-server/start-configuration b/minecraft-server/start-configuration old mode 100755 new mode 100644 diff --git a/minecraft-server/start-deployBukkitSpigot b/minecraft-server/start-deployBukkitSpigot old mode 100755 new mode 100644 diff --git a/minecraft-server/start-deployFTB b/minecraft-server/start-deployFTB old mode 100755 new mode 100644 diff --git a/minecraft-server/start-deployForge b/minecraft-server/start-deployForge old mode 100755 new mode 100644 diff --git a/minecraft-server/start-deployPaper b/minecraft-server/start-deployPaper old mode 100755 new mode 100644 diff --git a/minecraft-server/start-deploySpongeVanilla b/minecraft-server/start-deploySpongeVanilla old mode 100755 new mode 100644 diff --git a/minecraft-server/start-deployVanilla b/minecraft-server/start-deployVanilla old mode 100755 new mode 100644 diff --git a/minecraft-server/start-finalSetup01World b/minecraft-server/start-finalSetup01World old mode 100755 new mode 100644 diff --git a/minecraft-server/start-finalSetup02Modpack b/minecraft-server/start-finalSetup02Modpack old mode 100755 new mode 100644 diff --git a/minecraft-server/start-finalSetup03Modconfig b/minecraft-server/start-finalSetup03Modconfig old mode 100755 new mode 100644 diff --git a/minecraft-server/start-finalSetup04ServerProperties b/minecraft-server/start-finalSetup04ServerProperties old mode 100755 new mode 100644 diff --git a/minecraft-server/start-finalSetup05EnvVariables b/minecraft-server/start-finalSetup05EnvVariables index 7146955a..d1c44f49 100644 --- a/minecraft-server/start-finalSetup05EnvVariables +++ b/minecraft-server/start-finalSetup05EnvVariables @@ -6,8 +6,8 @@ if [ "$REPLACE_ENV_VARIABLES" = "TRUE" ]; then # check if name of env variable matches the prefix # sanity check environment variables to avoid code injections if [[ "$name" = $ENV_VARIABLE_PREFIX* ]] && [[ $value =~ ^[0-9a-zA-Z_:/=?.+\-]*$ ]] && [[ $name =~ ^[0-9a-zA-Z_\-]*$ ]]; then - echo "$name = $value" - find /data/ -type f -exec sed -i 's#${'"$name"'}#'"$value"'#g' {} \; + echo "Replacing $name with $value ..." + find /data/ -type f \( -name "*.yml" -or -name "*.yaml" -or -name "*.txt" -or -name "*.cfg" -or -name "*.properties" \) -exec sed -i 's#${'"$name"'}#'"$value"'#g' {} \; fi done < <(env) fi diff --git a/minecraft-server/start-minecraftFinalSetup b/minecraft-server/start-minecraftFinalSetup old mode 100755 new mode 100644 diff --git a/titan-gremlin/Dockerfile b/titan-gremlin/Dockerfile old mode 100755 new mode 100644 diff --git a/titan-gremlin/start-gremlin.sh b/titan-gremlin/start-gremlin.sh old mode 100755 new mode 100644 diff --git a/titandb/gremlin b/titandb/gremlin old mode 100755 new mode 100644 diff --git a/titandb/rexster b/titandb/rexster old mode 100755 new mode 100644 diff --git a/ubuntu-openjdk-7/Dockerfile b/ubuntu-openjdk-7/Dockerfile old mode 100755 new mode 100644