Skip to content

Commit

Permalink
fix(#100): add fallback logic for deprecated SERVER_PASSWORD env var
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-fabian-pittroff committed Nov 10, 2024
1 parent a02ce08 commit d9bf873
Showing 1 changed file with 24 additions and 5 deletions.
29 changes: 24 additions & 5 deletions scripts/default/enshrouded-bootstrap-shared
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ updateOrCreateEnshroudedServerConfig() {
cat >${install_path}/enshrouded_server.json << EOF
{
"name": "Enshrouded Server",
"password": "",
"saveDirectory": "./savegame",
"logDirectory": "./logs",
"ip": "0.0.0.0",
Expand All @@ -58,10 +57,6 @@ EOF
echo "$(jq --arg name "$SERVER_NAME" '.name = $name' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
fi

if [[ -n "$SERVER_PASSWORD" ]]; then
echo "$(jq --arg password "$SERVER_PASSWORD" '.password = $password' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
fi

if [[ -n "$SERVER_SAVE_DIR" ]]; then
echo "$(jq --arg saveDirectory "$SERVER_SAVE_DIR" '.saveDirectory = $saveDirectory' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
fi
Expand Down Expand Up @@ -97,6 +92,8 @@ updateUserGroupConfig() {
echo "$(jq '.userGroups = []' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
fi

handleDeprecatedServerPassword

# add missing groups
for group_index in $(seq 0 $group_count); do
if ! jq -e --argjson group_index "$group_index" '.userGroups | has($group_index)' ${install_path}/enshrouded_server.json >/dev/null; then
Expand Down Expand Up @@ -143,6 +140,28 @@ updateUserGroupConfig() {
done
}

handleDeprecatedServerPassword() {
if [[ -n "$SERVER_PASSWORD" ]]; then
warn "SERVER_PASSWORD is deprecated, pls consider using SERVER_ROLE_<index>_PASSWORD instead"
warn "falling back to \"Default\" server role password"

# check if group 0 exists if not create it
if ! jq -e '.userGroups[0]' ${install_path}/enshrouded_server.json >/dev/null; then
debug "default group (index: 0) does not exist, creating default group."
echo "$(jq --argjson group_index "$group_index" '.userGroups += [{"name": "Default", "password": "", "canKickBan": false, "canAccessInventories": false, "canEditBase": false, "canExtendBase": false, "reservedSlots": 0}]' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
fi

# check if group 0 name is "Default" if not return
if [[ "$(jq -r '.userGroups[0].name' ${install_path}/enshrouded_server.json)" != "Default" ]]; then
warn "default group (index: 0) has not the name \"Default\". Skipping password update!"
return
fi

debug "updating default group (index: 0) password to $SERVER_PASSWORD"
echo "$(jq --arg password "$SERVER_PASSWORD" '.userGroups[0].password = $password' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
fi
}

updateGameSettingsConfig() {
if [[ -n "$SERVER_GS_PRESET" ]]; then
debug "updating gameSettingsPreset to $SERVER_GS_PRESET"
Expand Down

0 comments on commit d9bf873

Please sign in to comment.