-
Notifications
You must be signed in to change notification settings - Fork 15
/
nomad-snapshots.nix
282 lines (239 loc) · 8.64 KB
/
nomad-snapshots.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{ config, pkgs, lib, ... }:
let
inherit (lib) mkIf mkEnableOption mkOption types;
inherit (types) addCheck bool int str submodule;
cfg = config.services.nomad-snapshots;
snapshotJobConfig = submodule {
options = {
enable = mkOption {
type = bool;
default = true;
description = ''
Creates a systemd service and timer to automatically save Nomad snapshots.
'';
};
backupCount = mkOption {
type = addCheck int (x: x >= 0);
default = null;
description = ''
The number of snapshots to keep. A sensible value matched to the onCalendar
interval parameter should be used. Examples of sensible suggestions may be:
168 backupCount for "hourly" interval (1 week of backups)
30 backupCount for "daily" interval (1 month of backups)
'';
};
backupDirPrefix = mkOption {
type = str;
default = "/var/lib/private/nomad/snapshots";
description = ''
The top level location to store the snapshots. The actual storage location
of the files will be this prefix path with the snapshot job name appended,
where the job is one of "hourly", "daily" or "custom".
Therefore, saved snapshot files will be found at:
$backupDirPrefix/$job/*.snap
'';
};
backupSuffix = mkOption {
type = addCheck str (x: x != "");
default = null;
description = ''
Sets the saved snapshot filename with a descriptive suffix prior to the file
extension. This will enable selective snapshot job pruning. The form is:
nomad-$(hostname)-$(date +"%Y-%m-%d_%H%M%SZ")-$backupSuffix.snap
'';
};
fixedRandomDelay = mkOption {
type = bool;
default = true;
description = ''
Makes randomizedDelaySec fixed between service restarts if true.
This will reduce jitter and allow the interval to remain fixed,
while still allowing start time randomization to avoid leader overload.
'';
};
includeLeader = mkOption {
type = bool;
default = true;
description = ''
Whether to include the leader in the servers which will save snapshots.
This may reduce load on the leader slightly, but by default snapshot
saves are proxied through the leader anyway.
Reducing leader load from snapshots may be best done by fixed time
snapshot randomization so snapshot concurrency remains 1.
'';
};
interval = mkOption {
type = addCheck str (x: x != "");
default = null;
description = ''
The default onCalendar systemd timer string to trigger snapshot backups.
Any valid systemd OnCalendar string may be used here. Sensible
defaults for backupCount and randomizedDelaySec should match this parameter.
Examples of sensible suggestions may be:
hourly: 3600 randomizedDelaySec, 168 backupCount (1 week)
daily: 86400 randomizedDelaySec, 30 backupCount (1 month)
'';
};
randomizedDelaySec = mkOption {
type = addCheck int (x: x >= 0);
default = 0;
description = ''
A randomization period to be added to each systemd timer to avoid
leader overload. By default fixedRandomDelay will also be true to minimize
jitter and maintain fixed interval snapshots. Sensible defaults for
backupCount and randomizedDelaySec should match this parameter.
Examples of sensible suggestions may be:
3600 randomizedDelaySec for "hourly" interval (1 hr randomization)
86400 randomizedDelaySec for "daily" interval (1 day randomization)
'';
};
owner = mkOption {
type = str;
default = "root:root";
description = ''
The user and group to own the snapshot storage directory and snapshot files.
'';
};
nomadAddress = mkOption {
type = str;
default = "https://127.0.0.1:4646";
description = ''
The local nomad server address, including protocol and port.
'';
};
};
};
snapshotTimer = job: {
partOf = [ "nomad-snapshots-${job}.service" ];
timerConfig = {
OnCalendar = cfg.${job}.interval;
RandomizedDelaySec = cfg.${job}.randomizedDelaySec;
FixedRandomDelay = cfg.${job}.fixedRandomDelay;
AccuracySec = "1us";
};
wantedBy = [ "timers.target" ];
};
snapshotService = job: {
path = with pkgs; [ coreutils curl findutils gawk hostname jq nomad ];
environment = {
OWNER = cfg.${job}.owner;
BACKUP_DIR = "${cfg.${job}.backupDirPrefix}/${job}";
BACKUP_SUFFIX = "-${cfg.${job}.backupSuffix}";
INCLUDE_LEADER = lib.boolToString cfg.${job}.includeLeader;
NOMAD_ADDR = cfg.${job}.nomadAddress;
};
serviceConfig = {
Type = "oneshot";
Restart = "on-failure";
RestartSec = "30s";
ExecStart = pkgs.writeBashChecked "nomad-snapshot-${job}-script" ''
set -exuo pipefail
SNAP_NAME="$BACKUP_DIR/nomad-$(hostname)-$(date +"%Y-%m-%d_%H%M%SZ''${BACKUP_SUFFIX}").snap"
applyPerms () {
TARGET="$1"
PERMS="$2"
chown "$OWNER" "$TARGET"
chmod "$PERMS" "$TARGET"
}
checkBackupDir () {
if [ ! -d "$BACKUP_DIR" ]; then
mkdir -p "$BACKUP_DIR"
applyPerms "$BACKUP_DIR" "0700"
fi
}
exportToken () {
if [ ! -f /run/keys/nomad-snapshot-token ]; then
echo "Suitable nomad token for snapshotting not found."
echo "Ensure the appropriate token for snapshotting is available.";
exit 0;
else
set +x
NOMAD_TOKEN="$(< /run/keys/nomad-snapshot-token)"
export NOMAD_TOKEN
set -x
fi
}
isNotLeader () {
[ "$INCLUDE_LEADER" = "true" ] || \
nomad agent-info --json | jq -e '.stats.nomad.leader == "false"'
}
takeNomadSnapshot () {
nomad operator snapshot save "$SNAP_NAME"
applyPerms "$SNAP_NAME" "0400"
}
export NOMAD_ADDR
exportToken
if isNotLeader; then
checkBackupDir
takeNomadSnapshot
fi
find "$BACKUP_DIR" \
-type f \
-name "*''${BACKUP_SUFFIX}.snap" \
-printf "%T@ %p\n" \
| sort -r -n \
| tail -n +${toString (cfg.${job}.backupCount + 1)} \
| awk '{print $2}' \
| xargs -r rm
'';
};
};
in {
options = {
services.nomad-snapshots = {
enable = mkEnableOption ''
Enable Nomad snapshots.
By default hourly snapshots will be taken and stored for 1 week on each nomad server.
Modify services.nomad-snapshots.hourly options to customize or disable.
By default daily snapshots will be taken and stored for 1 month on each nomad server.
Modify services.nomad-snapshots.daily options to customize or disable.
By default customized snapshots are disabled.
Modify services.nomad-snapshots.custom options to enable and customize.
'';
hourly = mkOption {
type = snapshotJobConfig;
default = {
enable = true;
backupCount = 168;
backupSuffix = "hourly";
interval = "hourly";
randomizedDelaySec = 3600;
};
};
daily = mkOption {
type = snapshotJobConfig;
default = {
enable = true;
backupCount = 30;
backupSuffix = "daily";
interval = "daily";
randomizedDelaySec = 86400;
};
};
custom = mkOption {
type = snapshotJobConfig;
default = {
enable = false;
backupSuffix = "custom";
};
};
};
};
config = mkIf cfg.enable {
# Hourly snapshot configuration
systemd.timers.nomad-snapshots-hourly =
mkIf cfg.hourly.enable (snapshotTimer "hourly");
systemd.services.nomad-snapshots-hourly =
mkIf cfg.hourly.enable (snapshotService "hourly");
# Daily snapshot configuration
systemd.timers.nomad-snapshots-daily =
mkIf cfg.daily.enable (snapshotTimer "daily");
systemd.services.nomad-snapshots-daily =
mkIf cfg.daily.enable (snapshotService "daily");
# Custom snapshot configuration
systemd.timers.nomad-snapshots-custom =
mkIf cfg.custom.enable (snapshotTimer "custom");
systemd.services.nomad-snapshots-custom =
mkIf cfg.custom.enable (snapshotService "custom");
};
}