Merge pull request #159187 from martinetd/logrotate

logrotate service enhancements
This commit is contained in:
Janne Heß 2022-02-23 11:24:17 +01:00 committed by GitHub
commit e5823f77b3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 89 additions and 26 deletions

View file

@ -970,6 +970,13 @@
Plugins are automatically repackaged using autoPatchelf.
</para>
</listitem>
<listitem>
<para>
<literal>services.logrotate.enable</literal> now defaults to
true if any rotate path has been defined, and some paths have
been added by default.
</para>
</listitem>
<listitem>
<para>
The <literal>zrepl</literal> package has been updated from

View file

@ -323,6 +323,9 @@ In addition to numerous new and upgraded packages, this release has the followin
- `services.mattermost.plugins` has been added to allow the declarative installation of Mattermost plugins.
Plugins are automatically repackaged using autoPatchelf.
- `services.logrotate.enable` now defaults to true if any rotate path has
been defined, and some paths have been added by default.
- The `zrepl` package has been updated from 0.4.0 to 0.5:
- The RPC protocol version was bumped; all zrepl daemons in a setup must be updated and restarted before replication can resume.

View file

@ -4,7 +4,6 @@ with lib;
let
cfg = config.services.logrotate;
inherit (config.users) groups;
pathOpts = { name, ... }: {
options = {
@ -85,10 +84,6 @@ let
};
config.name = name;
config.extraConfig = ''
missingok
notifempty
'';
};
mkConf = pathOpts: ''
@ -102,7 +97,11 @@ let
'';
paths = sortProperties (attrValues (filterAttrs (_: pathOpts: pathOpts.enable) cfg.paths));
configFile = pkgs.writeText "logrotate.conf" (concatStringsSep "\n" ((map mkConf paths) ++ [ cfg.extraConfig ]));
configFile = pkgs.writeText "logrotate.conf" (
concatStringsSep "\n" (
[ "missingok" "notifempty" cfg.extraConfig ] ++ (map mkConf paths)
)
);
in
{
@ -112,7 +111,10 @@ in
options = {
services.logrotate = {
enable = mkEnableOption "the logrotate systemd service";
enable = mkEnableOption "the logrotate systemd service" // {
default = foldr (n: a: a || n.enable) false (attrValues cfg.paths);
defaultText = literalExpression "cfg.paths != {}";
};
paths = mkOption {
type = with types; attrsOf (submodule pathOpts);
@ -163,25 +165,6 @@ in
}
) cfg.paths;
services.logrotate = {
paths = {
"/var/log/btmp" = {
frequency = mkDefault "monthly";
keep = mkDefault 1;
extraConfig = ''
create 0660 root ${groups.utmp.name}
'';
};
"/var/log/wtmp" = {
frequency = mkDefault "monthly";
keep = mkDefault 1;
extraConfig = ''
create 0664 root ${groups.utmp.name}
'';
};
};
};
systemd.services.logrotate = {
description = "Logrotate Service";
wantedBy = [ "multi-user.target" ];

View file

@ -988,5 +988,17 @@ in
nginx.gid = config.ids.gids.nginx;
};
services.logrotate.paths.nginx = mapAttrs (_: mkDefault) {
path = "/var/log/nginx/*.log";
frequency = "weekly";
keep = 26;
extraConfig = ''
compress
delaycompress
postrotate
[ ! -f /var/run/nginx/nginx.pid ] || kill -USR1 `cat /var/run/nginx/nginx.pid`
endscript
'';
};
};
}

View file

@ -1217,6 +1217,23 @@ in
boot.kernel.sysctl."kernel.pid_max" = mkIf pkgs.stdenv.is64bit (lib.mkDefault 4194304);
boot.kernelParams = optional (!cfg.enableUnifiedCgroupHierarchy) "systemd.unified_cgroup_hierarchy=0";
services.logrotate.paths = {
"/var/log/btmp" = mapAttrs (_: mkDefault) {
frequency = "monthly";
keep = 1;
extraConfig = ''
create 0660 root ${config.users.groups.utmp.name}
'';
};
"/var/log/wtmp" = mapAttrs (_: mkDefault) {
frequency = "monthly";
keep = 1;
extraConfig = ''
create 0664 root ${config.users.groups.utmp.name}
'';
};
};
};
# FIXME: Remove these eventually.

View file

@ -270,6 +270,7 @@ in
litestream = handleTest ./litestream.nix {};
locate = handleTest ./locate.nix {};
login = handleTest ./login.nix {};
logrotate = handleTest ./logrotate.nix {};
loki = handleTest ./loki.nix {};
lxd = handleTest ./lxd.nix {};
lxd-image = handleTest ./lxd-image.nix {};

35
nixos/tests/logrotate.nix Normal file
View file

@ -0,0 +1,35 @@
# Test logrotate service works and is enabled by default
import ./make-test-python.nix ({ pkgs, ...} : rec {
name = "logrotate";
meta = with pkgs.lib.maintainers; {
maintainers = [ martinetd ];
};
# default machine
machine = { ... }: {
};
testScript =
''
with subtest("whether logrotate works"):
machine.succeed(
# we must rotate once first to create logrotate stamp
"systemctl start --wait logrotate.service",
# wtmp is present in default config.
"rm -f /var/log/wtmp*",
"echo test > /var/log/wtmp",
# move into the future and rotate
"date -s 'now + 1 month + 1 day'",
# systemd will run logrotate from logrotate.timer automatically
# on date change, but if we want to wait for it to terminate
# it's easier to run again...
"systemctl start --wait logrotate.service",
# check rotate worked
"[ -e /var/log/wtmp.1 ]",
)
'';
})

View file

@ -1,6 +1,7 @@
{ lib, stdenv, fetchFromGitHub, gzip, popt, autoreconfHook
, mailutils ? null
, aclSupport ? true, acl
, nixosTests
}:
stdenv.mkDerivation rec {
@ -25,6 +26,10 @@ stdenv.mkDerivation rec {
nativeBuildInputs = [ autoreconfHook ];
buildInputs = [ popt ] ++ lib.optionals aclSupport [ acl ];
passthru.tests = {
nixos-logrotate = nixosTests.logrotate;
};
meta = with lib; {
homepage = "https://github.com/logrotate/logrotate";
description = "Rotates and compresses system logs";