mirror of
https://github.com/freedombox/FreedomBox.git
synced 2026-04-29 10:10:19 +00:00
Add option to run as non-daemon mode
- Pass on debug option with Django - Cleanup option parsing
This commit is contained in:
parent
db3b0ab9e6
commit
ab80dc34d6
2
cfg.py
2
cfg.py
@ -31,6 +31,8 @@ access_log_file = get_item(parser, 'Path', 'access_log_file')
|
|||||||
pidfile = get_item(parser, 'Path', 'pidfile')
|
pidfile = get_item(parser, 'Path', 'pidfile')
|
||||||
host = get_item(parser, 'Network', 'host')
|
host = get_item(parser, 'Network', 'host')
|
||||||
port = int(get_item(parser, 'Network', 'port'))
|
port = int(get_item(parser, 'Network', 'port'))
|
||||||
|
debug = False
|
||||||
|
no_daemon = False
|
||||||
|
|
||||||
main_menu = Menu()
|
main_menu = Menu()
|
||||||
|
|
||||||
|
|||||||
10
logger.py
10
logger.py
@ -2,9 +2,13 @@ import cherrypy
|
|||||||
import inspect
|
import inspect
|
||||||
import cfg
|
import cfg
|
||||||
|
|
||||||
cherrypy.log.error_file = cfg.status_log_file
|
|
||||||
cherrypy.log.access_file = cfg.access_log_file
|
def init():
|
||||||
cherrypy.log.screen = False
|
"""Initialize logging"""
|
||||||
|
cherrypy.log.error_file = cfg.status_log_file
|
||||||
|
cherrypy.log.access_file = cfg.access_log_file
|
||||||
|
if not cfg.no_daemon:
|
||||||
|
cherrypy.log.screen = False
|
||||||
|
|
||||||
|
|
||||||
class Logger(object):
|
class Logger(object):
|
||||||
|
|||||||
88
plinth.py
88
plinth.py
@ -10,12 +10,12 @@ if not os.path.join(cfg.file_root, "vendor") in sys.path:
|
|||||||
import cherrypy
|
import cherrypy
|
||||||
from cherrypy import _cpserver
|
from cherrypy import _cpserver
|
||||||
from cherrypy.process.plugins import Daemonizer
|
from cherrypy.process.plugins import Daemonizer
|
||||||
Daemonizer(cherrypy.engine).subscribe()
|
|
||||||
|
|
||||||
import module_loader
|
import module_loader
|
||||||
import plugin_mount
|
import plugin_mount
|
||||||
import service
|
import service
|
||||||
|
|
||||||
|
import logger
|
||||||
from logger import Logger
|
from logger import Logger
|
||||||
|
|
||||||
__version__ = "0.2.14"
|
__version__ = "0.2.14"
|
||||||
@ -28,42 +28,27 @@ __status__ = "Development"
|
|||||||
|
|
||||||
|
|
||||||
def parse_arguments():
|
def parse_arguments():
|
||||||
parser = argparse.ArgumentParser(description='Plinth web interface for the FreedomBox.')
|
"""Parse command line arguments"""
|
||||||
parser.add_argument('--pidfile',
|
parser = argparse.ArgumentParser(
|
||||||
help='specify a file in which the server may write its pid')
|
description='Plinth web interface for FreedomBox')
|
||||||
# FIXME make this work with basehref for static files.
|
parser.add_argument(
|
||||||
parser.add_argument('--server_dir',
|
'--pidfile', default='plinth.pid',
|
||||||
help='specify where to host the server.')
|
help='specify a file in which the server may write its pid')
|
||||||
parser.add_argument("--debug", action="store_true",
|
parser.add_argument(
|
||||||
help="Debug flag. Don't use.")
|
'--server_dir', default='/',
|
||||||
|
help='web server path under which to serve')
|
||||||
|
parser.add_argument(
|
||||||
|
'--debug', action='store_true', default=False,
|
||||||
|
help='enable debugging and run server *insecurely*')
|
||||||
|
parser.add_argument(
|
||||||
|
'--no-daemon', action='store_true', default=False,
|
||||||
|
help='do not start as a daemon')
|
||||||
|
|
||||||
args=parser.parse_args()
|
args = parser.parse_args()
|
||||||
set_config(args, "pidfile", "plinth.pid")
|
cfg.pidfile = args.pidfile
|
||||||
set_config(args, "server_dir", "/")
|
cfg.server_dir = args.server_dir
|
||||||
set_config(args, "debug", False)
|
cfg.debug = args.debug
|
||||||
|
cfg.no_daemon = args.no_daemon
|
||||||
return cfg
|
|
||||||
|
|
||||||
def set_config(args, element, default):
|
|
||||||
"""Sets *cfg* elements based on *args* values, or uses a reasonable default.
|
|
||||||
|
|
||||||
- If values are passed in from *args*, use those.
|
|
||||||
- If values are read from the config file, use those.
|
|
||||||
- If no values have been given, use default.
|
|
||||||
|
|
||||||
"""
|
|
||||||
try:
|
|
||||||
# cfg.(element) = args.(element)
|
|
||||||
setattr(cfg, element, getattr(args, element))
|
|
||||||
except AttributeError:
|
|
||||||
# if it fails, we didn't receive that argument.
|
|
||||||
try:
|
|
||||||
# if not cfg.(element): cfg.(element) = default
|
|
||||||
if not getattr(cfg, element):
|
|
||||||
setattr(cfg, element, default)
|
|
||||||
except AttributeError:
|
|
||||||
# it wasn't in the config file, but set the default anyway.
|
|
||||||
setattr(cfg, element, default)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_logging():
|
def setup_logging():
|
||||||
@ -71,21 +56,16 @@ def setup_logging():
|
|||||||
cfg.log = Logger()
|
cfg.log = Logger()
|
||||||
|
|
||||||
|
|
||||||
def setup_configuration():
|
|
||||||
cfg = parse_arguments()
|
|
||||||
|
|
||||||
try:
|
|
||||||
if cfg.pidfile:
|
|
||||||
from cherrypy.process.plugins import PIDFile
|
|
||||||
PIDFile(cherrypy.engine, cfg.pidfile).subscribe()
|
|
||||||
except AttributeError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
os.chdir(cfg.python_root)
|
|
||||||
|
|
||||||
|
|
||||||
def setup_server():
|
def setup_server():
|
||||||
"""Setup CherryPy server"""
|
"""Setup CherryPy server"""
|
||||||
|
# Set the PID file path
|
||||||
|
try:
|
||||||
|
if cfg.pidfile:
|
||||||
|
from cherrypy.process.plugins import PIDFile
|
||||||
|
PIDFile(cherrypy.engine, cfg.pidfile).subscribe()
|
||||||
|
except AttributeError:
|
||||||
|
pass
|
||||||
|
|
||||||
# Add an extra server
|
# Add an extra server
|
||||||
server = _cpserver.Server()
|
server = _cpserver.Server()
|
||||||
server.socket_host = '127.0.0.1'
|
server.socket_host = '127.0.0.1'
|
||||||
@ -107,6 +87,9 @@ def setup_server():
|
|||||||
'tools.staticdir.dir': '.'}}
|
'tools.staticdir.dir': '.'}}
|
||||||
cherrypy.tree.mount(None, cfg.server_dir + '/static', config)
|
cherrypy.tree.mount(None, cfg.server_dir + '/static', config)
|
||||||
|
|
||||||
|
if not cfg.no_daemon:
|
||||||
|
Daemonizer(cherrypy.engine).subscribe()
|
||||||
|
|
||||||
cherrypy.engine.signal_handler.subscribe()
|
cherrypy.engine.signal_handler.subscribe()
|
||||||
|
|
||||||
|
|
||||||
@ -141,7 +124,7 @@ def configure_django():
|
|||||||
template_directories = module_loader.get_template_directories()
|
template_directories = module_loader.get_template_directories()
|
||||||
sessions_directory = os.path.join(cfg.data_dir, 'sessions')
|
sessions_directory = os.path.join(cfg.data_dir, 'sessions')
|
||||||
django.conf.settings.configure(
|
django.conf.settings.configure(
|
||||||
DEBUG=False,
|
DEBUG=cfg.debug,
|
||||||
ALLOWED_HOSTS=['127.0.0.1', 'localhost'],
|
ALLOWED_HOSTS=['127.0.0.1', 'localhost'],
|
||||||
TEMPLATE_DIRS=template_directories,
|
TEMPLATE_DIRS=template_directories,
|
||||||
INSTALLED_APPS=['bootstrapform'],
|
INSTALLED_APPS=['bootstrapform'],
|
||||||
@ -154,11 +137,14 @@ def configure_django():
|
|||||||
|
|
||||||
def main():
|
def main():
|
||||||
"""Intialize and start the application"""
|
"""Intialize and start the application"""
|
||||||
|
parse_arguments()
|
||||||
|
|
||||||
setup_logging()
|
setup_logging()
|
||||||
|
logger.init()
|
||||||
|
|
||||||
service.init()
|
service.init()
|
||||||
|
|
||||||
setup_configuration()
|
os.chdir(cfg.python_root)
|
||||||
|
|
||||||
configure_django()
|
configure_django()
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user