Skip to content
Snippets Groups Projects
Commit 54b321f1 authored by JonasS's avatar JonasS
Browse files

refactor: Use Action.WatchProgress

- remove homebrew waitForAction impl, delegate to Action.WatchProgress
- add internal HTTP debug mode (instrumented build only)
parent 11a99a23
No related branches found
No related tags found
No related merge requests found
......@@ -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) {
......@@ -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")
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)
}
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()
if ret == nil {
log.Debugf(" -> finished %s[%d]", a.Command, a.ID)
}
time.Sleep(time.Duration(d.WaitOnPolling) * time.Second)
}
return nil
return ret
}
......@@ -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"
......@@ -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
}
......@@ -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% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment