-
Notifications
You must be signed in to change notification settings - Fork 3
/
instance.go
53 lines (44 loc) · 1.16 KB
/
instance.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
package dockerinitiator
import (
"context"
"time"
"github.com/docker/docker/api/types"
docker "github.com/docker/docker/client"
)
// Instance contains the setup for the docker instance
type Instance struct {
client *docker.Client
host string
probe Probe
container types.ContainerJSON
}
// Stop will remove the instance container
func (i *Instance) Stop() error {
ctx := context.Background()
return i.client.ContainerRemove(ctx, i.container.ID, types.ContainerRemoveOptions{Force: true})
}
// GetHost will fetch the host of the instance container
func (i *Instance) GetHost() string {
return i.host
}
// Probe will periodically, during a timeout, check for an active connection in the instance container
func (i *Instance) Probe(timeout time.Duration) error {
ctx, cancelFunc := context.WithTimeout(context.Background(), timeout)
defer cancelFunc()
if err := i.probe.DoProbe(i); err == nil {
return nil
}
for {
select {
case <-time.After(1 * time.Second):
if err := i.probe.DoProbe(i); err == nil {
return nil
}
case <-ctx.Done():
return ctx.Err()
}
}
}
func (i *Instance) Container() types.ContainerJSON {
return i.container
}