2018-08-05 14:26:19 +00:00
|
|
|
#! /usr/bin/env python3
|
|
|
|
# coding: utf8
|
|
|
|
import argparse
|
|
|
|
import codecs
|
|
|
|
import json
|
|
|
|
import os
|
|
|
|
import random
|
|
|
|
import string
|
|
|
|
import sys
|
|
|
|
|
|
|
|
from collections import OrderedDict
|
|
|
|
|
2018-08-05 14:40:51 +00:00
|
|
|
import jinja2
|
|
|
|
|
2018-08-05 14:26:19 +00:00
|
|
|
|
|
|
|
class Configurator:
|
|
|
|
|
|
|
|
def __init__(self, **default_overrides):
|
2018-09-15 13:51:41 +00:00
|
|
|
"""
|
|
|
|
Default values are read, in decreasing order of priority, from:
|
|
|
|
- SETTING_<name> environment variable
|
|
|
|
- Existing config file (in `default_overrides`)
|
|
|
|
- Value passed to add()
|
|
|
|
"""
|
2018-08-05 14:26:19 +00:00
|
|
|
self.__values = OrderedDict()
|
|
|
|
self.__default_values = default_overrides
|
|
|
|
try:
|
|
|
|
self.__input = raw_input
|
|
|
|
except NameError:
|
|
|
|
self.__input = input
|
|
|
|
|
|
|
|
def as_dict(self):
|
2018-08-16 13:59:31 +00:00
|
|
|
return self.__values
|
2018-08-05 14:26:19 +00:00
|
|
|
|
|
|
|
def mute(self):
|
|
|
|
self.__input = None
|
|
|
|
|
|
|
|
def add(self, name, question="", default=""):
|
2018-09-15 14:19:35 +00:00
|
|
|
default = self.get_default_value(name, default)
|
2018-08-16 13:59:31 +00:00
|
|
|
message = question + " (default: \"{}\"): ".format(default) if question else None
|
|
|
|
value = self.ask(message, default)
|
2018-08-05 14:26:19 +00:00
|
|
|
self.set(name, value)
|
|
|
|
|
|
|
|
return self
|
|
|
|
|
2018-09-15 13:51:41 +00:00
|
|
|
def add_bool(self, name, question="", default=False):
|
|
|
|
self.add(name, question=question, default=default)
|
|
|
|
value = self.get(name)
|
|
|
|
if value in [1, '1']:
|
|
|
|
return self.set(name, True)
|
2018-09-15 14:19:35 +00:00
|
|
|
if value in [0, '0', '']:
|
2018-09-15 13:51:41 +00:00
|
|
|
return self.set(name, False)
|
2018-09-15 14:19:35 +00:00
|
|
|
if value in [True, False]:
|
|
|
|
return self
|
|
|
|
return self.set(name, bool(value))
|
2018-09-15 13:51:41 +00:00
|
|
|
|
2018-09-15 14:19:35 +00:00
|
|
|
def get_default_value(self, name, default):
|
2018-09-15 13:51:41 +00:00
|
|
|
setting_name = 'SETTING_' + name.upper()
|
2018-09-15 14:19:35 +00:00
|
|
|
if os.environ.get(setting_name):
|
2018-09-15 13:51:41 +00:00
|
|
|
return os.environ[setting_name]
|
|
|
|
|
2018-09-15 14:19:35 +00:00
|
|
|
if name in self.__default_values:
|
|
|
|
return self.__default_values[name]
|
|
|
|
|
|
|
|
return default
|
2018-09-15 13:51:41 +00:00
|
|
|
|
2018-08-05 14:26:19 +00:00
|
|
|
def ask(self, message, default):
|
2018-08-16 13:59:31 +00:00
|
|
|
if self.__input and message:
|
2018-08-05 14:26:19 +00:00
|
|
|
return self.__input(message) or default
|
|
|
|
return default
|
|
|
|
|
|
|
|
def get(self, name):
|
|
|
|
return self.__values.get(name)
|
|
|
|
|
|
|
|
def set(self, name, value):
|
|
|
|
self.__values[name] = value
|
2018-08-16 13:59:31 +00:00
|
|
|
return self
|
2018-08-05 14:26:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
parser = argparse.ArgumentParser("Config file generator for Open edX")
|
|
|
|
parser.add_argument('-c', '--config', default=os.path.join("/", "openedx", "config", "config.json"),
|
|
|
|
help="Load default values from this file. Config values will be saved there.")
|
|
|
|
subparsers = parser.add_subparsers()
|
|
|
|
|
|
|
|
parser_interactive = subparsers.add_parser('interactive')
|
|
|
|
parser_interactive.add_argument('-s', '--silent', action='store_true',
|
|
|
|
help=(
|
|
|
|
"Be silent and accept all default values. "
|
|
|
|
"This is good for debugging and automation, but "
|
|
|
|
"probably not what you want"
|
|
|
|
))
|
|
|
|
parser_interactive.set_defaults(func=interactive)
|
|
|
|
|
|
|
|
parser_substitute = subparsers.add_parser('substitute')
|
2018-09-15 14:19:35 +00:00
|
|
|
parser_substitute.add_argument('src', help="Template source directory")
|
|
|
|
parser_substitute.add_argument('dst', help="Destination configuration directory")
|
2018-08-05 14:26:19 +00:00
|
|
|
parser_substitute.set_defaults(func=substitute)
|
|
|
|
|
|
|
|
args = parser.parse_args()
|
2018-08-16 13:59:31 +00:00
|
|
|
args.func(args)
|
2018-08-05 14:26:19 +00:00
|
|
|
|
2018-08-16 13:59:31 +00:00
|
|
|
def load_config(args):
|
2018-08-05 14:26:19 +00:00
|
|
|
if os.path.exists(args.config):
|
|
|
|
with open(args.config) as f:
|
2018-08-16 13:59:31 +00:00
|
|
|
return json.load(f)
|
|
|
|
return {}
|
2018-08-05 14:26:19 +00:00
|
|
|
|
2018-08-16 13:59:31 +00:00
|
|
|
def interactive(args):
|
|
|
|
print("\n====================================")
|
|
|
|
print(" Interactive configuration ")
|
|
|
|
print("====================================")
|
2018-08-05 14:26:19 +00:00
|
|
|
|
2018-08-16 13:59:31 +00:00
|
|
|
configurator = Configurator(**load_config(args))
|
2018-09-15 09:55:42 +00:00
|
|
|
if args.silent or os.environ.get('SILENT'):
|
2018-08-05 14:26:19 +00:00
|
|
|
configurator.mute()
|
|
|
|
configurator.add(
|
|
|
|
'LMS_HOST', "Your website domain name for students (LMS).", 'www.myopenedx.com'
|
|
|
|
).add(
|
2018-09-16 08:25:03 +00:00
|
|
|
'CMS_HOST', "Your website domain name for teachers (CMS).", 'studio.' + configurator.get('LMS_HOST')
|
2018-08-05 14:26:19 +00:00
|
|
|
).add(
|
2018-09-16 08:25:03 +00:00
|
|
|
'PLATFORM_NAME', "Your platform name/title", "My Open edX"
|
2018-09-05 10:24:07 +00:00
|
|
|
).add(
|
2018-09-16 08:25:03 +00:00
|
|
|
'CONTACT_EMAIL', "Your public contact email address", 'contact@' + configurator.get('LMS_HOST')
|
2018-08-05 14:26:19 +00:00
|
|
|
).add(
|
|
|
|
'SECRET_KEY', "", random_string(24)
|
|
|
|
).add(
|
|
|
|
'MYSQL_DATABASE', "", 'openedx'
|
|
|
|
).add(
|
|
|
|
'MYSQL_USERNAME', "", 'openedx'
|
|
|
|
).add(
|
2018-09-15 13:51:41 +00:00
|
|
|
'MYSQL_PASSWORD', "", random_string(8)
|
2018-08-05 14:26:19 +00:00
|
|
|
).add(
|
|
|
|
'MONGODB_DATABASE', "", 'openedx'
|
2018-09-15 13:19:57 +00:00
|
|
|
).add(
|
|
|
|
'NOTES_MYSQL_DATABASE', "", 'notes',
|
|
|
|
).add(
|
|
|
|
'NOTES_MYSQL_USERNAME', "", 'notes',
|
|
|
|
).add(
|
|
|
|
'NOTES_MYSQL_PASSWORD', "", random_string(8)
|
|
|
|
).add(
|
|
|
|
'NOTES_SECRET_KEY', "", random_string(24)
|
|
|
|
).add(
|
|
|
|
'NOTES_OAUTH2_SECRET', "", random_string(24)
|
2018-08-05 14:26:19 +00:00
|
|
|
).add(
|
|
|
|
'XQUEUE_AUTH_USERNAME', "", 'lms'
|
|
|
|
).add(
|
2018-09-15 13:51:41 +00:00
|
|
|
'XQUEUE_AUTH_PASSWORD', "", random_string(8)
|
2018-08-05 14:26:19 +00:00
|
|
|
).add(
|
|
|
|
'XQUEUE_MYSQL_DATABASE', "", 'xqueue',
|
|
|
|
).add(
|
|
|
|
'XQUEUE_MYSQL_USERNAME', "", 'xqueue',
|
|
|
|
).add(
|
2018-09-15 13:51:41 +00:00
|
|
|
'XQUEUE_MYSQL_PASSWORD', "", random_string(8)
|
2018-08-05 14:26:19 +00:00
|
|
|
).add(
|
2018-09-15 13:51:41 +00:00
|
|
|
'XQUEUE_SECRET_KEY', "", random_string(24)
|
2018-09-15 13:19:57 +00:00
|
|
|
).add_bool(
|
|
|
|
'ACTIVATE_NOTES', "", False
|
2018-09-15 13:51:41 +00:00
|
|
|
).add_bool(
|
|
|
|
'ACTIVATE_HTTPS', "", False
|
2018-09-29 13:09:19 +00:00
|
|
|
).add_bool(
|
|
|
|
'ACTIVATE_PORTAINER', "", False
|
2018-09-15 13:51:41 +00:00
|
|
|
).add_bool(
|
|
|
|
'ACTIVATE_XQUEUE', "", False
|
2018-09-18 18:17:42 +00:00
|
|
|
).add(
|
|
|
|
'ID', "", random_string(8)
|
2018-08-05 14:26:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
# Save values
|
|
|
|
with open(args.config, 'w') as f:
|
|
|
|
json.dump(configurator.as_dict(), f, sort_keys=True, indent=4)
|
|
|
|
print("\nConfiguration values were saved to ", args.config)
|
|
|
|
|
|
|
|
|
2018-08-16 13:59:31 +00:00
|
|
|
def substitute(args):
|
|
|
|
config = load_config(args)
|
2018-09-15 14:19:35 +00:00
|
|
|
|
|
|
|
for root, _, filenames in os.walk(args.src):
|
|
|
|
for filename in filenames:
|
|
|
|
if filename.startswith('.'):
|
|
|
|
# Skip hidden files, such as files generated by the IDE
|
|
|
|
continue
|
|
|
|
src_file = os.path.join(root, filename)
|
|
|
|
dst_file = os.path.join(args.dst, os.path.relpath(src_file, args.src))
|
|
|
|
substitute_file(config, src_file, dst_file)
|
|
|
|
|
|
|
|
def substitute_file(config, src, dst):
|
|
|
|
with codecs.open(src, encoding='utf-8') as fi:
|
2018-08-05 14:40:51 +00:00
|
|
|
template = jinja2.Template(fi.read(), undefined=jinja2.StrictUndefined)
|
2018-08-05 14:26:19 +00:00
|
|
|
try:
|
2018-08-16 13:59:31 +00:00
|
|
|
substituted = template.render(**config)
|
2018-08-05 14:40:51 +00:00
|
|
|
except jinja2.exceptions.UndefinedError as e:
|
2018-09-15 14:19:35 +00:00
|
|
|
sys.stderr.write("ERROR Missing config value '{}' for template {}\n".format(e.args[0], src))
|
2018-08-05 14:26:19 +00:00
|
|
|
sys.exit(1)
|
|
|
|
|
2018-09-15 14:19:35 +00:00
|
|
|
dst_dir = os.path.dirname(dst)
|
|
|
|
if not os.path.exists(dst_dir):
|
|
|
|
os.makedirs(dst_dir)
|
|
|
|
with codecs.open(dst, encoding='utf-8', mode='w') as fo:
|
2018-08-05 14:26:19 +00:00
|
|
|
fo.write(substituted)
|
|
|
|
|
2018-08-16 13:59:31 +00:00
|
|
|
# Set same permissions as original file
|
2018-09-15 14:19:35 +00:00
|
|
|
os.chmod(dst, os.stat(src).st_mode)
|
2018-08-16 13:59:31 +00:00
|
|
|
|
2018-09-15 14:19:35 +00:00
|
|
|
print("Generated file {} from template {}".format(dst, src))
|
2018-08-05 14:26:19 +00:00
|
|
|
|
|
|
|
|
|
|
|
def random_string(length):
|
|
|
|
return "".join([random.choice(string.ascii_letters + string.digits) for _ in range(length)])
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|