forked from lucasgolino/cloudflare-zone-manager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathczm.go
93 lines (72 loc) · 1.69 KB
/
czm.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
package czm
import (
"github.com/quan-to/slog"
"time"
)
type CloudflareZoneManager struct {
ConfigMap ConfigMap
SLog *slog.Instance
Srv struct {
CF *Cloudflare
Reporter string
Mods Modules
}
}
func (e *CloudflareZoneManager) Init() {
e.SLog = slog.Scope(`CZM`)
e.ConfigMap.ReadConfigMap()
e.InitServices()
for true {
e.VerifyAndUpdateZones()
time.Sleep(time.Second * 60)
}
}
func (e *CloudflareZoneManager) InitServices() {
e.Srv.CF = e.ConfigMap.Cloudflare.Initialize()
e.Srv.Mods.LoadAllModules()
}
func (e *CloudflareZoneManager) VerifyAndUpdateZones() {
var zones = &e.ConfigMap.Zones
for _, zone := range *zones {
zoneLog := e.SLog.SubScope(zone.Hostname)
if !e.Srv.CF.ExistsZone(&zone) {
continue
}
e.Srv.CF.LoadDNSRecords(&zone)
for _, dns := range zone.Dns {
dns.Module.Mods = &e.Srv.Mods // append address to dns mods
exists := e.Srv.CF.ExistsDNSRecord(&zone, &dns)
rules := Rules{
dns.Rules.NotExist,
dns.Rules.Update,
}
if !exists {
if rules.VerifyRule(RULES_NEXISTS_TAG) {
zoneLog.SubScope(dns.Name).Warn(`DNS Zone dosen't exists... skip`)
continue
}
err := e.Srv.CF.CreateDNSRecord(&zone, &dns)
if err != nil {
e.SLog.Error(err)
continue
}
continue
}
// Resolve content of DNS with Modules
if dns.Content == "" {
dns.Content = dns.Module.Resolve()
}
if e.Srv.CF.DNSRecordHasDiff(&zone, &dns) {
if rules.VerifyRule(RULES_UPDATE_TAG) {
zoneLog.SubScope(dns.Name).Warn(`DNS Zone has diff but rule say is mark to skip`)
continue
}
err := e.Srv.CF.UpdateDNSRecord(&zone, &dns)
if err != nil {
e.SLog.Error(err)
continue
}
}
}
}
}