-
Notifications
You must be signed in to change notification settings - Fork 0
/
keepalived.go
141 lines (128 loc) · 4.74 KB
/
keepalived.go
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
package main
import (
"fmt"
"log"
"bytes"
"text/template"
)
type File struct {
DN string `ldap:"dn"`
Path string `ldap:"path"`
Description string `ldap:"description"`
CN string `ldap:"cn"`
ObjectClass []string `ldap:"objectClass"`
Data string `ldap:"data"`
Perm string `ldap:"permissions"`
}
type Kalived struct {
Path string `ldap:"path"`
Perm string `ldap:"permissions"`
ServiceName string `ldap:"serviceName"`
GlobalNotificationEmail []string `ldap:"globalNotificationEmail"`
GlobalNotificationEmailFrom string `ldap:"globalNotificationEmailFrom"`
GlobalSMTPServer string `ldap:"globalSMTPServer"`
GlobalSMTPConnectTimeout int `ldap:"globalSMTPConnectTimeout"`
GlobalLVSId string `ldap:"globalLVSId"`
GroupName string `ldap:"groupName"`
GroupMember []string `ldap:"groupMember"`
NotifyMasterVRRPGroup string `ldap:"notifyMasterVRRPGroup"`
NotifyBackupVRRPGroup string `ldap:"notifyBackupVRRPGroup"`
NotifyFaultVRRPGroup string `ldap:"notifyFaultVRRPGroup"`
InstanceName string `ldap:"instanceName"`
/* Need to treat next value as string since
entry.Unmarshal method doesn't support bool */
SMTPAlert string `ldap:"smtpAlert"`
AuthType string `ldap:"authType"`
AuthPass string `ldap:"authPass"`
VirtualIPAddress []string `ldap:"virtualIPAddress"`
VirtualIPAddressExcluded []string `ldap:"virtualIPAddressExcluded"`
State string `ldap:"state"`
Interface string `ldap:"interface"`
McastSrcIP string `ldap:"mcastSrcIP"`
LVSSyncDaemonInterface string `ldap:"lvsSyncDaemonInterface"`
VirtualRouterID int `ldap:"virtualRouterID"`
Priority int `ldap:"priority"`
AdvertInt int `ldap:"advertInt"`
UnicastSrcIP string `ldap:"unicastSrcIP"`
UnicastPeer []string `ldap:"unicastPeer"`
NotifyMasterVRRPInstance string `ldap:"notifyMasterVRRPInstance"`
NotifyBackupVRRPInstance string `ldap:"notifyBackupVRRPInstance"`
NotifyFaultVRRPInstance string `ldap:"notifyFaultVRRPInstance"`
}
func FormatKeepalived(inter any, class string) error {
f := inter.(Kalived)
var tmpl *template.Template
var err error
switch class {
case "global":
tmpl, err = template.New("kglobal").Parse(`
global_defs {
{{if .GlobalNotificationEmail}}notification_email {
{{range .GlobalNotificationEmail}}{{.}}
{{end}}
}{{end}}
{{if .GlobalNotificationEmail}}notification_email_from {{.GlobalNotificationEmailFrom}}{{end}}
{{if .GlobalSMTPServer}}smtp_server {{.GlobalSMTPServer}}{{end}}
{{if .GlobalSMTPConnectTimeout}}smtp_connect_timeout {{.GlobalSMTPConnectTimeout}}{{end}}
{{if .GlobalLVSId}}lvs_id {{.GlobalLVSId}}{{end}}
}
`)
case "group":
tmpl, err = template.New("kgroup").Parse(`
vrrp_sync_group {{.GroupName}} {
group {
{{range .GroupMember}}{{.}}
{{end}}
}
{{if .NotifyMasterVRRPGroup}}notify_master {{.NotifyMasterVRRPGroup}}
{{end}}{{if .NotifyBackupVRRPGroup}}notify_backup {{.NotifyBackupVRRPGroup}}
{{end}}{{if .NotifyFaultVRRPGroup}}notify_fault {{.NotifyFaultVRRPGroup}}{{end}}
}
`)
case "instance":
tmpl, err = template.New("kinstance").Parse(`
vrrp_instance {{.InstanceName}} {
state {{.State}}
interface {{.Interface}}
{{if .McastSrcIP}}mcast_src_ip {{.McastSrcIP}}{{end}}
{{if .UnicastSrcIP}}unicast_src_ip {{.UnicastSrcIP}}
unicast_peer {
{{range .UnicastPeer}}{{.}}
{{end -}}
}{{end}}
{{if .LVSSyncDaemonInterface}}lvs_sync_daemon_interface {{.LVSSyncDaemonInterface}}{{end}}
virtual_router_id {{.VirtualRouterID}}
priority {{.Priority}}
{{if .AdvertInt}}advert_int {{.AdvertInt}}{{end}}
{{if .SMTPAlert}}smtp_alert {{.SMTPAlert}}{{end}}
authentication {
auth_type {{.AuthType}}
auth_pass {{.AuthPass}}
}
virtual_ipaddress {
{{range .VirtualIPAddress}}{{.}}
{{end -}}
}
{{if .VirtualIPAddressExcluded}}}virtual_ipaddress_excluded {
{{range .VirtualIPAddressExcluded}}{{.}}
{{end -}}
}{{end}}
{{if .NotifyMasterVRRPInstance}}notify_master {{.NotifyMasterVRRPInstance}}
{{end}}{{if .NotifyBackupVRRPInstance}}notify_backup {{.NotifyBackupVRRPInstance}}
{{end}}{{if .NotifyFaultVRRPInstance}}notify_fault {{.NotifyFaultVRRPInstance}}{{end}}
}
`)
default:
return fmt.Errorf("Template not found: %s\n", class)
}
if err != nil {
log.Fatalf("Template creation error: %v\n", err)
}
var newData bytes.Buffer
err = tmpl.Execute(&newData, f)
if err != nil {
log.Fatalf("Template creation error: %v\n", err)
}
KeepalivedFiles[f.Path] = KeepalivedFiles[f.Path] + newData.String()
return nil
}