~aasg/nixexprs

c7c1e753515a31451a6cae16d8fffd7bfab8448e — Aluísio Augusto Silva Gonçalves 9 days ago 4b0d90f
bird-lg: Add option to log to syslog instead of stderr
M modules/services/networking/monitoring/bird-lg-config.py => modules/services/networking/monitoring/bird-lg-config.py +40 -6
@@ 1,10 1,44 @@
# Put everything inside a function to avoid polluting the script's
# global namespace.
def _config_loader_main():
    import json
    import os
    for filename in os.environ.get("BIRD_LG_CONFIG_FILES", "").split(os.pathsep):
        with open(filename, "r") as file:
            config = json.load(file)
            globals().update(config)
    """ Loads bird-lg and gunicorn config files. """

    def log_to_syslog():
        """ Configures the logging module to log everything to syslog. """
        from logging.config import dictConfig
        import socket

        dictConfig(
            {
                "version": 1,
                "formatters": {"msgonly": {"format": "%(message)s"}},
                "handlers": {
                    "syslog": {
                        "class": "logging.handlers.SysLogHandler",
                        "formatter": "msgonly",
                        "address": "/dev/log",
                        "socktype": socket.SOCK_DGRAM,
                    }
                },
                "root": {"handlers": ["syslog"]},
            }
        )

    def load_config_files():
        """ Loads JSON config files specified in the BIRD_LG_CONFIG_FILES environment variable. """
        import json
        import os

        for filename in os.environ["BIRD_LG_CONFIG_FILES"].split(os.pathsep):
            with open(filename, "r") as file:
                config = json.load(file)
                globals().update(config)

    if os.environ.get("BIRD_LG_SYSLOG"):
        log_to_syslog()
    if os.environ.get("BIRD_LG_CONFIG_FILES"):
        load_config_files()


_config_loader_main()
del _config_loader_main

M modules/services/networking/monitoring/bird-lg.nix => modules/services/networking/monitoring/bird-lg.nix +26 -0
@@ 11,6 11,12 @@ in
    services.bird-lg.server = {
      enable = mkEnableOption "BIRD looking glass server";

      logToSyslog = mkOption {
        description = "Whether to log to journald via syslog instead of writing to stderr.";
        type = types.bool;
        default = true;
      };

      appSettings = mkOption {
        description = "Configuration for bird-lg's server.";
        type = settingsFormat.type;


@@ 33,6 39,12 @@ in
    services.bird-lg.client = {
      enable = mkEnableOption "BIRD looking glass client proxy";

      logToSyslog = mkOption {
        description = "Whether to log to journald via syslog instead of writing to stderr.";
        type = types.bool;
        default = true;
      };

      appSettings = mkOption {
        description = "Configuration for bird-lg's client proxy.";
        type = settingsFormat.type;


@@ 70,6 82,12 @@ in
      UNIFIED_DAEMON = mkDefault true;
    };

    services.bird-lg.server.gunicornSettings = mkIf cfg.server.logToSyslog {
      errorlog = mkDefault "/dev/null";
      syslog = mkDefault true;
      syslog_addr = mkDefault "unix:///dev/log";
    };

    systemd.services.bird-lg-server = mkIf cfg.server.enable {
      description = "BIRD looking glass web server";
      requires = [ "network-online.target" ];


@@ 81,6 99,7 @@ in
          (settingsFormat.generate "bird-lg-gunicorn.json" cfg.server.gunicornSettings)
          (settingsFormat.generate "bird-lg.json" cfg.server.appSettings)
        ] ++ cfg.server.extraConfigFiles);
        BIRD_LG_SYSLOG = toString cfg.server.logToSyslog;
      };
      serviceConfig = {
        Type = "simple";


@@ 113,6 132,12 @@ in
      BIRD6_SOCKET = mkDefault "/run/bird6.ctl";
    };

    services.bird-lg.client.gunicornSettings = mkIf cfg.client.logToSyslog {
      errorlog = mkDefault "/dev/null";
      syslog = mkDefault true;
      syslog_addr = mkDefault "unix:///dev/log#dgram";
    };

    systemd.services.bird-lg-client = mkIf cfg.client.enable {
      description = "BIRD looking glass client proxy";
      requires = [ "network-online.target" ];


@@ 124,6 149,7 @@ in
          (settingsFormat.generate "bird-lgproxy-gunicorn.json" cfg.client.gunicornSettings)
          (settingsFormat.generate "bird-lgproxy.json" cfg.client.appSettings)
        ] ++ cfg.client.extraConfigFiles);
        BIRD_LG_SYSLOG = toString cfg.client.logToSyslog;
      };
      serviceConfig = {
        Type = "simple";