Tmp
This commit is contained in:
parent
234419fdd6
commit
63f4599c14
3 changed files with 142 additions and 115 deletions
92
src/args.py
92
src/args.py
|
|
@ -6,6 +6,7 @@ import argparse
|
|||
import sys
|
||||
|
||||
from logger import Logger
|
||||
from constants import SERVER_ENVS
|
||||
|
||||
|
||||
def get_args() -> argparse.Namespace:
|
||||
|
|
@ -21,6 +22,7 @@ def get_args() -> argparse.Namespace:
|
|||
"""
|
||||
parser = argparse.ArgumentParser(
|
||||
description="Arguments for Docker Minecraft Server Management (DMSM)",
|
||||
add_help=False,
|
||||
)
|
||||
|
||||
if len(sys.argv) >= 2 and sys.argv[1] == "create":
|
||||
|
|
@ -69,7 +71,12 @@ def _create(parser: argparse.ArgumentParser):
|
|||
# Below are the more common commands, with shortcuts #
|
||||
########################################################
|
||||
|
||||
parser.add_argument(
|
||||
required = parser.add_argument_group("Required Arguments")
|
||||
optional = parser.add_argument_group(
|
||||
"Optional Arguments (oitsjustjo.se/u/Z9mLwAY40)"
|
||||
)
|
||||
|
||||
required.add_argument(
|
||||
"-p",
|
||||
"--port",
|
||||
help="The port to run the newly created server on",
|
||||
|
|
@ -77,91 +84,22 @@ def _create(parser: argparse.ArgumentParser):
|
|||
required=True,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
required.add_argument(
|
||||
"-v",
|
||||
"--version",
|
||||
help="The version of Minecraft to run",
|
||||
required=True,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
required.add_argument(
|
||||
"-r",
|
||||
"--root",
|
||||
help="The root on your disk for THIS server",
|
||||
required=True,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-s",
|
||||
"--seed",
|
||||
help="The seed to use for the world",
|
||||
required=False,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"-l",
|
||||
"--leveltype",
|
||||
help="The level type to use for the world",
|
||||
required=False,
|
||||
)
|
||||
|
||||
########################################################
|
||||
# Less common commands, with no shortcuts #
|
||||
########################################################
|
||||
|
||||
parser.add_argument(
|
||||
"--view",
|
||||
help="The view distance the server should run at",
|
||||
required=False,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--motd",
|
||||
help="The MOTD for the server",
|
||||
required=False,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--memory",
|
||||
help="The amount of dedicated memory for the server",
|
||||
required=False,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--aikar",
|
||||
help="If flag is included, loads the server using aikar's flags",
|
||||
action="store_true",
|
||||
required=False,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--java",
|
||||
help="Allows you to specify the java version",
|
||||
choices=['8', '11', '16'],
|
||||
default=16,
|
||||
required=False,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--forge",
|
||||
help="Allows you to provide either a URL or Path to a Forge installer",
|
||||
required=False,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--fabric",
|
||||
help="Allows you to provide either a URL or Path to a Fabric installer",
|
||||
required=False,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--modpack",
|
||||
help="Allows you to specify a CURSEFORGE modpack",
|
||||
required=False,
|
||||
)
|
||||
|
||||
parser.add_argument(
|
||||
"--players",
|
||||
help="Allows you to specify the max player count",
|
||||
required=False,
|
||||
)
|
||||
for env in SERVER_ENVS:
|
||||
optional.add_argument(
|
||||
f"--{env.lower()}",
|
||||
required=False,
|
||||
)
|
||||
|
|
|
|||
79
src/constants.py
Normal file
79
src/constants.py
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
"""
|
||||
Author: Jose Stovall | oitsjustjose
|
||||
"""
|
||||
|
||||
SERVER_ENVS = [
|
||||
"ALLOW_FLIGHT",
|
||||
"ALLOW_NETHER",
|
||||
"ANNOUNCE_PLAYER_ACHIEVEMENTS",
|
||||
"BUILD_FROM_SOURCE",
|
||||
"BUKKIT_DOWNLOAD_URL",
|
||||
"CANYON_BUILD",
|
||||
"CF_BASE_DIR",
|
||||
"CF_SERVER_MOD",
|
||||
"CONSOLE",
|
||||
"COPY_CONFIG_DEST",
|
||||
"DIFFICULTY",
|
||||
"ENABLE_AUTOPAUSE",
|
||||
"ENABLE_COMMAND_BLOCK",
|
||||
"ENABLE_JMX",
|
||||
"ENABLE_QUERY",
|
||||
"ENABLE_RCON",
|
||||
"ENABLE_ROLLING_LOGS",
|
||||
"EXEC_DIRECTLY",
|
||||
"FABRIC_INSTALLER_URL",
|
||||
"FABRIC_INSTALLER",
|
||||
"FORCE_GAMEMODE",
|
||||
"FORCE_REDOWNLOAD",
|
||||
"FORGE_INSTALLER_URL",
|
||||
"FORGE_INSTALLER",
|
||||
"FORGEVERSION",
|
||||
"FTB_LEGACYJAVAFIXER",
|
||||
"GENERATE_STRUCTURES",
|
||||
"GID",
|
||||
"GUI",
|
||||
"HARDCORE",
|
||||
"ICON",
|
||||
"LEVEL_TYPE",
|
||||
"LEVEL",
|
||||
"MAX_BUILD_HEIGHT",
|
||||
"MAX_PLAYERS",
|
||||
"MAX_TICK_TIME",
|
||||
"MAX_WORLD_SIZE",
|
||||
"MEMORY",
|
||||
"MODE",
|
||||
"MODPACK",
|
||||
"MODS",
|
||||
"MOTD",
|
||||
"ONLINE_MODE",
|
||||
"OPS",
|
||||
"OVERRIDE_SERVER_PROPERTIES",
|
||||
"PAPER_DOWNLOAD_URL",
|
||||
"PAPERBUILD",
|
||||
"PROXY",
|
||||
"PVP",
|
||||
"REMOVE_OLD_MODS",
|
||||
"SEED",
|
||||
"SERVER_NAME",
|
||||
"SERVER_PORT",
|
||||
"SNOOPER_ENABLED",
|
||||
"SPAWN_ANIMALS",
|
||||
"SPAWN_MONSTERS",
|
||||
"SPAWN_NPCS",
|
||||
"SPAWN_PROTECTION",
|
||||
"SPIGET_RESOURCES",
|
||||
"SPIGOT_DOWNLOAD_URL",
|
||||
"SPONGEVERSION",
|
||||
"TUNE_NURSERY_SIZES",
|
||||
"TUNE_VIRTUALIZED",
|
||||
"TYPE",
|
||||
"TZ",
|
||||
"UID",
|
||||
"USE_AIKAR_FLAGS",
|
||||
"USE_LARGE_PAGES",
|
||||
"USE_MODPACK_START_SCRIPT",
|
||||
"VIEW_DISTANCE",
|
||||
"WHITELIST",
|
||||
"WORLD",
|
||||
"RESOURCE_PACK",
|
||||
]
|
||||
|
|
@ -27,46 +27,8 @@ def _build_env(args: Namespace) -> Dict[str, any]:
|
|||
ret = {
|
||||
"VERSION": args.version,
|
||||
"EULA": True,
|
||||
"SPAWN_PROTECTION": 0,
|
||||
"ALLOW_FLIGHT": True,
|
||||
"ENFORCE_WHITELIST": True,
|
||||
}
|
||||
|
||||
if args.motd:
|
||||
ret["MOTD"] = args.motd
|
||||
if args.memory:
|
||||
ret["MEMORY"] = args.memory
|
||||
if args.aikar:
|
||||
ret["USE_AIKAR_FLAGS"] = args.aikar
|
||||
if args.forge:
|
||||
# Validate if url or not
|
||||
url = urlparse(args.forge)
|
||||
if all([url.scheme, url.netloc, url.path]):
|
||||
ret["FORGE_INSTALLER_URL"] = args.forge
|
||||
else:
|
||||
ret["FORGE_INSTALLER"] = args.forge
|
||||
ret["TYPE"] = "FORGE"
|
||||
if args.fabric and not args.forge:
|
||||
# Validate if url or not
|
||||
url = urlparse(args.fabric)
|
||||
if all([url.scheme, url.netloc, url.path]):
|
||||
ret["FABRIC_INSTALLER_URL"] = args.fabric
|
||||
else:
|
||||
ret["FABRIC_INSTALLER"] = args.fabric
|
||||
ret["TYPE"] = "FABRIC"
|
||||
if args.modpack:
|
||||
ret["CF_SERVER_MOD"] = args.modpack
|
||||
ret["TYPE"] = "CURSEFORGE"
|
||||
ret["USE_MODPACK_START_SCRIPT"] = False
|
||||
if args.players:
|
||||
ret["MAX_PLAYERS"] = args.players
|
||||
if args.seed:
|
||||
ret["SEED"] = args.seed
|
||||
if args.view:
|
||||
ret["VIEW_DISTANCE"] = args.view
|
||||
if args.leveltype:
|
||||
ret["LEVEL_TYPE"] = args.leveltype
|
||||
|
||||
return ret
|
||||
|
||||
|
||||
|
|
@ -79,6 +41,7 @@ class ServerManager:
|
|||
self._name = name
|
||||
self._client = docker.from_env()
|
||||
self._container = self._get_container()
|
||||
self._backup_container = None
|
||||
self._log = Logger(name=self._name)
|
||||
|
||||
def _get_container(self):
|
||||
|
|
@ -117,6 +80,26 @@ class ServerManager:
|
|||
self._log.err("Failed to Create Server")
|
||||
self._log.err(exception)
|
||||
|
||||
def _start_backup_cont(self, args: Namespace):
|
||||
"""
|
||||
Creates a container for the backup service
|
||||
"""
|
||||
try:
|
||||
self._backup_container = self._client.containers.run(
|
||||
"itzg/mc-backup",
|
||||
name=f"{self._name}-Backup",
|
||||
environment={},
|
||||
volumes={
|
||||
args.root: {"bind": "/data", "mode": "ro"},
|
||||
isnone(
|
||||
args.backupdir, f"{dir_above(args.root)}/{self._name}-Backups"
|
||||
): {"bind": "/backups", "mode": "rw"},
|
||||
},
|
||||
)
|
||||
except Exception as exception:
|
||||
self._log.err("Failed to Create Backup Service for Server")
|
||||
self._log.err(exception)
|
||||
|
||||
def delete_server(self):
|
||||
"""
|
||||
Deletes a docker container
|
||||
|
|
@ -201,3 +184,30 @@ class ServerManager:
|
|||
Connects the user to the server's console
|
||||
"""
|
||||
os.system(f"docker exec -i {self._name} rcon-cli")
|
||||
|
||||
|
||||
def isnone(value: any, default: any):
|
||||
"""
|
||||
Equivalent to ISNULL in SQL because I'm spoiled 😛
|
||||
Checks to see if value is none, returns the default if it is
|
||||
Arguments:
|
||||
value (any): The value to check
|
||||
default (any): The fallback value to use
|
||||
Returns:
|
||||
(any): value if value isn't None, else rdefault
|
||||
"""
|
||||
return default if value is None else value
|
||||
|
||||
|
||||
def dir_above(dir: str) -> str:
|
||||
"""
|
||||
Determines what directory is above the current directory
|
||||
Arguments:
|
||||
dir (str): the directory to 'cd ..' on
|
||||
Returns:
|
||||
(str): the directory up one level from dir, if any
|
||||
(None) if value is /
|
||||
"""
|
||||
|
||||
dir = dir[:-1] if dir.endswith("/") else dir
|
||||
return "/".join(dir.split("/")[:-1]) + "/"
|
||||
|
|
|
|||
Loading…
Reference in a new issue