-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentrypoint.py
104 lines (81 loc) · 3.19 KB
/
entrypoint.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
#!/usr/bin/env python3
import argparse
from pathlib import Path
import shutil
import subprocess
SERVER_ARGS = [
"--mod-directory", "/opt/factorio/mods",
"--map-gen-settings", "/opt/factorio/config/map-gen-settings.json",
"--map-settings", "/opt/factorio/config/map-settings.json",
"--server-settings", "/opt/factorio/config/server-settings.json",
"--server-banlist", "/opt/factorio/config/server-banlist.json",
"--server-adminlist", "/opt/factorio/config/server-adminlist.json",
]
CONFIG_FILES = [
{
"path": "/opt/factorio/config/map-gen-settings.json",
"template": "/opt/factorio/data/map-gen-settings.example.json"
},
{
"path": "/opt/factorio/config/map-settings.json",
"template": "/opt/factorio/data/map-settings.example.json"
},
{
"path": "/opt/factorio/config/server-settings.json",
"template": "/opt/factorio/data/server-settings.example.json"
},
{
"path": "/opt/factorio/config/server-whitelist.json",
"template": "/opt/factorio/data/server-whitelist.example.json"
},
]
def main():
populate_volume()
args = parse_args()
run_args = ["/opt/factorio/bin/x64/factorio", "--start-server"]
save_path = f"/opt/factorio/saves/{args.save}"
run_args.append(save_path)
if args.whitelist:
print("Starting server with whitelist...")
run_args.extend(["--use-server-whitelist", "true", "--server-whitelist", "/opt/factorio/config/server-whitelist.json"])
else:
print("Starting server without whitelist...")
run_args.extend(SERVER_ARGS)
subprocess.call(run_args)
def parse_args():
parser = argparse.ArgumentParser(description="A Factorio Server. The script is a lightweight utilitiy for handling the configuration and starting of the real Factorio server.")
whitelist = parser.add_mutually_exclusive_group(required=True)
whitelist.add_argument("--whitelist", dest="whitelist", action="store_true", help="Enable the server whitelist. (Note: Either this or --no-whitelist must be present)")
whitelist.add_argument("--no-whitelist", dest="whitelist", action="store_false", help="Disable the server whitelist. (Note: Either this or --whitelist must be present)")
parser.add_argument("-s", "--save-file", dest="save", required=True, help="The name of the save file to load out of the saves directory (Eg. default.zip).")
args = parser.parse_args()
return args
def populate_volume():
for dir in ["/mnt/factorio/config", "/mnt/factorio/mods", "/mnt/factorio/saves"]:
if dir_is_accessible(dir):
continue
else:
Path(dir).mkdir()
populate_config_files()
def populate_config_files():
for config in CONFIG_FILES:
if file_is_accessible(config["path"]):
continue
else:
shutil.copy(config["template"], config["path"])
def dir_is_accessible(path):
try:
(Path(path) / "hello").touch()
(Path(path) / "hello").unlink()
except IOError:
return False
return True
def file_is_accessible(path):
try:
f = open(path)
f.close()
except IOError:
return False
return True
if __name__ == "__main__":
main()