1
0
mirror of https://github.com/StackExchange/dnscontrol.git synced 2024-05-11 05:55:12 +00:00

Refactor to use better cli command framework (#177)

* starting to refactor commands

* work

* not sure

* all commands working!

* actually add file

* work in delay flag again

* start to refactor out console printing

* i hate line endings

* simple travis test to find direct output

* remove all direct printing from push/preview

* checkin vendor

* don't need this yet

* forgot to commit these

* make version explicit command

* some code review

* Add "check" subcommand.

* move stuff to commands package

* fix

* comment out check for printlns. for now

* alphabet hax

* activedir flags gone. use creds instead

* active dir doc update

* remove bind specific flags. creds instead

* default to zones dir

* fix linux build

* fix test

* cleanup random global* vars

* Clean up PowerShell docs

* rename dump-ir to print-ir. combine with print-js
This commit is contained in:
Craig Peterson
2017-09-13 10:00:41 -04:00
committed by GitHub
parent e7006f3767
commit 1d9d2b1a19
33 changed files with 6188 additions and 391 deletions

213
commands/commands.go Normal file
View File

@ -0,0 +1,213 @@
package commands
import (
"encoding/json"
"fmt"
"os"
"sort"
"strings"
"github.com/StackExchange/dnscontrol/models"
"github.com/urfave/cli"
)
// categories of commands
const (
catMain = "\b main" // screwed up to alphebatize first
catDebug = "debug"
catUtils = "utility"
)
var commands = []cli.Command{}
var version string
func cmd(cat string, c *cli.Command) bool {
c.Category = cat
commands = append(commands, *c)
return true
}
var _ = cmd(catDebug, &cli.Command{
Name: "version",
Usage: "Print version information",
Action: func(c *cli.Context) {
fmt.Println(version)
},
})
// Run will execute the CLI
func Run(v string) error {
version = v
app := cli.NewApp()
app.Version = version
app.Name = "dnscontrol"
app.HideVersion = true
app.Usage = "dnscontrol is a compiler and DSL for managing dns zones"
sort.Sort(cli.CommandsByName(commands))
app.Commands = commands
app.EnableBashCompletion = true
app.Run(os.Args)
return nil
}
// Shared config types
// GetDNSConfigArgs contains what we need to get a valid dns config.
// Could come from parsing js, or from stored json
type GetDNSConfigArgs struct {
ExecuteDSLArgs
JSONFile string
}
func (args *GetDNSConfigArgs) flags() []cli.Flag {
return append(args.ExecuteDSLArgs.flags(),
cli.StringFlag{
Destination: &args.JSONFile,
Name: "ir",
Usage: "Read IR (json) directly from this file. Do not process DSL at all",
},
cli.StringFlag{
Destination: &args.JSONFile,
Name: "json",
Hidden: true,
Usage: "same as -ir. only here for backwards compatibility, hence hidden",
},
)
}
func GetDNSConfig(args GetDNSConfigArgs) (*models.DNSConfig, error) {
if args.JSONFile != "" {
f, err := os.Open(args.JSONFile)
if err != nil {
return nil, err
}
defer f.Close()
dec := json.NewDecoder(f)
cfg := &models.DNSConfig{}
if err = dec.Decode(cfg); err != nil {
return nil, err
}
return cfg, nil
}
return ExecuteDSL(args.ExecuteDSLArgs)
}
// ExecuteDSLArgs are used anytime we need to read and execute dnscontrol DSL
type ExecuteDSLArgs struct {
JSFile string
JSONFile string
DevMode bool
}
func (args *ExecuteDSLArgs) flags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "config",
Value: "dnsconfig.js",
Destination: &args.JSFile,
Usage: "File containing dns config in javascript DSL",
},
cli.StringFlag{
Name: "js",
Value: "dnsconfig.js",
Hidden: true,
Destination: &args.JSFile,
Usage: "same as config. for back compatibility",
},
cli.BoolFlag{
Name: "dev",
Destination: &args.DevMode,
Usage: "Use helpers.js from disk instead of embedded copy",
},
}
}
// PrintJSONArgs are used anytime a command may print some json
type PrintJSONArgs struct {
Pretty bool
Output string
}
func (args *PrintJSONArgs) flags() []cli.Flag {
return []cli.Flag{
cli.BoolFlag{
Name: "pretty",
Destination: &args.Pretty,
Usage: "Pretty print IR JSON",
},
cli.StringFlag{
Name: "out",
Destination: &args.Output,
Usage: "File to write IR JSON to (default stdout)",
},
}
}
type GetCredentialsArgs struct {
CredsFile string
}
func (args *GetCredentialsArgs) flags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "creds",
Destination: &args.CredsFile,
Usage: "Provider credentials JSON file",
Value: "creds.json",
},
}
}
type FilterArgs struct {
Providers string
Domains string
}
func (args *FilterArgs) flags() []cli.Flag {
return []cli.Flag{
cli.StringFlag{
Name: "providers",
Destination: &args.Providers,
Usage: `Providers to enable (comma separated list); default is all. Can exclude individual providers from default by adding '"_exclude_from_defaults": "true"' to the credentials file for a provider`,
Value: "",
},
cli.StringFlag{
Name: "domains",
Destination: &args.Domains,
Usage: `Comma separated list of domain names to include`,
Value: "",
},
}
}
func (args *FilterArgs) shouldRunProvider(p string, dc *models.DomainConfig, nonDefaultProviders []string) bool {
if args.Providers == "all" {
return true
}
if args.Providers == "" {
for _, pr := range nonDefaultProviders {
if pr == p {
return false
}
}
return true
}
for _, prov := range strings.Split(args.Providers, ",") {
if prov == p {
return true
}
}
return false
}
func (args *FilterArgs) shouldRunDomain(d string) bool {
if args.Domains == "" {
return true
}
for _, dom := range strings.Split(args.Domains, ",") {
if dom == d {
return true
}
}
return false
}

62
commands/createDomains.go Normal file
View File

@ -0,0 +1,62 @@
package commands
import (
"fmt"
"log"
"github.com/StackExchange/dnscontrol/providers"
"github.com/urfave/cli"
)
var _ = cmd(catUtils, func() *cli.Command {
var args CreateDomainsArgs
return &cli.Command{
Name: "create-domains",
Usage: "ensures that all domains in your configuration are present in all providers.",
Action: func(ctx *cli.Context) error {
return exit(CreateDomains(args))
},
Flags: args.flags(),
}
}())
type CreateDomainsArgs struct {
GetDNSConfigArgs
GetCredentialsArgs
}
func (args *CreateDomainsArgs) flags() []cli.Flag {
flags := args.GetDNSConfigArgs.flags()
flags = append(flags, args.GetCredentialsArgs.flags()...)
return flags
}
func CreateDomains(args CreateDomainsArgs) error {
cfg, err := GetDNSConfig(args.GetDNSConfigArgs)
if err != nil {
return err
}
registrars, dnsProviders, _, err := InitializeProviders(args.CredsFile, cfg)
if err != nil {
return err
}
fmt.Printf("Initialized %d registrars and %d dns service providers.\n", len(registrars), len(dnsProviders))
for _, domain := range cfg.Domains {
fmt.Println("*** ", domain.Name)
for prov := range domain.DNSProviders {
dsp, ok := dnsProviders[prov]
if !ok {
log.Fatalf("DSP %s not declared.", prov)
}
if creator, ok := dsp.(providers.DomainCreator); ok {
fmt.Println(" -", prov)
// TODO: maybe return bool if it did anything.
err := creator.EnsureDomainExists(domain.Name)
if err != nil {
fmt.Printf("Error creating domain: %s\n", err)
}
}
}
}
return nil
}

224
commands/previewPush.go Normal file
View File

@ -0,0 +1,224 @@
package commands
import (
"fmt"
"log"
"os"
"time"
"github.com/StackExchange/dnscontrol/models"
"github.com/StackExchange/dnscontrol/pkg/nameservers"
"github.com/StackExchange/dnscontrol/pkg/normalize"
"github.com/StackExchange/dnscontrol/pkg/printer"
"github.com/StackExchange/dnscontrol/providers"
"github.com/StackExchange/dnscontrol/providers/config"
"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
Delay int
}
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.IntFlag{
Name: "d",
Destination: &args.Delay,
Usage: "delay between domains to avoid rate limits (in ms)",
})
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(),
}
}())
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
}
func Preview(args PreviewArgs) error {
return run(args, false, false, printer.ConsolePrinter{})
}
func Push(args PushArgs) error {
return run(args.PreviewArgs, true, args.Interactive, printer.ConsolePrinter{})
}
// 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 fmt.Errorf("Exiting due to validation errors")
}
registrars, dnsProviders, nonDefaultProviders, err := InitializeProviders(args.CredsFile, cfg)
if err != nil {
return err
}
out.Debugf("Initialized %d registrars and %d dns service providers.\n", len(registrars), len(dnsProviders))
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, 0, dnsProviders)
if err != nil {
return err
}
domain.Nameservers = nsList
nameservers.AddNSRecords(domain)
for prov := range domain.DNSProviders {
dc, err := domain.Copy()
if err != nil {
return err
}
shouldrun := args.shouldRunProvider(prov, dc, nonDefaultProviders)
out.StartDNSProvider(prov, !shouldrun)
if !shouldrun {
continue
}
// TODO: make provider discovery like this a validate-time operation
dsp, ok := dnsProviders[prov]
if !ok {
log.Fatalf("DSP %s not declared.", prov)
}
corrections, err := dsp.GetDomainCorrections(dc)
out.EndProvider(len(corrections), err)
if err != nil {
anyErrors = true
continue DomainLoop
}
totalCorrections += len(corrections)
anyErrors = printOrRunCorrections(corrections, out, push, interactive) || anyErrors
}
run := args.shouldRunProvider(domain.Registrar, domain, nonDefaultProviders)
out.StartRegistrar(domain.Registrar, !run)
if !run {
continue
}
reg, ok := registrars[domain.Registrar]
if !ok {
log.Fatalf("Registrar %s not declared.", reg)
}
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 := reg.GetRegistrarCorrections(dc)
out.EndProvider(len(corrections), err)
if err != nil {
anyErrors = true
continue
}
totalCorrections += len(corrections)
anyErrors = printOrRunCorrections(corrections, out, push, interactive) || anyErrors
if args.Delay != 0 {
time.Sleep(time.Duration(args.Delay) * time.Millisecond)
}
}
if os.Getenv("TEAMCITY_VERSION") != "" {
fmt.Fprintf(os.Stderr, "##teamcity[buildStatus status='SUCCESS' text='%d corrections']", totalCorrections)
}
out.Debugf("Done. %d corrections.\n", totalCorrections)
if anyErrors {
return fmt.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) (registrars map[string]providers.Registrar, dnsProviders map[string]providers.DNSServiceProvider, nonDefaultProviders []string, err error) {
var providerConfigs map[string]map[string]string
providerConfigs, err = config.LoadProviderConfigs(credsFile)
if err != nil {
return
}
nonDefaultProviders = []string{}
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" {
nonDefaultProviders = append(nonDefaultProviders, name)
}
}
registrars, err = providers.CreateRegistrars(cfg, providerConfigs)
if err != nil {
return
}
dnsProviders, err = providers.CreateDsps(cfg, providerConfigs)
if err != nil {
return
}
return
}
func printOrRunCorrections(corrections []*models.Correction, out printer.CLI, push bool, interactive bool) (anyErrors bool) {
anyErrors = false
if len(corrections) == 0 {
return false
}
for i, correction := range corrections {
out.PrintCorrection(i, correction)
if push {
if interactive && !out.PromptToRun() {
continue
}
err := correction.F()
out.EndCorrection(err)
if err != nil {
anyErrors = true
}
}
}
return anyErrors
}

132
commands/printIR.go Normal file
View File

@ -0,0 +1,132 @@
package commands
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"github.com/StackExchange/dnscontrol/models"
"github.com/StackExchange/dnscontrol/pkg/js"
"github.com/StackExchange/dnscontrol/pkg/normalize"
"github.com/urfave/cli"
)
var _ = cmd(catDebug, func() *cli.Command {
var args PrintIRArgs
return &cli.Command{
Name: "print-ir",
Usage: "Output intermediate representation (IR) after running validation and normalization logic.",
Action: func(c *cli.Context) error {
return exit(PrintIR(args))
},
Flags: args.flags(),
}
}())
var _ = cmd(catDebug, func() *cli.Command {
var args PrintIRArgs
// This is the same as print-ir but output defaults to /dev/null.
return &cli.Command{
Name: "check",
Usage: "Check and validate dnsconfig.js. Do not access providers.",
Action: func(c *cli.Context) error {
if args.Output == "" {
args.Output = os.DevNull
}
return exit(PrintIR(args))
},
Flags: args.flags(),
}
}())
type PrintIRArgs struct {
GetDNSConfigArgs
PrintJSONArgs
Raw bool
}
func (args *PrintIRArgs) flags() []cli.Flag {
flags := append(args.GetDNSConfigArgs.flags(), args.PrintJSONArgs.flags()...)
flags = append(flags, &cli.BoolFlag{
Name: "raw",
Usage: "Skip validation and normalization. Just print js result.",
Destination: &args.Raw,
})
return flags
}
func PrintIR(args PrintIRArgs) error {
cfg, err := GetDNSConfig(args.GetDNSConfigArgs)
if err != nil {
return err
}
if !args.Raw {
errs := normalize.NormalizeAndValidateConfig(cfg)
if PrintValidationErrors(errs) {
return fmt.Errorf("Exiting due to validation errors")
}
}
return PrintJSON(args.PrintJSONArgs, cfg)
}
func PrintValidationErrors(errs []error) (fatal bool) {
if len(errs) == 0 {
return false
}
fmt.Printf("%d Validation errors:\n", len(errs))
for _, err := range errs {
if _, ok := err.(normalize.Warning); ok {
fmt.Printf("WARNING: %s\n", err)
} else {
fatal = true
fmt.Printf("ERROR: %s\n", err)
}
}
return
}
func ExecuteDSL(args ExecuteDSLArgs) (*models.DNSConfig, error) {
if args.JSFile == "" {
return nil, fmt.Errorf("No config specified")
}
text, err := ioutil.ReadFile(args.JSFile)
if err != nil {
return nil, fmt.Errorf("Reading js file %s: %s", args.JSFile, err)
}
dnsConfig, err := js.ExecuteJavascript(string(text), args.DevMode)
if err != nil {
return nil, fmt.Errorf("Executing javascript in %s: %s", args.JSFile, err)
}
return dnsConfig, nil
}
func PrintJSON(args PrintJSONArgs, config *models.DNSConfig) (err error) {
var dat []byte
if args.Pretty {
dat, err = json.MarshalIndent(config, "", " ")
} else {
dat, err = json.Marshal(config)
}
if err != nil {
return err
}
if args.Output != "" {
f, err := os.Create(args.Output)
if err != nil {
return err
}
defer f.Close()
_, err = f.Write(dat)
return err
}
fmt.Println(string(dat))
return nil
}
func exit(err error) error {
if err == nil {
return nil
}
return cli.NewExitError(err, 1)
}