-
Notifications
You must be signed in to change notification settings - Fork 2
/
cron.go
64 lines (54 loc) · 1.43 KB
/
cron.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
package main
import (
"fmt"
"log"
"github.com/robfig/cron/v3"
)
type cronClient struct {
scheduler *cron.Cron
}
func NewCron() cronClient {
c := cronClient{
scheduler: cron.New(),
}
return c
}
func (c cronClient) RunCloudflareCheck(ApiToken string, Email string, Domain string, Hosts []string) {
log.Println("Starting check...")
log.Println("Checking the current IP Address")
currentIp, err := GetCurrentIpAddress()
if err != nil {
log.Println(err)
return
}
cf := NewCloudFlareClient(ApiToken, Email)
log.Println("Checking domain information on CloudFlare")
domainDetails, err := cf.GetDomainByName(Domain)
if err != nil {
log.Println("Unable to get information from CloudFlare.")
log.Println("Double check the API Token to make sure it's valid.")
log.Println(err)
return
}
for _, host := range Hosts {
hostname := fmt.Sprintf("%v.%v", host, Domain)
log.Printf("Reviewing '%v'", hostname)
dns, err := cf.GetDnsEntriesByDomain(domainDetails.Result[0].ID, host, Domain)
if err != nil {
log.Println("failed to collect dns entry")
return
}
var result = dns.Result[0]
if result.Content != currentIp {
log.Println("IP Address no longer matches, sending an update")
err = cf.UpdateDnsEntry(domainDetails.Result[0].ID, dns, currentIp)
if err != nil {
log.Println("Failed to update the DNS record!")
}
}
}
log.Println("Done!")
}
func (c cronClient) HelloWorldJob() {
log.Print("Hello World")
}