Skip to content

Commit

Permalink
Add build tags for additional output on call failure
Browse files Browse the repository at this point in the history
  • Loading branch information
JonasProgrammer committed Aug 13, 2022
1 parent 913904b commit 983b950
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 6 deletions.
16 changes: 10 additions & 6 deletions driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,10 @@ func (d *Driver) GetCreateFlags() []mcnflag.Flag {
// SetConfigFromFlags handles additional driver arguments as retrieved by [Driver.GetCreateFlags];
// see [drivers.Driver.SetConfigFromFlags]
func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
return d.setConfigFromFlags(opts)
}

func (d *Driver) setConfigFromFlagsImpl(opts drivers.DriverOptions) error {
d.AccessToken = opts.String(flagAPIToken)
d.Image = opts.String(flagImage)
d.ImageID = opts.Int(flagImageID)
Expand All @@ -267,7 +271,7 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
d.placementGroup = opts.String(flagPlacementGroup)
if opts.Bool(flagAutoSpread) {
if d.placementGroup != "" {
return errors.Errorf(flagAutoSpread + " and " + flagPlacementGroup + " are mutually exclusive")
return d.flagFailure("%v and %v are mutually exclusive", flagAutoSpread, flagPlacementGroup)
}
d.placementGroup = autoSpreadPgName
}
Expand All @@ -280,17 +284,17 @@ func (d *Driver) SetConfigFromFlags(opts drivers.DriverOptions) error {
d.SetSwarmConfigFromFlags(opts)

if d.AccessToken == "" {
return errors.Errorf("hetzner requires --%v to be set", flagAPIToken)
return d.flagFailure("hetzner requires --%v to be set", flagAPIToken)
}

if d.ImageID != 0 && d.Image != "" && d.Image != defaultImage /* support legacy behaviour */ {
return errors.Errorf("--%v and --%v are mutually exclusive", flagImage, flagImageID)
return d.flagFailure("--%v and --%v are mutually exclusive", flagImage, flagImageID)
} else if d.ImageID == 0 && d.Image == "" {
d.Image = defaultImage
}

if d.DisablePublic4 && d.DisablePublic6 && !d.UsePrivateNetwork {
return errors.Errorf("--%v must be used if public networking is disabled (hint: implicitly set by --%v)",
return d.flagFailure("--%v must be used if public networking is disabled (hint: implicitly set by --%v)",
flagUsePrivateNetwork, flagDisablePublic)
}

Expand All @@ -312,7 +316,7 @@ func (d *Driver) setLabelsFromFlags(opts drivers.DriverOptions) error {
for _, label := range opts.StringSlice(flagServerLabel) {
split := strings.SplitN(label, "=", 2)
if len(split) != 2 {
return errors.Errorf("server label %v is not in key=value format", label)
return d.flagFailure("server label %v is not in key=value format", label)
}
d.ServerLabels[split[0]] = split[1]
}
Expand All @@ -331,7 +335,7 @@ func (d *Driver) setLabelsFromFlags(opts drivers.DriverOptions) error {
func (d *Driver) PreCreateCheck() error {
if d.IsExistingKey {
if d.originalKey == "" {
return errors.New("specifying an existing key ID requires the existing key path to be set as well")
return d.flagFailure("specifying an existing key ID requires the existing key path to be set as well")
}

key, err := d.getKey()
Expand Down
16 changes: 16 additions & 0 deletions flag_failure.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//go:build !flag_debug

package main

import (
"github.com/docker/machine/libmachine/drivers"
"github.com/pkg/errors"
)

func (d *Driver) flagFailure(format string, args ...interface{}) error {
return errors.Errorf(format, args...)
}

func (d *Driver) setConfigFromFlags(opts drivers.DriverOptions) error {
return d.setConfigFromFlagsImpl(opts)
}
32 changes: 32 additions & 0 deletions flag_failure_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
//go:build flag_debug

package main

import (
"encoding/json"
"fmt"

"github.com/docker/machine/libmachine/drivers"
"github.com/pkg/errors"
)

var lastOpts drivers.DriverOptions

func (d *Driver) flagFailure(format string, args ...interface{}) error {
// machine driver may not flush logs received when getting an RPC error, so we have to resort to this terribleness
line1 := fmt.Sprintf("Flag failure detected:\n -> last opts: %v\n -> driver state %v", lastOpts, d)
var line2 string
if out, err := json.MarshalIndent(d, "", " "); err == nil {
line2 = fmt.Sprintf(" -> driver json:\n%s", out)
} else {
line2 = fmt.Sprintf("could not encode driver json: %v", err)
}

combined := append([]interface{}{line1, line2}, args...)
return errors.Errorf("%s\n%s\n"+format, combined...)
}

func (d *Driver) setConfigFromFlags(opts drivers.DriverOptions) error {
lastOpts = opts
return d.setConfigFromFlagsImpl(opts)
}

0 comments on commit 983b950

Please sign in to comment.