forked from alxhlz/hcloud-failover-keepalived
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmodule.nix
169 lines (147 loc) · 4.89 KB
/
module.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
{ config, pkgs, lib, ... }:
with lib;
let
cfg = config.services.robot-failover;
in
{
options = {
services.robot-failover = with types; {
enable = mkEnableOption "mgit hetzner robot failover";
thisRouterID = mkOption {
description = "Virtual router ID of this machine's failover IPs";
type = types.int;
};
common = {
urlFloating = mkOption {
description = "Endpoint for failover switching";
internal = true;
default = "https://robot-ws.your-server.de/failover/{}";
type = types.str;
};
ipv6Suffix = mkOption {
description = "IPv6 Suffix";
default = "2";
type = types.str;
};
floatingIPs = mkOption {
description = "Floating IPs";
type = types.listOf (types.submodule ({
options = {
ip = mkOption {
type = types.str;
description = "Floating IP without netmask and ipv6 suffix";
};
router = mkOption {
type = types.int;
description = "Virtual router id";
};
owner = mkOption {
type = types.nullOr types.int;
description = "Router id of server to which this IP belongs";
default = null;
};
};
}));
};
mainIPs = mkOption {
description = "main IPs";
type = types.attrsOf (types.submodule ({
options = {
ipv4 = mkOption {
type = types.str;
description = "Main IPv4 without netmask";
};
ipv6 = mkOption {
type = types.str;
description = "Main IPv6 without netmask and suffix";
};
};
}));
};
interface = mkOption {
description = "Interface where to assign IPs";
type = types.str;
example = "enpXYZ";
};
dummyInterface = mkOption {
description = "Dummy interface to use for L4 load-balancing";
type = types.nullOr types.str;
example = "lo";
default = null;
};
keepaliveInterface = mkOption {
description = "Interface where to broadcast keepalive messages";
type = types.str;
example = "enpXYZ";
};
extraKeepalivedOptions = mkOption {
# todo: limit to vrrp options
type = types.functionTo (types.attrsOf types.anything);
default = { thisRouterID, keepaliveInterface, router }: {};
defaultText = literalExpression "{ thisRouterID, keepaliveInterface, router }: {}";
example = literalExpression ''
{ thisRouterID, keepaliveInterface, router }: {
}
'';
description = ''
Extra options for keepalived.
'';
};
robotAuth = mkOption {
description = "Robot user:pass for all servers";
type = types.nullOr types.str;
default = null;
};
robotAuths = mkOption {
description = "Robot user:pass for individual servers";
type = types.nullOr (types.attrsOf types.str);
example = {
"1" = "...";
};
default = null;
};
};
};
};
config = mkIf (cfg.enable) {
environment.systemPackages = with pkgs; [
robot-failover
];
environment.etc."robot-failover/config.json".text = builtins.toJSON {
this_router_id = cfg.thisRouterID;
iproute2_bin = "${pkgs.iproute2}/bin/ip";
url_floating = cfg.common.urlFloating;
ipv6_suffix = cfg.common.ipv6Suffix;
floating_ips = cfg.common.floatingIPs;
main_ips = cfg.common.mainIPs;
interface = cfg.common.interface;
dummy_interface = cfg.common.dummyInterface;
# assert one of these two if not use_vlan_ips
robot_auth = cfg.common.robotAuth;
robot_auths = cfg.common.robotAuths;
};
services.keepalived = {
enable = true;
extraGlobalDefs = ''
vrrp_notify_priority_changes true
# vrrp_version 3
'';
vrrpInstances = let
uniqueRouters = unique (map (i: i.router) cfg.common.floatingIPs);
in listToAttrs (map (router: nameValuePair ("robot_${toString router}") ({
interface = cfg.common.keepaliveInterface;
state = if cfg.thisRouterID != router then "BACKUP" else "MASTER";
priority = if cfg.thisRouterID != router then router else cfg.thisRouterID + 10;
virtualRouterId = router;
extraConfig = ''
notify "${pkgs.robot-failover}/bin/robot_failover ${toString router}"
'';
} // (cfg.common.extraKeepalivedOptions {
inherit (cfg)
thisRouterID
keepaliveInterface;
inherit router;
}))) uniqueRouters);
};
};
}