Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/sugar-network-server
blob: b5feb0e719eaebcb656f0a5343a32e0a7acddf95 (plain)
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
#!/usr/bin/env python

# Copyright (C) 2012 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 logging
from os.path import exists

import active_document as ad
import sugar_network_webui as webui
from active_toolkit import coroutine, application
from active_toolkit.options import Option
from sugar_network import node, local
from sugar_network.local.mounts import LocalMount
from sugar_network.local.mountset import Mountset
from sugar_network.local.mounts import LocalMount
from sugar_network.node import stats, obs
from sugar_network.node.commands import MasterCommands
from sugar_network.node.router import Router
from sugar_network.resources.volume import Volume
from sugar_network.toolkit import sugar, sneakernet


class Application(application.Daemon):

    jobs = coroutine.Pool()

    def run(self):
        if node.tmpdir.value and not exists(node.tmpdir.value):
            os.makedirs(node.tmpdir.value)
        sneakernet.TMPDIR = node.tmpdir.value

        ssl_args = {}
        if node.keyfile.value:
            ssl_args['keyfile'] = node.keyfile.value
        if node.certfile.value:
            ssl_args['certfile'] = node.certfile.value

        volume = Volume(node.data_root.value)
        self.jobs.spawn(volume.populate)
        cp = MasterCommands(volume)

        logging.info('Listening for requests on %s:%s',
                node.host.value, node.port.value)
        server = coroutine.WSGIServer((node.host.value, node.port.value),
                Router(cp), **ssl_args)
        self.jobs.spawn(server.serve_forever)

        if webui.webui.value:
            # XXX Until implementing regular web users
            sugar.uid = lambda: 'demo'
            sugar.nickname = lambda: 'demo'
            sugar.color = lambda: '#000000,#000000'

            # Point client API to volume directly
            local.mounts_root.value = None
            mountset = Mountset(None)
            mountset['/'] = LocalMount(volume)

            host = (webui.webui_host.value, webui.webui_port.value)
            logging.info('Start Web server on %s:%s port', *host)
            server = coroutine.WSGIServer(host, webui.get_app(mountset))
            self.jobs.spawn(server.serve_forever)

        try:
            self.jobs.join()
        finally:
            volume.close()

    def shutdown(self):
        self.jobs.kill()


locale.setlocale(locale.LC_ALL, '')

Option.seek('main', application)
Option.seek('webui', webui)
Option.seek('node', node)
Option.seek('stats', stats)
Option.seek('obs', obs)
Option.seek('active-document', ad)

app = Application(
        name='sugar-network-server',
        description='Sugar Network server',
        epilog='See http://wiki.sugarlabs.org/go/Sugar_Network ' \
                 'for details.',
        config_files=[
            '/etc/sugar-network-server/config',
            '~/.config/sugar-network-server/config',
            ])
app.start()