示例#1
0
def start():
    """
    Start the server
    :return: Ok if everything is fine, 400 error f server already running
    """
    global server  # Get the global value for reallocation
    if not server or not server.isalive():  # Check if the server is offline
        update_properties()  # Update server.properties
        # Start the server
        server = pexpect.spawn("java", ["-Xms"+conf["Server min ram"], "-Xmx"+conf["Server max ram"], "-jar",
                                        conf["Jar server"], "nogui"], cwd=Path(conf["Path"]), echo=False)
        if not isfile(Path(conf["Path"]) / "server.properties"):  # If no server.properties reboot to apply the changes
            # Wait the creation of the properties fle
            while not isfile(Path(conf["Path"]) / "server.properties"):
                sleep(1)
            kill()  # Kill the server
            # Wait for processe full exit
            while server.isalive():
                sleep(1)
            start()  # Start again the server
        # Wait the log file to be archived
        while open(Path(conf["Path"])/"logs"/"latest.log", "r").read() != "":
            sleep(1)
        return "Ok"
    else:
        abort(400, "Server is running")
示例#2
0
def stop():
    """
    Stop the server
    :return: The result of the /stop command or 400 error if server is not running
    """
    if server and server.isalive():  # Check if server is running
        return cmd("/stop")  # Launch /stop command
    else:
        abort(400, "Server is not running")
示例#3
0
def logs():
    """
    Get the server logs
    :return: Server last logs or 400 error if server is not running
    """
    if server and server.isalive():  # Check if server is running
        return open(Path(conf["Path"])/"logs"/"latest.log", "r").read()  # Send the content of the server logs
    else:
        abort(400, "Server is not running")
示例#4
0
def kill():
    """
    Kill the server
    :return: Ok or 400 if the server is not running
    """
    if server and server.isalive():  # Check if the server is running
        server.terminate()  # Kill the process
        return "Ok"
    else:
        abort(400, "Server is not running")
示例#5
0
def root():
    """
    Show server status
    :return: Actual status of the server
    """
    if server and server.isalive():  # Check is server is online
        try:  # In case of connexion errors
            status = mcq.status()
            query = mcq.query()
            return f"The server has {status.players.online} players and replied in {status.latency} ms"\
                   + "\n Online players: " + ", ".join(query.players.names)
        except OSError:
            abort(400, "Server did not respond")
    else:
        return "Server is offline"
示例#6
0
def cmd(command=None):
    """
    Execute a command by stdin on the server
    :param command: The command to execute
    :return: Ok or 400 if server is not running
    """
    if not command:
        try:
            command = request.json["command"]
        except (json.JSONDecodeError, KeyError):
            raise TypeError

    if server and server.isalive():
        server.sendline(command)
        return "Ok"
    else:
        abort(400, "Server is not running")
示例#7
0
def rcmd(command=None):
    """
    Execute a command by Rcon on the server
    :param command: The command to execute
    :return: The result of the command or 400 error if server not running also if Rcon connexion fail
    """
    if not command:
        try:
            command = request.json["command"]
        except (json.JSONDecodeError, KeyError):
            raise TypeError

    if server and server.isalive():  # Check if server is running
        try:  # In case of Rcon connexion fail
            with mcr:  # Open a Rco connexion
                resp = mcr.command(command)  # Send the command
        except (TimeoutError, ConnectionRefusedError):
            abort(400, "Server did not respond")
        else:
            return str(resp)
    else:
        abort(400, "Server is not running")