forked from freeciv/freeciv-web
-
Notifications
You must be signed in to change notification settings - Fork 0
/
publite2.py
185 lines (159 loc) · 7.24 KB
/
publite2.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
#!/usr/bin/env python3
'''**********************************************************************
Publite2 is a process manager which launches multiple Freeciv-web servers.
Copyright (C) 2009-2017 The Freeciv-web project
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
***********************************************************************'''
from os import *
import sys
import time
import http.client
import configparser
from pubstatus import PubStatus
from civlauncher import Civlauncher
import os.path
import glob
metahost = "localhost"
metaport = 8080
metapath = "/freeciv-web/meta/metaserver"
statuspath = "/freeciv-web/meta/status"
settings_file = "settings.ini"
game_types = ["singleplayer", "multiplayer", "pbem"]
metachecker_interval = 40
port = 6000
# The Metachecker class connects to the Freeciv-web metaserver, gets the number of available
# Freeciv-web server instances, and launches new servers if needed.
class metachecker():
def __init__(self):
self.server_list = []
if not os.path.isfile(settings_file):
print("ERROR: Publite2 isn't setup correctly. Copy settings.ini.dist to settings.ini " +
"and update it do match your system.")
sys.exit(1)
settings = configparser.ConfigParser()
settings.read(settings_file)
self.server_capacity_single = int(settings.get("Resource usage", "server_capacity_single",
fallback = 5))
self.server_capacity_multi = int(settings.get("Resource usage", "server_capacity_multi",
fallback = 2))
self.server_capacity_pbem = int(settings.get("Resource usage", "server_capacity_pbem",
fallback = 2))
self.server_limit = int(settings.get("Resource usage", "server_limit",
fallback = 250))
self.savesdir = settings.get("Config", "save_directory",
fallback = "/var/lib/tomcat10/webapps/data/savegames/")
self.check_count = 0;
self.total = 0;
self.single = 0;
self.multi = 0;
self.pbem = 0;
self.longturn = set()
self.html_doc = "-";
self.last_http_status = -1;
s = PubStatus(self)
s.start();
def check(self, port):
while 1:
try:
time.sleep(1);
conn = http.client.HTTPConnection(metahost, metaport);
conn.request("GET", statuspath);
r1 = conn.getresponse();
self.check_count += 1;
self.last_http_status = r1.status;
if (r1.status == 200):
self.html_doc = r1.read()
meta_status = self.html_doc.decode('ascii').split(";");
if (len(meta_status) == 5):
self.total = int(meta_status[1]);
self.single = int(meta_status[2]);
self.multi = int(meta_status[3]);
self.pbem = int(meta_status[4]);
fork_bomb_preventer = (self.total == 0 and self.server_limit < len(self.server_list))
if fork_bomb_preventer:
print("Error: Have tried to start more than " + str(self.server_limit)
+ " servers (the server limit) but according to the"
+ " metaserver it has found none.");
# Start LongTurn games, one per pass
lt_scripts = glob.glob('pubscript_longturn_*.serv')
self.longturn.intersection_update(lt_scripts)
for script in lt_scripts:
if script not in self.longturn:
# script[10:-5] is the magic needed for now because
# init-freeciv-web adds its own 'pubscript_' and '.serv'
new_server = Civlauncher("longturn", script[10:-5], port, metahost + ":" + str(metaport) + metapath, self.savesdir)
self.server_list.append(new_server)
new_server.start()
port += 1
self.longturn.add(script)
break
while (self.single < self.server_capacity_single
and self.total <= self.server_limit
and not fork_bomb_preventer):
time.sleep(1)
new_server = Civlauncher(game_types[0], game_types[0], port, metahost + ":" + str(metaport) + metapath, self.savesdir);
self.server_list.append(new_server);
new_server.start();
port += 1;
self.total += 1;
self.single += 1;
while (self.multi < self.server_capacity_multi
and self.total <= self.server_limit
and not fork_bomb_preventer):
time.sleep(1)
new_server = Civlauncher(game_types[1], game_types[1], port, metahost + ":" + str(metaport) + metapath, self.savesdir)
self.server_list.append(new_server);
new_server.start();
port += 1;
self.total += 1;
self.multi += 1;
while (self.pbem < self.server_capacity_pbem
and self.total <= self.server_limit
and not fork_bomb_preventer):
time.sleep(1)
new_server = Civlauncher(game_types[2], game_types[2], port, metahost + ":" + str(metaport) + metapath, self.savesdir)
self.server_list.append(new_server);
new_server.start();
port += 1;
self.total += 1;
self.pbem += 1;
else:
print("Error: Invalid metaserver status");
except Exception as e:
self.html_doc = ("Error: Publite2 is unable to connect to Freeciv-web metaserver on http://" +
metahost + metapath + ", error" + str(e));
print(self.html_doc);
finally:
conn.close();
time.sleep(metachecker_interval);
if __name__ == '__main__':
# Perform a test-request to the Metaserver
try:
conn = http.client.HTTPConnection(metahost, metaport);
conn.request("GET", statuspath);
conn.getresponse();
except Exception as e:
print("Error: Publite2 is unable to connect to Freeciv-web metaserver on http://"
+ metahost + metapath + ", error" + str(e));
sys.exit(1)
finally:
conn.close();
# Start the initial Freeciv-web servers
mc = metachecker()
for type in game_types:
new_server = Civlauncher(type, type, port, metahost + ":" + str(metaport) + metapath, mc.savesdir)
mc.server_list.append(new_server);
new_server.start();
port += 1;
print("Publite2 started!");
time.sleep(20);
mc.check(port);