2019-03-22 17:50:16 +00:00
|
|
|
from . import env
|
2019-05-21 09:56:49 +00:00
|
|
|
from . import exceptions
|
2019-03-22 17:50:16 +00:00
|
|
|
from . import fmt
|
2019-05-29 09:14:06 +00:00
|
|
|
from . import plugins
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-05-05 09:45:24 +00:00
|
|
|
|
2019-05-21 09:56:49 +00:00
|
|
|
class BaseRunner:
|
|
|
|
def __init__(self, root, config):
|
|
|
|
self.root = root
|
|
|
|
self.config = config
|
|
|
|
|
2019-06-05 13:43:51 +00:00
|
|
|
def run(self, service, *path):
|
2019-06-05 17:57:30 +00:00
|
|
|
command = self.render(*path)
|
2019-05-21 09:56:49 +00:00
|
|
|
self.exec(service, command)
|
|
|
|
|
2019-06-05 17:57:30 +00:00
|
|
|
def render(self, *path):
|
|
|
|
return env.render_file(self.config, *path).strip()
|
|
|
|
|
2019-05-21 09:56:49 +00:00
|
|
|
def exec(self, service, command):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def check_service_is_activated(self, service):
|
|
|
|
if not self.is_activated(service):
|
|
|
|
raise exceptions.TutorError(
|
|
|
|
"This command may only be executed on the server where the {} is running".format(
|
|
|
|
service
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def is_activated(self, service):
|
|
|
|
return self.config["ACTIVATE_" + service.upper()]
|
|
|
|
|
2019-06-06 19:58:21 +00:00
|
|
|
def iter_plugin_hooks(self, hook):
|
|
|
|
yield from plugins.iter_hooks(self.config, hook)
|
2019-06-05 13:43:51 +00:00
|
|
|
|
2019-05-21 09:56:49 +00:00
|
|
|
|
2019-06-05 17:28:06 +00:00
|
|
|
def initialise(runner):
|
|
|
|
fmt.echo_info("Initialising all services...")
|
2019-09-03 08:36:10 +00:00
|
|
|
runner.run("mysql", "hooks", "mysql", "init")
|
2019-07-05 06:19:23 +00:00
|
|
|
for plugin_name, hook in runner.iter_plugin_hooks("pre-init"):
|
|
|
|
for service in hook:
|
|
|
|
fmt.echo_info(
|
2019-07-05 06:31:28 +00:00
|
|
|
"Plugin {}: running pre-init for service {}...".format(
|
|
|
|
plugin_name, service
|
|
|
|
)
|
2019-07-05 06:19:23 +00:00
|
|
|
)
|
|
|
|
runner.run(service, plugin_name, "hooks", service, "pre-init")
|
2019-07-03 14:09:33 +00:00
|
|
|
for service in ["lms", "cms", "forum"]:
|
2019-05-29 09:14:06 +00:00
|
|
|
if runner.is_activated(service):
|
2019-06-05 17:28:06 +00:00
|
|
|
fmt.echo_info("Initialising {}...".format(service))
|
2019-06-06 19:58:21 +00:00
|
|
|
runner.run(service, "hooks", service, "init")
|
2019-07-02 20:16:44 +00:00
|
|
|
for plugin_name, hook in runner.iter_plugin_hooks("init"):
|
|
|
|
for service in hook:
|
|
|
|
fmt.echo_info(
|
|
|
|
"Plugin {}: running init for service {}...".format(plugin_name, service)
|
|
|
|
)
|
|
|
|
runner.run(service, plugin_name, "hooks", service, "init")
|
2019-06-05 17:28:06 +00:00
|
|
|
fmt.echo_info("All services initialised.")
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-05-05 09:45:24 +00:00
|
|
|
|
2019-07-11 03:55:12 +00:00
|
|
|
def create_user_command(superuser, staff, username, email, password=None):
|
2019-11-14 08:27:28 +00:00
|
|
|
command = """
|
|
|
|
export DJANGO_SETTINGS_MODULE=$SERVICE_VARIANT.envs.$SETTINGS
|
|
|
|
echo "Loading settings $DJANGO_SETTINGS_MODULE"
|
|
|
|
"""
|
|
|
|
|
2019-06-05 13:43:51 +00:00
|
|
|
opts = ""
|
2019-03-22 17:50:16 +00:00
|
|
|
if superuser:
|
2019-06-05 13:43:51 +00:00
|
|
|
opts += " --superuser"
|
2019-03-22 17:50:16 +00:00
|
|
|
if staff:
|
2019-06-05 13:43:51 +00:00
|
|
|
opts += " --staff"
|
2019-11-14 08:27:28 +00:00
|
|
|
command += """
|
|
|
|
./manage.py lms manage_user {opts} {username} {email}
|
|
|
|
"""
|
2019-07-11 03:55:12 +00:00
|
|
|
if password:
|
2019-11-14 08:27:28 +00:00
|
|
|
command += """
|
|
|
|
./manage.py lms shell -c "from django.contrib.auth import get_user_model; u = get_user_model().objects.get(username='{username}'); u.set_password('{password}'); u.save()"
|
|
|
|
"""
|
2019-07-11 03:55:12 +00:00
|
|
|
else:
|
2019-11-14 08:27:28 +00:00
|
|
|
command += """
|
|
|
|
./manage.py lms changepassword {username}
|
|
|
|
"""
|
|
|
|
|
2019-07-11 03:55:12 +00:00
|
|
|
return command.format(opts=opts, username=username, email=email, password=password)
|
2019-01-22 20:25:04 +00:00
|
|
|
|
2019-05-05 09:45:24 +00:00
|
|
|
|
2019-05-21 09:56:49 +00:00
|
|
|
def import_demo_course(runner):
|
|
|
|
runner.check_service_is_activated("cms")
|
2019-06-06 19:58:21 +00:00
|
|
|
runner.run("cms", "hooks", "cms", "importdemocourse")
|