#!/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 . 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, toolkit 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 toolkit.tmpdir.value and not exists(toolkit.tmpdir.value): os.makedirs(toolkit.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('node', [toolkit.tmpdir]) 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()