forked from learningequality/kolibri-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
kolibri_server_setup.py
executable file
·227 lines (195 loc) · 7.24 KB
/
kolibri_server_setup.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
#!/usr/bin/python3
import argparse
import os
import subprocess
import kolibri.utils.pskolibri as psutil
from kolibri.core.content.utils.paths import get_content_dir_path
from kolibri.core.utils.cache import RedisSettingsHelper
from kolibri.core.utils.cache import process_cache
from kolibri.utils.options import update_options_file
from kolibri.utils.conf import OPTIONS
from kolibri.utils.conf import KOLIBRI_HOME
# read the config file options
port = OPTIONS["Deployment"]["HTTP_PORT"]
zip_content_port = OPTIONS["Deployment"]["ZIP_CONTENT_PORT"]
path_prefix = OPTIONS["Deployment"]["URL_PATH_PREFIX"]
redis_db = OPTIONS["Cache"]["CACHE_REDIS_DB"]
listen_address = OPTIONS["Deployment"]["LISTEN_ADDRESS"]
if path_prefix != "/":
path_prefix = "/" + path_prefix
def start_debconf_dialog():
"""
Auxiliar function to start a dialog with debconf database
"""
args = ["debconf-communicate", "-fnoninteractive", "kolibri-server"]
dccomm = subprocess.Popen(
args,
stdin=subprocess.PIPE,
stdout=subprocess.PIPE,
close_fds=True,
universal_newlines=True,
)
return dccomm
def stop_debconf_dialog(dccomm):
"""
Auxiliar function to end a dialog with debconf database
"""
dccomm.stdin.write("STOP\n")
dccomm.stdin.flush()
def set_debconf_ports(port, zip_content_port):
"""
Sets the port kolibri uses in debconf database, to be used by future
reconfigurations or upgrades of the kolibri-server package
"""
dccomm = start_debconf_dialog()
dccomm.stdin.write("SET kolibri-server/port {}\n".format(port))
dccomm.stdin.write("SET kolibri-server/zip_content_port {}\n".format(zip_content_port))
dccomm.stdin.flush()
stop_debconf_dialog(dccomm)
def set_port(port):
"""
Modify Kolibri options to set the TCP port the server will listen on
"""
update_options_file("Deployment", "HTTP_PORT", port)
def set_zip_content_port(port):
"""
Modify Kolibri options to set the TCP port the hashi files will be served on
"""
update_options_file("Deployment", "ZIP_CONTENT_PORT", port)
def delete_redis_cache():
"""
Delete previous cache in redis to reset it when the service starts.
The purpose is avoiding redis memory usage growing infinitely.
"""
redis_args = [
(str(redis_db), ":1:views.decorators.*"),
(str(redis_db), ":1:CHANNEL_STATS_CACHED_KEYS*"),
(str(redis_db), ":1:*_dataset"),
(str(redis_db), ":1:content_cache_key"),
(str(redis_db), ":1:device_settings_cache_key"),
(str(redis_db + 1), "built_files:1:*"),
]
for arg in redis_args:
search = ["redis-cli", "-n", arg[0], "--scan", "--pattern", arg[1]]
delete = ["xargs", "--no-run-if-empty", "redis-cli", "-n", arg[0], "unlink"]
exe_search = subprocess.Popen(search, stdout=subprocess.PIPE)
subprocess.Popen(delete, stdin=exe_search.stdout)
def enable_redis_cache():
"""
Set redis as the cache backend.
When multiple processes run the server we need to use
redis to ensure the cache is shared among them.
It also limits redis memory usage to avoid server problems
if the cache grows too much
"""
update_options_file("Cache", "CACHE_BACKEND", "redis")
update_options_file("Cache", "CACHE_REDIS_MAXMEMORY_POLICY", "allkeys-lru")
delete_redis_cache()
server_memory = psutil.virtual_memory().total
max_memory = round(server_memory / 10)
if hasattr(process_cache, "get_master_client"):
helper = RedisSettingsHelper(process_cache.get_master_client())
redis_memory = helper.get_used_memory()
if max_memory < redis_memory:
max_memory = redis_memory + 2000
update_options_file("Cache", "CACHE_REDIS_MAXMEMORY", max_memory)
def disable_redis_cache():
"""
Set memory as the cache backend .
If redis is not active, enabling it will break kolibri
"""
update_options_file("Cache", "CACHE_BACKEND", "memory")
def check_redis_service():
"""
Checks if redis is running in the system
"""
status = False
args = ["service", "redis", "status"]
try:
subprocess.check_call(args, stdout=subprocess.PIPE)
except subprocess.CalledProcessError:
pass # redis is not running
except FileNotFoundError:
pass # 'service' is not an available command
else:
status = True
return status
def save_nginx_conf_port(port, zip_port, listen_address="0.0.0.0", nginx_conf=None):
"""
Adds the port for nginx to run to an existing config file.
"""
if nginx_conf is None:
nginx_conf = os.path.join(KOLIBRI_HOME, "nginx.conf")
if listen_address != "0.0.0.0":
address_port = "{}:{}".format(listen_address, port)
address_zip_port = "{}:{}".format(listen_address, zip_port)
else:
address_port = port
address_zip_port = zip_port
configuration = (
"# This file is maintained AUTOMATICALLY and will be overwritten\n"
"#\n"
"# Do not edit this file. If you are using the kolibri-server"
"package,\n"
"# please write custom configurations in /etc/kolibri/nginx.d/\n"
"\n"
"server{{\n"
" listen {port};\n"
" location {path_prefix}favicon.ico {{\n"
" empty_gif;\n"
" }}\n\n"
" location {path_prefix} {{\n"
" include uwsgi_params;\n"
" uwsgi_pass unix:///tmp/kolibri_uwsgi.sock;\n"
" proxy_ignore_headers Vary;\n"
" }}\n\n"
" error_page 502 = @error502;\n"
" location @error502 {{\n"
" ssi on;\n"
" internal;\n"
" root /usr/share/kolibri/error_pages;\n"
" rewrite ^(.*)$ $error502 break;\n"
" }}\n"
"}}\n"
"\n"
"server{{\n"
" listen {zip_port};\n"
" location {path_prefix} {{\n"
" include uwsgi_params;\n"
" uwsgi_pass unix:///tmp/kolibri_hashi_uwsgi.sock;\n"
" }}\n"
"}}\n"
).format(port=address_port, path_prefix=path_prefix, zip_port=address_zip_port)
with open(nginx_conf, "w") as nginx_conf_file:
nginx_conf_file.write(configuration)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Tool to configure kolibri-server")
parser.add_argument(
"-d",
"--debconfport",
required=False,
default="",
help="Initial port to be used when installing/reconfiguring kolibri-server package",
)
parser.add_argument(
"-z",
"--debconfzipport",
required=False,
default="",
help="Port to run hashi iframes used when installing/reconfiguring kolibri-server package",
)
args = parser.parse_args()
if (
args.debconfport
): # To be executed only when installing/reconfiguring the Debian package
set_port(args.debconfport)
if args.debconfzipport:
set_zip_content_port(args.debconfzipport)
else:
if check_redis_service():
enable_redis_cache()
else:
disable_redis_cache()
save_nginx_conf_port(port, zip_content_port, listen_address)
# Let's update debconf, just in case the user has changed the port in options.ini:
set_debconf_ports(port, zip_content_port)