Skip to content

Commit eb81078

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

File tree

11 files changed

+141
-58
lines changed

11 files changed

+141
-58
lines changed

.editorconfig

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# EditorConfig is awesome: https://EditorConfig.org
2+
3+
# top-most EditorConfig file
4+
root = true
5+
6+
[*.nix]
7+
indent_style = space
8+
indent_size = 2
9+
end_of_line = lf
10+
charset = utf-8
11+
trim_trailing_whitespace = true
12+
insert_final_newline = true

ansible/files/logrotate_config/logrotate-postgres-auth.conf

Lines changed: 0 additions & 8 deletions
This file was deleted.

ansible/files/logrotate_config/logrotate-postgres-csv.conf

Lines changed: 0 additions & 11 deletions
This file was deleted.

ansible/files/logrotate_config/logrotate-postgres.conf

Lines changed: 0 additions & 9 deletions
This file was deleted.

ansible/files/logrotate_config/logrotate-walg.conf

Lines changed: 0 additions & 9 deletions
This file was deleted.

ansible/tasks/finalize-ami.yml

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -41,18 +41,6 @@
4141
policy: deny
4242
direction: incoming
4343

44-
- name: Move logrotate files to /etc/logrotate.d/
45-
copy:
46-
src: "files/logrotate_config/{{ item.file }}"
47-
dest: "/etc/logrotate.d/{{ item.file }}"
48-
mode: "0700"
49-
owner: root
50-
loop:
51-
- { file: "logrotate-postgres-csv.conf" }
52-
- { file: "logrotate-postgres.conf" }
53-
- { file: "logrotate-walg.conf" }
54-
- { file: "logrotate-postgres-auth.conf" }
55-
5644
- name: Ensure default Postgres logrotate config is removed
5745
file:
5846
path: /etc/logrotate.d/postgresql-common
@@ -63,14 +51,6 @@
6351
src: files/cron.deny
6452
dest: /etc/cron.deny
6553

66-
- name: Configure logrotation to run every hour
67-
shell:
68-
cmd: |
69-
cp /usr/lib/systemd/system/logrotate.timer /etc/systemd/system/logrotate.timer
70-
sed -i -e 's;daily;*:0/5;' /etc/systemd/system/logrotate.timer
71-
systemctl reenable logrotate.timer
72-
become: yes
73-
7454
- name: import pgsodium_getkey script
7555
template:
7656
src: files/pgsodium_getkey_readonly.sh.j2

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: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
# Overide systemd logrotate.timer to run every 5 minutes:
84+
systemd.timers.logrotate = {
85+
wantedBy = [ "timers.target" ];
86+
timerConfig.OnCalendar = "*:0/5";
87+
timerConfig.Persistent = true;
88+
};
89+
};
90+
}

0 commit comments

Comments
 (0)