-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathlauncher.py
executable file
·630 lines (503 loc) · 17.9 KB
/
launcher.py
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
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
import re
import os
import sys
import glob
import stat
import time
import errno
#start:
import subprocess
import getpass
import pwd
import tempfile
from . import manager
#config:
from .shared import find_config, open_resource, decode_if_bytes
#attach:
from . import user_client
#send/stop/kill
import json
import socket
#jar-list/jar-get
from . import servers
from twisted.internet import reactor
usage_text = "usage: mark2 command [options] [...]"
help_text = """
mark2 is a minecraft server wrapper
{usage}
commands:
{commands}
examples:
mark2 start /home/you/mcservers/pvp
mark2 attach
mark2 send say hello!
mark2 stop
"""
help_sub_text = """
mark2 {subcommand}: {doc}
usage: mark2 {subcommand} {value_spec}
"""
class Mark2Error(Exception):
def __init__(self, error):
self.error = error
def __str__(self):
return "error: %s" % self.error
class Mark2ParseError(Mark2Error):
def __str__(self):
return "%s\nparse error: %s" % (usage_text, self.error)
class Command(object):
name = ""
value_spec = ""
options_spec = tuple()
def __init__(self):
pass
def do_start(self):
pass
def do_end(self):
pass
@classmethod
def get_bases(cls):
o = []
while True:
cls = cls.__bases__[0]
if cls is object:
break
o.append(cls)
return o
@classmethod
def get_options_spec(cls):
return sum([list(b.options_spec) for b in [cls] + cls.get_bases()[::-1]], [])
def parse_options(self, c_args):
options = {}
options_tys = {}
#transform
for opt in self.__class__.get_options_spec():
for flag in opt[1]:
options_tys[flag] = opt
while len(c_args) > 0:
head = c_args[0]
if head[0] != '-':
break
elif head == '--':
c_args.pop(0)
break
elif head in options_tys:
opt = options_tys[c_args.pop(0)]
try:
options[opt[0]] = c_args.pop(0) if opt[2] != '' else True
except IndexError:
raise Mark2ParseError("option `%s` missing argument" % opt[0])
else:
raise Mark2Error("%s: unknown option %s" % (self.name, head))
self.options = options
self.value = ' '.join(c_args) if len(c_args) else None
def start(self):
bases = self.__class__.get_bases()
for b in bases[::-1]:
b.do_start(self)
self.run()
for b in bases:
b.do_end(self)
def run(self):
raise NotImplementedError
class CommandTyStateful(Command):
options_spec = (('base', ('-b', '--base'), 'PATH', 'the directory to put mark2-related temp files (default: /tmp/mark2)'),)
def do_start(self):
self.shared_path = self.options.get('base', '/tmp/mark2')
self.make_writable(self.shared_path)
#get servers
o = []
for path in glob.glob(self.shared('pid', '*')):
with open(path) as fp:
pid_string = fp.read()
if pid_string == "": # If the pid file is blank.
os.remove(path)
continue
pid = int(pid_string)
try:
os.kill(pid, 0)
except OSError as err:
if err.errno == errno.ESRCH:
os.remove(path)
continue
f = os.path.basename(path)
f = os.path.splitext(f)[0]
o.append(f)
self.servers = sorted(o)
def shared(self, ty, name=None):
if name is None:
name = self.server_name
return os.path.join(self.shared_path, "%s.%s" % (name, ty))
def make_writable(self, directory):
need_modes = stat.S_IRWXU | stat.S_IRGRP | stat.S_IXGRP | stat.S_IROTH | stat.S_IXOTH | stat.S_IRWXG | stat.S_IRWXO
good_modes = stat.S_IRWXU | stat.S_IRWXG | stat.S_IRWXO
if not os.path.exists(directory):
os.makedirs(directory, good_modes)
st = os.stat(directory)
if (st.st_mode & need_modes) == need_modes:
return True
try:
os.chmod(directory, good_modes)
return True
except Exception:
raise Mark2Error('%s does not have the necessary modes to run mark2 and I do not have permission to change them!' % directory)
class CommandTySelective(CommandTyStateful):
options_spec = (('name', ('-n', '--name'), 'NAME', 'create or select a server with this name'),)
name_should_exist = True
server_name = None
def do_start(self):
name = self.options.get('name', None)
if self.name_should_exist:
if name is None:
if len(self.servers) > 0:
name = self.servers[0]
else:
raise Mark2Error("no servers running!")
elif name not in self.servers:
raise Mark2Error("server not running: %s" % name)
else:
if name is None:
pass #CommandStart will fill it.
elif name in self.servers:
raise Mark2Error("server already running: %s" % name)
self.server_name = name
def do_send(self, data):
d = {
'type': 'input',
'user': '@external',
'line': data
}
d = json.dumps(d) + "\n"
s = socket.socket(socket.AF_UNIX, socket.SOCK_STREAM)
s.connect(self.shared('sock'))
s.send(d.encode())
s.close()
class CommandTyTerminal(CommandTySelective):
options_spec = (
('wait', ('-w', '--wait'), 'REGEX', 'wait for this line of output to appear on console before returning.'),
('only', ('-o', '--only'), '', 'print the matched line and no others'),
('immediate', ('-i', '--immediate'), '', 'don\'t wait for any output'))
wait = None
wait_from_start = False
only = False
def do_end(self):
if 'wait' in self.options:
self.wait = self.options['wait']
if 'only' in self.options:
self.only = True
if 'immediate' in self.options:
self.wait = None
try:
self.do_wait()
except KeyboardInterrupt:
pass
def do_wait(self):
if self.wait is None:
return
while not os.path.exists(self.shared('log')):
time.sleep(0.1)
with open(self.shared('log'), 'r') as f:
if not self.wait_from_start:
f.seek(0,2)
while True:
line = f.readline().rstrip()
if not line:
time.sleep(0.1)
continue
if line[0] in (" ", "\t"):
print(line)
continue
line = line.split(" ", 3)
if line[2] == '[mark2]':
line2 = line[3].split(" ", 2)
if re.search(self.wait, line2[2]):
print(line[3])
return
elif not self.only:
print(line[3])
class CommandHelp(Command):
"""display help and available options"""
name = 'help'
value_spec = "[COMMAND]"
def run(self):
if self.value is None:
print(help_text.format(
usage=usage_text,
commands=self.columns([(c.name, c.value_spec, c.__doc__) for c in commands]))
)
elif self.value in commands_d:
cls = commands_d[self.value]
print(help_sub_text.format(
subcommand = self.value,
doc = cls.__doc__,
value_spec = cls.value_spec
)
)
opts = cls.get_options_spec()
if len(opts) > 0:
print("options: \n{}\n".format(self.columns([(' '.join(o[1]), o[2], o[3]) for o in opts])))
else:
raise Mark2Error("Unknown command: %s" % self.value)
def columns(self, data):
o = []
for tokens in data:
line = ""
for i, token in enumerate(tokens):
line += token
line += " "*(((i+1)*12)-len(line))
o.append(line)
return "\n".join(" "+l for l in o)
class CommandStart(CommandTyTerminal):
"""start a server"""
name = 'start'
value_spec='[PATH]'
name_should_exist = False
def get_server_path(self):
self.jar_file = None
self.server_path = os.path.realpath("" if self.value is None else self.value)
if os.path.isdir(self.server_path):
pass
elif os.path.isfile(self.server_path):
if self.server_path.endswith('.jar'):
self.server_path, self.jar_file = os.path.split(self.server_path)
else:
raise Mark2Error("unknown file type: " + self.server_path)
else:
raise Mark2Error("path does not exist: " + self.server_path)
def check_config(self):
new_cfg = find_config('mark2.properties', ignore_errors=True)
if os.path.exists(new_cfg):
return
if os.path.exists(os.path.realpath(os.path.join(os.path.dirname(__file__), '..', 'config'))):
new_dir = os.path.dirname(new_cfg)
raise Mark2Error("mark2's configuration location has changed! move your config files to {}".format(new_dir))
else:
raise Mark2Error("mark2 is unconfigured! run `mark2 config` or `mkdir /etc/mark2 && touch /etc/mark2/mark2.properties` as root")
def check_ownership(self):
d_user = pwd.getpwuid(os.stat(self.server_path).st_uid).pw_name
m_user = getpass.getuser()
if d_user != m_user:
e = "server directory is owned by '{d_user}', but mark2 is running as '{m_user}'. " + \
"please start mark2 as `sudo -u {d_user} mark2 start ...`"
raise Mark2Error(e.format(d_user=d_user,m_user=m_user))
def daemonize(self):
if os.fork() > 0:
return 1
os.chdir(".")
os.setsid()
if os.fork() > 0:
sys.exit(0)
null = os.open(os.devnull, os.O_RDWR)
for fileno in (sys.stdin.fileno(), sys.stdout.fileno(), sys.stderr.fileno()):
try:
os.dup2(null, fileno)
except:
pass
return 0
def run(self):
# parse the server path
self.get_server_path()
# get server name
if self.server_name is None:
self.server_name = os.path.basename(self.server_path)
if self.server_name in self.servers:
raise Mark2Error("server already running: %s" % self.server_name)
# check for mark2.properties
self.check_config()
# check we own the server dir
self.check_ownership()
# clear old stuff
for x in ('log', 'sock', 'pid'):
if os.path.exists(self.shared(x)):
os.remove(self.shared(x))
i = 1
while True:
p = self.shared("log.%d" % i)
if not os.path.exists(p):
break
os.remove(p)
i += 1
if self.daemonize() == 0:
with open(self.shared('pid'), 'w') as f:
f.write("{}\n".format(os.getpid()))
mgr = manager.Manager(self.shared_path, self.server_name, self.server_path, self.jar_file)
reactor.callWhenRunning(mgr.startup)
reactor.run()
sys.exit(0)
self.wait = r'# mark2 started|stopped\.'
self.wait_from_start = True
class CommandConfig(Command):
"""configure mark2"""
options_spec = (('ask', ('-a', '--ask'), '', 'Ask before starting an editor'),)
name = 'config'
def check_executable(self, cmd):
return subprocess.call(
["command", "-v", cmd],
shell=True,
stdout=subprocess.PIPE,
stderr=subprocess.PIPE
) == 0
def copy_config(self, src, dest, header=''):
l0 = ''
while l0.strip() == '' or l0.startswith('### ###'):
l0 = decode_if_bytes(src.readline())
dest.write(header)
while l0 != '':
dest.write(l0)
l0 = decode_if_bytes(src.readline())
src.close()
dest.close()
def diff_config(self, src, dest):
diff = ""
d0 = src.readlines()
d1 = dest.readlines()
import difflib
ignore = " \t\f\r\n"
s = difflib.SequenceMatcher(lambda x: x in ignore, d0, d1)
for tag, i0, i1, j0, j1 in s.get_opcodes():
if tag in ('replace', 'insert'):
for l1 in d1[j0:j1]:
if l1.strip(ignore) != '':
diff += l1
return diff
def run(self):
path_old = 'resources/mark2.default.properties'
path_new = find_config('mark2.properties')
def write_config(data=''):
data = "# see resources/mark2.default.properties for details\n" + data
with open(path_new, 'w') as file_new:
file_new.write(data)
if "MARK2_TEST" not in os.environ and self.options.get('ask', False):
response = input('would you like to configure mark2 now? [yes] ') or 'yes'
if response != 'yes':
return write_config() if not os.path.exists(path_new) else None
editors = ["editor", "nano", "vim", "vi", "emacs"]
if "EDITOR" in os.environ:
editors.insert(0, os.environ["EDITOR"])
for editor in editors:
if self.check_executable(editor):
break
else:
if not os.path.exists(path_new):
write_config()
raise Mark2Error("no editor found. please set the $EDITOR environment variable.")
if os.path.exists(path_new):
subprocess.call([editor, path_new])
else:
#launch our editor
fd_tmp, path_tmp = tempfile.mkstemp(prefix='mark2.properties.', text=True)
with open_resource(path_old) as src:
with open(path_tmp, 'w') as dst:
self.copy_config(src, dst)
subprocess.call([editor, path_tmp])
#diff the files
with open_resource(path_old) as src:
with open(path_tmp, 'r') as dst:
write_config(self.diff_config(src, dst))
os.remove(path_tmp)
class CommandList(CommandTyStateful):
"""list running servers"""
name = 'list'
def run(self):
for s in self.servers:
print(s)
class CommandAttach(CommandTySelective):
"""attach to a server"""
name = 'attach'
def run(self):
f = user_client.UserClientFactory(self.server_name, self.shared_path)
f.main()
class CommandStop(CommandTyTerminal):
"""stop mark2"""
name = 'stop'
def run(self):
self.do_send('~stop')
self.wait=r'# mark2 stopped\.'
class CommandKill(CommandTyTerminal):
"""kill mark2"""
name = 'kill'
def run(self):
self.do_send('~kill')
self.wait = r'# mark2 stopped\.'
class CommandSend(CommandTyTerminal):
"""send a console command"""
name = 'send'
value_spec='INPUT...'
def run(self):
if self.value is None:
raise Mark2ParseError("nothing to send!")
self.do_send(self.value)
class CommandSendAll(CommandTyTerminal):
"""send a console command to all servers"""
name = 'sendall'
value_spec='INPUT...'
def run(self):
if self.value is None:
raise Mark2ParseError("nothing to send!")
for s in self.servers:
self.server_name = s
self.do_send(self.value)
class CommandJarList(Command):
"""list server jars"""
name = 'jar-list'
def run(self):
def err(what):
print("error: {}".format(what.value))
if reactor.running:
reactor.stop()
def handle(listing):
if reactor.running:
reactor.stop()
if len(listing) == 0:
print("error: no server jars found!")
else:
print("The following server jars/zips are available:")
print(listing)
def start():
d = servers.jar_list()
d.addCallbacks(handle, err)
reactor.callWhenRunning(start)
reactor.run()
class CommandJarGet(Command):
"""download a server jar"""
name = 'jar-get'
value_spec = 'NAME'
def run(self):
if self.value is None:
raise Mark2ParseError("missing jar type!")
def err(what):
print("error: {}".format(what.value))
if reactor.running:
reactor.stop()
def handle(filename):
if reactor.running:
reactor.stop()
if filename is None:
print("There was an error when saving the file!")
if os.path.exists(filename):
print("success! saved as {}".format(filename))
def start():
d = servers.jar_get(self.value)
d.addCallback(handle)
d.addErrback(err)
reactor.callWhenRunning(start)
reactor.run()
commands = (CommandHelp, CommandStart, CommandList, CommandAttach, CommandStop, CommandKill, CommandSend, CommandSendAll, CommandJarList, CommandJarGet, CommandConfig)
commands_d = dict([(c.name, c) for c in commands])
def main():
try:
c_args = sys.argv[1:]
if len(c_args) == 0:
command_name = 'help'
else:
command_name = c_args.pop(0)
command_cls = commands_d.get(command_name, None)
if command_cls is None:
raise Mark2ParseError("unknown command: %s" % command_name)
command = command_cls()
command.parse_options(c_args)
command.start()
return 0
except Mark2Error as e:
print(e)
return 1