-
Notifications
You must be signed in to change notification settings - Fork 0
/
start.py
782 lines (706 loc) · 33.2 KB
/
start.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
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
#!/usr/bin/env python3
from __future__ import annotations
import argparse, sys, os, subprocess
import json, secrets, shutil
from datetime import datetime
pref = "\033["
reset = f"{pref}0m"
class g:
keep_file = False
composefile = "oasis-compose-tmp-file.yml"
container_name = "oasis_gameserver"
compose_project_name = "oasis"
name = "Oasis"
config_file = "oasis-setup-config.json"
gammeserver_config_file = "game_server/config.yml"
prebuild_image = "oasis-prebuilder"
prebuilded_container = "oasis-prebuilded"
prebuilt_image = "oasis-vm-base"
use_build_on_compose = True
os.chdir(os.path.dirname(os.path.realpath(__file__)))
#Terminal colors
class colors:
black = "30m"
red = "31m"
green = "32m"
yellow = "33m"
blue = "34m"
magenta = "35m"
cyan = "36m"
white = "37m"
def dict_to_yaml(data, indent_spaces:int=4, base_indent:int=0, additional_spaces:int=0, add_text_on_dict:str|None=None):
yaml = ''
spaces = ' '*((indent_spaces*base_indent)+additional_spaces)
if isinstance(data, dict):
for key, value in data.items():
if not add_text_on_dict is None:
spaces_len = len(spaces)-len(add_text_on_dict)
spaces = (' '*max(spaces_len, 0))+add_text_on_dict
add_text_on_dict = None
if isinstance(value, dict) or isinstance(value, list):
yaml += f"{spaces}{key}:\n"
yaml += dict_to_yaml(value, indent_spaces=indent_spaces, base_indent=base_indent+1, additional_spaces=additional_spaces)
else:
yaml += f"{spaces}{key}: {value}\n"
spaces = ' '*((indent_spaces*base_indent)+additional_spaces)
elif isinstance(data, list):
for item in data:
if isinstance(item, dict):
yaml += dict_to_yaml(item, indent_spaces=indent_spaces, base_indent=base_indent, additional_spaces=additional_spaces+2, add_text_on_dict="- ")
elif isinstance(item, list):
yaml += dict_to_yaml(item, indent_spaces=indent_spaces, base_indent=base_indent+1, additional_spaces=additional_spaces)
else:
yaml += f"{spaces}- {item}\n"
else:
yaml += f"{data}\n"
return yaml
def puts(text, *args, color=colors.white, is_bold=False, **kwargs):
print(f'{pref}{1 if is_bold else 0};{color}' + text + reset, *args, **kwargs)
def sep(): puts("-----------------------------------", is_bold=True)
def cmd_check(program, get_output=False, print_output=False, no_stderr=False):
if get_output:
return subprocess.getoutput(program)
if print_output:
return subprocess.call(program, shell=True) == 0
return subprocess.call(program, stdout=subprocess.DEVNULL, stderr=subprocess.DEVNULL if no_stderr else subprocess.STDOUT, shell=True) == 0
def composecmd(cmd, composefile=None):
if composefile:
cmd = f"-f {composefile} {cmd}"
if not cmd_check("podman --version"):
return puts("Podman not found! please install podman!", color=colors.red)
elif not cmd_check("podman ps"):
return puts("Cannot use podman, the user hasn't the permission or podman isn't running", color=colors.red)
elif cmd_check("podman compose --version"):
return os.system(f"podman compose -p {g.compose_project_name} {cmd}")
elif cmd_check("podman-compose --version"):
return os.system(f"podman-compose -p {g.compose_project_name} {cmd}")
else:
return puts("Podman compose not found! please install podman compose!", color=colors.red)
def check_already_running():
return g.container_name in cmd_check(f'podman ps --filter "name=^{g.container_name}$"', get_output=True)
def prebuilder_exists():
return g.prebuild_image in cmd_check(f'podman image ls --filter "reference={g.prebuild_image}"', get_output=True)
def prebuilt_exists():
return g.prebuilt_image in cmd_check(f'podman image ls --filter "reference={g.prebuilt_image}"', get_output=True)
def remove_prebuilder():
return cmd_check(f'podman image rm {g.prebuild_image}')
def remove_prebuilt():
return cmd_check(f'podman image rm {g.prebuilt_image}')
def remove_prebuilded():
return cmd_check(f'podman container rm {g.prebuilded_container}')
def remove_database_volume():
return cmd_check(f'podman volume rm -f oasis_oasis-postgres-db')
def build_prebuilder():
return cmd_check(f'podman build -t {g.prebuild_image} -f ./vm/Dockerfile.prebuilder ./vm/', print_output=True)
def build_prebuilt():
return cmd_check(f'podman run -it --device /dev/fuse --cap-add audit_write,net_admin --security-opt label=disable --name {g.prebuilded_container} {g.prebuild_image}', print_output=True)
def kill_builder():
return cmd_check(f'podman kill {g.prebuilded_container}', no_stderr=True)
def commit_prebuilt():
return cmd_check(f'podman commit {g.prebuilded_container} {g.prebuilt_image}:latest', print_output=True)
def gen_args(args_to_parse: list[str]|None = None):
#Main parser
parser = argparse.ArgumentParser(description=f"{g.name} Manager")
parser.add_argument('--clear', dest="bef_clear", required=False, action="store_true", help=f'Delete volumes folder associated to {g.name} and oasis json config', default=False)
subcommands = parser.add_subparsers(dest="command", help="Command to execute", required=True)
#Compose Command
parser_compose = subcommands.add_parser('compose', help='Run podman compose command')
parser_compose.add_argument('compose_args', nargs=argparse.REMAINDER, help='Arguments to pass to podman compose', default=[])
#Start Command
parser_start = subcommands.add_parser('start', help=f'Start {g.name}')
parser_start.add_argument('--logs', required=False, action="store_true", help=f'Show {g.name} logs', default=False)
parser_start.add_argument('--reset', required=False, action="store_true", help=f'Regenerate VM base image and configuration', default=False)
parser_start.add_argument('--rebuild', required=False, action="store_true", help=f'Rebuild VM base image', default=False)
#Gameserve options
parser_start.add_argument('--wireguard-start-port', type=int, default=51000, help='Wireguard start port')
parser_start.add_argument('--gameserver-log-level', default="info", help='Log level for game server')
parser_start.add_argument('--max-vm-mem', type=str, default="2G", help='Max memory for VMs')
parser_start.add_argument('--max-vm-cpus', type=str, default="1", help='Max CPUs for VMs')
parser_start.add_argument('--wireguard-profiles', type=int, default=30, help='Number of wireguard profiles')
parser_start.add_argument('--dns', type=str, default="1.1.1.1", help='DNS server')
parser_start.add_argument('--submission-timeout', type=int, default=10, help='Submission timeout rate limit')
parser_start.add_argument('--flag-expire-ticks', type=int, default=5, help='Flag expire ticks')
parser_start.add_argument('--initial-service-score', type=int, default=5000, help='Initial service score')
parser_start.add_argument('--max-flags-per-request', type=int, default=2000, help='Max flags per request')
parser_start.add_argument('--start-time', type=str, help='Start time (ISO 8601)')
parser_start.add_argument('--end-time', type=str, help='End time (ISO 8601)')
parser_start.add_argument('--max-disk-size', type=str, default="30G", help='Max disk size for VMs')
parser_start.add_argument('--network-limit-bandwidth', type=str, default="20mbit", help='Network limit bandwidth')
#init options
parser_start.add_argument('--privileged', '-P', action='store_true', help='Use privileged mode for VMs')
parser_start.add_argument('--expose-gameserver', '-E', action='store_true', help='Expose gameserver port')
parser_start.add_argument('--gameserver-port', default="127.0.0.1:8888", help='Gameserver port')
parser_start.add_argument('--config-only', '-C', action='store_true', help='Only generate config file')
parser_start.add_argument('--disk-limit', '-D', action='store_true', help='Limit disk size for VMs (NEED TO ENABLE QUOTAS)')
#Stop Command
parser_stop = subcommands.add_parser('stop', help=f'Stop {g.name}')
parser_stop.add_argument('--clear', required=False, action="store_true", help=f'Delete podman volume associated to {g.name} resetting all the settings', default=False)
parser_restart = subcommands.add_parser('restart', help=f'Restart {g.name}')
parser_restart.add_argument('--logs', required=False, action="store_true", help=f'Show {g.name} logs', default=False)
parser_restart.add_argument('--privileged', '-P', action='store_true', help='Use privileged mode for VMs')
parser_restart.add_argument('--disk-limit', '-D', action='store_true', help='Limit disk size for VMs')
parser_restart.add_argument('--expose-gameserver', '-E', action='store_true', help='Expose gameserver port')
parser_restart.add_argument('--gameserver-port', default="127.0.0.1:8888", help='Gameserver port')
subcommands.add_parser('enable-quotas', help=f'Enable quotas for VMs (Need XFS and this file has to be running directly in the host) (Need to be run only once)')
args = parser.parse_args(args=args_to_parse)
if not "clear" in args:
args.clear = False
if not "privileged" in args:
args.privileged = False
if not "expose_gameserver" in args:
args.expose_gameserver = False
if not "gameserver_port" in args:
args.gameserver_port = "127.0.0.1:8888"
if not "disk_limit" in args:
args.disk_limit = False
if not check_for_quotas() and args.disk_limit:
if not ask_for_quota_command():
exit(1)
args.clear = args.bef_clear or args.clear
return args
def ask_for_quota_command():
print("This command will set some settings for podman to enable quotas")
print("- Run this only once")
print("- Run me directly in the host that runs the containers")
print("- Run me with root privilages")
puts("If one of these conditions are not met, please cancel this command", color=colors.red)
if input('You are running the command correctly? (y/N): ').lower() == 'y':
enable_quotas()
puts("Quotas enabled!", color=colors.green)
return True
else:
puts("Operation cancelled", color=colors.red)
return False
quota_setting_xfs = [
("100000:/var/lib/containers/storage/overlay", "/etc/projects"),
("200000:/var/lib/containers/storage/volumes", "/etc/projects"),
("storage:100000", "/etc/projid"),
("volumes:200000", "/etc/projid"),
]
def check_for_quotas():
for data_to_write, filename in quota_setting_xfs:
try:
with open(filename, 'r') as f:
data = f.read()
if not data_to_write in data:
return False
except FileNotFoundError:
return False
return True
def enable_quotas():
for data_to_write, filename in quota_setting_xfs:
try:
with open(filename, 'r') as f:
data = f.read()
except FileNotFoundError:
data = ""
if not data_to_write in data:
with open(filename, 'a') as f:
f.write(data_to_write+'\n')
if not cmd_check("xfs_quota -x -c 'project -s storage volumes' /", print_output=True):
puts("Failed to setup xfs quotas", color=colors.red)
exit(1)
args = gen_args()
def write_compose(data):
with open(g.composefile,"wt") as compose:
compose.write(dict_to_yaml({
"services": {
"router": {
"hostname": f"router",
"dns": [data['dns']],
"build": "./router",
"cap_add": [
"NET_ADMIN",
"SYS_MODULE",
"SYS_ADMIN",
],
"sysctls": [
"net.ipv4.ip_forward=1",
"net.ipv4.tcp_timestamps=0",
"net.ipv4.conf.all.rp_filter=1",
"net.ipv6.conf.all.forwarding=0",
"net.ipv6.conf.eth0.autoconf=0"
],
"environment": {
"NTEAM": len(data['teams']),
"RATE_NET": data['network_limit_bandwidth'],
},
"volumes": [
"unixsk:/unixsk/"
],
"restart": "unless-stopped",
"networks": {
**{f"vm-team{team['id']}": {
"priority": 10,
"ipv4_address": f"10.60.{team['id']}.250"
} for team in data['teams']},
"gameserver": {
"priority": 10,
"ipv4_address": "10.10.0.250"
},
"externalnet": {
"priority": 1,
},
**{
f"players{team['id']}":{
"priority": 10,
"ipv4_address": f"10.80.{team['id']}.250"
} for team in data['teams'] if not team['nop']
}
}
},
"database": {
"hostname": f"oasis-database",
"dns": [data['dns']],
"image": "postgres:17",
"restart": "unless-stopped",
"environment": {
"POSTGRES_USER": "oasis",
"POSTGRES_PASSWORD": "oasis",
"POSTGRES_DB": "oasis"
},
"volumes": [
"oasis-postgres-db:/var/lib/postgresql/data"
],
"networks": {
"internalnet": "",
}
},
"gameserver": {
"hostname": f"gameserver",
"dns": [data['dns']],
"build": "./game_server",
"restart": "unless-stopped",
"container_name": g.container_name,
"cap_add": [
"NET_ADMIN"
],
**({
"ports": [
f"{args.gameserver_port}:80"
]
} if args.expose_gameserver else {}),
"depends_on": [
"router",
"database"
],
"networks": {
"internalnet": {
"priority": 1
},
"gameserver": {
"priority": 10,
"ipv4_address": "10.10.0.1"
}
},
"volumes": [
"./game_server/checkers/:/app/checkers/:z",
"unixsk:/unixsk/:z",
"./game_server/config.yml:/app/config.yml:z"
]
},
**{
f"team{team['id']}": {
"hostname": f"team{team['id']}",
"dns": [data['dns']],
"cap_add": [
"CAP_AUDIT_WRITE",
"NET_ADMIN",
"NET_RAW",
],
"security_opt":[
"label=disable",
],
"build": {
"context": "./vm",
"args": {
"TOKEN": team['token'],
}
},
**({ "storage_opt": {"size":data['max_disk_size']} } if args.disk_limit else {}),
"sysctls": [
"net.ipv4.ip_unprivileged_port_start=1" #Allow non-privilaged podman to bind all ports
],
**({"privileged": "true"} if args.privileged else {}),
"restart": "unless-stopped",
"devices": [
"/dev/fuse",
"/dev/net/tun"
],
#Allow to edit net.* sysctls (kernel will show only sys option in the network namespace of the container)
"volumes": [
"/proc/sys/net:/proc/sys/net",
],
"networks": {
f"vm-team{team['id']}": {
"ipv4_address": f"10.60.{team['id']}.1"
}
},
"deploy":{
"resources":{
"limits":{
"cpus": f'"{data["max_vm_cpus"]}"',
"memory": data['max_vm_mem']
}
}
}
} for team in data['teams']
},
**{
f"wireguard{team['id']}": {
"hostname": f"wireguard{team['id']}",
"dns": [data['dns']],
"build": "./wireguard",
"restart": "unless-stopped",
"cap_add": [
"NET_ADMIN",
"SYS_MODULE"
],
"sysctls": [
"net.ipv4.ip_forward=1",
"net.ipv4.conf.all.src_valid_mark=1",
],
"volumes": [
f"./wireguard/conf{team['id']}/:/config/:z"
],
"networks": {
f"players{team['id']}": {
"ipv4_address": f"10.80.{team['id']}.128"
}
},
"ports": [
f"{data['wireguard_start_port']+team['id']}:51820/udp"
],
"environment": {
"PUID": 1000,
"PGID": 1000,
"TZ": "Etc/UTC",
"PEERS": data['wireguard_profiles'],
"PEERDNS": data['dns'],
"ALLOWEDIPS": "10.10.0.0/16, 10.60.0.0/16, 10.80.0.0/16",
"SERVERURL": data['server_addr'],
"SERVERPORT": data['wireguard_start_port']+team['id'],
"INTERNAL_SUBNET": f"10.80.{team['id']}.0/24",
}
} for team in data['teams'] if not team['nop']
}
},
"volumes": {
"unixsk": "",
"oasis-postgres-db": ""
},
"networks": {
"externalnet": "",
"internalnet": "",
"gameserver": {
"internal": "true",
"driver": "macvlan",
"ipam": {
"driver": "default",
"config": [
{
"subnet": "10.10.0.0/24",
"gateway": "10.10.0.254",
}
]
}
},
**{
f"vm-team{team['id']}": {
"internal": "true",
"driver": "macvlan",
"ipam": {
"driver": "default",
"config": [
{
"subnet": f"10.60.{team['id']}.0/24",
"gateway": f"10.60.{team['id']}.254",
}
]
}
} for team in data['teams']
},
**{
f"players{team['id']}": {
"driver": "bridge",
"ipam": {
"driver": "default",
"config": [
{
"subnet": f"10.80.{team['id']}.0/24",
"gateway": f"10.80.{team['id']}.254",
}
]
}
} for team in data['teams'] if not team['nop']
}
}
}))
def try_to_remove(file):
try:
os.remove(file)
except FileNotFoundError:
pass
def clear_data(
remove_config=True,
remove_prebuilded_container=True,
remove_prebuilder_image=True,
remove_prebuilt_image=True,
remove_wireguard=True,
remove_checkers_data=True,
remove_gameserver_config=True,
remove_gameserver_data=True
):
if remove_gameserver_data:
remove_database_volume()
if remove_wireguard:
for file in os.listdir("./wireguard"):
if file.startswith("conf"):
shutil.rmtree(f"./wireguard/{file}", ignore_errors=True)
if remove_config:
try_to_remove(g.config_file)
if remove_gameserver_config:
try_to_remove(g.gammeserver_config_file)
if remove_prebuilded_container:
remove_prebuilded()
if remove_prebuilder_image:
remove_prebuilder()
if remove_prebuilt_image:
remove_prebuilt()
if remove_checkers_data:
for service in os.listdir("./game_server/checkers"):
shutil.rmtree(f"./game_server/checkers/{service}/flag_ids", ignore_errors=True)
def try_mkdir(path):
try:
os.mkdir(path)
except FileExistsError:
pass
def dict_to_yaml(data, indent_spaces:int=4, base_indent:int=0, additional_spaces:int=0, add_text_on_dict:str|None=None):
yaml = ''
spaces = ' '*((indent_spaces*base_indent)+additional_spaces)
if isinstance(data, dict):
for key, value in data.items():
if not add_text_on_dict is None:
spaces_len = len(spaces)-len(add_text_on_dict)
spaces = (' '*max(spaces_len, 0))+add_text_on_dict
add_text_on_dict = None
if isinstance(value, dict) or isinstance(value, list):
yaml += f"{spaces}{key}:\n"
yaml += dict_to_yaml(value, indent_spaces=indent_spaces, base_indent=base_indent+1, additional_spaces=additional_spaces)
else:
yaml += f"{spaces}{key}: {value}\n"
spaces = ' '*((indent_spaces*base_indent)+additional_spaces)
elif isinstance(data, list):
for item in data:
if isinstance(item, dict):
yaml += dict_to_yaml(item, indent_spaces=indent_spaces, base_indent=base_indent, additional_spaces=additional_spaces+2, add_text_on_dict="- ")
elif isinstance(item, list):
yaml += dict_to_yaml(item, indent_spaces=indent_spaces, base_indent=base_indent+1, additional_spaces=additional_spaces)
else:
yaml += f"{spaces}- {item}\n"
else:
yaml += f"{data}\n"
return yaml
def generate_teams_array(number_of_teams: int, enable_nop_team: bool, wireguard_start_port: int):
teams = []
for i in range(number_of_teams + (1 if enable_nop_team else 0)):
team = {
'id': i,
'name': f'Team {i}',
'token': secrets.token_hex(32),
'wireguard_port': wireguard_start_port+i,
'nop': False,
'image': "null"
}
if i == 0 and enable_nop_team:
team['nop'] = True
team['name'] = 'Nop Team'
teams.append(team)
return teams
def config_input():
data = {}
if args.privileged:
puts("Privileged mode enabled (DO NOT USE THIS IN PRODUCTION)", color=colors.yellow)
while True:
number_of_teams = int(input('Number of teams: '))
if number_of_teams < 1:
print('Number of teams must be greater than 0')
elif number_of_teams > 250:
print('Number of teams must be less or equal than 250')
else:
break
if args.wireguard_start_port <= 0:
print('Wireguard start port must be greater than 0')
exit(1)
elif args.wireguard_start_port+number_of_teams > 65535:
print(f'Wireguard start port must be less or equal than {args.wireguard_start_port+number_of_teams}')
exit(1)
data['wireguard_start_port'] = args.wireguard_start_port
data['dns'] = args.dns
data['wireguard_profiles'] = args.wireguard_profiles
data['max_vm_cpus'] = args.max_vm_cpus
data['max_vm_mem'] = args.max_vm_mem
data['gameserver_token'] = secrets.token_hex(32)
data['gameserver_log_level'] = args.gameserver_log_level
data['flag_expire_ticks'] = args.flag_expire_ticks
data['initial_service_score'] = args.initial_service_score
data['max_flags_per_request'] = args.max_flags_per_request
data['start_time'] = datetime.fromisoformat(args.start_time).isoformat() if args.start_time else None
data['end_time'] = datetime.fromisoformat(args.end_time).isoformat() if args.end_time else None
data['submission_timeout'] = args.submission_timeout
enable_nop_team = input('Enable NOP team? (Y/n): ').lower() != 'n'
data['server_addr'] = input('Server address: ')
data['network_limit_bandwidth'] = args.network_limit_bandwidth
data['max_disk_size'] = args.max_disk_size
while True:
try:
data['tick_time'] = abs(int(input('Tick time (s): ')))
break
except Exception:
print('Invalid tick time')
pass
data['teams'] = generate_teams_array(number_of_teams, enable_nop_team, args.wireguard_start_port)
return data
def create_config(data):
with open(g.config_file, 'w') as f:
json.dump(data, f, indent=4)
return data
def config_exists():
return os.path.isfile(g.config_file)
def read_config():
with open(g.config_file) as f:
return json.load(f)
def write_gameserver_config(data):
nop_team = list(filter(lambda x: x['nop'], data['teams']))
nop_team = nop_team[0]['id'] if nop_team else None
gameserver_config = {
"log_level": data['gameserver_log_level'],
"round_len": data['tick_time']*1000,
"token": data['gameserver_token'],
"nop": f"10.60.{nop_team}.1" if not nop_team is None else "null",
"submitter_limit": data['submission_timeout']*1000,
"teams": {
**{
f"10.60.{team['id']}.1": {
"token": team['token'],
"name": team['name'],
"image": team['image']
} for team in data['teams']
},
},
"flag_expire_ticks": data['flag_expire_ticks'],
"initial_service_score": data['initial_service_score'],
"max_flags_per_request": data['max_flags_per_request'],
"start_time": data['start_time'] if data['start_time'] else "null",
"end_time": data['end_time'] if data['end_time'] else "null",
"debug": "false",
}
with open(g.gammeserver_config_file, 'w') as f:
f.write(dict_to_yaml(gameserver_config))
def main():
if not cmd_check("podman --version"):
puts("Podman not found! please install podman!", color=colors.red)
if not cmd_check("podman ps"):
puts("Podman is not running, please install podman and podman compose!", color=colors.red)
exit()
elif not cmd_check("podman-compose --version") and not cmd_check("podman compose --version"):
puts("Podman compose not found! please install podman compose!", color=colors.red)
exit()
if args.command:
match args.command:
case "enable-quotas":
ask_for_quota_command()
return
case "start":
if check_already_running():
puts(f"{g.name} is already running!", color=colors.yellow)
if args.reset or not config_exists() or args.config_only:
config = config_input()
create_config(config)
else:
config = read_config()
if args.config_only:
puts(f"Config file generated!, you can customize editing {g.config_file}", color=colors.green)
return
if args.reset:
puts("Clearing old setup images and volumes", color=colors.yellow)
clear_data(remove_config=False)
elif args.rebuild:
clear_data(remove_config=False, remove_checkers_data=False, remove_gameserver_data=False, remove_wireguard=False, remove_prebuilder_image=False)
write_gameserver_config(config)
if not prebuilt_exists():
if not (args.reset or args.rebuild):
puts("Prebuilt image not found!", color=colors.yellow)
puts("Clearing old setup images...", color=colors.yellow)
#If these images exists, we need to remove them to avoid errors
remove_prebuilded()
remove_prebuilt()
puts("Building the prebuilder image", color=colors.yellow)
if not build_prebuilder():
puts("Error building prebuilder image", color=colors.red)
exit(1)
puts("Executing prebuilder to creating VMs base image", color=colors.yellow)
if not build_prebuilt():
puts("Error building prebuilt image", color=colors.red)
exit(1)
puts("Creating base VM image (this action takes time and gives no output)", color=colors.yellow)
if not commit_prebuilt():
puts("Error commiting prebuilt image", color=colors.red)
exit(1)
puts("Clear unused images", color=colors.yellow)
remove_prebuilded()
if not config_exists():
puts(f"Config file not found! please run {sys.argv[0]} start", color=colors.red)
else:
puts(f"{g.name} is starting!", color=colors.yellow)
write_compose(read_config())
puts(f"Running 'podman compose up -d{' --build' if use_build_on_compose else ''}'\n", color=colors.green)
composecmd(f"up -d{' --build' if use_build_on_compose else ''} --remove-orphans", g.composefile)
case "compose":
if not config_exists():
puts(f"Config file not found! please run {sys.argv[0]} start", color=colors.red)
else:
write_compose(read_config())
compose_cmd = " ".join(args.compose_args)
puts(f"Running 'podman compose {compose_cmd}'\n", color=colors.green)
composecmd(compose_cmd, g.composefile)
case "restart":
if not config_exists():
puts(f"Config file not found! please run {sys.argv[0]} start", color=colors.red)
elif check_already_running():
write_compose(read_config())
puts("Running 'podman compose restart'\n", color=colors.green)
composecmd("restart", g.composefile)
else:
puts(f"{g.name} is not running!" , color=colors.red, is_bold=True, flush=True)
case "stop":
if not config_exists():
puts(f"Config file not found! please run {sys.argv[0]} start", color=colors.red)
elif check_already_running():
write_compose(read_config())
puts("Running 'podman compose down'\n", color=colors.green)
composecmd("down --remove-orphans", g.composefile)
else:
puts(f"{g.name} is not running!" , color=colors.red, is_bold=True, flush=True)
if args.clear:
if check_already_running():
puts(f"{g.name} is running! please stop it before clear the data", color=colors.red)
exit(1)
clear_data()
puts(f"Volumes and config clean!", color=colors.green, is_bold=True)
if "logs" in args and args.logs:
if config_exists():
write_compose(read_config())
else:
puts(f"Config file not found! please run {sys.argv[0]} start", color=colors.red)
composecmd("logs -f")
if __name__ == "__main__":
try:
try:
main()
finally:
kill_builder()
if os.path.isfile(g.composefile) and not g.keep_file:
os.remove(g.composefile)
except KeyboardInterrupt:
print()