Move config.py

main
Bram van den Heuvel 2023-10-01 16:41:00 +02:00
parent 5d86b17a01
commit 610c3bb01d
6 changed files with 83 additions and 71 deletions

View File

@ -7,13 +7,17 @@ WORKDIR /usr/src/app
COPY requirements.txt ./ COPY requirements.txt ./
RUN pip install --no-cache-dir -r requirements.txt RUN pip install --no-cache-dir -r requirements.txt
# Prepare MC server # # Prepare MC server
COPY server.jar ./ # COPY server.jar ./
RUN java -jar server.jar --nogui # RUN java -jar server.jar --nogui
COPY . . COPY src/ ./src/
COPY main.py ./
COPY LICENSE.md ./
COPY config.yaml ./
# Buffer Python's stdout for debugging during runtime # Buffer Python's stdout for debugging during runtime
ENV PYTHONUNBUFFERED=1 ENV PYTHONUNBUFFERED=1
CMD ["python", "main.py", "java", "-Xmx1024M", "-Xms1024M", "-jar", "server.jar", "nogui"] CMD ["python", "main.py"]

7
main.py Normal file
View File

@ -0,0 +1,7 @@
import src.mc_wrapper as mc_wrapper
import src.mxclient as matrix_client
import asyncio
# Start the Minecraft process
asyncio.run(matrix_client.start())

View File

@ -3,7 +3,7 @@
correct configuration. correct configuration.
""" """
import config import src.config as config
def write_eula(): def write_eula():
""" """

View File

@ -1,62 +1,62 @@
""" """
This module loads and parses the config.yaml file. This module loads and parses the config.yaml file.
""" """
from typing import Any, List, Optional from typing import Any, List, Optional
import yaml import yaml
with open('config.yaml', 'r') as open_file: with open('config.yaml', 'r') as open_file:
SETTINGS = yaml.load(open_file) SETTINGS = yaml.load(open_file)
def at(keys : List[str]) -> Optional[Any]: def at(keys : List[str]) -> Optional[Any]:
""" """
Potentially get a value. If it doesn't exist, return None. Potentially get a value. If it doesn't exist, return None.
""" """
return at_value(keys, SETTINGS) return at_value(keys, SETTINGS)
def at_value(keys : List[str], value : Any) -> Optional[Any]: def at_value(keys : List[str], value : Any) -> Optional[Any]:
try: try:
head, tail = keys[0], keys[1:] head, tail = keys[0], keys[1:]
except IndexError: except IndexError:
return value return value
else: else:
try: try:
new_value = value[head] new_value = value[head]
except TypeError: except TypeError:
return None return None
except KeyError: except KeyError:
return None return None
else: else:
return at_value(tail, new_value) return at_value(tail, new_value)
# EULA # EULA
EULA = at(['config', 'eula']) or False EULA = at(['config', 'eula']) or False
# Minecraft bridge credentials # Minecraft bridge credentials
MATRIX_HOMESERVER = at(['matrix', 'homeserver']) or "https://matrix.example.org/" MATRIX_HOMESERVER = at(['matrix', 'homeserver']) or "https://matrix.example.org/"
MATRIX_USERNAME = at(['matrix', 'username']) or "@alice:example.org" MATRIX_USERNAME = at(['matrix', 'username']) or "@alice:example.org"
MATRIX_PASSWORD = at(['matrix', 'password']) or "bridge_password" MATRIX_PASSWORD = at(['matrix', 'password']) or "bridge_password"
# Matrix bridge room # Matrix bridge room
MATRIX_ROOM = at(['matrix', 'room_id']) or "!channel_id:example.org" MATRIX_ROOM = at(['matrix', 'room_id']) or "!channel_id:example.org"
SERVER_IP = at(['matrix', 'server_address']) or 'unknown ip' SERVER_IP = at(['matrix', 'server_address']) or 'unknown ip'
MATRIX_ADMINS = at(['matrix', 'mc-admins']) or [] MATRIX_ADMINS = at(['matrix', 'mc-admins']) or []
try: try:
RAM_SIZE = int(at(['config', 'ram'])) RAM_SIZE = int(at(['config', 'ram']))
except TypeError: except TypeError:
RAM_SIZE = 1024 RAM_SIZE = 1024
except ValueError: except ValueError:
RAM_SIZE = 1024 RAM_SIZE = 1024
SERVER_JAR_LOCATION = at(['config', 'server_jar']) or 'server.jar' SERVER_JAR_LOCATION = at(['config', 'server_jar']) or 'server.jar'
RUN_COMMAND = [ RUN_COMMAND = [
'java', 'java',
f'-Xmx{RAM_SIZE}M', f'-Xmx{RAM_SIZE}M',
f'-Xms{RAM_SIZE}M', f'-Xms{RAM_SIZE}M',
'-jar', SERVER_JAR_LOCATION, '-jar', SERVER_JAR_LOCATION,
'nogui' 'nogui'
] ]

View File

@ -11,7 +11,7 @@ import asyncio
import json import json
import re import re
from src.nbsr import NonBlockingStreamReader as NBSR from src.nbsr import NonBlockingStreamReader as NBSR
import config import src.config as config
import src.build_server as build import src.build_server as build
# Write the appropriate files # Write the appropriate files

View File

@ -5,7 +5,7 @@ import re
from nio import AsyncClient, MatrixRoom, RoomMessageText from nio import AsyncClient, MatrixRoom, RoomMessageText
import src.mc_wrapper as mc_wrapper import src.mc_wrapper as mc_wrapper
import config import src.config as config
STARTUP_TIME = time.time() STARTUP_TIME = time.time()
@ -57,4 +57,5 @@ async def start():
mc_wrapper.start(client, config.MATRIX_ROOM) mc_wrapper.start(client, config.MATRIX_ROOM)
) )
asyncio.run(start()) if __name__ == '__main__':
asyncio.run(start())