Skip to content

Commit

Permalink
refactor: Use Action.WatchProgress
Browse files Browse the repository at this point in the history
- remove homebrew waitForAction impl, delegate to Action.WatchProgress
- add internal HTTP debug mode (instrumented build only)
  • Loading branch information
JonasProgrammer committed Sep 24, 2023
1 parent 11a99a2 commit 54b321f
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 19 deletions.
45 changes: 26 additions & 19 deletions driver/hetzner_query.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,15 @@ import (
)

func (d *Driver) getClient() *hcloud.Client {
return hcloud.NewClient(hcloud.WithToken(d.AccessToken), hcloud.WithApplication("docker-machine-driver", d.version))
opts := []hcloud.ClientOption{
hcloud.WithToken(d.AccessToken),
hcloud.WithApplication("docker-machine-driver", d.version),
hcloud.WithPollBackoffFunc(hcloud.ConstantBackoff(time.Duration(d.WaitOnPolling) * time.Second)),
}

opts = d.setupClientInstrumentation(opts)

return hcloud.NewClient(opts...)
}

func (d *Driver) getLocationNullable() (*hcloud.Location, error) {
Expand Down Expand Up @@ -166,25 +174,24 @@ func (d *Driver) getServerHandleNullable() (*hcloud.Server, error) {
}

func (d *Driver) waitForAction(a *hcloud.Action) error {
for {
act, _, err := d.getClient().Action.GetByID(context.Background(), a.ID)
if err != nil {
return errors.Wrap(err, "could not get client by ID")
}
if act == nil {
return fmt.Errorf("action not found: %v", a.ID)
}

if act.Status == hcloud.ActionStatusSuccess {
log.Debugf(" -> finished %s[%d]", act.Command, act.ID)
break
} else if act.Status == hcloud.ActionStatusRunning {
log.Debugf(" -> %s[%d]: %d %%", act.Command, act.ID, act.Progress)
} else if act.Status == hcloud.ActionStatusError {
return act.Error()
progress, done := d.getClient().Action.WatchProgress(context.Background(), a)

running := true
var ret error

for running {
select {
case <-done:
ret = <-done
running = false
case <-progress:
log.Debugf(" -> %s[%d]: %d %%", a.Command, a.ID, <-progress)
}
}

time.Sleep(time.Duration(d.WaitOnPolling) * time.Second)
if ret == nil {
log.Debugf(" -> finished %s[%d]", a.Command, a.ID)
}
return nil

return ret
}
17 changes: 17 additions & 0 deletions driver/instrumentation_impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ package driver

import (
"encoding/json"
"github.com/hetznercloud/hcloud-go/v2/hcloud"
"os"
"runtime/debug"

"github.com/docker/machine/libmachine/log"
Expand All @@ -20,3 +22,18 @@ func instrumented[T any](input T) T {
log.Debugf("%v\n%v\n", string(debug.Stack()), string(j))
return input
}

type debugLogWriter struct {
}

func (x debugLogWriter) Write(data []byte) (int, error) {
log.Debug(string(data))
return len(data), nil
}

func (d *Driver) setupClientInstrumentation(opts []hcloud.ClientOption) []hcloud.ClientOption {
if os.Getenv("HETZNER_DRIVER_HTTP_DEBUG") == "42" {
opts = append(opts, hcloud.WithDebugWriter(debugLogWriter{}))
}
return opts
}
8 changes: 8 additions & 0 deletions driver/instrumentation_stub.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,16 @@

package driver

import (
"github.com/hetznercloud/hcloud-go/v2/hcloud"
)

const runningInstrumented = false

func instrumented[T any](input T) T {
return input
}

func (d *Driver) setupClientInstrumentation(opts []hcloud.ClientOption) []hcloud.ClientOption {
return opts
}

0 comments on commit 54b321f

Please sign in to comment.