Huge updates/additions
This commit is contained in:
parent
2974be547a
commit
f718ea9a46
4 changed files with 267 additions and 80 deletions
167
src/args.py
Normal file
167
src/args.py
Normal file
|
|
@ -0,0 +1,167 @@
|
||||||
|
"""
|
||||||
|
Author: Jose Stovall | oitsjustjose
|
||||||
|
"""
|
||||||
|
|
||||||
|
import argparse
|
||||||
|
import sys
|
||||||
|
|
||||||
|
from logger import Logger
|
||||||
|
|
||||||
|
|
||||||
|
def get_args() -> argparse.Namespace:
|
||||||
|
"""
|
||||||
|
Cleanly and with education gets the arguments for the program
|
||||||
|
Does so based on the current task given, if any
|
||||||
|
Throws errors and a tantrum if you don't give it candy
|
||||||
|
Please give it candy 🍬
|
||||||
|
Arguments:
|
||||||
|
None
|
||||||
|
Returns:
|
||||||
|
(argparse.NameSpace) the arguments for the program
|
||||||
|
"""
|
||||||
|
parser = argparse.ArgumentParser(
|
||||||
|
description="Arguments for Docker Minecraft Server Management (DMSM)",
|
||||||
|
)
|
||||||
|
|
||||||
|
if len(sys.argv) >= 2 and sys.argv[1] == "create":
|
||||||
|
_create(parser)
|
||||||
|
|
||||||
|
if len(sys.argv) >= 2 and sys.argv[1] in ["stop", "restart"]:
|
||||||
|
_stop_restart(parser)
|
||||||
|
|
||||||
|
if not _validate(parser):
|
||||||
|
sys.exit(1)
|
||||||
|
|
||||||
|
return parser.parse_args(args=sys.argv[3:])
|
||||||
|
|
||||||
|
|
||||||
|
def _validate(parser: argparse.ArgumentParser) -> bool:
|
||||||
|
if len(sys.argv) < 3:
|
||||||
|
logger = Logger()
|
||||||
|
logger.err("This utility requires at least the following:")
|
||||||
|
logger.info(
|
||||||
|
f"\t{sys.argv[0]} (create|start|stop|restart|delete) SERVERNAME [options]"
|
||||||
|
)
|
||||||
|
parser.print_help()
|
||||||
|
return False
|
||||||
|
return True
|
||||||
|
|
||||||
|
|
||||||
|
def _stop_restart(parser: argparse.ArgumentParser):
|
||||||
|
"""
|
||||||
|
Handles adding the stop/restart arguments
|
||||||
|
"""
|
||||||
|
parser.add_argument(
|
||||||
|
"-f",
|
||||||
|
"--force",
|
||||||
|
help="Will forcibly shut down the server",
|
||||||
|
action="store_true",
|
||||||
|
required=False,
|
||||||
|
)
|
||||||
|
|
||||||
|
|
||||||
|
def _create(parser: argparse.ArgumentParser):
|
||||||
|
"""
|
||||||
|
Handles adding the create arguments
|
||||||
|
"""
|
||||||
|
|
||||||
|
########################################################
|
||||||
|
# Below are the more common commands, with shortcuts #
|
||||||
|
########################################################
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"-p",
|
||||||
|
"--port",
|
||||||
|
help="The port to run the newly created server on",
|
||||||
|
default="25565",
|
||||||
|
required=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.add_argument(
|
||||||
|
"-v",
|
||||||
|
"--version",
|
||||||
|
help="The version of Minecraft to run",
|
||||||
|
required=True,
|
||||||
|
)
|
||||||
|
|
||||||
|
parser.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,
|
||||||
|
)
|
||||||
75
src/dmsm.py
75
src/dmsm.py
|
|
@ -2,83 +2,14 @@
|
||||||
Author: Jose Stovall | oitsjustjose
|
Author: Jose Stovall | oitsjustjose
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import argparse
|
|
||||||
import sys
|
import sys
|
||||||
|
from argparse import Namespace
|
||||||
|
|
||||||
|
from args import get_args
|
||||||
from docker_mgr import ServerManager
|
from docker_mgr import ServerManager
|
||||||
from logger import Logger
|
|
||||||
|
|
||||||
|
|
||||||
def get_args() -> argparse.Namespace:
|
def main(args: Namespace) -> None:
|
||||||
"""
|
|
||||||
Cleanly and with education gets the arguments for the program
|
|
||||||
Does so based on the current task given, if any
|
|
||||||
Throws errors and a tantrum if you don't give it candy
|
|
||||||
Please give it candy.
|
|
||||||
Arguments:
|
|
||||||
None
|
|
||||||
Returns:
|
|
||||||
(argparse.NameSpace) the arguments for the program
|
|
||||||
"""
|
|
||||||
parser = argparse.ArgumentParser(
|
|
||||||
description="Arguments for Docker Minecraft Server Management (DMSM)",
|
|
||||||
)
|
|
||||||
|
|
||||||
if len(sys.argv) >= 2 and sys.argv[1] == "create":
|
|
||||||
parser.add_argument(
|
|
||||||
"-p",
|
|
||||||
"--port",
|
|
||||||
help="The port to run the newly created server on",
|
|
||||||
default="25565",
|
|
||||||
required=True,
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"-v",
|
|
||||||
"--version",
|
|
||||||
help="The version of Minecraft to run",
|
|
||||||
required=True,
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--root",
|
|
||||||
"-r",
|
|
||||||
help="The root on your disk for THIS server",
|
|
||||||
required=True,
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"--motd",
|
|
||||||
help="The MOTD for the server",
|
|
||||||
required=False,
|
|
||||||
default="A Minecraft Server",
|
|
||||||
)
|
|
||||||
parser.add_argument(
|
|
||||||
"-m",
|
|
||||||
"--memory",
|
|
||||||
help="The amount of dedicated memory for the server",
|
|
||||||
default="2G",
|
|
||||||
required=False,
|
|
||||||
)
|
|
||||||
|
|
||||||
if len(sys.argv) >= 2 and sys.argv[1] in ["stop", "restart"]:
|
|
||||||
parser.add_argument(
|
|
||||||
"-f",
|
|
||||||
"--force",
|
|
||||||
help="Will forcibly shut down the server",
|
|
||||||
action="store_true",
|
|
||||||
)
|
|
||||||
|
|
||||||
if len(sys.argv) < 3:
|
|
||||||
logger = Logger()
|
|
||||||
logger.err("This utility requires at least the following:")
|
|
||||||
logger.info(
|
|
||||||
f"\t{sys.argv[0]} (create|start|stop|restart|delete) SERVERNAME [options]"
|
|
||||||
)
|
|
||||||
parser.print_help()
|
|
||||||
sys.exit(0)
|
|
||||||
|
|
||||||
return parser.parse_args(args=sys.argv[3:])
|
|
||||||
|
|
||||||
|
|
||||||
def main(args: argparse.Namespace) -> None:
|
|
||||||
"""
|
"""
|
||||||
The main program function
|
The main program function
|
||||||
Arguments:
|
Arguments:
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,72 @@ Author: Jose Stovall | oitsjustjose
|
||||||
# pylint: disable=broad-except, bare-except
|
# pylint: disable=broad-except, bare-except
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import subprocess
|
||||||
from argparse import Namespace
|
from argparse import Namespace
|
||||||
|
from typing import Dict
|
||||||
|
from urllib.parse import urlparse
|
||||||
|
|
||||||
import docker
|
import docker
|
||||||
|
|
||||||
from logger import Logger
|
from logger import Logger
|
||||||
|
|
||||||
|
|
||||||
|
def _build_env(args: Namespace) -> Dict[str, any]:
|
||||||
|
"""
|
||||||
|
This function builds an Environment dict that can be
|
||||||
|
directly passed into the docker container creation
|
||||||
|
|
||||||
|
Arguments:
|
||||||
|
args (Namespace): The CLI args
|
||||||
|
Return:
|
||||||
|
(Dict[str, any]): the args, dictified
|
||||||
|
"""
|
||||||
|
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
|
||||||
|
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
|
||||||
|
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
|
||||||
|
|
||||||
|
print(ret)
|
||||||
|
|
||||||
|
return ret
|
||||||
|
|
||||||
|
|
||||||
class ServerManager:
|
class ServerManager:
|
||||||
"""
|
"""
|
||||||
A Management system for a docker container
|
A Management system for a docker container
|
||||||
|
|
@ -44,15 +103,10 @@ class ServerManager:
|
||||||
f"Server root '{args.root}' exists - there may be problems!"
|
f"Server root '{args.root}' exists - there may be problems!"
|
||||||
)
|
)
|
||||||
self._client.containers.run(
|
self._client.containers.run(
|
||||||
"itzg/minecraft-server",
|
f"itzg/minecraft-server:java{args.java}",
|
||||||
name=self._name,
|
name=self._name,
|
||||||
ports={25565: args.port},
|
ports={25565: args.port},
|
||||||
environment={
|
environment=_build_env(args),
|
||||||
"VERSION": args.version,
|
|
||||||
"EULA": True,
|
|
||||||
"MOTD": args.motd,
|
|
||||||
"MEMORY": args.maxr,
|
|
||||||
},
|
|
||||||
volumes={args.root: {"bind": "/data", "mode": "rw"}},
|
volumes={args.root: {"bind": "/data", "mode": "rw"}},
|
||||||
detach=True,
|
detach=True,
|
||||||
)
|
)
|
||||||
|
|
@ -116,9 +170,34 @@ class ServerManager:
|
||||||
"""
|
"""
|
||||||
Gets the status of a contianer
|
Gets the status of a contianer
|
||||||
"""
|
"""
|
||||||
self._log.info(self._container.status)
|
health = str(
|
||||||
|
subprocess.check_output(
|
||||||
|
[
|
||||||
|
"docker",
|
||||||
|
"container",
|
||||||
|
"inspect",
|
||||||
|
"-f",
|
||||||
|
"{{ .State.Health.Status }}",
|
||||||
|
self._name,
|
||||||
|
]
|
||||||
|
)
|
||||||
|
.decode()
|
||||||
|
.replace("\n", "")
|
||||||
|
)
|
||||||
|
container_status: str = self._container.status
|
||||||
|
|
||||||
|
if container_status.lower() == "running":
|
||||||
|
if health.lower() == "healthy":
|
||||||
|
self._log.success("Server is running and healthy")
|
||||||
|
elif health.lower() == "starting":
|
||||||
|
self._log.notice("Server is starting")
|
||||||
|
else:
|
||||||
|
self._log.warn(f"Server is running but in degraded state: {health}")
|
||||||
|
else:
|
||||||
|
self._log.info(f"Server is {self._container.status}")
|
||||||
|
|
||||||
def open_console(self):
|
def open_console(self):
|
||||||
"""
|
"""
|
||||||
Connects the user to the server's console
|
Connects the user to the server's console
|
||||||
"""
|
"""
|
||||||
|
os.system(f"docker exec -i {self._name} rcon-cli")
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ Author: Jose Stovall | oitsjustjose
|
||||||
"""
|
"""
|
||||||
|
|
||||||
from typing import Union
|
from typing import Union
|
||||||
|
|
||||||
from pip._vendor.colorama import Fore
|
from pip._vendor.colorama import Fore
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -58,3 +59,12 @@ class Logger:
|
||||||
_with_color(f"[{self._name}]: {msg}", Fore.RED)
|
_with_color(f"[{self._name}]: {msg}", Fore.RED)
|
||||||
else:
|
else:
|
||||||
_with_color(msg, Fore.RED)
|
_with_color(msg, Fore.RED)
|
||||||
|
|
||||||
|
def notice(self, msg: str) -> None:
|
||||||
|
"""
|
||||||
|
Prints out a notice in a color that will be seen
|
||||||
|
"""
|
||||||
|
if self._name:
|
||||||
|
_with_color(f"[{self._name}]: {msg}", Fore.BLUE)
|
||||||
|
else:
|
||||||
|
_with_color(msg, Fore.BLUE)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue