-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsugar-network-node
executable file
·306 lines (253 loc) · 10.1 KB
/
sugar-network-node
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
#!/usr/bin/env python
# Copyright (C) 2012-2014 Aleksey Lim
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU 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 General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os
import logging
from os.path import exists, join, isabs
from sugar_network.toolkit import coroutine
coroutine.inject()
from sugar_network import db, toolkit
from sugar_network.model.post import Post
from sugar_network.node.auth import SugarAuth
from sugar_network.node.avatars import Avatars
from sugar_network.node import master, slave, model, stats
from sugar_network.toolkit.http import Connection
from sugar_network.toolkit.router import Router
from sugar_network.toolkit.coroutine import this
from sugar_network.toolkit.spec import parse_version
from sugar_network.toolkit import application, i18n, Option, enforce
data_root = Option(
'path to a directory to place node data',
default='/var/lib/sugar-network', name='data_root')
mode = Option(
'node running mode, should be one of "slave", "proxy", or, "master"',
default='slave', name='mode')
host = Option(
'hostname to listen for incomming connections and '
'using for publicly visible urls',
default='127.0.0.1', name='host')
port = Option(
'port number to listen incomming connections',
default=8000, type_cast=int, name='port')
default_api = Option(
'API version to use by default, i.e., '
'if clients do not specify it in requests',
name='default-api')
master_url = Option(
'master API url either to connect to (for slave or proxy nodes), or, '
'to provide from (for master nodes)', name='master-url')
static_url = Option(
'url to provide static content from; if omitted, content '
'will be provided from the same host', name='static-url')
backdoor = Option(
'path to a UNIX socket to serve administrative API requests; '
'the entry point is not authenticated and assumes root privileges, '
'thus, make sure that --backdoor path is accessible only by admins; '
'not absolute path will be prefixed by the --rundir',
default='backdoor', name='backdoor')
http_logdir = Option(
'path to a directory to log HTTP requests; '
'not absolute path will be prefixed by the --logdir',
name='http-logdir')
find_limit = Option(
'limit the resulting list for search requests',
default=64, type_cast=int, name='find-limit')
keyfile = Option(
'path to SSL certificate keyfile to serve requests via HTTPS',
name='keyfile')
certfile = Option(
'path to SSL certificate file to serve requests via HTTPS',
name='certfile')
avatars = Option(
'for missed User.avatar, reuse external avatars hosting; '
'supported values: gravatar',
name='avatars')
SUPPORTED_API = {
'master': {
'0.2': master.MasterRoutes,
},
'slave': {
'0.2': slave.SlaveRoutes,
},
}
EPILOG = """\
Supported API versions:
%s
See http://wiki.sugarlabs.org/go/Sugar_Network for details.\
"""
class Application(application.Daemon):
jobs = coroutine.Pool()
servers = []
routes = {}
def prolog(self):
if not exists(data_root.value):
os.makedirs(data_root.value)
enforce(os.access(data_root.value, os.W_OK),
'No write access to %r directory', data_root.value)
for opt, dirname in [
(toolkit.cachedir, 'cache'),
(application.logdir, 'log'),
(application.rundir, 'run'),
]:
if not opt.value:
opt.value = join(data_root.value, dirname)
if not exists(opt.value):
os.makedirs(opt.value)
if http_logdir.value and not isabs(http_logdir.value):
http_logdir.value = \
join(application.logdir.value, http_logdir.value)
if not isabs(backdoor.value):
backdoor.value = join(application.rundir.value, backdoor.value)
if static_url.value:
this.static_prefix = static_url.value
def run(self):
enforce(master_url.value, 'Option --master-api missed')
ssl_args = {}
if keyfile.value:
ssl_args['keyfile'] = keyfile.value
if certfile.value:
ssl_args['certfile'] = certfile.value
apis = SUPPORTED_API.get(mode.value)
enforce(apis, 'Mode is not supported')
if default_api.value:
enforce(default_api.value in apis, 'No such API version')
else:
default_api.value = max(apis, key=lambda x: parse_version(x))
logging.info('Start node mode=%s default_api=%s',
mode.value, default_api.value)
this.volume = model.Volume(data_root.value,
apis[default_api.value].RESOURCES)
stats_monitor = stats.Monitor(this.volume,
stats.stats_step.value, stats.stats_rras.value)
routes_args = {
'master_url': master_url.value,
'auth': SugarAuth(data_root.value),
'stats': stats_monitor,
'find_limit': find_limit.value,
}
self.routes = dict([(v, c(**routes_args)) for v, c in apis.items()])
if avatars.value:
enforce(avatars.value == 'gravatar', 'Only gravatar is supported')
this.avatars = Avatars()
self.jobs.spawn(stats_monitor.auto_commit)
self.jobs.spawn(this.volume.populate)
logging.info('Listen requests on %s:%s', host.value, port.value)
server = coroutine.WSGIServer((host.value, port.value),
Router(self.routes, default_api=default_api.value),
http_log=open_http_logfile('access'), **ssl_args)
self.jobs.spawn(server.serve_forever)
self.servers.append(server)
logging.info('Listen admin requests on %s', backdoor.value)
sock = coroutine.listen_unix_socket(backdoor.value,
reuse_address=True, mode=0660)
server = coroutine.WSGIServer(sock,
Router(self.routes[default_api.value]),
http_log=open_http_logfile('backdoor'))
self.jobs.spawn(server.serve_forever)
self.servers.append(server)
self.accept()
try:
self.jobs.join()
finally:
stats_monitor.commit()
this.volume.close()
os.unlink(backdoor.value)
def shutdown(self):
self.jobs.kill()
def reload(self):
if http_logdir.value:
for server in self.servers:
if server.http_log is None:
continue
server.http_log.close()
server.http_log = file(server.http_log.name, 'a+')
for routes in self.routes.values():
routes.reload()
@application.command(
'direct synchronization with master node',
name='online-sync')
def online_sync(self):
enforce(mode.value == 'slave', 'Node is not slave')
self._ensure_instance().post(cmd='online_sync')
@application.command(
'sneakernet synchronization with other nodes using files '
'placed to the specified directory',
args='PATH', name='offline-sync')
def offline_sync(self):
enforce(mode.value == 'slave', 'Node is not slave')
enforce(self.args, 'PATH was not specified')
path = self.args.pop(0)
self._ensure_instance().post(cmd='offline_sync', path=path)
@application.command(
'generate node statistics', name='stat')
def stat(self):
enforce(not self.check_for_instance(), 'Node should be stopped')
this.volume = model.Volume(data_root.value,
master.MasterRoutes.RESOURCES)
stats_monitor = stats.Monitor(this.volume,
stats.stats_step.value, stats.stats_rras.value)
try:
this.volume.populate()
stats_monitor.regen()
finally:
this.volume.close()
@application.command(
'generate calculated property values', name='calc')
def calc(self):
enforce(not self.check_for_instance(), 'Node should be stopped')
this.volume = model.Volume(data_root.value,
master.MasterRoutes.RESOURCES)
try:
this.volume.populate()
Post.recalc()
finally:
this.volume.close()
def _ensure_instance(self):
enforce(self.check_for_instance(), 'Node is not started')
return Connection('file://' + backdoor.value)
def open_http_logfile(name):
if not http_logdir.value:
return None
if not exists(http_logdir.value):
os.makedirs(http_logdir.value)
return file(join(http_logdir.value, name + '.log'), 'a+')
def api_names():
result = ''
for side, apis in SUPPORTED_API.items():
result += application.INDENT_FORMAT % side
result += ', '.join(apis.keys()) + '\n'
return result
i18n.init('sugar-network')
# New defaults
application.logdir.value = None
application.rundir.value = None
Option.seek('main', application)
Option.seek('main', [toolkit.cachedir])
Option.seek('node', stats)
Option.seek('node', [
data_root, mode, host, port, default_api, master_url, static_url,
backdoor, http_logdir, find_limit, keyfile, certfile, avatars,
])
Option.seek('db', db)
app = Application(
name='sugar-network-node',
description='Sugar Network node.',
epilog=EPILOG % api_names(),
config_files=[
'/etc/sugar-network.d',
'/etc/sugar-network.conf',
'~/.config/sugar-network/config',
])
app.start()