-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
201 lines (178 loc) · 4.6 KB
/
main.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
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
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package main
import (
"flag"
"fmt"
"strings"
"github.com/ovh/go-ovh/ovh"
log "github.com/sirupsen/logrus"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/TheoBrigitte/ovh-dyndns/pkg/lookup/hostname"
"github.com/TheoBrigitte/ovh-dyndns/pkg/lookup/ip"
"github.com/TheoBrigitte/ovh-dyndns/pkg/ovh/client"
)
const (
configFileName = "ovh-dyndns"
)
var (
Version = "n/a"
)
func main() {
var (
zone string
recordType string
generateConsumerKey bool
subDomains []string
err error
)
{
// Define and process arguments.
flag.Bool("consumer-key", false, "generate a consumer key")
flag.String("zone", "", "DNS zone name (e.g. example.com)")
flag.String("record-type", "", "DNS record typ (e.g. CNAME)")
flag.String("subdomains", "", "DNS sub domain filter. Multiple entries separated by comma (e.g. www,ftp)")
flag.Bool("version", false, "print version and exit")
pflag.CommandLine.AddGoFlagSet(flag.CommandLine)
pflag.Parse()
viper.BindPFlags(pflag.CommandLine)
if viper.GetBool("version") {
fmt.Printf("version: %s\n", Version)
return
}
// Load config file
viper.SetConfigName(configFileName)
viper.AddConfigPath("/etc/ovh-dyndns")
viper.AddConfigPath("$HOME/")
viper.AddConfigPath(".")
err = viper.ReadInConfig()
if err != nil {
log.Fatalf("viper.ReadInConfig %v", err)
return
}
zone = viper.GetString("zone")
recordType = strings.ToUpper(viper.GetString("record-type"))
generateConsumerKey = viper.GetBool("consumer-key")
subDomains = strings.Split(viper.GetString("subdomains"), ",")
log.WithFields(log.Fields{
"zone": zone,
"record-type": recordType,
"subdomains": subDomains,
"generateConsumerKey": generateConsumerKey,
}).Info("arguments")
}
// Create client for OVH API.
var ovhClient client.Client
{
// Create a client using credentials from config files or environment variables
c, err := ovh.NewDefaultClient()
if err != nil {
log.Fatalf("ovh.NewDefaultClient %v", err)
return
}
ovhClient = client.New(*c)
}
if generateConsumerKey {
// Generate customer key.
fields := log.Fields{
"status": "generating",
}
log.WithFields(fields).Info("customer key")
res, err := ovhClient.GenerateConsumerKey()
if err != nil {
log.Fatalf("ovhClient.GenerateConsumerKey %v", err)
return
}
fields["status"] = "done"
log.WithFields(fields).Info("customer key")
log.Printf("response\n%v", res)
return
}
// Read public IP adresse.
var ipAdresse string
{
fields := log.Fields{
"status": "reading",
}
log.WithFields(fields).Info("ip adresse")
ipAdresse, err = ip.GetPublic()
if err != nil {
log.Fatalf("ip.GetPublic %v", err)
return
}
fields["status"] = "done"
fields["ip"] = ipAdresse
log.WithFields(fields).Info("ip adresse")
}
// Lookup hostname.
var hostnames []string
{
if recordType == "CNAME" {
fields := log.Fields{
"status": "reading",
}
log.WithFields(fields).Info("hostname")
hostnames, err = hostname.Get(ipAdresse)
if err != nil {
log.Fatalf("hostname.Get %v", err)
return
}
fields["status"] = "done"
fields["hostname"] = hostnames[0]
log.WithFields(fields).Info("hostname")
}
}
// Update dns records.
for _, domain := range subDomains {
// Find the dns record.
fields := log.Fields{
"status": "finding",
"domain": domain,
"zone": zone,
"recordType": recordType,
}
log.WithFields(fields).Info("dns record")
id, err := ovhClient.FindRecord(zone, recordType, domain)
if err != nil {
log.Fatalf("ovhClient.FindRecord %v", err)
return
}
fields["ID"] = id
fields["status"] = "reading"
log.WithFields(fields).Info("dns record")
// Read the dns record
record, err := ovhClient.GetRecord(zone, id)
if err != nil {
log.Fatalf("ovhClient.GetRecord %v", err)
return
}
fields["record"] = record
fields["status"] = "updating"
log.WithFields(fields).Info("dns record")
// Update the dns record
var target string
switch record.FieldType {
case "CNAME":
target = hostnames[0]
case "A":
target = ipAdresse
default:
log.Fatalf("unsuported record type: %q", record.FieldType)
return
}
// Skip update when target is the same.
if record.Target == target {
fields["status"] = "ok"
log.WithFields(fields).Info("dns record")
continue
}
record.Target = target
err = ovhClient.UpdateRecord(zone, id, *record)
if err != nil {
log.Fatalf("ovhClient.UpdateRecord %v", err)
return
}
fields["status"] = "updated"
log.WithFields(fields).Info("dns record")
}
log.Info("success")
}