-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
86 lines (70 loc) · 1.61 KB
/
Copy pathmain.go
File metadata and controls
86 lines (70 loc) · 1.61 KB
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
package main
import (
"context"
"log"
"os"
"os/signal"
"syscall"
"time"
"github.com/digitalocean/godo"
"github.com/kstkn/woody/goip"
)
var (
token string
ip string
)
func update() {
// find out current ip
loc, err := goip.NewClient().GetLocation()
if err != nil {
log.Fatal(err)
}
log.Printf("current public IP: %s\n", loc.Query)
if loc.Query == ip {
// do nothing, ip hasn't changed
log.Println("public IP hasn't changed")
return
}
// store and update ip
ip = loc.Query
do := godo.NewFromToken(token)
r, _, _ := do.Domains.RecordsByTypeAndName(context.Background(), "kstkn.com", "A", "pp.kstkn.com", nil)
if len(r) != 1 {
log.Fatal("no DNS records found for update")
}
if r[0].Data == ip {
log.Println("DNS record already has correct IP")
return
}
do.Domains.EditRecord(context.Background(), "kstkn.com", r[0].ID, &godo.DomainRecordEditRequest{
Data: ip,
})
log.Println("DNS records updated")
}
func main() {
token = os.Getenv("DIGITALOCEAN_ACCESS_TOKEN")
if token == "" {
log.Fatal("Environment variable DIGITALOCEAN_ACCESS_TOKEN is not set (https://cloud.digitalocean.com/account/api/tokens)")
}
d, err := time.ParseDuration(os.Getenv("WOODY_PERIOD"))
if err != nil {
d = 5 * time.Minute
}
log.Printf("running with %s update interval\n", d)
signalNotifyContext, stop := signal.NotifyContext(context.Background(), syscall.SIGTERM, syscall.SIGINT)
defer stop()
update()
ticker := time.NewTicker(d)
tickerDone := make(chan bool)
go func() {
for {
select {
case <-tickerDone:
return
case <-ticker.C:
update()
}
}
}()
<-signalNotifyContext.Done()
}