2014-08-31 11:06:39 -04:00
|
|
|
# -*- coding: utf-8 -*-
|
2015-01-06 08:21:39 -05:00
|
|
|
# Copyright 2014, 2015 OpenMarket Ltd
|
2014-08-31 11:06:39 -04: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
|
|
|
|
import sys
|
|
|
|
import os
|
2014-09-01 15:44:43 -04:00
|
|
|
import yaml
|
2014-08-31 11:06:39 -04:00
|
|
|
|
|
|
|
|
2014-09-02 05:48:05 -04:00
|
|
|
class ConfigError(Exception):
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
2014-08-31 11:06:39 -04:00
|
|
|
class Config(object):
|
|
|
|
def __init__(self, args):
|
|
|
|
pass
|
|
|
|
|
2015-02-11 10:01:15 -05:00
|
|
|
@staticmethod
|
|
|
|
def parse_size(string):
|
|
|
|
sizes = {"K": 1024, "M": 1024 * 1024}
|
|
|
|
size = 1
|
|
|
|
suffix = string[-1]
|
|
|
|
if suffix in sizes:
|
|
|
|
string = string[:-1]
|
|
|
|
size = sizes[suffix]
|
|
|
|
return int(string) * size
|
|
|
|
|
2014-09-01 10:51:15 -04:00
|
|
|
@staticmethod
|
|
|
|
def abspath(file_path):
|
|
|
|
return os.path.abspath(file_path) if file_path else file_path
|
|
|
|
|
2014-09-02 05:48:05 -04:00
|
|
|
@classmethod
|
|
|
|
def check_file(cls, file_path, config_name):
|
|
|
|
if file_path is None:
|
|
|
|
raise ConfigError(
|
|
|
|
"Missing config for %s."
|
2014-11-14 08:30:06 -05: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 05:48:05 -04:00
|
|
|
% (config_name,)
|
|
|
|
)
|
|
|
|
if not os.path.exists(file_path):
|
|
|
|
raise ConfigError(
|
2014-12-13 13:03:01 -05:00
|
|
|
"File %s config for %s doesn't exist."
|
2014-09-02 05:48:05 -04:00
|
|
|
" Try running again with --generate-config"
|
2014-12-13 13:03:01 -05:00
|
|
|
% (file_path, config_name,)
|
2014-09-02 05:48:05 -04:00
|
|
|
)
|
|
|
|
return cls.abspath(file_path)
|
|
|
|
|
2015-02-09 13:29:36 -05:00
|
|
|
@classmethod
|
|
|
|
def ensure_directory(cls, dir_path):
|
|
|
|
dir_path = cls.abspath(dir_path)
|
2014-12-02 14:51:47 -05:00
|
|
|
if not os.path.exists(dir_path):
|
|
|
|
os.makedirs(dir_path)
|
|
|
|
if not os.path.isdir(dir_path):
|
|
|
|
raise ConfigError(
|
|
|
|
"%s is not a directory" % (dir_path,)
|
|
|
|
)
|
|
|
|
return dir_path
|
|
|
|
|
2014-09-02 05:48:05 -04:00
|
|
|
@classmethod
|
|
|
|
def read_file(cls, file_path, config_name):
|
|
|
|
cls.check_file(file_path, config_name)
|
2014-08-31 11:06:39 -04:00
|
|
|
with open(file_path) as file_stream:
|
|
|
|
return file_stream.read()
|
|
|
|
|
2015-04-24 06:26:19 -04:00
|
|
|
@classmethod
|
|
|
|
def read_yaml_file(cls, file_path, config_name):
|
|
|
|
cls.check_file(file_path, config_name)
|
|
|
|
with open(file_path) as file_stream:
|
|
|
|
try:
|
|
|
|
return yaml.load(file_stream)
|
2015-04-24 12:01:34 -04:00
|
|
|
except:
|
2015-04-24 06:26:19 -04:00
|
|
|
raise ConfigError(
|
2015-04-24 12:01:34 -04:00
|
|
|
"Error parsing yaml in file %r" % (file_path,)
|
2015-04-24 06:26:19 -04:00
|
|
|
)
|
|
|
|
|
2014-12-02 14:51:47 -05:00
|
|
|
@staticmethod
|
|
|
|
def default_path(name):
|
|
|
|
return os.path.abspath(os.path.join(os.path.curdir, name))
|
|
|
|
|
2014-08-31 11:06:39 -04:00
|
|
|
@staticmethod
|
|
|
|
def read_config_file(file_path):
|
2014-09-01 15:44:43 -04:00
|
|
|
with open(file_path) as file_stream:
|
|
|
|
return yaml.load(file_stream)
|
2014-08-31 11:06:39 -04:00
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def add_arguments(cls, parser):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def generate_config(cls, args, config_dir_path):
|
|
|
|
pass
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def load_config(cls, description, argv, generate_section=None):
|
|
|
|
config_parser = argparse.ArgumentParser(add_help=False)
|
|
|
|
config_parser.add_argument(
|
|
|
|
"-c", "--config-path",
|
|
|
|
metavar="CONFIG_FILE",
|
|
|
|
help="Specify config file"
|
|
|
|
)
|
2014-09-01 10:51:15 -04:00
|
|
|
config_parser.add_argument(
|
|
|
|
"--generate-config",
|
|
|
|
action="store_true",
|
|
|
|
help="Generate config file"
|
|
|
|
)
|
2014-08-31 11:06:39 -04:00
|
|
|
config_args, remaining_args = config_parser.parse_known_args(argv)
|
|
|
|
|
2014-09-01 10:51:15 -04:00
|
|
|
if config_args.generate_config:
|
2014-08-31 11:06:39 -04:00
|
|
|
if not config_args.config_path:
|
|
|
|
config_parser.error(
|
|
|
|
"Must specify where to generate the config file"
|
|
|
|
)
|
|
|
|
config_dir_path = os.path.dirname(config_args.config_path)
|
|
|
|
if os.path.exists(config_args.config_path):
|
|
|
|
defaults = cls.read_config_file(config_args.config_path)
|
2014-09-01 10:51:15 -04:00
|
|
|
else:
|
|
|
|
defaults = {}
|
2014-08-31 11:06:39 -04:00
|
|
|
else:
|
|
|
|
if config_args.config_path:
|
|
|
|
defaults = cls.read_config_file(config_args.config_path)
|
|
|
|
else:
|
|
|
|
defaults = {}
|
|
|
|
|
|
|
|
parser = argparse.ArgumentParser(
|
|
|
|
parents=[config_parser],
|
|
|
|
description=description,
|
|
|
|
formatter_class=argparse.RawDescriptionHelpFormatter,
|
|
|
|
)
|
2014-09-01 10:51:15 -04:00
|
|
|
cls.add_arguments(parser)
|
2014-08-31 11:06:39 -04:00
|
|
|
parser.set_defaults(**defaults)
|
|
|
|
|
|
|
|
args = parser.parse_args(remaining_args)
|
|
|
|
|
2014-09-01 10:51:15 -04:00
|
|
|
if config_args.generate_config:
|
2014-08-31 11:06:39 -04:00
|
|
|
config_dir_path = os.path.dirname(config_args.config_path)
|
|
|
|
config_dir_path = os.path.abspath(config_dir_path)
|
2014-09-02 05:57:42 -04:00
|
|
|
if not os.path.exists(config_dir_path):
|
|
|
|
os.makedirs(config_dir_path)
|
2014-08-31 11:06:39 -04:00
|
|
|
cls.generate_config(args, config_dir_path)
|
2014-09-01 15:44:43 -04:00
|
|
|
config = {}
|
2014-08-31 11:06:39 -04:00
|
|
|
for key, value in vars(args).items():
|
2014-09-01 10:51:15 -04:00
|
|
|
if (key not in set(["config_path", "generate_config"])
|
2014-10-30 07:10:17 -04:00
|
|
|
and value is not None):
|
2014-09-01 15:44:43 -04:00
|
|
|
config[key] = value
|
2014-08-31 11:06:39 -04:00
|
|
|
with open(config_args.config_path, "w") as config_file:
|
2015-04-28 05:17:10 -04:00
|
|
|
# TODO(mark/paul) We might want to output emacs-style mode
|
|
|
|
# markers as well as vim-style mode markers into the file,
|
|
|
|
# to further hint to people this is a YAML file.
|
|
|
|
config_file.write("# vim:ft=yaml\n")
|
2014-09-01 15:44:43 -04:00
|
|
|
yaml.dump(config, config_file, default_flow_style=False)
|
2014-10-30 07:10:17 -04:00
|
|
|
print (
|
|
|
|
"A config file has been generated in %s for server name"
|
|
|
|
" '%s' with corresponding SSL keys and self-signed"
|
|
|
|
" certificates. Please review this file and customise it to"
|
|
|
|
" your needs."
|
|
|
|
) % (
|
|
|
|
config_args.config_path, config['server_name']
|
|
|
|
)
|
|
|
|
print (
|
|
|
|
"If this server name is incorrect, you will need to regenerate"
|
|
|
|
" the SSL certificates"
|
|
|
|
)
|
2014-09-01 10:51:15 -04:00
|
|
|
sys.exit(0)
|
2014-08-31 11:06:39 -04:00
|
|
|
|
|
|
|
return cls(args)
|