Skip to content

Commit

Permalink
feat: add server role handling
Browse files Browse the repository at this point in the history
  • Loading branch information
cp-fabian-pittroff committed Jun 9, 2024
1 parent 3a7df26 commit 2c8cbf5
Show file tree
Hide file tree
Showing 4 changed files with 119 additions and 24 deletions.
50 changes: 31 additions & 19 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,24 +13,32 @@ updates and cleanup.

## Environment Variables

| Variable | Required | Default | Contraints | Description | WIP |
|------------------------|:--------:|---------------------|-----------------------|--------------------------------------------------------------------------------------------------------------------|:---:|
| `SERVER_NAME` | | `Enshrouded Server` | string | The name of the server ||
| `SERVER_PASSWORD` | | | string | The password for the server | |
| `SERVER_SLOT_COUNT` | | `16` | integer (1-16) | Max allowed concurrent players | |
| `SERVER_QUERYPORT` | | `15637` | integer | The steam query port for the server | |
| `SERVER_IP` | | `0.0.0.0` | string (ipv4) | Server IP for internal network configuration | |
| `SERVER_SAVE_DIR` | | `./savegame` | string | Folder for savegames (relative and absolute paths are supported) | |
| `SERVER_LOG_DIR` | | `./logs` | string | Folder for logs (relative and absolute paths are supported) | |
| `PUID` | | `4711` | integer | The UID to run server as (file permission) | |
| `PGID` | | `4711` | integer | The GID to run server as (file permission) | |
| `UPDATE_CRON` | | | string (cron format) | Update game server files cron (eg. `*/30 * * * *` check for updates every 30 minutes) | |
| `UPDATE_CHECK_PLAYERS` | | `false` | boolean (true, false) | Should the update check if someone is connected | |
| `BACKUP_CRON` | | | string (cron format) | Backup game server files cron (eg. `*/15 * * * *` backup saves every 15 minutes) - don't set cron under 10 minutes | |
| `BACKUP_DIR` | | `./backup` | string | Folder for backups (relative and absolute paths are supported) | |
| `BACKUP_MAX_COUNT` | | `0` | integer | Number of backups to keep (0 means infinite) | |
| `GAME_BRANCH` | | `public` | string | Steam branch (eg. testing) of the Enshrouded server | |
| `STEAMCMD_ARGS` | | `validate` | string | Additional steamcmd args for the updater | |
| Variable | Required | Default | Contraints | Description | WIP |
|----------------------------------------------------------------------------------------------------------------------|:--------:|---------------------|-----------------------|--------------------------------------------------------------------------------------------------------------------|:---:|
| `SERVER_NAME` | | `Enshrouded Server` | string | The name of the server ||
| `SERVER_PASSWORD` | | | string | The password for the server | |
| `SERVER_SLOT_COUNT` | | `16` | integer (1-16) | Max allowed concurrent players | |
| `SERVER_QUERYPORT` | | `15637` | integer | The steam query port for the server | |
| `SERVER_IP` | | `0.0.0.0` | string (ipv4) | Server IP for internal network configuration | |
| `SERVER_SAVE_DIR` | | `./savegame` | string | Folder for savegames (relative and absolute paths are supported) | |
| `SERVER_LOG_DIR` | | `./logs` | string | Folder for logs (relative and absolute paths are supported) | |
| `PUID` | | `4711` | integer | The UID to run server as (file permission) | |
| `PGID` | | `4711` | integer | The GID to run server as (file permission) | |
| `UPDATE_CRON` | | | string (cron format) | Update game server files cron (eg. `*/30 * * * *` check for updates every 30 minutes) | |
| `UPDATE_CHECK_PLAYERS` | | `false` | boolean (true, false) | Should the update check if someone is connected | |
| `BACKUP_CRON` | | | string (cron format) | Backup game server files cron (eg. `*/15 * * * *` backup saves every 15 minutes) - don't set cron under 10 minutes | |
| `BACKUP_DIR` | | `./backup` | string | Folder for backups (relative and absolute paths are supported) | |
| `BACKUP_MAX_COUNT` | | `0` | integer | Number of backups to keep (0 means infinite) | |
| `GAME_BRANCH` | | `public` | string | Steam branch (eg. testing) of the Enshrouded server | |
| `STEAMCMD_ARGS` | | `validate` | string | Additional steamcmd args for the updater | |
| **[Server Roles]((https://enshrouded.zendesk.com/hc/en-us/articles/16055441447709-Dedicated-Server-Configuration))** | | | | | |
| `SERVER_ROLE_<index>_NAME` | | `Default` | string | The name of the server role on index (starting with 0) | ⚠️ |
| `SERVER_ROLE_<index>_PASSWORD` | | `""` | string | The password for the server role | ⚠️ |
| `SERVER_ROLE_<index>_CAN_KICK_BAN` | | `false` | boolean (true, false) | Permission to kick and ban players | ⚠️ |
| `SERVER_ROLE_<index>_CAN_ACCESS_INVENTORIES` | | `false` | boolean (true, false) | Permission to access inventories | ⚠️ |
| `SERVER_ROLE_<index>_CAN_EDIT_BASE` | | `false` | boolean (true, false) | Permission to edit the base | ⚠️ |
| `SERVER_ROLE_<index>_CAN_EXTEND_BASE` | | `false` | boolean (true, false) | Permission to extend the base | ⚠️ |
| `SERVER_ROLE_<index>_RESERVED_SLOTS` | | `0` | integer | Number of reserved slots for the server role | ⚠️ |

All environment Variables prefixed with SERVER, are the available enshrouded_server.json options
(see [Enshrouded Docs](https://enshrouded.zendesk.com/hc/en-us/articles/16055441447709-Dedicated-Server-Configuration))
Expand Down Expand Up @@ -148,7 +156,7 @@ services:
restart: unless-stopped
stop_grace_period: 90s
ports:
- "15636-15637:15636-15637/udp"
- "15637:15637/udp"
volumes:
- game:/opt/enshrouded
environment:
Expand Down Expand Up @@ -181,6 +189,10 @@ again. If you want to keep the current savegame, make sure to make a backup befo
```bash
docker compose exec enshrouded supervisorctl start enshrouded-force-update
```
* **Reset Server Roles:** (Restarts the whole docker container) ⚠️
```bash
docker compose exec enshrouded supervisorctl start enshrouded-reset-roles
```

## Known Issues

Expand Down
65 changes: 60 additions & 5 deletions scripts/default/enshrouded-bootstrap-shared
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ initCrontab() {
}

prepareSteamA2sPythonLibrary() {
pip3 install python-a2s==1.3.0
pip3 install python-a2s==1.3.0 --break-system-packages
}

bootstrapHook() {
Expand Down Expand Up @@ -74,15 +74,70 @@ EOF
echo "$(jq --arg ip "$SERVER_IP" '.ip = $ip' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
fi

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

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

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

updateUserGroupConfig
}

updateUserGroupConfig() {
local group_count group_index group_param group_value
# get largest group index of env vars
group_count=$(compgen -A variable | grep -E "^SERVER_ROLE_[0-9]+_" | cut -d'_' -f3 | sort -nr | head -n1)

# check if userGroups array exists if not create empty array
if ! jq -e 'has("userGroups")' ${install_path}/enshrouded_server.json >/dev/null; then
debug "userGroups array does not exist, creating empty array"
echo "$(jq '.userGroups = []' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
fi

# 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
debug "group $group_index 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
done

for i in $(compgen -A variable | grep -E "^SERVER_ROLE_[0-9]+_"); do
group_index=$(echo $i | cut -d'_' -f3)
group_param=$(echo $i | cut -d'_' -f4-)
group_value=$(eval echo \$$i)

case $group_param in
NAME)
debug "updating group $group_index name to $group_value"
echo "$(jq --argjson group_index "$group_index" --arg name "$group_value" '.userGroups[$group_index].name = $name' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
;;
PASSWORD)
debug "updating group $group_index password to $group_value"
echo "$(jq --argjson group_index "$group_index" --arg password "$group_value" '.userGroups[$group_index].password = $password' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
;;
CAN_KICK_BAN)
debug "updating group $group_index canKickBan to $group_value"
echo "$(jq --argjson group_index "$group_index" --argjson canKickBan "$group_value" '.userGroups[$group_index].canKickBan = $canKickBan' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
;;
CAN_ACCESS_INVENTORIES)
debug "updating group $group_index canAccessInventories to $group_value"
echo "$(jq --argjson group_index "$group_index" --argjson canAccessInventories "$group_value" '.userGroups[$group_index].canAccessInventories = $canAccessInventories' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
;;
CAN_EDIT_BASE)
debug "updating group $group_index canEditBase to $group_value"
echo "$(jq --argjson group_index "$group_index" --argjson canEditBase "$group_value" '.userGroups[$group_index].canEditBase = $canEditBase' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
;;
CAN_EXTEND_BASE)
debug "updating group $group_index canExtendBase to $group_value"
echo "$(jq --argjson group_index "$group_index" --argjson canExtendBase "$group_value" '.userGroups[$group_index].canExtendBase = $canExtendBase' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
;;
RESERVED_SLOTS)
debug "updating group $group_index reservedSlots to $group_value"
echo "$(jq --argjson group_index "$group_index" --argjson reservedSlots "$group_value" '.userGroups[$group_index].reservedSlots = $reservedSlots' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json
;;
esac
done
}
16 changes: 16 additions & 0 deletions scripts/default/enshrouded-reset-roles
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#!/bin/bash
. "$(dirname "$0")/common"
. "$(dirname "$0")/defaults"

main() {
info "Stopping enshrouded server"
supervisorctl stop enshrouded-server

info "Reset enshrouded server roles"
echo "$(jq 'del(.userGroups)' ${install_path}/enshrouded_server.json)" > ${install_path}/enshrouded_server.json

info "Stop container for a clean start"
kill -INT $(cat /var/run/supervisord.pid)
}

main
12 changes: 12 additions & 0 deletions supervisord.conf
Original file line number Diff line number Diff line change
Expand Up @@ -98,5 +98,17 @@ autostart=false
autorestart=false
startsecs=0

[program:enshrouded-reset-roles]
user=root
environment=HOME="/home/enshrouded",USER="enshrouded",LANG="en_US.UTF-8",PATH="/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
command=/usr/local/etc/enshrouded/enshrouded-reset-roles
stdout_syslog=true
stderr_syslog=true
stdout_logfile_maxbytes=1MB
stderr_logfile_maxbytes=1MB
autostart=false
autorestart=false
startsecs=0

[include]
files = /usr/local/etc/supervisor/conf.d/*.conf

0 comments on commit 2c8cbf5

Please sign in to comment.