Browse Source

Add generate_config and generate_sample_config scripts

Azrenbeth 2 years ago
parent
commit
63580f8c37
4 changed files with 130 additions and 0 deletions
  1. 1 0
      .gitignore
  2. 60 0
      scripts/generate_config
  3. 18 0
      scripts/generate_sample_config
  4. 51 0
      sydent/config/__init__.py

+ 1 - 0
.gitignore

@@ -11,6 +11,7 @@ _trial_temp.lock
 
 # Runtime files
 /sydent.conf
+/sydent.yaml
 /sydent.db
 /sydent.pid
 /matrix_is_test/sydent.stderr

+ 60 - 0
scripts/generate_config

@@ -0,0 +1,60 @@
+#!/usr/bin/env python3
+
+import argparse
+import sys
+
+from sydent.config import SydentConfig
+
+if __name__ == "__main__":
+    parser = argparse.ArgumentParser()
+    parser.add_argument(
+        "--template-dir",
+        default="TEMPLATE_DIR",
+        help="The root path where the template files are kept. Default: %(default)s",
+    )
+
+    parser.add_argument(
+        "--server-name",
+        default="SERVER_NAME",
+        help="The server name. Used to initialise the server_name config param. "
+        "Default: %(default)s",
+    )
+
+    parser.add_argument(
+        "--pid-file",
+        default="sydent.pid",
+        help="The file where the PID of the running Sydent process will be written. "
+        "Default: %(default)s",
+    )
+
+    parser.add_argument(
+        "--db-path",
+        default="sydent.db",
+        help="The SQLite Database file for Sydent to use. Default: %(default)s",
+    )
+
+    parser.add_argument(
+        "--no-secrets",
+        action="store_true",
+        help="Disable the automatic generation of secrets like the signing key",
+    )
+
+    parser.add_argument(
+        "-o",
+        "--output-file",
+        type=argparse.FileType("w"),
+        default=sys.stdout,
+        help="File to write the configuration to. Default: stdout",
+    )
+
+    args = parser.parse_args()
+
+    conf = SydentConfig().generate_config(
+        template_dir_path=args.template_dir,
+        server_name=args.server_name,
+        pid_file=args.pid_file,
+        db_path=args.db_path,
+        generate_secrets=(not args.no_secrets),
+    )
+
+    args.output_file.write(conf)

+ 18 - 0
scripts/generate_sample_config

@@ -0,0 +1,18 @@
+#!/usr/bin/env bash
+#
+# Update/check the docs/sample_config.yaml
+
+set -e
+
+cd $(dirname $0)/..
+
+SAMPLE_CONFIG="docs/sample_config.yaml"
+
+if [ "$1" == "--check" ]; then
+    diff -u "$SAMPLE_CONFIG" <(./scripts/generate_config --no-secrets) >/dev/null || {
+        echo -e "\033[1m\033[31m$SAMPLE_CONFIG is not up-to-date. Regenerate it with \`scripts-dev/generate_sample_config\`.\033[0m" >&2
+        exit 1
+    }
+else
+    ./scripts/generate_config --no-secrets -o "$SAMPLE_CONFIG"
+fi

+ 51 - 0
sydent/config/__init__.py

@@ -16,6 +16,7 @@ import copy
 import logging
 import os
 from configparser import DEFAULTSECT, ConfigParser
+from textwrap import dedent
 from typing import Dict
 
 from twisted.python import log
@@ -29,6 +30,18 @@ from sydent.config.sms import SMSConfig
 
 logger = logging.getLogger(__name__)
 
+CONFIG_FILE_HEADER = """\
+# Configuration file for Sydent.
+#
+# This is a YAML file: see [1] for a quick introduction. Note in particular
+# that *indentation is important*: all the elements of a list or dictionary
+# should have the same indentation.
+#
+# [1] https://docs.ansible.com/ansible/latest/reference_appendices/YAMLSyntax.html
+
+
+"""
+
 CONFIG_DEFAULTS = {
     "general": {
         "server.name": os.environ.get("SYDENT_SERVER_NAME", ""),
@@ -267,6 +280,44 @@ class SydentConfig:
 
         self.parse_from_config_parser(cfg)
 
+    def generate_config(
+        self,
+        template_dir_path: str,
+        server_name: str,
+        pid_file: str,
+        db_path: str,
+        generate_secrets: bool,
+    ) -> str:
+        """
+        Build a default configuration file
+        This is used when the user explicitly asks us to generate a config file
+        (eg with the generate_config script).
+
+        :param template_dir_path: The root path where the template files are kept.
+        :param server_name: The server name. Used to initialise the server_name
+            config param.
+        :param pid_file: The file where the PID of the running Sydent process will
+            be written.
+        :param db_path : The SQLite Database file for Sydent to use.
+        :param generate_secrets: True if we should generate new secrets for things
+            like the sigining key. If False, these parameters will be left unset.
+        ...
+        :return: the yaml config file contents
+        """
+
+        return CONFIG_FILE_HEADER + "\n\n".join(
+            dedent(
+                conf.generate_config_section(
+                    template_dir_path=template_dir_path,
+                    server_name=server_name,
+                    pid_file=pid_file,
+                    db_path=db_path,
+                    generate_secrets=generate_secrets,
+                )
+            )
+            for conf in self.config_sections
+        )
+
 
 def setup_logging(cfg: ConfigParser) -> None:
     """