Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New Feature: Creating a default Variables JSON from environment variables #469

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ Typically, each property is configured instead by the UPPER_SNAKE_CASE equivalen
- `EMIT_SERVER_TELEMETRY`
- `MSA_GAMERTAGS_ONLY`
- `ITEM_TRANSACTION_LOGGING_ENABLED`
- `VARIABLES`

For example, to configure a flat, creative server instead of the default use:

Expand Down Expand Up @@ -191,6 +192,24 @@ There are various tools to look XUIDs up online and they are also printed to the
-e ALLOW_LIST_USERS="player1:1234567890,player2:0987654321"
```

## Variables

Custom server variables are passed in just like the allowlist or as a full JSON string.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would be good to somehow include here the link to the Microsoft docs you mentioned in your reply.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do.


Server variables are parsed into their most likely type (number-like turn into numbers, all other inputs are treated as string) using jq's `fromjson` command. In the example below, `var1` is a string, `var2` is a number, and `var3` is a string.

For greater control on types, users can provide a full string JSON representation that is used as-is.

All variables are written to the config/default/variables.json. There is no support for Module-specific variable handling at this time.

```shell
# passing in simple expressions
-e VARIABLES="var1:customStringValue,var2:1234,var3:true"
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems like in non-json form it would be more intuitive to use equal signs such as

var1=value,var2=value

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we want to diverge from the format for the allowlist variable? I only kept using colons to follow the previous input formatting. not an issue to change if we're okay with different formats for different env var inputs.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that's ok since your scenario is a definitive name=value whereas the allowlist is a pairing of values due to the XUID weirdness.


# pass in a full json object:
-e VARIABLES='{"mobSpawnRate":22,"enableCheats":true,"worldArray":["My World", "Abc", 123]}'
```

## Mods Addons

Also known as behavior or resource packs, in order to add mods into your server you can follow these steps, tested with [OPS (One Player Sleep)](https://foxynotail.com/addons/ops/) and [bedrocktweaks](https://bedrocktweaks.net/resource-packs/)
Expand Down
28 changes: 28 additions & 0 deletions bedrock-entry.sh
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,34 @@ if [[ -n "$ALLOW_LIST_USERS" || -n "$WHITE_LIST_USERS" ]]; then
fi
fi

if [[ -n "$VARIABLES" ]]; then
echo "Setting variables"
mkdir -p config/default

# Try to parse VARIABLES as JSON
if echo "$VARIABLES" | jq empty >/dev/null 2>&1; then
# VARIABLES is valid JSON
echo "$VARIABLES" | jq '.' > "config/default/variables.json"
else
# VARIABLES is not valid JSON, attempt to parse as custom format
echo "VARIABLES is not valid JSON, attempting to parse as custom format"

# Parse the VARIABLES using custom format (key:value,key:value)
# Note: Values should not contain unescaped commas or colons
jq -n --arg vars "$VARIABLES" '
$vars
| split(",")
| map(
split(":") as $kv |
{ ($kv[0]): ($kv[1] | fromjson? // $kv[1]) }
)
| add
' > "config/default/variables.json"
fi
fi



# prevent issue with bind mounted server.properties which can not be moved (sed tries to move the file when '-i' is used)
_SERVER_PROPERTIES=$(sed '/^white-list=.*/d' server.properties) #Removes white-list= line from server.properties
echo "${_SERVER_PROPERTIES}" > server.properties
Expand Down