Skip to content

Commit 1bd80f4

Browse files
committed
feat: deploy logrotate using system manager
1 parent fc77a26 commit 1bd80f4

File tree

5 files changed

+123
-1
lines changed

5 files changed

+123
-1
lines changed

nix/packages/docker-ubuntu.nix

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,10 @@ let
1414
in
1515
runCommand "ubuntu-cloudimg" { nativeBuildInputs = [ xz ]; } ''
1616
mkdir -p $out
17+
# FIXME: remove (among other things) builtin logrotate to avoid conflicts with the one set-up by system-manager
18+
# --exclude='etc/systemd/system/timers.target.wants/logrotate.timer' \
19+
# --exclude='usr/lib/systemd/system/logrotate.service' \
20+
# --exclude='usr/lib/systemd/system/logrotate.timer' \
1721
tar --exclude='dev/*' \
1822
--exclude='etc/systemd/system/network-online.target.wants/systemd-networkd-wait-online.service' \
1923
--exclude='etc/systemd/system/multi-user.target.wants/systemd-resolved.service' \

nix/systemConfigs.nix

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
{ self, inputs, ... }:
22
let
33
mkModules = system: [
4+
self.systemModules.logrotate
45
({
56
services.nginx.enable = true;
67
nixpkgs.hostPlatform = system;
8+
supabase.services.logrotate.enable = true;
79
})
810
];
911

nix/systemModules/default.nix

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
{
55
imports = [ ./tests ];
66
flake = {
7-
systemModules = { };
7+
systemModules = {
8+
logrotate = ./logrotate.nix;
9+
};
810
};
911
}

nix/systemModules/logrotate.nix

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
{
2+
lib,
3+
nixosModulesPath,
4+
config,
5+
...
6+
}:
7+
let
8+
cfg = config.supabase.services.logrotate;
9+
in
10+
{
11+
imports = map (path: nixosModulesPath + path) [
12+
# FIXME: we can't use the logrotate module from nixpkgs becauce it's defined as a no-op option in system-manager:
13+
# https://github.com/numtide/system-manager/blob/main/nix/modules/default.nix#L102-L108
14+
#
15+
# error: The option `services.logrotate' in module `/nix/store/...-source/nix/modules'
16+
# would be a parent of the following options,but its type `attribute set' does not support nested options.
17+
#
18+
# "/services/logging/logrotate.nix"
19+
];
20+
21+
options = {
22+
supabase.services.logrotate = {
23+
enable = lib.mkEnableOption "Whether to enable the logrotate systemd service.";
24+
};
25+
};
26+
27+
config = lib.mkIf cfg.enable {
28+
environment.etc = {
29+
"logrotate.d/logrotate-postgres-auth.conf".text = ''
30+
/var/log/postgresql/auth-failures.csv {
31+
size 10M
32+
rotate 5
33+
compress
34+
delaycompress
35+
notifempty
36+
missingok
37+
}
38+
'';
39+
"logrotate.d/logrotate-postgres-csv.conf".text = ''
40+
/var/log/postgresql/postgresql.csv {
41+
size 50M
42+
rotate 9
43+
compress
44+
delaycompress
45+
notifempty
46+
missingok
47+
postrotate
48+
sudo -u postgres /usr/lib/postgresql/bin/pg_ctl -D /var/lib/postgresql/data logrotate
49+
endscript
50+
}
51+
'';
52+
"logrotate.d/logrotate-postgres.conf".text = ''
53+
/var/log/postgresql/postgresql.log {
54+
size 50M
55+
rotate 3
56+
copytruncate
57+
delaycompress
58+
compress
59+
notifempty
60+
missingok
61+
}
62+
'';
63+
"logrotate.d/logrotate-walg.conf".text = ''
64+
/var/log/wal-g/*.log {
65+
size 50M
66+
rotate 3
67+
copytruncate
68+
delaycompress
69+
compress
70+
notifempty
71+
missingok
72+
}
73+
'';
74+
};
75+
76+
# FIXME: logrotate.service isn't a valid unit file (missing ExecStart), because it's already provided by Ubuntu:
77+
# systemd.services.logrotate = {
78+
# wantedBy = lib.mkForce [
79+
# "system-manager.target"
80+
# ];
81+
# };
82+
83+
# Try to overide systemd logrotate.timer to run every 5 minutes
84+
# https://umuttechin.medium.com/overriding-systemd-unit-files-on-linux-30f44d925f72
85+
systemd.timers.logrotate = {
86+
wantedBy = [ "timers.target" ];
87+
timerConfig.OnCalendar = "*:0/5";
88+
timerConfig.Persistent = true;
89+
};
90+
};
91+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
def test_logrotate_timer(host):
2+
timer = host.service("logrotate.timer")
3+
assert timer.is_enabled
4+
assert timer.is_running
5+
6+
7+
def test_logrotate_service_unit(host):
8+
svc = host.service("logrotate.service")
9+
assert svc.is_valid
10+
result = host.run("systemctl start logrotate.service")
11+
assert result.rc == 0
12+
13+
14+
def test_logrotate_configs(host):
15+
for fname in [
16+
"/etc/logrotate.d/logrotate-postgres-auth.conf",
17+
"/etc/logrotate.d/logrotate-postgres-csv.conf",
18+
"/etc/logrotate.d/logrotate-postgres.conf",
19+
"/etc/logrotate.d/logrotate-walg.conf",
20+
]:
21+
f = host.file(fname)
22+
assert f.exists
23+
assert f.user == "root"

0 commit comments

Comments
 (0)