2017-09-13 22:00:41 +08:00
|
|
|
package commands
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
|
|
|
"os"
|
|
|
|
|
2020-02-04 01:44:11 +08:00
|
|
|
"github.com/urfave/cli/v2"
|
Switch to Go 1.13 error wrapping (#604)
* Replaced errors.Wrap with fmt.Errorf (#589)
* Find: errors\.Wrap\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Replaced errors.Wrapf with fmt.Errorf (#589)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])\)
Replace: fmt.Errorf($2: %w$3, $1)
* Find: errors\.Wrapf\(([^,]+),\s+(["`][^"`]*)(["`])(,[^)]+)\)
* Replace: fmt.Errorf($2: %w$3$4, $1)
* Replaced errors.Errorf with fmt.Errorf (#589)
* Find: errors\.Errorf
Replace: fmt.Errorf
* Cleaned up remaining imports
* Cleanup
* Regenerate provider support matrix
This was broken by #533 ... and it's now the third time this has been missed.
2020-01-29 00:06:56 +08:00
|
|
|
|
2020-04-15 04:47:30 +08:00
|
|
|
"github.com/StackExchange/dnscontrol/v3/models"
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/pkg/nameservers"
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/pkg/normalize"
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/pkg/notifications"
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/pkg/printer"
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/providers"
|
|
|
|
"github.com/StackExchange/dnscontrol/v3/providers/config"
|
2017-09-13 22:00:41 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
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
|
2019-06-27 13:21:23 +08:00
|
|
|
Notify bool
|
2019-02-14 09:11:26 +08:00
|
|
|
WarnChanges bool
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
func (args *PreviewArgs) flags() []cli.Flag {
|
|
|
|
flags := args.GetDNSConfigArgs.flags()
|
|
|
|
flags = append(flags, args.GetCredentialsArgs.flags()...)
|
|
|
|
flags = append(flags, args.FilterArgs.flags()...)
|
2020-02-04 01:44:11 +08:00
|
|
|
flags = append(flags, &cli.BoolFlag{
|
2018-01-12 00:15:19 +08:00
|
|
|
Name: "notify",
|
|
|
|
Destination: &args.Notify,
|
|
|
|
Usage: `set to true to send notifications to configured destinations`,
|
|
|
|
})
|
2020-02-04 01:44:11 +08:00
|
|
|
flags = append(flags, &cli.BoolFlag{
|
2019-02-14 09:11:26 +08:00
|
|
|
Name: "expect-no-changes",
|
|
|
|
Destination: &args.WarnChanges,
|
|
|
|
Usage: `set to true for non-zero return code if there are changes`,
|
|
|
|
})
|
2017-09-13 22:00:41 +08:00
|
|
|
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(),
|
|
|
|
}
|
|
|
|
}())
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// PushArgs contains all data/flags needed to run push, independently of CLI
|
2017-09-13 22:00:41 +08:00
|
|
|
type PushArgs struct {
|
|
|
|
PreviewArgs
|
|
|
|
Interactive bool
|
|
|
|
}
|
|
|
|
|
|
|
|
func (args *PushArgs) flags() []cli.Flag {
|
|
|
|
flags := args.PreviewArgs.flags()
|
2020-02-04 01:44:11 +08:00
|
|
|
flags = append(flags, &cli.BoolFlag{
|
2017-09-13 22:00:41 +08:00
|
|
|
Name: "i",
|
|
|
|
Destination: &args.Interactive,
|
|
|
|
Usage: "Interactive. Confirm or Exclude each correction before they run",
|
|
|
|
})
|
|
|
|
return flags
|
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// Preview implements the preview subcommand.
|
2017-09-13 22:00:41 +08:00
|
|
|
func Preview(args PreviewArgs) error {
|
2018-10-09 04:10:44 +08:00
|
|
|
return run(args, false, false, printer.DefaultPrinter)
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
|
|
|
|
2018-01-10 01:53:16 +08:00
|
|
|
// Push implements the push subcommand.
|
2017-09-13 22:00:41 +08:00
|
|
|
func Push(args PushArgs) error {
|
2018-10-09 04:10:44 +08:00
|
|
|
return run(args.PreviewArgs, true, args.Interactive, printer.DefaultPrinter)
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|
2020-03-11 04:53:17 +08:00
|
|
|
errs := normalize.ValidateAndNormalizeConfig(cfg)
|
2017-09-13 22:00:41 +08:00
|
|
|
if PrintValidationErrors(errs) {
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("exiting due to validation errors")
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
2018-02-02 00:45:53 +08:00
|
|
|
// TODO:
|
|
|
|
notifier, err := InitializeProviders(args.CredsFile, cfg, args.Notify)
|
2017-09-13 22:00:41 +08:00
|
|
|
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)
|
2018-02-02 00:45:53 +08:00
|
|
|
nsList, err := nameservers.DetermineNameservers(domain)
|
2017-09-13 22:00:41 +08:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
domain.Nameservers = nsList
|
|
|
|
nameservers.AddNSRecords(domain)
|
2018-02-02 00:45:53 +08:00
|
|
|
for _, provider := range domain.DNSProviderInstances {
|
2017-09-13 22:00:41 +08:00
|
|
|
dc, err := domain.Copy()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2018-02-02 00:45:53 +08:00
|
|
|
shouldrun := args.shouldRunProvider(provider.Name, dc)
|
|
|
|
out.StartDNSProvider(provider.Name, !shouldrun)
|
2017-09-13 22:00:41 +08:00
|
|
|
if !shouldrun {
|
|
|
|
continue
|
|
|
|
}
|
2018-02-02 00:45:53 +08:00
|
|
|
corrections, err := provider.Driver.GetDomainCorrections(dc)
|
2017-09-13 22:00:41 +08:00
|
|
|
out.EndProvider(len(corrections), err)
|
|
|
|
if err != nil {
|
|
|
|
anyErrors = true
|
|
|
|
continue DomainLoop
|
|
|
|
}
|
|
|
|
totalCorrections += len(corrections)
|
2018-02-02 00:45:53 +08:00
|
|
|
anyErrors = printOrRunCorrections(domain.Name, provider.Name, corrections, out, push, interactive, notifier) || anyErrors
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
2018-02-02 00:45:53 +08:00
|
|
|
run := args.shouldRunProvider(domain.RegistrarName, domain)
|
|
|
|
out.StartRegistrar(domain.RegistrarName, !run)
|
2017-09-13 22:00:41 +08:00
|
|
|
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)
|
|
|
|
}
|
2018-02-02 00:45:53 +08:00
|
|
|
corrections, err := domain.RegistrarInstance.Driver.GetRegistrarCorrections(dc)
|
2017-09-13 22:00:41 +08:00
|
|
|
out.EndProvider(len(corrections), err)
|
|
|
|
if err != nil {
|
|
|
|
anyErrors = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
totalCorrections += len(corrections)
|
2018-02-02 00:45:53 +08:00
|
|
|
anyErrors = printOrRunCorrections(domain.Name, domain.RegistrarName, corrections, out, push, interactive, notifier) || anyErrors
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
|
|
|
if os.Getenv("TEAMCITY_VERSION") != "" {
|
|
|
|
fmt.Fprintf(os.Stderr, "##teamcity[buildStatus status='SUCCESS' text='%d corrections']", totalCorrections)
|
|
|
|
}
|
2018-01-12 00:15:19 +08:00
|
|
|
notifier.Done()
|
2018-10-09 04:10:44 +08:00
|
|
|
out.Printf("Done. %d corrections.\n", totalCorrections)
|
2017-09-13 22:00:41 +08:00
|
|
|
if anyErrors {
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("completed with errors")
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
2019-02-14 09:11:26 +08:00
|
|
|
if totalCorrections != 0 && args.WarnChanges {
|
2020-08-31 07:52:37 +08:00
|
|
|
return fmt.Errorf("there are pending changes")
|
2019-02-14 09:11:26 +08:00
|
|
|
}
|
2017-09-13 22:00:41 +08:00
|
|
|
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.
|
2018-02-02 00:45:53 +08:00
|
|
|
func InitializeProviders(credsFile string, cfg *models.DNSConfig, notifyFlag bool) (notify notifications.Notifier, err error) {
|
2017-09-13 22:00:41 +08:00
|
|
|
var providerConfigs map[string]map[string]string
|
2018-01-12 00:15:19 +08:00
|
|
|
var notificationCfg map[string]string
|
|
|
|
defer func() {
|
|
|
|
notify = notifications.Init(notificationCfg)
|
|
|
|
}()
|
2017-09-13 22:00:41 +08:00
|
|
|
providerConfigs, err = config.LoadProviderConfigs(credsFile)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
2018-01-12 00:15:19 +08:00
|
|
|
if notifyFlag {
|
|
|
|
notificationCfg = providerConfigs["notifications"]
|
|
|
|
}
|
2018-02-02 00:45:53 +08:00
|
|
|
isNonDefault := map[string]bool{}
|
2017-09-13 22:00:41 +08:00
|
|
|
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" {
|
2018-02-02 00:45:53 +08:00
|
|
|
isNonDefault[name] = true
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
|
|
|
}
|
2018-02-02 00:45:53 +08:00
|
|
|
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]
|
|
|
|
}
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-01-12 00:15:19 +08:00
|
|
|
func printOrRunCorrections(domain string, provider string, corrections []*models.Correction, out printer.CLI, push bool, interactive bool, notifier notifications.Notifier) (anyErrors bool) {
|
2017-09-13 22:00:41 +08:00
|
|
|
anyErrors = false
|
|
|
|
if len(corrections) == 0 {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
for i, correction := range corrections {
|
|
|
|
out.PrintCorrection(i, correction)
|
2018-01-12 00:15:19 +08:00
|
|
|
var err error
|
2017-09-13 22:00:41 +08:00
|
|
|
if push {
|
|
|
|
if interactive && !out.PromptToRun() {
|
|
|
|
continue
|
|
|
|
}
|
2018-01-12 00:15:19 +08:00
|
|
|
err = correction.F()
|
2017-09-13 22:00:41 +08:00
|
|
|
out.EndCorrection(err)
|
|
|
|
if err != nil {
|
|
|
|
anyErrors = true
|
|
|
|
}
|
|
|
|
}
|
2018-01-12 00:15:19 +08:00
|
|
|
notifier.Notify(domain, provider, correction.Msg, err, !push)
|
2017-09-13 22:00:41 +08:00
|
|
|
}
|
|
|
|
return anyErrors
|
|
|
|
}
|