From c023dd1449ceb26f9dbbb3a5c83f54bc644f09c0 Mon Sep 17 00:00:00 2001 From: JonasS Date: Fri, 15 Dec 2023 16:14:43 +0100 Subject: [PATCH] chore: Switch to std lib errors package --- driver/cleanup.go | 8 +++--- driver/driver.go | 42 ++++++++++++++-------------- driver/driver_test.go | 7 +++-- driver/flag_failure.go | 5 ++-- driver/flag_failure_debug.go | 3 +- driver/flag_processing.go | 9 +++--- driver/hetzner_query.go | 50 ++++++++++++++++++++++++++-------- driver/instrumentation_impl.go | 3 +- driver/networking.go | 8 +++--- driver/placement_groups.go | 1 + driver/setup.go | 31 +++++++++++---------- driver/ssh_keys.go | 34 +++++++++++------------ go.mod | 1 - go.sum | 2 -- 14 files changed, 117 insertions(+), 87 deletions(-) diff --git a/driver/cleanup.go b/driver/cleanup.go index f72280b..98685b2 100644 --- a/driver/cleanup.go +++ b/driver/cleanup.go @@ -3,9 +3,9 @@ package driver import ( "context" "fmt" + "github.com/docker/machine/libmachine/log" "github.com/hetznercloud/hcloud-go/v2/hcloud" - "github.com/pkg/errors" ) func (d *Driver) destroyDangling() { @@ -44,7 +44,7 @@ func (d *Driver) destroyServer() error { srv, err := d.getServerHandleNullable() if err != nil { - return errors.Wrap(err, "could not get server handle") + return fmt.Errorf("could not get server handle: %w", err) } if srv == nil { @@ -54,7 +54,7 @@ func (d *Driver) destroyServer() error { res, _, err := d.getClient().Server.DeleteWithResult(context.Background(), srv) if err != nil { - return errors.Wrap(err, "could not delete server") + return fmt.Errorf("could not delete server: %w", err) } // failure to remove a placement group is not a hard error @@ -64,7 +64,7 @@ func (d *Driver) destroyServer() error { // wait for the server to actually be deleted if err = d.waitForAction(res.Action); err != nil { - return errors.Wrap(err, "could not wait for deletion") + return fmt.Errorf("could not wait for deletion: %w", err) } } diff --git a/driver/driver.go b/driver/driver.go index 49666ec..5003188 100644 --- a/driver/driver.go +++ b/driver/driver.go @@ -2,6 +2,7 @@ package driver import ( "context" + "errors" "fmt" "net" "strconv" @@ -12,7 +13,6 @@ import ( "github.com/docker/machine/libmachine/mcnflag" "github.com/docker/machine/libmachine/state" "github.com/hetznercloud/hcloud-go/v2/hcloud" - "github.com/pkg/errors" ) // Driver contains hetzner-specific data to implement [drivers.Driver] @@ -328,7 +328,7 @@ func flagI64(opts drivers.DriverOptions, key string) (int64, error) { ret, err := strconv.ParseInt(raw, 10, 64) if err != nil { - return 0, errors.Wrapf(err, "could not parse int64 for %v", key) + return 0, fmt.Errorf("could not parse int64 for %v: %w", key, err) } return ret, nil @@ -439,17 +439,17 @@ func (d *Driver) PreCreateCheck() error { } if serverType, err := d.getType(); err != nil { - return errors.Wrap(err, "could not get type") + return fmt.Errorf("could not get type: %w", err) } else if d.ImageArch != "" && serverType.Architecture != d.ImageArch { log.Warnf("supplied architecture %v differs from server architecture %v", d.ImageArch, serverType.Architecture) } if _, err := d.getImage(); err != nil { - return errors.Wrap(err, "could not get image") + return fmt.Errorf("could not get image: %w", err) } if _, err := d.getLocationNullable(); err != nil { - return errors.Wrap(err, "could not get location") + return fmt.Errorf("could not get location: %w", err) } if _, err := d.getPlacementGroup(); err != nil { @@ -465,7 +465,7 @@ func (d *Driver) PreCreateCheck() error { } if d.UsePrivateNetwork && len(d.Networks) == 0 { - return errors.Errorf("No private network attached.") + return fmt.Errorf("no private network attached") } return nil @@ -494,12 +494,12 @@ func (d *Driver) Create() error { srv, _, err := d.getClient().Server.Create(context.Background(), instrumented(*srvopts)) if err != nil { time.Sleep(time.Duration(d.WaitOnError) * time.Second) - return errors.Wrap(err, "could not create server") + return fmt.Errorf("could not create server: %w", err) } log.Infof(" -> Creating server %s[%d] in %s[%d]", srv.Server.Name, srv.Server.ID, srv.Action.Command, srv.Action.ID) if err = d.waitForAction(srv.Action); err != nil { - return errors.Wrap(err, "could not wait for action") + return fmt.Errorf("could not wait for action: %w", err) } d.ServerID = srv.Server.ID @@ -530,12 +530,12 @@ func (d *Driver) GetSSHHostname() (string, error) { // GetURL retrieves the URL of the docker daemon on the machine; see [drivers.Driver.GetURL] func (d *Driver) GetURL() (string, error) { if err := drivers.MustBeRunning(d); err != nil { - return "", errors.Wrap(err, "could not execute drivers.MustBeRunning") + return "", fmt.Errorf("could not execute drivers.MustBeRunning: %w", err) } ip, err := d.GetIP() if err != nil { - return "", errors.Wrap(err, "could not get IP") + return "", fmt.Errorf("could not get IP: %w", err) } return fmt.Sprintf("tcp://%s", net.JoinHostPort(ip, "2376")), nil @@ -545,7 +545,7 @@ func (d *Driver) GetURL() (string, error) { func (d *Driver) GetState() (state.State, error) { srv, _, err := d.getClient().Server.GetByID(context.Background(), d.ServerID) if err != nil { - return state.None, errors.Wrap(err, "could not get server by ID") + return state.None, fmt.Errorf("could not get server by ID: %w", err) } if srv == nil { return state.None, errors.New("server not found") @@ -588,7 +588,7 @@ func (d *Driver) Remove() error { if !d.IsExistingKey && d.KeyID != 0 { key, err := d.getKeyNullable() if err != nil { - return errors.Wrap(err, "could not get ssh key") + return fmt.Errorf("could not get ssh key: %w", err) } if key == nil { log.Infof(" -> SSH key does not exist anymore") @@ -598,7 +598,7 @@ func (d *Driver) Remove() error { log.Infof(" -> Destroying SSHKey %s[%d]...", key.Name, key.ID) if _, err := d.getClient().SSHKey.Delete(context.Background(), key); err != nil { - return errors.Wrap(err, "could not delete ssh key") + return fmt.Errorf("could not delete ssh key: %w", err) } } @@ -609,7 +609,7 @@ func (d *Driver) Remove() error { func (d *Driver) Restart() error { srv, err := d.getServerHandle() if err != nil { - return errors.Wrap(err, "could not get server handle") + return fmt.Errorf("could not get server handle: %w", err) } if srv == nil { return errors.New("server not found") @@ -617,7 +617,7 @@ func (d *Driver) Restart() error { act, _, err := d.getClient().Server.Reboot(context.Background(), srv) if err != nil { - return errors.Wrap(err, "could not reboot server") + return fmt.Errorf("could not reboot server: %w", err) } log.Infof(" -> Rebooting server %s[%d] in %s[%d]...", srv.Name, srv.ID, act.Command, act.ID) @@ -629,12 +629,12 @@ func (d *Driver) Restart() error { func (d *Driver) Start() error { srv, err := d.getServerHandle() if err != nil { - return errors.Wrap(err, "could not get server handle") + return fmt.Errorf("could not get server handle: %w", err) } act, _, err := d.getClient().Server.Poweron(context.Background(), srv) if err != nil { - return errors.Wrap(err, "could not power on server") + return fmt.Errorf("could not power on server: %w", err) } log.Infof(" -> Starting server %s[%d] in %s[%d]...", srv.Name, srv.ID, act.Command, act.ID) @@ -646,12 +646,12 @@ func (d *Driver) Start() error { func (d *Driver) Stop() error { srv, err := d.getServerHandle() if err != nil { - return errors.Wrap(err, "could not get server handle") + return fmt.Errorf("could not get server handle: %w", err) } act, _, err := d.getClient().Server.Shutdown(context.Background(), srv) if err != nil { - return errors.Wrap(err, "could not shutdown server") + return fmt.Errorf("could not shutdown server: %w", err) } log.Infof(" -> Shutting down server %s[%d] in %s[%d]...", srv.Name, srv.ID, act.Command, act.ID) @@ -663,12 +663,12 @@ func (d *Driver) Stop() error { func (d *Driver) Kill() error { srv, err := d.getServerHandle() if err != nil { - return errors.Wrap(err, "could not get server handle") + return fmt.Errorf("could not get server handle: %w", err) } act, _, err := d.getClient().Server.Poweroff(context.Background(), srv) if err != nil { - return errors.Wrap(err, "could not poweroff server") + return fmt.Errorf("could not poweroff server: %w", err) } log.Infof(" -> Powering off server %s[%d] in %s[%d]...", srv.Name, srv.ID, act.Command, act.ID) diff --git a/driver/driver_test.go b/driver/driver_test.go index 05d1680..7828fc0 100644 --- a/driver/driver_test.go +++ b/driver/driver_test.go @@ -1,13 +1,14 @@ package driver import ( - "github.com/docker/machine/commands/commandstest" - "github.com/docker/machine/libmachine/drivers" - "github.com/hetznercloud/hcloud-go/v2/hcloud" "os" "strconv" "strings" "testing" + + "github.com/docker/machine/commands/commandstest" + "github.com/docker/machine/libmachine/drivers" + "github.com/hetznercloud/hcloud-go/v2/hcloud" ) var defaultFlags = map[string]interface{}{ diff --git a/driver/flag_failure.go b/driver/flag_failure.go index f2a43f7..d5fb659 100644 --- a/driver/flag_failure.go +++ b/driver/flag_failure.go @@ -3,12 +3,13 @@ package driver import ( + "fmt" + "github.com/docker/machine/libmachine/drivers" - "github.com/pkg/errors" ) func (d *Driver) flagFailure(format string, args ...interface{}) error { - return errors.Errorf(format, args...) + return fmt.Errorf(format, args...) } func (d *Driver) setConfigFromFlags(opts drivers.DriverOptions) error { diff --git a/driver/flag_failure_debug.go b/driver/flag_failure_debug.go index d4b533b..1e86e86 100644 --- a/driver/flag_failure_debug.go +++ b/driver/flag_failure_debug.go @@ -7,7 +7,6 @@ import ( "fmt" "github.com/docker/machine/libmachine/drivers" - "github.com/pkg/errors" ) var lastOpts drivers.DriverOptions @@ -23,7 +22,7 @@ func (d *Driver) flagFailure(format string, args ...interface{}) error { } combined := append([]interface{}{line1, line2}, args...) - return errors.Errorf("%s\n%s\n"+format, combined...) + return fmt.Errorf("%s\n%s\n"+format, combined...) } func (d *Driver) setConfigFromFlags(opts drivers.DriverOptions) error { diff --git a/driver/flag_processing.go b/driver/flag_processing.go index 4e7622f..8d4a604 100644 --- a/driver/flag_processing.go +++ b/driver/flag_processing.go @@ -1,11 +1,12 @@ package driver import ( + "fmt" + "strings" + "github.com/docker/machine/libmachine/drivers" "github.com/docker/machine/libmachine/log" "github.com/hetznercloud/hcloud-go/v2/hcloud" - "github.com/pkg/errors" - "strings" ) var legacyDefaultImages = [...]string{ @@ -33,7 +34,7 @@ func (d *Driver) setImageArch(arch string) error { case string(hcloud.ArchitectureX86): d.ImageArch = hcloud.ArchitectureX86 default: - return errors.Errorf("unknown architecture %v", arch) + return fmt.Errorf("unknown architecture %v", arch) } return nil } @@ -112,7 +113,7 @@ func (d *Driver) setLabelsFromFlags(opts drivers.DriverOptions) error { for _, label := range opts.StringSlice(flagKeyLabel) { split := strings.SplitN(label, "=", 2) if len(split) != 2 { - return errors.Errorf("key label %v is not in key=value format", label) + return fmt.Errorf("key label %v is not in key=value format", label) } d.keyLabels[split[0]] = split[1] } diff --git a/driver/hetzner_query.go b/driver/hetzner_query.go index 9d9ca7e..9b419d1 100644 --- a/driver/hetzner_query.go +++ b/driver/hetzner_query.go @@ -2,12 +2,13 @@ package driver import ( "context" + "errors" "fmt" + "time" + "github.com/docker/machine/libmachine/log" "github.com/hetznercloud/hcloud-go/v2/hcloud" - "github.com/pkg/errors" "golang.org/x/crypto/ssh" - "time" ) func (d *Driver) getClient() *hcloud.Client { @@ -32,7 +33,7 @@ func (d *Driver) getLocationNullable() (*hcloud.Location, error) { location, _, err := d.getClient().Location.GetByName(context.Background(), d.Location) if err != nil { - return nil, errors.Wrap(err, "could not get location by name") + return nil, fmt.Errorf("could not get location by name: %w", err) } if location == nil { return nil, fmt.Errorf("unknown location: %v", d.Location) @@ -48,7 +49,7 @@ func (d *Driver) getType() (*hcloud.ServerType, error) { stype, _, err := d.getClient().ServerType.GetByName(context.Background(), d.Type) if err != nil { - return nil, errors.Wrap(err, "could not get type by name") + return nil, fmt.Errorf("could not get type by name: %w", err) } if stype == nil { return nil, fmt.Errorf("unknown server type: %v", d.Type) @@ -68,7 +69,7 @@ func (d *Driver) getImage() (*hcloud.Image, error) { if d.ImageID != 0 { image, _, err = d.getClient().Image.GetByID(context.Background(), d.ImageID) if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("could not get image by id %v", d.ImageID)) + return nil, fmt.Errorf("could not get image by id %v: %w", d.ImageID, err) } if image == nil { return nil, fmt.Errorf("image id not found: %v", d.ImageID) @@ -76,12 +77,12 @@ func (d *Driver) getImage() (*hcloud.Image, error) { } else { arch, err := d.getImageArchitectureForLookup() if err != nil { - return nil, errors.Wrap(err, "could not determine image architecture") + return nil, fmt.Errorf("could not determine image architecture: %w", err) } image, _, err = d.getClient().Image.GetByNameAndArchitecture(context.Background(), d.Image, arch) if err != nil { - return nil, errors.Wrap(err, fmt.Sprintf("could not get image by name %v", d.Image)) + return nil, fmt.Errorf("could not get image by name %v: %w", d.Image, err) } if image == nil { return nil, fmt.Errorf("image not found: %v[%v]", d.Image, arch) @@ -123,7 +124,7 @@ func (d *Driver) getKeyNullable() (*hcloud.SSHKey, error) { key, _, err := d.getClient().SSHKey.GetByID(context.Background(), d.KeyID) if err != nil { - return nil, errors.Wrap(err, "could not get sshkey by ID") + return nil, fmt.Errorf("could not get sshkey by ID: %w", err) } d.cachedKey = key return instrumented(key), nil @@ -132,14 +133,14 @@ func (d *Driver) getKeyNullable() (*hcloud.SSHKey, error) { func (d *Driver) getRemoteKeyWithSameFingerprintNullable(publicKeyBytes []byte) (*hcloud.SSHKey, error) { publicKey, _, _, _, err := ssh.ParseAuthorizedKey(publicKeyBytes) if err != nil { - return nil, errors.Wrap(err, "could not parse ssh public key") + return nil, fmt.Errorf("could not parse ssh public key: %w", err) } fp := ssh.FingerprintLegacyMD5(publicKey) remoteKey, _, err := d.getClient().SSHKey.GetByFingerprint(context.Background(), fp) if err != nil { - return remoteKey, errors.Wrap(err, "could not get sshkey by fingerprint") + return remoteKey, fmt.Errorf("could not get sshkey by fingerprint: %w", err) } return instrumented(remoteKey), nil } @@ -166,7 +167,7 @@ func (d *Driver) getServerHandleNullable() (*hcloud.Server, error) { srv, _, err := d.getClient().Server.GetByID(context.Background(), d.ServerID) if err != nil { - return nil, errors.Wrap(err, "could not get client by ID") + return nil, fmt.Errorf("could not get client by ID: %w", err) } d.cachedServer = srv @@ -195,3 +196,30 @@ func (d *Driver) waitForAction(a *hcloud.Action) error { return ret } + +func (d *Driver) waitForMultipleActions(step string, a []*hcloud.Action) error { + ctx, cancel := context.WithCancel(context.Background()) + defer cancel() + progress, watchErr := d.getClient().Action.WatchOverallProgress(ctx, a) + + running := true + var ret error + + for running { + select { + case <-watchErr: + ret = errors.Join(ret, <-watchErr) + cancel() + case <-progress: + log.Debugf(" -> %s: %d %%", step, <-progress) + default: + running = false + } + } + + if ret == nil { + log.Debugf(" -> finished %s", step) + } + + return ret +} diff --git a/driver/instrumentation_impl.go b/driver/instrumentation_impl.go index 52b8002..a1e1915 100644 --- a/driver/instrumentation_impl.go +++ b/driver/instrumentation_impl.go @@ -4,10 +4,11 @@ package driver import ( "encoding/json" - "github.com/hetznercloud/hcloud-go/v2/hcloud" "os" "runtime/debug" + "github.com/hetznercloud/hcloud-go/v2/hcloud" + "github.com/docker/machine/libmachine/log" ) diff --git a/driver/networking.go b/driver/networking.go index 9fd9322..6039ce1 100644 --- a/driver/networking.go +++ b/driver/networking.go @@ -3,11 +3,11 @@ package driver import ( "context" "fmt" - "github.com/docker/machine/libmachine/log" - "github.com/hetznercloud/hcloud-go/v2/hcloud" - "github.com/pkg/errors" "net" "time" + + "github.com/docker/machine/libmachine/log" + "github.com/hetznercloud/hcloud-go/v2/hcloud" ) func (d *Driver) getPrimaryIPv4() (*hcloud.PrimaryIP, error) { @@ -87,7 +87,7 @@ func (d *Driver) configureNetworkAccess(srv hcloud.ServerCreateResult) error { log.Infof("Wait until private network attached ...") server, _, err := d.getClient().Server.GetByID(context.Background(), srv.Server.ID) if err != nil { - return errors.Wrapf(err, "could not get newly created server [%d]", srv.Server.ID) + return fmt.Errorf("could not get newly created server [%d]: %w", srv.Server.ID, err) } if server.PrivateNet != nil { d.IPAddress = server.PrivateNet[0].IP.String() diff --git a/driver/placement_groups.go b/driver/placement_groups.go index c063977..ad314d3 100644 --- a/driver/placement_groups.go +++ b/driver/placement_groups.go @@ -3,6 +3,7 @@ package driver import ( "context" "fmt" + "github.com/docker/machine/libmachine/log" "github.com/hetznercloud/hcloud-go/v2/hcloud" ) diff --git a/driver/setup.go b/driver/setup.go index bf71756..d24710e 100644 --- a/driver/setup.go +++ b/driver/setup.go @@ -2,11 +2,12 @@ package driver import ( "context" - "github.com/docker/machine/libmachine/state" - "github.com/hetznercloud/hcloud-go/v2/hcloud" - "github.com/pkg/errors" + "fmt" "os" "time" + + "github.com/docker/machine/libmachine/state" + "github.com/hetznercloud/hcloud-go/v2/hcloud" ) func (d *Driver) waitForRunningServer() error { @@ -14,7 +15,7 @@ func (d *Driver) waitForRunningServer() error { for { srvstate, err := d.GetState() if err != nil { - return errors.Wrap(err, "could not get state") + return fmt.Errorf("could not get state: %w", err) } if srvstate == state.Running { @@ -23,7 +24,7 @@ func (d *Driver) waitForRunningServer() error { elapsed_time := time.Since(start_time).Seconds() if d.WaitForRunningTimeout > 0 && int(elapsed_time) > d.WaitForRunningTimeout { - return errors.Errorf("server exceeded wait-for-running-timeout.") + return fmt.Errorf("server exceeded wait-for-running-timeout") } time.Sleep(time.Duration(d.WaitOnPolling) * time.Second) @@ -73,17 +74,17 @@ func (d *Driver) makeCreateServerOptions() (*hcloud.ServerCreateOpts, error) { srvopts.Volumes = volumes if srvopts.Location, err = d.getLocationNullable(); err != nil { - return nil, errors.Wrap(err, "could not get location") + return nil, fmt.Errorf("could not get location: %w", err) } if srvopts.ServerType, err = d.getType(); err != nil { - return nil, errors.Wrap(err, "could not get type") + return nil, fmt.Errorf("could not get type: %w", err) } if srvopts.Image, err = d.getImage(); err != nil { - return nil, errors.Wrap(err, "could not get image") + return nil, fmt.Errorf("could not get image: %w", err) } key, err := d.getKey() if err != nil { - return nil, errors.Wrap(err, "could not get ssh key") + return nil, fmt.Errorf("could not get ssh key: %w", err) } srvopts.SSHKeys = append(d.cachedAdditionalKeys, key) return &srvopts, nil @@ -107,10 +108,10 @@ func (d *Driver) createNetworks() ([]*hcloud.Network, error) { for _, networkIDorName := range d.Networks { network, _, err := d.getClient().Network.Get(context.Background(), networkIDorName) if err != nil { - return nil, errors.Wrap(err, "could not get network by ID or name") + return nil, fmt.Errorf("could not get network by ID or name: %w", err) } if network == nil { - return nil, errors.Errorf("network '%s' not found", networkIDorName) + return nil, fmt.Errorf("network '%s' not found", networkIDorName) } networks = append(networks, network) } @@ -122,10 +123,10 @@ func (d *Driver) createFirewalls() ([]*hcloud.ServerCreateFirewall, error) { for _, firewallIDorName := range d.Firewalls { firewall, _, err := d.getClient().Firewall.Get(context.Background(), firewallIDorName) if err != nil { - return nil, errors.Wrap(err, "could not get firewall by ID or name") + return nil, fmt.Errorf("could not get firewall by ID or name: %w", err) } if firewall == nil { - return nil, errors.Errorf("firewall '%s' not found", firewallIDorName) + return nil, fmt.Errorf("firewall '%s' not found", firewallIDorName) } firewalls = append(firewalls, &hcloud.ServerCreateFirewall{Firewall: *firewall}) } @@ -137,10 +138,10 @@ func (d *Driver) createVolumes() ([]*hcloud.Volume, error) { for _, volumeIDorName := range d.Volumes { volume, _, err := d.getClient().Volume.Get(context.Background(), volumeIDorName) if err != nil { - return nil, errors.Wrap(err, "could not get volume by ID or name") + return nil, fmt.Errorf("could not get volume by ID or name: %w", err) } if volume == nil { - return nil, errors.Errorf("volume '%s' not found", volumeIDorName) + return nil, fmt.Errorf("volume '%s' not found", volumeIDorName) } volumes = append(volumes, volume) } diff --git a/driver/ssh_keys.go b/driver/ssh_keys.go index 6fdaefb..129f24d 100644 --- a/driver/ssh_keys.go +++ b/driver/ssh_keys.go @@ -3,13 +3,13 @@ package driver import ( "context" "fmt" + "os" + "github.com/docker/machine/libmachine/log" "github.com/docker/machine/libmachine/mcnutils" mcnssh "github.com/docker/machine/libmachine/ssh" "github.com/hetznercloud/hcloud-go/v2/hcloud" - "github.com/pkg/errors" "golang.org/x/crypto/ssh" - "os" ) func (d *Driver) setupExistingKey() error { @@ -23,23 +23,23 @@ func (d *Driver) setupExistingKey() error { key, err := d.getKey() if err != nil { - return errors.Wrap(err, "could not get key") + return fmt.Errorf("could not get key: %w", err) } buf, err := os.ReadFile(d.originalKey + ".pub") if err != nil { - return errors.Wrap(err, "could not read public key") + return fmt.Errorf("could not read public key: %w", err) } // Will also parse `ssh-rsa w309jwf0e39jf asdf` public keys pubk, _, _, _, err := ssh.ParseAuthorizedKey(buf) if err != nil { - return errors.Wrap(err, "could not parse authorized key") + return fmt.Errorf("could not parse authorized key: %w", err) } if key.Fingerprint != ssh.FingerprintLegacyMD5(pubk) && key.Fingerprint != ssh.FingerprintSHA256(pubk) { - return errors.Errorf("remote key %d does not match local key %s", d.KeyID, d.originalKey) + return fmt.Errorf("remote key %d does not match local key %s", d.KeyID, d.originalKey) } return nil @@ -47,15 +47,15 @@ func (d *Driver) setupExistingKey() error { func (d *Driver) copySSHKeyPair(src string) error { if err := mcnutils.CopyFile(src, d.GetSSHKeyPath()); err != nil { - return errors.Wrap(err, "could not copy ssh key") + return fmt.Errorf("could not copy ssh key: %w", err) } if err := mcnutils.CopyFile(src+".pub", d.GetSSHKeyPath()+".pub"); err != nil { - return errors.Wrap(err, "could not copy ssh public key") + return fmt.Errorf("could not copy ssh public key: %w", err) } if err := os.Chmod(d.GetSSHKeyPath(), 0600); err != nil { - return errors.Wrap(err, "could not set permissions on the ssh key") + return fmt.Errorf("could not set permissions on the ssh key: %w", err) } return nil @@ -67,12 +67,12 @@ func (d *Driver) createRemoteKeys() error { buf, err := os.ReadFile(d.GetSSHKeyPath() + ".pub") if err != nil { - return errors.Wrap(err, "could not read ssh public key") + return fmt.Errorf("could not read ssh public key: %w", err) } key, err := d.getRemoteKeyWithSameFingerprintNullable(buf) if err != nil { - return errors.Wrap(err, "error retrieving potentially existing key") + return fmt.Errorf("error retrieving potentially existing key: %w", err) } if key == nil { log.Infof("SSH key not found in Hetzner. Uploading...") @@ -91,14 +91,14 @@ func (d *Driver) createRemoteKeys() error { for i, pubkey := range d.AdditionalKeys { key, err := d.getRemoteKeyWithSameFingerprintNullable([]byte(pubkey)) if err != nil { - return errors.Wrapf(err, "error checking for existing key for %v", pubkey) + return fmt.Errorf("error checking for existing key for %v: %w", pubkey, err) } if key == nil { log.Infof("Creating new key for %v...", pubkey) key, err = d.makeKey(fmt.Sprintf("%v-additional-%d", d.GetMachineName(), i), pubkey, d.keyLabels) if err != nil { - return errors.Wrapf(err, "error creating new key for %v", pubkey) + return fmt.Errorf("error creating new key for %v: %w", pubkey, err) } log.Infof(" -> Created %v", key.ID) @@ -116,12 +116,12 @@ func (d *Driver) prepareLocalKey() error { if d.originalKey != "" { log.Debugf("Copying SSH key...") if err := d.copySSHKeyPair(d.originalKey); err != nil { - return errors.Wrap(err, "could not copy ssh key pair") + return fmt.Errorf("could not copy ssh key pair: %w", err) } } else { log.Debugf("Generating SSH key...") if err := mcnssh.GenerateSSHKey(d.GetSSHKeyPath()); err != nil { - return errors.Wrap(err, "could not generate ssh key") + return fmt.Errorf("could not generate ssh key: %w", err) } } return nil @@ -137,9 +137,9 @@ func (d *Driver) makeKey(name string, pubkey string, labels map[string]string) ( key, _, err := d.getClient().SSHKey.Create(context.Background(), instrumented(keyopts)) if err != nil { - return nil, errors.Wrap(err, "could not create ssh key") + return nil, fmt.Errorf("could not create ssh key: %w", err) } else if key == nil { - return nil, errors.Errorf("key upload did not return an error, but key was nil") + return nil, fmt.Errorf("key upload did not return an error, but key was nil") } d.dangling = append(d.dangling, func() { diff --git a/go.mod b/go.mod index 2ff356f..d73d5d9 100644 --- a/go.mod +++ b/go.mod @@ -5,7 +5,6 @@ go 1.21 require ( github.com/docker/machine v0.16.2 github.com/hetznercloud/hcloud-go/v2 v2.5.1 - github.com/pkg/errors v0.9.1 golang.org/x/crypto v0.16.0 ) diff --git a/go.sum b/go.sum index e088280..bd317c1 100644 --- a/go.sum +++ b/go.sum @@ -29,8 +29,6 @@ github.com/matttproud/golang_protobuf_extensions v1.0.4 h1:mmDVorXM7PCGKw94cs5zk github.com/matttproud/golang_protobuf_extensions v1.0.4/go.mod h1:BSXmuO+STAnVfrANrmjBb36TMTDstsz7MSK+HVaYKv4= github.com/moby/term v0.0.0-20221205130635-1aeaba878587 h1:HfkjXDfhgVaN5rmueG8cL8KKeFNecRCXFhaJ2qZ5SKA= github.com/moby/term v0.0.0-20221205130635-1aeaba878587/go.mod h1:8FzsFHVUBGZdbDsJw/ot+X+d5HLUbvklYLJ9uGfcI3Y= -github.com/pkg/errors v0.9.1 h1:FEBLx1zS214owpjy7qsBeixbURkuhQAwrK5UwLGTwt4= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v1.17.0 h1:rl2sfwZMtSthVU752MqfjQozy7blglC+1SOtjMAMh+Q=