forked from StackExchange/dnscontrol
-
Notifications
You must be signed in to change notification settings - Fork 0
/
previewPush.go
244 lines (231 loc) · 7.24 KB
/
previewPush.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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package commands
import (
"fmt"
"log"
"os"
"github.com/StackExchange/dnscontrol/models"
"github.com/StackExchange/dnscontrol/pkg/nameservers"
"github.com/StackExchange/dnscontrol/pkg/normalize"
"github.com/StackExchange/dnscontrol/pkg/notifications"
"github.com/StackExchange/dnscontrol/pkg/printer"
"github.com/StackExchange/dnscontrol/providers"
"github.com/StackExchange/dnscontrol/providers/config"
"github.com/pkg/errors"
"github.com/urfave/cli"
)
var _ = cmd(catMain, func() *cli.Command {
var args PreviewArgs
return &cli.Command{
Name: "preview",
Usage: "read live configuration and identify changes to be made, without applying them",
Action: func(ctx *cli.Context) error {
return exit(Preview(args))
},
Flags: args.flags(),
}
}())
// PreviewArgs contains all data/flags needed to run preview, independently of CLI
type PreviewArgs struct {
GetDNSConfigArgs
GetCredentialsArgs
FilterArgs
Notify bool
}
func (args *PreviewArgs) flags() []cli.Flag {
flags := args.GetDNSConfigArgs.flags()
flags = append(flags, args.GetCredentialsArgs.flags()...)
flags = append(flags, args.FilterArgs.flags()...)
flags = append(flags, cli.BoolFlag{
Name: "notify",
Destination: &args.Notify,
Usage: `set to true to send notifications to configured destinations`,
})
return flags
}
var _ = cmd(catMain, func() *cli.Command {
var args PushArgs
return &cli.Command{
Name: "push",
Usage: "identify changes to be made, and perform them",
Action: func(ctx *cli.Context) error {
return exit(Push(args))
},
Flags: args.flags(),
}
}())
// PushArgs contains all data/flags needed to run push, independently of CLI
type PushArgs struct {
PreviewArgs
Interactive bool
}
func (args *PushArgs) flags() []cli.Flag {
flags := args.PreviewArgs.flags()
flags = append(flags, cli.BoolFlag{
Name: "i",
Destination: &args.Interactive,
Usage: "Interactive. Confirm or Exclude each correction before they run",
})
return flags
}
// Preview implements the preview subcommand.
func Preview(args PreviewArgs) error {
return run(args, false, false, printer.DefaultPrinter)
}
// Push implements the push subcommand.
func Push(args PushArgs) error {
return run(args.PreviewArgs, true, args.Interactive, printer.DefaultPrinter)
}
// run is the main routine common to preview/push
func run(args PreviewArgs, push bool, interactive bool, out printer.CLI) error {
// TODO: make truly CLI independent. Perhaps return results on a channel as they occur
cfg, err := GetDNSConfig(args.GetDNSConfigArgs)
if err != nil {
return err
}
errs := normalize.NormalizeAndValidateConfig(cfg)
if PrintValidationErrors(errs) {
return errors.Errorf("Exiting due to validation errors")
}
// TODO:
notifier, err := InitializeProviders(args.CredsFile, cfg, args.Notify)
if err != nil {
return err
}
anyErrors := false
totalCorrections := 0
DomainLoop:
for _, domain := range cfg.Domains {
if !args.shouldRunDomain(domain.Name) {
continue
}
out.StartDomain(domain.Name)
nsList, err := nameservers.DetermineNameservers(domain)
if err != nil {
return err
}
domain.Nameservers = nsList
nameservers.AddNSRecords(domain)
for _, provider := range domain.DNSProviderInstances {
dc, err := domain.Copy()
if err != nil {
return err
}
shouldrun := args.shouldRunProvider(provider.Name, dc)
out.StartDNSProvider(provider.Name, !shouldrun)
if !shouldrun {
continue
}
corrections, err := provider.Driver.GetDomainCorrections(dc)
out.EndProvider(len(corrections), err)
if err != nil {
anyErrors = true
continue DomainLoop
}
totalCorrections += len(corrections)
anyErrors = printOrRunCorrections(domain.Name, provider.Name, corrections, out, push, interactive, notifier) || anyErrors
}
run := args.shouldRunProvider(domain.RegistrarName, domain)
out.StartRegistrar(domain.RegistrarName, !run)
if !run {
continue
}
if len(domain.Nameservers) == 0 && domain.Metadata["no_ns"] != "true" {
out.Warnf("No nameservers declared; skipping registrar. Add {no_ns:'true'} to force.\n")
continue
}
dc, err := domain.Copy()
if err != nil {
log.Fatal(err)
}
corrections, err := domain.RegistrarInstance.Driver.GetRegistrarCorrections(dc)
out.EndProvider(len(corrections), err)
if err != nil {
anyErrors = true
continue
}
totalCorrections += len(corrections)
anyErrors = printOrRunCorrections(domain.Name, domain.RegistrarName, corrections, out, push, interactive, notifier) || anyErrors
}
if os.Getenv("TEAMCITY_VERSION") != "" {
fmt.Fprintf(os.Stderr, "##teamcity[buildStatus status='SUCCESS' text='%d corrections']", totalCorrections)
}
notifier.Done()
out.Printf("Done. %d corrections.\n", totalCorrections)
if anyErrors {
return errors.Errorf("Completed with errors")
}
return nil
}
// InitializeProviders takes a creds file path and a DNSConfig object. Creates all providers with the proper types, and returns them.
// nonDefaultProviders is a list of providers that should not be run unless explicitly asked for by flags.
func InitializeProviders(credsFile string, cfg *models.DNSConfig, notifyFlag bool) (notify notifications.Notifier, err error) {
var providerConfigs map[string]map[string]string
var notificationCfg map[string]string
defer func() {
notify = notifications.Init(notificationCfg)
}()
providerConfigs, err = config.LoadProviderConfigs(credsFile)
if err != nil {
return
}
if notifyFlag {
notificationCfg = providerConfigs["notifications"]
}
isNonDefault := map[string]bool{}
for name, vals := range providerConfigs {
// add "_exclude_from_defaults":"true" to a provider to exclude it from being run unless
// -providers=all or -providers=name
if vals["_exclude_from_defaults"] == "true" {
isNonDefault[name] = true
}
}
registrars := map[string]providers.Registrar{}
dnsProviders := map[string]providers.DNSServiceProvider{}
for _, d := range cfg.Domains {
if registrars[d.RegistrarName] == nil {
rCfg := cfg.RegistrarsByName[d.RegistrarName]
r, err := providers.CreateRegistrar(rCfg.Type, providerConfigs[d.RegistrarName])
if err != nil {
return nil, err
}
registrars[d.RegistrarName] = r
}
d.RegistrarInstance.Driver = registrars[d.RegistrarName]
d.RegistrarInstance.IsDefault = !isNonDefault[d.RegistrarName]
for _, pInst := range d.DNSProviderInstances {
if dnsProviders[pInst.Name] == nil {
dCfg := cfg.DNSProvidersByName[pInst.Name]
prov, err := providers.CreateDNSProvider(dCfg.Type, providerConfigs[dCfg.Name], dCfg.Metadata)
if err != nil {
return nil, err
}
dnsProviders[pInst.Name] = prov
}
pInst.Driver = dnsProviders[pInst.Name]
pInst.IsDefault = !isNonDefault[pInst.Name]
}
}
return
}
func printOrRunCorrections(domain string, provider string, corrections []*models.Correction, out printer.CLI, push bool, interactive bool, notifier notifications.Notifier) (anyErrors bool) {
anyErrors = false
if len(corrections) == 0 {
return false
}
for i, correction := range corrections {
out.PrintCorrection(i, correction)
var err error
if push {
if interactive && !out.PromptToRun() {
continue
}
err = correction.F()
out.EndCorrection(err)
if err != nil {
anyErrors = true
}
}
notifier.Notify(domain, provider, correction.Msg, err, !push)
}
return anyErrors
}