2014-08-31 17:06:39 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-01-06 14:21:39 +01:00
|
|
|
# Copyright 2014, 2015 OpenMarket Ltd
|
2014-08-31 17:06:39 +02:00
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License.
|
|
|
|
|
|
|
|
import argparse
|
2015-11-12 12:58:48 +01:00
|
|
|
import errno
|
2014-08-31 17:06:39 +02:00
|
|
|
import os
|
2014-09-01 21:44:43 +02:00
|
|
|
import yaml
|
2015-04-30 05:24:44 +02:00
|
|
|
import sys
|
|
|
|
from textwrap import dedent
|
2014-08-31 17:06:39 +02:00
|
|
|
|
|
|
|
|
2014-09-02 11:48:05 +02:00
|
|
|
class ConfigError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2015-11-19 14:05:51 +01:00
|
|
|
# We split these messages out to allow packages to override with package
|
|
|
|
# specific instructions.
|
2015-11-18 19:37:03 +01:00
|
|
|
MISSING_REPORT_STATS_CONFIG_INSTRUCTIONS = """\
|
|
|
|
Please opt in or out of reporting anonymized homeserver usage statistics, by
|
|
|
|
setting the `report_stats` key in your config file to either True or False.
|
|
|
|
"""
|
|
|
|
|
|
|
|
MISSING_REPORT_STATS_SPIEL = """\
|
|
|
|
We would really appreciate it if you could help our project out by reporting
|
|
|
|
anonymized usage statistics from your homeserver. Only very basic aggregate
|
|
|
|
data (e.g. number of users) will be reported, but it helps us to track the
|
|
|
|
growth of the Matrix community, and helps us to make Matrix a success, as well
|
|
|
|
as to convince other networks that they should peer with us.
|
|
|
|
|
|
|
|
Thank you.
|
|
|
|
"""
|
|
|
|
|
|
|
|
MISSING_SERVER_NAME = """\
|
|
|
|
Missing mandatory `server_name` config option.
|
|
|
|
"""
|
2014-08-31 17:06:39 +02:00
|
|
|
|
2015-09-22 13:57:40 +02:00
|
|
|
|
2015-11-18 19:37:03 +01:00
|
|
|
class Config(object):
|
2015-02-11 16:01:15 +01:00
|
|
|
@staticmethod
|
2015-04-30 17:04:02 +02:00
|
|
|
def parse_size(value):
|
|
|
|
if isinstance(value, int) or isinstance(value, long):
|
|
|
|
return value
|
2015-02-11 16:01:15 +01:00
|
|
|
sizes = {"K": 1024, "M": 1024 * 1024}
|
|
|
|
size = 1
|
2015-04-30 17:04:02 +02:00
|
|
|
suffix = value[-1]
|
2015-02-11 16:01:15 +01:00
|
|
|
if suffix in sizes:
|
2015-04-30 17:04:02 +02:00
|
|
|
value = value[:-1]
|
2015-02-11 16:01:15 +01:00
|
|
|
size = sizes[suffix]
|
2015-04-30 17:04:02 +02:00
|
|
|
return int(value) * size
|
2015-02-11 16:01:15 +01:00
|
|
|
|
2015-04-30 05:24:44 +02:00
|
|
|
@staticmethod
|
2015-04-30 17:04:02 +02:00
|
|
|
def parse_duration(value):
|
|
|
|
if isinstance(value, int) or isinstance(value, long):
|
|
|
|
return value
|
2015-04-30 05:24:44 +02:00
|
|
|
second = 1000
|
|
|
|
hour = 60 * 60 * second
|
|
|
|
day = 24 * hour
|
|
|
|
week = 7 * day
|
|
|
|
year = 365 * day
|
|
|
|
sizes = {"s": second, "h": hour, "d": day, "w": week, "y": year}
|
|
|
|
size = 1
|
2015-04-30 17:04:02 +02:00
|
|
|
suffix = value[-1]
|
2015-04-30 05:24:44 +02:00
|
|
|
if suffix in sizes:
|
2015-04-30 17:04:02 +02:00
|
|
|
value = value[:-1]
|
2015-04-30 05:24:44 +02:00
|
|
|
size = sizes[suffix]
|
2015-04-30 17:04:02 +02:00
|
|
|
return int(value) * size
|
2015-04-30 05:24:44 +02:00
|
|
|
|
2014-09-01 16:51:15 +02:00
|
|
|
@staticmethod
|
|
|
|
def abspath(file_path):
|
|
|
|
return os.path.abspath(file_path) if file_path else file_path
|
|
|
|
|
2014-09-02 11:48:05 +02:00
|
|
|
@classmethod
|
|
|
|
def check_file(cls, file_path, config_name):
|
|
|
|
if file_path is None:
|
|
|
|
raise ConfigError(
|
|
|
|
"Missing config for %s."
|
2014-11-14 14:30:06 +01:00
|
|
|
" You must specify a path for the config file. You can "
|
|
|
|
"do this with the -c or --config-path option. "
|
|
|
|
"Adding --generate-config along with --server-name "
|
|
|
|
"<server name> will generate a config file at the given path."
|
2014-09-02 11:48:05 +02:00
|
|
|
% (config_name,)
|
|
|
|
)
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
raise ConfigError(
|
2014-12-13 19:03:01 +01:00
|
|
|
"File %s config for %s doesn't exist."
|
2014-09-02 11:48:05 +02:00
|
|
|
" Try running again with --generate-config"
|
2014-12-13 19:03:01 +01:00
|
|
|
% (file_path, config_name,)
|
2014-09-02 11:48:05 +02:00
|
|
|
)
|
|
|
|
return cls.abspath(file_path)
|
|
|
|
|
2015-02-09 19:29:36 +01:00
|
|
|
@classmethod
|
|
|
|
def ensure_directory(cls, dir_path):
|
|
|
|
dir_path = cls.abspath(dir_path)
|
2015-11-12 12:58:48 +01:00
|
|
|
try:
|
2014-12-02 20:51:47 +01:00
|
|
|
os.makedirs(dir_path)
|
2015-11-12 12:58:48 +01:00
|
|
|
except OSError, e:
|
|
|
|
if e.errno != errno.EEXIST:
|
|
|
|
raise
|
2014-12-02 20:51:47 +01:00
|
|
|
if not os.path.isdir(dir_path):
|
|
|
|
raise ConfigError(
|
|
|
|
"%s is not a directory" % (dir_path,)
|
|
|
|
)
|
|
|
|
return dir_path
|
|
|
|
|
2014-09-02 11:48:05 +02:00
|
|
|
@classmethod
|
|
|
|
def read_file(cls, file_path, config_name):
|
|
|
|
cls.check_file(file_path, config_name)
|
2014-08-31 17:06:39 +02:00
|
|
|
with open(file_path) as file_stream:
|
|
|
|
return file_stream.read()
|
|
|
|
|
2014-12-02 20:51:47 +01:00
|
|
|
@staticmethod
|
|
|
|
def default_path(name):
|
|
|
|
return os.path.abspath(os.path.join(os.path.curdir, name))
|
|
|
|
|
2014-08-31 17:06:39 +02:00
|
|
|
@staticmethod
|
|
|
|
def read_config_file(file_path):
|
2014-09-01 21:44:43 +02:00
|
|
|
with open(file_path) as file_stream:
|
|
|
|
return yaml.load(file_stream)
|
2014-08-31 17:06:39 +02:00
|
|
|
|
2015-04-30 05:24:44 +02:00
|
|
|
def invoke_all(self, name, *args, **kargs):
|
|
|
|
results = []
|
|
|
|
for cls in type(self).mro():
|
|
|
|
if name in cls.__dict__:
|
|
|
|
results.append(getattr(cls, name)(self, *args, **kargs))
|
|
|
|
return results
|
2014-08-31 17:06:39 +02:00
|
|
|
|
2015-09-22 13:57:40 +02:00
|
|
|
def generate_config(self, config_dir_path, server_name, report_stats=None):
|
2015-04-30 05:24:44 +02:00
|
|
|
default_config = "# vim:ft=yaml\n"
|
|
|
|
|
|
|
|
default_config += "\n\n".join(dedent(conf) for conf in self.invoke_all(
|
2015-09-22 13:57:40 +02:00
|
|
|
"default_config",
|
|
|
|
config_dir_path=config_dir_path,
|
|
|
|
server_name=server_name,
|
|
|
|
report_stats=report_stats,
|
2015-04-30 05:24:44 +02:00
|
|
|
))
|
|
|
|
|
|
|
|
config = yaml.load(default_config)
|
|
|
|
|
2015-04-30 14:48:15 +02:00
|
|
|
return default_config, config
|
2014-08-31 17:06:39 +02:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_config(cls, description, argv, generate_section=None):
|
2015-04-30 14:48:15 +02:00
|
|
|
obj = cls()
|
2015-04-30 05:24:44 +02:00
|
|
|
|
2014-08-31 17:06:39 +02:00
|
|
|
config_parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
config_parser.add_argument(
|
|
|
|
"-c", "--config-path",
|
2015-04-30 14:48:15 +02:00
|
|
|
action="append",
|
2014-08-31 17:06:39 +02:00
|
|
|
metavar="CONFIG_FILE",
|
2015-08-25 17:25:54 +02:00
|
|
|
help="Specify config file. Can be given multiple times and"
|
|
|
|
" may specify directories containing *.yaml files."
|
2014-08-31 17:06:39 +02:00
|
|
|
)
|
2014-09-01 16:51:15 +02:00
|
|
|
config_parser.add_argument(
|
|
|
|
"--generate-config",
|
2015-04-30 14:48:15 +02:00
|
|
|
action="store_true",
|
2015-04-30 05:24:44 +02:00
|
|
|
help="Generate a config file for the server name"
|
2014-09-01 16:51:15 +02:00
|
|
|
)
|
2015-09-22 13:57:40 +02:00
|
|
|
config_parser.add_argument(
|
|
|
|
"--report-stats",
|
|
|
|
action="store",
|
|
|
|
help="Stuff",
|
|
|
|
choices=["yes", "no"]
|
|
|
|
)
|
2015-08-07 17:42:27 +02:00
|
|
|
config_parser.add_argument(
|
|
|
|
"--generate-keys",
|
|
|
|
action="store_true",
|
|
|
|
help="Generate any missing key files then exit"
|
|
|
|
)
|
2015-08-25 17:58:01 +02:00
|
|
|
config_parser.add_argument(
|
2015-08-25 18:31:22 +02:00
|
|
|
"--keys-directory",
|
2015-08-25 17:58:01 +02:00
|
|
|
metavar="DIRECTORY",
|
2015-08-25 18:31:22 +02:00
|
|
|
help="Used with 'generate-*' options to specify where files such as"
|
2015-08-25 18:37:21 +02:00
|
|
|
" certs and signing keys should be stored in, unless explicitly"
|
|
|
|
" specified in the config."
|
2015-08-25 17:58:01 +02:00
|
|
|
)
|
2015-04-30 14:48:15 +02:00
|
|
|
config_parser.add_argument(
|
|
|
|
"-H", "--server-name",
|
|
|
|
help="The server name to generate a config file for"
|
|
|
|
)
|
2014-08-31 17:06:39 +02:00
|
|
|
config_args, remaining_args = config_parser.parse_known_args(argv)
|
|
|
|
|
2015-08-12 12:57:37 +02:00
|
|
|
generate_keys = config_args.generate_keys
|
|
|
|
|
2015-08-25 17:25:54 +02:00
|
|
|
config_files = []
|
|
|
|
if config_args.config_path:
|
|
|
|
for config_path in config_args.config_path:
|
|
|
|
if os.path.isdir(config_path):
|
|
|
|
# We accept specifying directories as config paths, we search
|
|
|
|
# inside that directory for all files matching *.yaml, and then
|
|
|
|
# we apply them in *sorted* order.
|
2015-08-25 18:08:23 +02:00
|
|
|
files = []
|
|
|
|
for entry in os.listdir(config_path):
|
|
|
|
entry_path = os.path.join(config_path, entry)
|
|
|
|
if not os.path.isfile(entry_path):
|
|
|
|
print (
|
|
|
|
"Found subdirectory in config directory: %r. IGNORING."
|
|
|
|
) % (entry_path, )
|
|
|
|
continue
|
|
|
|
|
|
|
|
if not entry.endswith(".yaml"):
|
|
|
|
print (
|
|
|
|
"Found file in config directory that does not"
|
|
|
|
" end in '.yaml': %r. IGNORING."
|
|
|
|
) % (entry_path, )
|
|
|
|
continue
|
|
|
|
|
2015-09-02 18:27:59 +02:00
|
|
|
files.append(entry_path)
|
2015-08-28 11:37:17 +02:00
|
|
|
|
2015-08-25 18:08:23 +02:00
|
|
|
config_files.extend(sorted(files))
|
2015-08-25 17:25:54 +02:00
|
|
|
else:
|
|
|
|
config_files.append(config_path)
|
|
|
|
|
2014-09-01 16:51:15 +02:00
|
|
|
if config_args.generate_config:
|
2015-09-22 13:57:40 +02:00
|
|
|
if config_args.report_stats is None:
|
|
|
|
config_parser.error(
|
|
|
|
"Please specify either --report-stats=yes or --report-stats=no\n\n" +
|
2015-11-18 19:37:03 +01:00
|
|
|
MISSING_REPORT_STATS_SPIEL
|
2015-09-22 13:57:40 +02:00
|
|
|
)
|
2015-08-25 17:25:54 +02:00
|
|
|
if not config_files:
|
2015-05-05 18:38:10 +02:00
|
|
|
config_parser.error(
|
|
|
|
"Must supply a config file.\nA config file can be automatically"
|
2015-06-16 17:03:35 +02:00
|
|
|
" generated using \"--generate-config -H SERVER_NAME"
|
2015-05-05 18:38:10 +02:00
|
|
|
" -c CONFIG-FILE\""
|
|
|
|
)
|
2015-08-25 17:25:54 +02:00
|
|
|
(config_path,) = config_files
|
2015-08-12 12:57:37 +02:00
|
|
|
if not os.path.exists(config_path):
|
2015-08-25 18:31:22 +02:00
|
|
|
if config_args.keys_directory:
|
|
|
|
config_dir_path = config_args.keys_directory
|
2015-08-25 17:58:01 +02:00
|
|
|
else:
|
|
|
|
config_dir_path = os.path.dirname(config_path)
|
2015-08-12 12:57:37 +02:00
|
|
|
config_dir_path = os.path.abspath(config_dir_path)
|
|
|
|
|
|
|
|
server_name = config_args.server_name
|
|
|
|
if not server_name:
|
|
|
|
print "Must specify a server_name to a generate config for."
|
2015-05-01 14:54:38 +02:00
|
|
|
sys.exit(1)
|
2015-08-12 12:57:37 +02:00
|
|
|
if not os.path.exists(config_dir_path):
|
|
|
|
os.makedirs(config_dir_path)
|
|
|
|
with open(config_path, "wb") as config_file:
|
|
|
|
config_bytes, config = obj.generate_config(
|
2015-09-22 13:57:40 +02:00
|
|
|
config_dir_path=config_dir_path,
|
|
|
|
server_name=server_name,
|
|
|
|
report_stats=(config_args.report_stats == "yes"),
|
2015-08-12 12:57:37 +02:00
|
|
|
)
|
|
|
|
obj.invoke_all("generate_files", config)
|
|
|
|
config_file.write(config_bytes)
|
|
|
|
print (
|
|
|
|
"A config file has been generated in %r for server name"
|
|
|
|
" %r with corresponding SSL keys and self-signed"
|
|
|
|
" certificates. Please review this file and customise it"
|
|
|
|
" to your needs."
|
|
|
|
) % (config_path, server_name)
|
|
|
|
print (
|
|
|
|
"If this server name is incorrect, you will need to"
|
|
|
|
" regenerate the SSL certificates"
|
2015-05-01 14:54:38 +02:00
|
|
|
)
|
|
|
|
sys.exit(0)
|
2015-08-12 12:57:37 +02:00
|
|
|
else:
|
2015-05-01 14:54:38 +02:00
|
|
|
print (
|
2015-08-12 12:57:37 +02:00
|
|
|
"Config file %r already exists. Generating any missing key"
|
|
|
|
" files."
|
|
|
|
) % (config_path,)
|
|
|
|
generate_keys = True
|
2014-08-31 17:06:39 +02:00
|
|
|
|
2015-05-05 18:38:10 +02:00
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
parents=[config_parser],
|
|
|
|
description=description,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
|
|
)
|
|
|
|
|
|
|
|
obj.invoke_all("add_arguments", parser)
|
|
|
|
args = parser.parse_args(remaining_args)
|
|
|
|
|
2015-08-25 17:25:54 +02:00
|
|
|
if not config_files:
|
2015-05-05 18:38:10 +02:00
|
|
|
config_parser.error(
|
|
|
|
"Must supply a config file.\nA config file can be automatically"
|
2015-06-16 17:03:35 +02:00
|
|
|
" generated using \"--generate-config -H SERVER_NAME"
|
2015-05-05 18:38:10 +02:00
|
|
|
" -c CONFIG-FILE\""
|
|
|
|
)
|
|
|
|
|
2015-08-25 18:31:22 +02:00
|
|
|
if config_args.keys_directory:
|
|
|
|
config_dir_path = config_args.keys_directory
|
2015-08-25 17:58:01 +02:00
|
|
|
else:
|
|
|
|
config_dir_path = os.path.dirname(config_args.config_path[-1])
|
2015-05-05 18:38:10 +02:00
|
|
|
config_dir_path = os.path.abspath(config_dir_path)
|
|
|
|
|
2015-04-30 14:48:15 +02:00
|
|
|
specified_config = {}
|
2015-08-25 17:25:54 +02:00
|
|
|
for config_file in config_files:
|
|
|
|
yaml_config = cls.read_config_file(config_file)
|
2015-04-30 14:48:15 +02:00
|
|
|
specified_config.update(yaml_config)
|
|
|
|
|
2015-11-18 19:37:03 +01:00
|
|
|
if "server_name" not in specified_config:
|
|
|
|
sys.stderr.write("\n" + MISSING_SERVER_NAME + "\n")
|
|
|
|
sys.exit(1)
|
|
|
|
|
2015-04-30 14:48:15 +02:00
|
|
|
server_name = specified_config["server_name"]
|
2015-09-22 13:57:40 +02:00
|
|
|
_, config = obj.generate_config(
|
|
|
|
config_dir_path=config_dir_path,
|
|
|
|
server_name=server_name
|
|
|
|
)
|
2015-04-30 17:52:57 +02:00
|
|
|
config.pop("log_config")
|
2015-04-30 14:48:15 +02:00
|
|
|
config.update(specified_config)
|
2015-09-22 13:57:40 +02:00
|
|
|
if "report_stats" not in config:
|
|
|
|
sys.stderr.write(
|
2015-11-18 19:37:03 +01:00
|
|
|
"\n" + MISSING_REPORT_STATS_CONFIG_INSTRUCTIONS + "\n" +
|
|
|
|
MISSING_REPORT_STATS_SPIEL + "\n")
|
2015-09-22 13:57:40 +02:00
|
|
|
sys.exit(1)
|
2015-04-30 14:48:15 +02:00
|
|
|
|
2015-08-12 12:57:37 +02:00
|
|
|
if generate_keys:
|
|
|
|
obj.invoke_all("generate_files", config)
|
|
|
|
sys.exit(0)
|
|
|
|
|
2015-04-30 14:48:15 +02:00
|
|
|
obj.invoke_all("read_config", config)
|
2015-04-30 05:24:44 +02:00
|
|
|
|
2015-04-30 14:48:15 +02:00
|
|
|
obj.invoke_all("read_arguments", args)
|
2015-04-30 05:24:44 +02:00
|
|
|
|
2015-04-30 14:48:15 +02:00
|
|
|
return obj
|