aboutsummaryrefslogtreecommitdiff
path: root/mcwrapper
blob: e65a0f5573df1159eaa48ee1c55b6e1b6992f858 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
#!/usr/bin/env python3
# vim: expandtab ft=python ts=4 sw=4 sts=4:
import os
import sys
import subprocess
import signal
import time
import atexit
import argparse
from threading import Thread
###############################################################################
# Exit codes and prints helpers
verbose_level = 0


def __print_message__(message, file=sys.stdout, notime=False):
    if notime:
        print(message, file=file)
    else:
        print('[' + time.strftime('%H:%M:%S') + '] ' + message, file=file)


def info(message, minverbose=0, notime=False):
    "Prints message to stdout if minverbose >= verbose_level"
    if verbose_level >= minverbose:
        __print_message__(message, notime=notime)


def warning(message, minverbose=-1, notime=False):
    "Prints message to stderr if minverbose >= verbose_level"
    if verbose_level >= minverbose:
        __print_message__(message, file=sys.stderr, notime=notime)


def error(message, minverbose=-2, errcode=-1, notime=False):
    "Prints message to stderr if minverbose >= verbose_level"
    if verbose_level >= minverbose:
        __print_message__(message, file=sys.stderr, notime=notime)
    sys.exit(errcode)

###############################################################################

__STATUSSTRINGS__ = {
    0: "Not running",
    1: "Starting",
    2: "Running",
    3: "Stopping",
    }

__INPUTPIPE__ = 'input_pipe'
__STATUSFILE__ = 'status'
__PLAYERSFILE__ = 'players'
__PIDFILE__ = 'server.pid'


class MCServer:
    "Minecraft server wrapper class"
    def __init__(self, command, statusfile=False, playersfile=False):
        self.players = set()
        self.status = 0
        self.process = None
        self.command = command
        self.statusfile = statusfile
        self.plaersfile = playersfile
        info("Server wrapper initializing")
        if os.path.isfile(__PIDFILE__):
            with open(__PIDFILE__) as file:
                lpid = int(file.readline())
            try:
                os.kill(lpid, 0)
            except OSError:
                warning("Detected forced termination of previous server "
                        "wrapper instance.")
            else:
                error("Another wrapper is running with given identifier.",
                      -1, 1)
        try:
            os.mkfifo(__INPUTPIPE__, 0o640)
        except FileExistsError:
            pass
        if statusfile:
            with open(__STATUSFILE__, 'w') as file:
                file.write(__STATUSSTRINGS__[0] + '\n')
        if playersfile:
            open(__PLAYERSFILE__, 'w')
        self.inputthread = Thread(target=self.__input_thread__,
                                  daemon=True)
        self.outputhread = Thread(target=self.__output_thread__,
                                  daemon=True)

    def clean(self):
        "Cleans files generated by wrapper"
        info("Server wrapper clean.")
        try:
            os.remove(__INPUTPIPE__)
        except FileNotFoundError:
            pass
        try:
            os.path.isfile(__PIDFILE__)
        except FileNotFoundError:
            pass
        try:
            os.remove(__STATUSFILE__)
        except FileNotFoundError:
            pass
        try:
            os.remove(__STATUSFILE__)
        except FileNotFoundError:
            pass

    def execstart(self):
        "Start execution of Minecraft server and hold until its exits"
        self.start()
        self.process.wait()

    def start(self):
        "Start Minecraft server"
        self.process = subprocess.Popen(
            self.command, stdin=subprocess.PIPE,
            stdout=subprocess.PIPE, stderr=subprocess.STDOUT,
            start_new_session=False)
        with open(__PIDFILE__, "w") as file:
            file.write(str(self.process.pid))
        self.status = 1
        if self.statusfile:
            with open(__STATUSFILE__, 'w') as file:
                file.write(__STATUSSTRINGS__[1] + '\n')
        if not self.inputthread.is_alive():
            self.inputthread.start()
        if not self.outputhread.is_alive():
            self.outputhread.start()

    def stop(self):
        "Sends /stop command to Minecraft server"
        if self.running():
            self.process.stdin.write(bytes(
                "/stop\n", sys.getdefaultencoding()))
            self.process.stdin.flush()

    def running(self):
        "Returns True if mc server is running. Othervise False."
        return bool(self.status)

    def write_to_terminal(self, text):
        "Write to server terminal. If server not running it does nothing"
        if self.status == 2:
            info("Input: " + text, 1)
            self.process.stdin.write(bytes(text, sys.getdefaultencoding()))
            self.process.stdin.flush()
            return True
        else:
            return False

    def __user_join__(self, username):
        info("User '" + username + "' joined server.")
        self.players.add(username)
        if self.plaersfile:
            with open(__PLAYERSFILE__, 'a') as file:
                file.write(username + '\n')

    def __user_leave__(self, username):
        info("User '" + username + "' left server.")
        self.players.remove(username)
        if self.plaersfile:
            with open(__PLAYERSFILE__, 'w') as file:
                file.writelines(self.players)
                if self.players:
                    file.write('\n')

    def __parse_line__(self, line):
        if ': Done' in line:
            info("Server start.")
            self.status = 2
            if self.statusfile:
                with open(__STATUSFILE__, 'w') as file:
                    file.write(__STATUSSTRINGS__[2] + '\n')
        elif ': Stopping the server' in line:
            info("Server stop.")
            self.status = 3
            if self.statusfile:
                with open(__STATUSFILE__, 'w') as file:
                    file.write(__STATUSSTRINGS__[3] + '\n')
        elif 'logged in with entity id' in line:
            name = line[len('[00:00:00] [Server thread/INFO]: '):]
            name = name[:name.index('[')]
            self.__user_join__(name)
        elif 'left the game' in line:
            name = line[len('[00:00:00] [Server thread/INFO]: '):]
            name = name[:name.index(' ')]
            self.__user_leave__(name)

    def __output_thread__(self):
        for linen in self.process.stdout:
            line = linen.decode(sys.getdefaultencoding())
            info(line.rstrip(), 2, notime=True)
            self.__parse_line__(line.rstrip())
        if self.statusfile:
            with open(__STATUSFILE__, 'w') as file:
                file.write(__STATUSSTRINGS__[0] + '\n')

    def __input_thread__(self):
        with open(__INPUTPIPE__, 'r') as pipe:
            while True:
                line = pipe.readline().rstrip()
                if line:
                    self.write_to_terminal(line + "\n")
                else:
                    time.sleep(3)

###############################################################################
mcserver = None


def __wrapper_atexit__():
    "This is called when wrapper is exiting"
    mcserver.clean()


def __wrapper_toexit__():
    "This function is called when system signalizes that mcwrapper should exit"
    mcserver.stop()


def __signal_term__(_signo, _stack_frame):
    __wrapper_toexit__()


__HELP_DESC__ = """
    This script is executing Minecraft server and reads its output. From output
    is extracted server status and list of online players. And standard input
    can be accessed by fifo file.
    """


def main():
    "Main function"
    global verbose_level
    parser = argparse.ArgumentParser(description=__HELP_DESC__)
    parser.add_argument('--verbose', '-v', action='count', default=0,
                        help="Increase verbose level of output")
    parser.add_argument('--quiet', '-q', action='count', default=0,
                        help="Decrease verbose level of output")
    parser.add_argument('--status-file', '-s', action='store_true',
                        help="Outputs server status to file \"status\"")
    parser.add_argument('--players-file', '-p', action='store_true',
                        help="""Outputs list of online players to file
                        \"players\" """)
    parser.add_argument('command', nargs=argparse.REMAINDER,
                        help="""Command to be executed to start Minecraft
                        server.""")
    args = parser.parse_args()

    verbose_level = args.verbose - args.quiet
    command = args.command
    sfile = args.status_file
    pfile = args.players_file

    if not command:
        parser.print_help()
    if 'nogui' not in command:
        command.append('nogui')

    global mcserver
    mcserver = MCServer(command, pfile, sfile)
    signal.signal(signal.SIGTERM, __signal_term__)
    signal.signal(signal.SIGINT, __signal_term__)
    atexit.register(__wrapper_atexit__)

    mcserver.execstart()

if __name__ == '__main__':
    main()