forked from hexparrot/mineos
/
mineos_console.py
executable file
·138 lines (130 loc) · 6.05 KB
/
mineos_console.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
#!/usr/bin/python3
import os
import pprint
import types
from argparse import ArgumentParser
from getpass import getuser
from mineos import mc
from procfs_reader import path_owner
if __name__ == "__main__":
parser = ArgumentParser(description='MineOS command line execution scripts')
parser.add_argument('cmd',
help='the command to execute')
parser.add_argument('-s',
dest='server_name',
help='the server to act upon')
parser.add_argument('-d',
dest='base_directory',
help='the base of the mc file structure',
default='/var/games/minecraft')
parser.add_argument('--this',
action='store_true',
default=False,
help='use pwd to determine -d and -s values')
parser.add_argument('argv',
nargs='*',
help='additional arguments to pass to the command() function',
default=None)
args = parser.parse_args()
args.cmd = args.cmd.lower()
arguments = list(args.argv)
available_methods = list(m for m in dir(mc) if callable(getattr(mc, m)) and not m.startswith('_'))
available_properties = list(m for m in dir(mc) if not callable(getattr(mc, m)) and not m.startswith('_'))
pp = pprint.PrettyPrinter(indent=4)
if args.this:
args.server_name = os.path.basename(os.getcwd())
args.base_directory = os.path.dirname(os.path.dirname(os.getcwd()))
else:
args.base_directory = args.base_directory or os.path.expanduser("~")
mc._make_skeleton(args.base_directory)
if args.server_name:
owner = mc.has_server_rights(getuser(), args.server_name, args.base_directory)
if not owner and os.path.isdir(
os.path.join(args.base_directory, mc.DEFAULT_PATHS['servers'], args.server_name)):
raise OSError("User '%s' does not have rights to %s" % (
getuser(), os.path.join(args.base_directory, args.server_name)))
else:
instance = mc(args.server_name, owner, args.base_directory)
if args.cmd in ['screen', 'console']:
instance._command_direct('screen -r %s' % instance.screen_pid, instance.env['cwd'])
elif args.cmd in available_methods:
retval = getattr(instance, args.cmd)(*arguments)
if retval:
if isinstance(retval, types.GeneratorType):
pp.pprint(list(retval))
else:
pp.pprint(retval)
else:
print('{%s} completed successfully.' % args.cmd)
elif args.cmd in available_properties:
try:
previous_value = getattr(instance, args.cmd)
setattr(instance, args.cmd, arguments[0])
print('previous value:')
pp.pprint(previous_value)
print('current value:')
pp.pprint(getattr(instance, args.cmd))
except IndexError:
pp.pprint(getattr(instance, args.cmd))
else:
text = '%s %s' % (args.cmd, ' '.join(arguments))
instance._command_stuff(text)
print('{%s} sent to gameserver console [screen_pid:%s] successfully.' % (text, instance.screen_pid))
else:
init_args = {
'server_name': 'throwaway',
'owner': getuser(),
'base_directory': args.base_directory
}
if args.cmd == 'start':
for i in mc.list_servers_start_at_boot(args.base_directory):
try:
owner = path_owner(os.path.join(args.base_directory, mc.DEFAULT_PATHS['servers'], i))
print('starting %s...' % i, end=' ')
mc(i, owner, args.base_directory).start()
print(' done')
except Exception as ex:
print(ex)
elif args.cmd == 'stop':
for i in mc.list_servers_up():
try:
srv_ = i.server_name
owner = path_owner(os.path.join(args.base_directory, mc.DEFAULT_PATHS['servers'], srv_))
print("sending '%s' to %s..." % (args.cmd, srv_), end=' ')
instance = mc(srv_, owner, args.base_directory)._command_stuff(args.cmd)
print(' done')
except Exception as ex:
print(ex)
elif args.cmd in ['backup', 'archive']:
for i in mc.list_servers_to_act(args.cmd, args.base_directory):
try:
owner = path_owner(os.path.join(args.base_directory, mc.DEFAULT_PATHS['servers'], i))
print('starting %s for %s...' % (args.cmd, i), end=' ')
getattr(mc, args.cmd)(i, owner, args.base_directory)
print(' done')
except Exception as ex:
print(ex)
elif args.cmd == 'restore':
for i in mc.list_servers_restore_at_boot(args.base_directory):
try:
owner = path_owner(os.path.join(args.base_directory, mc.DEFAULT_PATHS['backup'], i))
print('starting %s for %s...' % (args.cmd, i), end=' ')
mc(i, owner, args.base_directory).restore()
print(' done')
except Exception as ex:
print(ex)
elif args.cmd in available_properties:
for i in sorted(mc.list_servers(args.base_directory)):
prop_ = getattr(mc(i, None, args.base_directory), args.cmd)
print('%s: %s' % (i, prop_))
elif args.cmd in available_methods:
retval = getattr(mc, args.cmd)(*arguments)
if retval:
if isinstance(retval, types.GeneratorType):
pp.pprint(list(retval))
else:
pp.pprint(retval)
else:
print('{%s} completed without error.' % args.cmd)
else:
raise NotImplementedError