2013-09-08 14:30:26 +02:00
|
|
|
#!/usr/bin/env python
|
|
|
|
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from django.core.management import execute_from_command_line
|
|
|
|
|
2015-02-18 16:44:26 +01:00
|
|
|
from openslides import __version__ as openslides_version
|
2013-09-08 14:30:26 +02:00
|
|
|
from openslides.utils.main import (
|
2015-01-16 14:18:34 +01:00
|
|
|
ExceptionArgumentParser,
|
2015-06-16 10:37:23 +02:00
|
|
|
UnknownCommand,
|
|
|
|
get_default_settings_path,
|
2016-02-27 18:27:03 +01:00
|
|
|
get_local_settings_path,
|
|
|
|
is_local_installation,
|
2015-06-16 10:37:23 +02:00
|
|
|
setup_django_settings_module,
|
2013-10-28 16:34:53 +01:00
|
|
|
start_browser,
|
2015-06-16 10:37:23 +02:00
|
|
|
write_settings,
|
|
|
|
)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
"""
|
|
|
|
Main entrance to OpenSlides.
|
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
parser = get_parser()
|
|
|
|
try:
|
|
|
|
known_args, unknown_args = parser.parse_known_args()
|
|
|
|
except UnknownCommand:
|
|
|
|
unknown_command = True
|
2013-10-13 17:17:56 +02:00
|
|
|
else:
|
2015-01-16 14:18:34 +01:00
|
|
|
unknown_command = False
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
if unknown_command:
|
|
|
|
# Run a command, that is defined by the django management api
|
2016-02-27 18:27:03 +01:00
|
|
|
local_installation = is_local_installation()
|
|
|
|
setup_django_settings_module(local_installation=local_installation)
|
2015-01-16 14:18:34 +01:00
|
|
|
execute_from_command_line(sys.argv)
|
|
|
|
else:
|
|
|
|
# Run a command that is defined here
|
|
|
|
# These are commands that can not rely on an existing settings
|
|
|
|
known_args.callback(known_args)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
def get_parser():
|
2013-09-08 14:30:26 +02:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
Parses all command line arguments.
|
2013-09-08 14:30:26 +02:00
|
|
|
"""
|
2016-02-27 18:27:03 +01:00
|
|
|
if len(sys.argv) == 1 and not is_local_installation():
|
2013-09-08 14:30:26 +02:00
|
|
|
sys.argv.append('start')
|
|
|
|
|
2013-10-13 17:17:56 +02:00
|
|
|
# Init parser
|
2013-09-08 14:30:26 +02:00
|
|
|
description = 'Start script for OpenSlides.'
|
|
|
|
if 'manage.py' not in sys.argv[0]:
|
2016-02-24 00:42:45 +01:00
|
|
|
description += """
|
|
|
|
If it is called without any argument, this will be treated as
|
|
|
|
if it is called with the 'start' subcommand. That means
|
|
|
|
OpenSlides will setup default settings and database, start the
|
|
|
|
tornado webserver, launch the default web browser and open the
|
|
|
|
webinterface.
|
|
|
|
"""
|
|
|
|
epilog = """
|
|
|
|
There are some more subcommands available. They belong to Django's
|
|
|
|
command-line utility for administrative tasks. Type '%(prog)s help'
|
|
|
|
(without the two hyphen-minus characters) to list them all. Type
|
|
|
|
'%(prog)s help <subcommand>' for help on a specific subcommand.
|
|
|
|
"""
|
|
|
|
parser = ExceptionArgumentParser(
|
|
|
|
description=description,
|
|
|
|
epilog=epilog)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2013-10-13 17:17:56 +02:00
|
|
|
# Add version argument
|
2013-09-08 14:30:26 +02:00
|
|
|
parser.add_argument(
|
|
|
|
'--version',
|
|
|
|
action='version',
|
2015-02-18 16:44:26 +01:00
|
|
|
version=openslides_version,
|
2013-09-08 14:30:26 +02:00
|
|
|
help='Show version number and exit.')
|
|
|
|
|
2013-10-13 17:17:56 +02:00
|
|
|
# Init subparsers
|
2013-09-08 14:30:26 +02:00
|
|
|
subparsers = parser.add_subparsers(
|
|
|
|
dest='subcommand',
|
|
|
|
title='Available subcommands',
|
2013-11-23 18:49:51 +01:00
|
|
|
description="Type '%s <subcommand> --help' for help on a "
|
2013-10-13 17:17:56 +02:00
|
|
|
"specific subcommand." % parser.prog,
|
2016-02-24 00:42:45 +01:00
|
|
|
help='You can choose only one subcommand at once.',
|
|
|
|
metavar='')
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
# Subcommand start
|
2016-02-24 00:42:45 +01:00
|
|
|
start_help = (
|
|
|
|
'Setup settings and database, start tornado webserver, launch the '
|
|
|
|
'default web browser and open the webinterface. The environment '
|
|
|
|
'variable DJANGO_SETTINGS_MODULE is ignored.')
|
2013-09-08 14:30:26 +02:00
|
|
|
subcommand_start = subparsers.add_parser(
|
|
|
|
'start',
|
2016-02-24 00:42:45 +01:00
|
|
|
description=start_help,
|
|
|
|
help=start_help)
|
2013-10-13 17:17:56 +02:00
|
|
|
subcommand_start.add_argument(
|
|
|
|
'--no-browser',
|
|
|
|
action='store_true',
|
|
|
|
help='Do not launch the default web browser.')
|
2016-03-20 22:33:03 +01:00
|
|
|
subcommand_start.add_argument(
|
|
|
|
'--host',
|
|
|
|
action='store',
|
|
|
|
default='0.0.0.0',
|
|
|
|
help='IP address to listen on. Default is 0.0.0.0.')
|
|
|
|
subcommand_start.add_argument(
|
|
|
|
'--port',
|
|
|
|
action='store',
|
|
|
|
default='8000',
|
|
|
|
help='Port to listen on. Default is 8000.')
|
2015-01-16 14:18:34 +01:00
|
|
|
subcommand_start.add_argument(
|
|
|
|
'--settings_path',
|
|
|
|
action='store',
|
|
|
|
default=None,
|
|
|
|
help='The used settings file. The file is created, if it does not exist.')
|
2013-09-08 14:30:26 +02:00
|
|
|
subcommand_start.set_defaults(callback=start)
|
2015-01-16 14:18:34 +01:00
|
|
|
subcommand_start.add_argument(
|
2016-02-27 18:27:03 +01:00
|
|
|
'--local-installation',
|
2013-09-08 14:30:26 +02:00
|
|
|
action='store_true',
|
2016-02-27 18:27:03 +01:00
|
|
|
help='Store settings and user files in a local directory.')
|
2015-01-16 14:18:34 +01:00
|
|
|
|
|
|
|
# Subcommand createsettings
|
2016-02-24 00:42:45 +01:00
|
|
|
createsettings_help = 'Creates the settings file.'
|
2015-01-16 14:18:34 +01:00
|
|
|
subcommand_createsettings = subparsers.add_parser(
|
|
|
|
'createsettings',
|
2016-02-24 00:42:45 +01:00
|
|
|
description=createsettings_help,
|
|
|
|
help=createsettings_help)
|
2015-01-16 14:18:34 +01:00
|
|
|
subcommand_createsettings.set_defaults(callback=createsettings)
|
|
|
|
subcommand_createsettings.add_argument(
|
|
|
|
'--settings_path',
|
|
|
|
action='store',
|
|
|
|
default=None,
|
|
|
|
help='The used settings file. The file is created, even if it exists.')
|
|
|
|
subcommand_createsettings.add_argument(
|
2016-02-27 18:27:03 +01:00
|
|
|
'--local-installation',
|
2015-01-16 14:18:34 +01:00
|
|
|
action='store_true',
|
2016-02-27 18:27:03 +01:00
|
|
|
help='Store settings and user files in a local directory.')
|
2016-02-24 00:42:45 +01:00
|
|
|
|
|
|
|
# Help text for several Django subcommands
|
|
|
|
django_subcommands = (
|
|
|
|
('backupdb', 'Backups the SQLite3 database.'),
|
|
|
|
('createsuperuser', 'Creates or resets the admin user.'),
|
|
|
|
('migrate', 'Updates database schema.'),
|
|
|
|
('runserver', 'Starts the Tornado webserver.'),
|
|
|
|
)
|
|
|
|
for django_subcommand, help_text in django_subcommands:
|
|
|
|
subparsers._choices_actions.append(
|
|
|
|
subparsers._ChoicesPseudoAction(
|
|
|
|
django_subcommand,
|
|
|
|
(),
|
|
|
|
help_text))
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
return parser
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
def start(args):
|
2013-09-08 14:30:26 +02:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
Starts OpenSlides: Runs migrations and runs runserver.
|
2013-09-08 14:30:26 +02:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
settings_path = args.settings_path
|
2016-02-27 18:27:03 +01:00
|
|
|
local_installation = is_local_installation()
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
if settings_path is None:
|
2016-02-27 18:27:03 +01:00
|
|
|
if local_installation:
|
|
|
|
settings_path = get_local_settings_path()
|
2015-01-16 14:18:34 +01:00
|
|
|
else:
|
|
|
|
settings_path = get_default_settings_path()
|
2013-10-28 16:34:53 +01:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
# Write settings if it does not exists.
|
|
|
|
if not os.path.isfile(settings_path):
|
|
|
|
createsettings(args)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
# Set the django setting module and run migrations
|
|
|
|
# A manual given environment variable will be overwritten
|
2016-02-27 18:27:03 +01:00
|
|
|
setup_django_settings_module(settings_path, local_installation=local_installation)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
execute_from_command_line(['manage.py', 'migrate'])
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2016-03-20 22:33:03 +01:00
|
|
|
# Open the browser
|
2015-01-16 14:18:34 +01:00
|
|
|
if not args.no_browser:
|
2016-03-20 22:33:03 +01:00
|
|
|
if args.host == '0.0.0.0':
|
|
|
|
# Windows does not support 0.0.0.0, so use 'localhost' instead
|
|
|
|
start_browser('http://localhost:%s' % args.port)
|
|
|
|
else:
|
|
|
|
start_browser('http://%s:%s' % (args.host, args.port))
|
2013-09-08 14:30:26 +02:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
# Start the webserver
|
2016-08-17 07:48:59 +02:00
|
|
|
# Use flag --noreload to tell Django not to reload the server.
|
|
|
|
# Use flag --insecure to serve static files even if DEBUG is False.
|
|
|
|
# Use flag --nothreading to tell Django Channels to run in single thread mode.
|
2016-05-29 08:29:14 +02:00
|
|
|
execute_from_command_line([
|
|
|
|
'manage.py',
|
|
|
|
'runserver',
|
|
|
|
'{}:{}'.format(args.host, args.port),
|
|
|
|
'--noreload',
|
2016-08-17 07:48:59 +02:00
|
|
|
'--insecure',
|
|
|
|
'--nothreading'])
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
def createsettings(args):
|
2013-10-28 16:34:53 +01:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
Creates settings for OpenSlides.
|
2013-10-28 16:34:53 +01:00
|
|
|
"""
|
2015-01-16 14:18:34 +01:00
|
|
|
settings_path = args.settings_path
|
2016-02-27 18:27:03 +01:00
|
|
|
local_installation = is_local_installation()
|
2015-01-16 14:18:34 +01:00
|
|
|
context = {}
|
2013-10-28 16:34:53 +01:00
|
|
|
|
2016-02-27 18:27:03 +01:00
|
|
|
if local_installation:
|
2015-01-16 14:18:34 +01:00
|
|
|
if settings_path is None:
|
2016-02-27 18:27:03 +01:00
|
|
|
settings_path = get_local_settings_path()
|
2015-01-16 14:18:34 +01:00
|
|
|
context = {
|
2016-02-27 18:27:03 +01:00
|
|
|
'openslides_user_data_path': repr(os.path.join(os.getcwd(), 'personal_data', 'var')),
|
2015-01-16 14:18:34 +01:00
|
|
|
'debug': 'True'}
|
2013-10-28 16:34:53 +01:00
|
|
|
|
2015-01-16 14:18:34 +01:00
|
|
|
settings_path = write_settings(settings_path, **context)
|
|
|
|
print('Settings created at %s' % settings_path)
|
2013-09-08 14:30:26 +02:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2016-04-07 00:43:53 +02:00
|
|
|
sys.exit(main())
|