-
Notifications
You must be signed in to change notification settings - Fork 0
/
fastd-peergroup-nodes.nix
153 lines (124 loc) · 4.35 KB
/
fastd-peergroup-nodes.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
{ config, pkgs, lib, ...}:
with lib;
let
cfg = config.services.fastd-peergroup-nodes;
in
{
options.services.fastd-peergroup-nodes = {
enable = mkEnableOption "fastd peergroup nodes sync service";
repoUrl = mkOption {
type = types.str;
example = "https://github.com/nix-freifunk/fastd-keys.git";
description = "The repo that should be synced.";
};
repoBranch = mkOption {
type = types.str;
default = "main";
description = "The repo branch that should be synced.";
};
reloadServices = mkOption {
type = types.listOf types.str;
default = [];
description = "The services that should be reloaded if something changed.";
};
peerDir = mkOption {
type = types.path;
default = "/var/lib/fastd/peer_groups/nodes";
description = "The path to where to keep the nodes dir.";
};
timerEnable = mkOption {
type = types.bool;
description = "fastd peergroup nodes sync timer";
default = true;
};
timerIntervall = mkOption {
type = types.str;
default = "*-*-* *:0/5:00";
description = "The intervall in which the sync should be run. Default is every 5 minutes.";
};
timerRandomDelay = mkOption {
type = types.str;
default = "4m";
description = "Add a random delay to the timer.";
};
timerFixedRandomDelay = mkOption {
type = types.bool;
default = true;
description = "Add a random delay to the timer.";
};
unitName = mkOption {
type = types.str;
default = "fastd-peergroup-nodes";
readOnly = true;
description = "The name of the periodic reload service.";
};
unitNameSetup = mkOption {
type = types.str;
default = "${cfg.unitName}-setup";
readOnly = true;
description = "The name of the service to conditionally create the peer dir.";
};
sshCommand = mkOption {
type = types.str;
default = "${pkgs.openssh}/bin/ssh";
description = "The ssh command to use.";
};
};
config = mkIf cfg.enable {
systemd.services."${cfg.unitNameSetup}" = {
serviceConfig.Type = "oneshot";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
script = ''
set -x
export GIT_COMMITTER_NAME="system"
export GIT_COMMITTER_EMAIL="info@example.org"
export GIT_SSH_COMMAND="${cfg.sshCommand}"
PEER_DIR="${cfg.peerDir}"
BIN_MKDIR=("${pkgs.coreutils}/bin/mkdir")
BIN_GIT=("${pkgs.git}/bin/git -C $PEER_DIR")
if [ ! -d "$PEER_DIR" ]; then
$BIN_MKDIR --parents $PEER_DIR
$BIN_GIT init --initial-branch=main
$BIN_GIT commit --allow-empty -m "init" --author "$GIT_COMMITTER_NAME <$GIT_COMMITTER_EMAIL>"
$BIN_GIT remote add origin ${cfg.repoUrl}
$BIN_GIT fetch origin
$BIN_GIT checkout -b master --track origin/${cfg.repoBranch}
fi
'';
};
systemd.services."${cfg.unitName}" = {
serviceConfig.Type = "oneshot";
after = [ "network-online.target" ];
wants = [ "network-online.target" ];
script = ''
set -x
export GIT_SSH_COMMAND="${cfg.sshCommand}"
BIN_GIT=("${pkgs.git}/bin/git -C ${cfg.peerDir}")
BIN_SYSTEMCTL=("${pkgs.systemd}/bin/systemctl")
BIN_ECHO=("${pkgs.coreutils}/bin/echo")
HEAD_PRE=$($BIN_GIT rev-parse HEAD || echo 0)
$BIN_GIT remote set-url origin ${cfg.repoUrl}
$BIN_GIT fetch origin --prune
$BIN_GIT branch --move --force ${cfg.repoBranch}
$BIN_GIT branch --set-upstream-to=origin/${cfg.repoBranch}
$BIN_GIT reset --hard origin/${cfg.repoBranch} --
HEAD_POST=$($BIN_GIT rev-parse HEAD)
if [ "$HEAD_PRE" != "$HEAD_POST" ]; then
$BIN_ECHO "changes detected, reloading services"
${concatMapStringsSep "\n " (service: "$BIN_SYSTEMCTL is-active --quiet ${service} && $BIN_SYSTEMCTL reload ${service}") cfg.reloadServices}
fi
exit 0
'';
};
systemd.timers."${cfg.unitName}" = mkIf cfg.timerEnable {
wantedBy = [ "timers.target" ];
partOf = [ "fastd-peergroup-nodes.service" ];
timerConfig = {
OnCalendar = [ "${cfg.timerIntervall}" ];
RandomizedDelaySec = "${cfg.timerRandomDelay}";
FixedRandomDelay = cfg.timerFixedRandomDelay;
};
};
};
}