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
|
#!/usr/bin/env python
# Copyright (C) 2012-2013 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 locale
import gettext
import logging
from os.path import exists
from sugar_network.toolkit import coroutine
coroutine.inject()
from sugar_network import db, node, toolkit
from sugar_network.node.auth import SugarAuth
from sugar_network.node.model import Volume
from sugar_network.node import obs, master, slave
from sugar_network.toolkit.http import Connection
from sugar_network.toolkit.router import Router
from sugar_network.toolkit import application, Option, enforce
gettext.textdomain('sugar-network')
class Application(application.Daemon):
jobs = coroutine.Pool()
node = None
def ensure_run(self):
if toolkit.cachedir.value and not exists(toolkit.cachedir.value):
os.makedirs(toolkit.cachedir.value)
if not exists(node.data_root.value):
os.makedirs(node.data_root.value)
enforce(os.access(node.data_root.value, os.W_OK),
'No write access to %r directory', node.data_root.value)
def run(self):
ssl_args = {}
if node.keyfile.value:
ssl_args['keyfile'] = node.keyfile.value
if node.certfile.value:
ssl_args['certfile'] = node.certfile.value
if node.mode.value == 'master':
node_routes_class = master.MasterRoutes
resources = master.RESOURCES
logging.info('Start master node')
else:
node_routes_class = slave.SlaveRoutes
resources = slave.RESOURCES
logging.info('Start slave node')
volume = Volume(node.data_root.value, resources)
node_routes = node_routes_class(volume=volume,
auth=SugarAuth(node.data_root.value),
find_limit=node.find_limit.value)
self.jobs.spawn(volume.populate)
logging.info('Listening for requests on %s:%s',
node.host.value, node.port.value)
server = coroutine.WSGIServer((node.host.value, node.port.value),
Router(node_routes), **ssl_args)
self.jobs.spawn(server.serve_forever)
self.accept()
try:
self.jobs.join()
finally:
volume.close()
def shutdown(self):
self.jobs.kill()
@application.command(
'direct synchronization with master node',
name='online-sync')
def online_sync(self):
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(self.args, 'PATH was not specified')
path = self.args.pop(0)
self._ensure_instance().post(cmd='offline-sync', path=path)
def _ensure_instance(self):
enforce(self.check_for_instance(), 'Node is not started')
return Connection('http://localhost:%s' %
node.port.value, trust_env=False)
locale.setlocale(locale.LC_ALL, '')
Option.seek('main', application)
Option.seek('main', [toolkit.cachedir])
Option.seek('node', node)
Option.seek('obs', obs)
Option.seek('db', db)
app = Application(
name='sugar-network-node',
description='Sugar Network node server',
epilog='See http://wiki.sugarlabs.org/go/Sugar_Network '
'for details.',
config_files=[
'/etc/sugar-network.d',
'/etc/sugar-network.conf',
'~/.config/sugar-network/config',
])
app.start()
|