Skip to content

Commit 54b321f

Browse files
refactor: Use Action.WatchProgress
- remove homebrew waitForAction impl, delegate to Action.WatchProgress - add internal HTTP debug mode (instrumented build only)
1 parent 11a99a2 commit 54b321f

File tree

3 files changed

+51
-19
lines changed

3 files changed

+51
-19
lines changed

driver/hetzner_query.go

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,15 @@ import (
1111
)
1212

1313
func (d *Driver) getClient() *hcloud.Client {
14-
return hcloud.NewClient(hcloud.WithToken(d.AccessToken), hcloud.WithApplication("docker-machine-driver", d.version))
14+
opts := []hcloud.ClientOption{
15+
hcloud.WithToken(d.AccessToken),
16+
hcloud.WithApplication("docker-machine-driver", d.version),
17+
hcloud.WithPollBackoffFunc(hcloud.ConstantBackoff(time.Duration(d.WaitOnPolling) * time.Second)),
18+
}
19+
20+
opts = d.setupClientInstrumentation(opts)
21+
22+
return hcloud.NewClient(opts...)
1523
}
1624

1725
func (d *Driver) getLocationNullable() (*hcloud.Location, error) {
@@ -166,25 +174,24 @@ func (d *Driver) getServerHandleNullable() (*hcloud.Server, error) {
166174
}
167175

168176
func (d *Driver) waitForAction(a *hcloud.Action) error {
169-
for {
170-
act, _, err := d.getClient().Action.GetByID(context.Background(), a.ID)
171-
if err != nil {
172-
return errors.Wrap(err, "could not get client by ID")
173-
}
174-
if act == nil {
175-
return fmt.Errorf("action not found: %v", a.ID)
176-
}
177-
178-
if act.Status == hcloud.ActionStatusSuccess {
179-
log.Debugf(" -> finished %s[%d]", act.Command, act.ID)
180-
break
181-
} else if act.Status == hcloud.ActionStatusRunning {
182-
log.Debugf(" -> %s[%d]: %d %%", act.Command, act.ID, act.Progress)
183-
} else if act.Status == hcloud.ActionStatusError {
184-
return act.Error()
177+
progress, done := d.getClient().Action.WatchProgress(context.Background(), a)
178+
179+
running := true
180+
var ret error
181+
182+
for running {
183+
select {
184+
case <-done:
185+
ret = <-done
186+
running = false
187+
case <-progress:
188+
log.Debugf(" -> %s[%d]: %d %%", a.Command, a.ID, <-progress)
185189
}
190+
}
186191

187-
time.Sleep(time.Duration(d.WaitOnPolling) * time.Second)
192+
if ret == nil {
193+
log.Debugf(" -> finished %s[%d]", a.Command, a.ID)
188194
}
189-
return nil
195+
196+
return ret
190197
}

driver/instrumentation_impl.go

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ package driver
44

55
import (
66
"encoding/json"
7+
"github.com/hetznercloud/hcloud-go/v2/hcloud"
8+
"os"
79
"runtime/debug"
810

911
"github.com/docker/machine/libmachine/log"
@@ -20,3 +22,18 @@ func instrumented[T any](input T) T {
2022
log.Debugf("%v\n%v\n", string(debug.Stack()), string(j))
2123
return input
2224
}
25+
26+
type debugLogWriter struct {
27+
}
28+
29+
func (x debugLogWriter) Write(data []byte) (int, error) {
30+
log.Debug(string(data))
31+
return len(data), nil
32+
}
33+
34+
func (d *Driver) setupClientInstrumentation(opts []hcloud.ClientOption) []hcloud.ClientOption {
35+
if os.Getenv("HETZNER_DRIVER_HTTP_DEBUG") == "42" {
36+
opts = append(opts, hcloud.WithDebugWriter(debugLogWriter{}))
37+
}
38+
return opts
39+
}

driver/instrumentation_stub.go

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,16 @@
22

33
package driver
44

5+
import (
6+
"github.com/hetznercloud/hcloud-go/v2/hcloud"
7+
)
8+
59
const runningInstrumented = false
610

711
func instrumented[T any](input T) T {
812
return input
913
}
14+
15+
func (d *Driver) setupClientInstrumentation(opts []hcloud.ClientOption) []hcloud.ClientOption {
16+
return opts
17+
}

0 commit comments

Comments
 (0)