Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactored ops and whitelist processing into its own file (#1306)
- Loading branch information
Showing
10 changed files
with
142 additions
and
97 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
#!/bin/bash | ||
|
||
# shellcheck source=start-utils | ||
. "${SCRIPTS:-/}start-utils" | ||
isDebugging && set -x | ||
|
||
if versionLessThan 1.7.6; then | ||
opsFile=ops.txt | ||
whitelistFile=white-list.txt | ||
else | ||
opsFile=ops.json | ||
whitelistFile=whitelist.json | ||
fi | ||
|
||
function process_user_file() { | ||
local output=$1 | ||
local source=$2 | ||
|
||
if isURL "$source"; then | ||
log "Downloading $output from $source" | ||
if ! get -o "/data/$output" "$source"; then | ||
log "ERROR: failed to download from $source" | ||
exit 2 | ||
fi | ||
else | ||
log "Copying $output from $source" | ||
if ! cp "$source" "/data/$output"; then | ||
log "ERROR: failed to copy from $source" | ||
exit 1 | ||
fi | ||
fi | ||
} | ||
|
||
function process_user_csv() { | ||
local output=$1 | ||
local list=$2 | ||
local playerDataList | ||
|
||
if [[ "$output" == *"ops"* ]]; then | ||
# Extra data for ops.json | ||
userData='{"uuid": .id, "name": .username, "level": 4}' | ||
else | ||
userData='{"uuid": .id, "name": .username}' | ||
fi | ||
|
||
log "Updating ${output%.*}" | ||
for i in ${list//,/ } | ||
do | ||
if [ -e "$output" ] && grep -q "$i" "$output"; then | ||
log "$i already present in $output, skipping" | ||
continue | ||
fi | ||
if ! playerData=$(get "https://playerdb.co/api/player/minecraft/$i" | jq -re ".data.player"); then | ||
log "WARNING: Could not lookup user $i for ${output} addition" | ||
else | ||
playerDataList=$playerDataList$(echo "$playerData" | jq -r "$userData") | ||
fi | ||
done | ||
local newUsers=$(echo "$playerDataList" | jq -s .) | ||
if [[ $output =~ .*\.txt ]]; then | ||
# username list for txt config (Minecraft <= 1.7.5) | ||
echo $newUsers | jq -r '.[].name' >> "/data/${output}" | ||
sort -u /data/${output} -o /data/${output} | ||
elif [ -e /data/${output} ]; then | ||
# Merge with existing json file | ||
local currentUsers=$(cat "/data/${output}") | ||
jq --argjson current "$currentUsers" --argjson new "$newUsers" -n '$new + $current | unique_by(.uuid)' > "/data/${output}" | ||
else | ||
# New json file | ||
echo $newUsers > "/data/${output}" | ||
fi | ||
} | ||
|
||
if isTrue "${OVERRIDE_OPS}"; then | ||
log "Recreating ${opsFile} file at server startup" | ||
rm -f /data/${opsFile} | ||
fi | ||
if [ -n "${OPS_FILE}" ] && [ ! -e "/data/${opsFile}" ]; then | ||
process_user_file ${opsFile} "$OPS_FILE" | ||
fi | ||
if [ -n "${OPS}" ]; then | ||
process_user_csv ${opsFile} "$OPS" | ||
fi | ||
|
||
if isTrue "${OVERRIDE_WHITELIST}"; then | ||
log "Recreating ${whitelistFile} file at server startup" | ||
rm -f /data/${whitelistFile} | ||
fi | ||
if [ -n "${WHITELIST_FILE}" ] && [ ! -e "/data/${whitelistFile}" ]; then | ||
process_user_file ${whitelistFile} "$WHITELIST_FILE" | ||
fi | ||
if [ -n "${WHITELIST}" ]; then | ||
process_user_csv ${whitelistFile} "$WHITELIST" | ||
fi | ||
|
||
|
||
|
||
exec "${SCRIPTS:-/}start-finalExec" "$@" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
version: "3" | ||
|
||
services: | ||
mc: | ||
restart: "no" | ||
image: ${IMAGE_TO_TEST:-itzg/minecraft-server} | ||
environment: | ||
EULA: "TRUE" | ||
SETUP_ONLY: "TRUE" | ||
VERSION: ${MINECRAFT_VERSION:-LATEST} | ||
OPS: itzg | ||
volumes: | ||
- ./data:/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
mc-image-helper assert jsonPathEquals --file=ops.json --path='$[0].name' --expect=itzg | ||
mc-image-helper assert jsonPathEquals --file=ops.json --path='$[0].uuid' --expect=5cddfd26-fc86-4981-b52e-c42bb10bfdef | ||
mc-image-helper assert jsonPathEquals --file=ops.json --path='$[0].level' --expect=4 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
15 changes: 15 additions & 0 deletions
15
tests/setuponlytests/whitelist_from_scratch/docker-compose.yml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
version: "3" | ||
|
||
services: | ||
mc: | ||
restart: "no" | ||
image: ${IMAGE_TO_TEST:-itzg/minecraft-server} | ||
environment: | ||
EULA: "TRUE" | ||
SETUP_ONLY: "TRUE" | ||
VERSION: ${MINECRAFT_VERSION:-LATEST} | ||
WHITELIST: itzg | ||
ENFORCE_WHITELIST: "true" | ||
OVERRIDE_SERVER_PROPERTIES: "true" | ||
volumes: | ||
- ./data:/data |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
mc-image-helper assert jsonPathEquals --file=whitelist.json --path='$[0].name' --expect=itzg | ||
mc-image-helper assert jsonPathEquals --file=whitelist.json --path='$[0].uuid' --expect=5cddfd26-fc86-4981-b52e-c42bb10bfdef | ||
mc-image-helper assert propertyEquals --file=server.properties --property=white-list --expect=true | ||
mc-image-helper assert propertyEquals --file=server.properties --property=enforce-whitelist --expect=true |