-
Notifications
You must be signed in to change notification settings - Fork 1
/
docker.go
62 lines (48 loc) · 1.12 KB
/
docker.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
54
55
56
57
58
59
60
61
62
package docker
import (
"context"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)
type DockerConfig struct {
Client *client.Client
NodeID string
}
func NewDockerClient() (*DockerConfig, error) {
cli, err := client.NewEnvClient()
if err != nil {
return nil, err
}
return &DockerConfig{Client: cli}, nil
}
func (c *DockerConfig) Close() error {
return c.Client.Close()
}
func (c *DockerConfig) GetNodeID() error {
info, err := c.Client.Info(context.Background())
if err != nil {
return err
}
c.NodeID = info.Swarm.NodeID
return nil
}
func (c *DockerConfig) IsServiceRunning() (bool, error) {
filterID := fmt.Sprintf("{\"label\": {\"flip.enabled=true\": true}, \"node\": {\"%s\": true}, \"desired-state\": {\"running\": true}}", c.NodeID)
f, err := filters.FromParam(filterID)
if err != nil {
return false, err
}
opts := types.TaskListOptions{
Filters: f,
}
tasks, err := c.Client.TaskList(context.Background(), opts)
if err != nil {
return false, err
}
if len(tasks) == 1 {
return true, nil
}
return false, nil
}