-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker.go
151 lines (118 loc) · 3.49 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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
package cli
import (
"bufio"
"context"
"errors"
"os/exec"
"strings"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/filters"
"github.com/docker/docker/client"
)
type Container struct {
Id string `json:"id"`
Name string `json:"name"`
Image string `json:"image"`
Launched bool `json:"-"`
}
type DockerClient struct {
Client *client.Client
}
var Docker DockerClient
func StopContainer(ctx context.Context, c *Container) error {
stats, err := Docker.Client.ContainerInspect(ctx, c.Id)
if err != nil {
return err
}
if stats.State.Dead || stats.State.OOMKilled {
return errors.New("Container already stopped.")
}
timeout := new(int)
*timeout = 60
return Docker.Client.ContainerStop(ctx, c.Id, container.StopOptions{Timeout: timeout})
}
func LaunchContainer(ctx context.Context, container *Container) error {
running, err := IsContainerRunning(ctx, container)
if err != nil {
return err
}
if running {
return nil
}
if err := Docker.Client.ContainerStart(ctx, container.Id, types.ContainerStartOptions{}); err != nil {
return err
}
return nil
}
func GetCreatedContainers(ctx context.Context) (*[]Container, error) {
foundContainers, err := Docker.Client.ContainerList(ctx, types.ContainerListOptions{
Filters: filters.NewArgs(filters.KeyValuePair{Key: "name", Value: "vemta-"}),
})
containers := make([]Container, 0)
if err != nil {
return nil, err
}
for _, container := range foundContainers {
containers = append(containers, Container{
Id: container.ID,
Name: container.Names[0],
Image: container.Image,
Launched: container.State == "running",
})
}
return &containers, nil
}
func IsContainerRunning(ctx context.Context, container *Container) (bool, error) {
stats, err := Docker.Client.ContainerInspect(ctx, container.Id)
if err != nil {
return false, err
}
return stats.State.Running || stats.State.Paused, nil
}
func BackendNetworkExists(ctx context.Context) bool {
cmd := exec.Command("docker", "network", "ls", "--filter", "name="+Configuration.BackendNetwork, "{{.Name}}")
output, _ := cmd.StdoutPipe()
cmd.Stderr = cmd.Stdout
scanner := bufio.NewScanner(output)
ok := false
if err := cmd.Run(); err != nil {
return false
}
if scanner.Scan() {
line := scanner.Text()
ok = line == Configuration.BackendNetwork
}
return ok
}
func CreateBackendNetwork(ctx context.Context) error {
_, err := Docker.Client.NetworkCreate(ctx, Configuration.BackendNetwork, types.NetworkCreate{})
return err
}
func GetContainers() *[]Container {
cmd := exec.Command("docker", "container", "ls", "-a", "--no-trunc", "--filter", "name=vemta-", "--format", "{{.ID}} {{.Names}} {{.Image}} {{.Status}}")
return parseContainers(cmd)
}
func GetContainersOfService(service *VemtaService) *[]Container {
cmd := exec.Command("docker", "container", "ls", "-a", "--no-trunc", "--filter", "name=vemta-"+service.DockerPrefix, "--format", "{{.ID}} {{.Names}} {{.Image}} {{.Status}}")
return parseContainers(cmd)
}
func parseContainers(cmd *exec.Cmd) *[]Container {
containers := make([]Container, 0)
output, _ := cmd.StdoutPipe()
cmd.Stderr = cmd.Stdout
scanner := bufio.NewScanner(output)
cmd.Start()
for scanner.Scan() {
line := scanner.Text()
params := strings.Split(line, " ")
containers = append(containers, Container{
Id: params[0],
Name: params[1],
Image: params[2],
Launched: params[3] == "Up",
})
}
cmd.Wait()
return &containers
}