/
docker.go
149 lines (134 loc) · 3.56 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
package docker
import (
"context"
"errors"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/strslice"
"github.com/docker/docker/client"
"io"
"log"
"os"
"strings"
)
// ContainerManager is a wrapper over Docker SDK to easily manage docker container status
// inside analysis pipeline
type ContainerManager struct {
}
func (cm *ContainerManager) RunContainer(containerName string, imageName string) (*types.Container, error) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
if strings.Contains(imageName, "/") {
out, err := cli.ImagePull(ctx, imageName, types.ImagePullOptions{})
if err != nil {
return nil, err
}
defer out.Close()
io.Copy(os.Stdout, out)
}
resp, err := cli.ContainerCreate(ctx, &container.Config{
Image: imageName,
Hostname: "localhost",
Labels: map[string]string{"name": containerName},
AttachStderr: true,
AttachStdin: true,
AttachStdout: true,
Entrypoint: strslice.StrSlice{"tail", "-f", "/dev/null"},
}, nil, nil, nil, containerName)
if err != nil {
return nil, err
}
if err := cli.ContainerStart(ctx, resp.ID, types.ContainerStartOptions{}); err != nil {
return nil, err
}
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
All: true,
})
if err != nil {
return nil, err
}
for _, item := range containers {
if item.ID == resp.ID {
return &item, nil
}
}
return nil, errors.New("container failed to start")
}
func (cm *ContainerManager) DeleteContainer() {
}
// StopAllContainers stops all existing containers in the server
func (cm *ContainerManager) StopAllContainers() error {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
defer cli.Close()
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
All: true,
})
if err != nil {
return err
}
for _, item := range containers {
fmt.Print("Stopping container ", item.ID[:10], "... ")
if err := cli.ContainerStop(ctx, item.ID, nil); err != nil {
log.Println(err)
}
fmt.Println("Success")
}
return nil
}
// RemoveAllContainers removes all existing containers in the server
func (cm *ContainerManager) RemoveAllContainers() error {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return err
}
defer cli.Close()
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
All: true,
})
if err != nil {
return err
}
for _, item := range containers {
fmt.Print("Removing container ", item.ID[:10], "... ")
if err := cli.ContainerRemove(ctx, item.ID, types.ContainerRemoveOptions{
Force: true,
}); err != nil {
log.Println(err)
}
fmt.Println("Success")
}
return nil
}
func (cm *ContainerManager) IsContainerAlive(name string) (*types.Container, error) {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
defer cli.Close()
containers, err := cli.ContainerList(ctx, types.ContainerListOptions{
All: true,
})
if err != nil {
return nil, err
}
for _, item := range containers {
containerName := item.Names[0]
if containerName[0] == '/' {
containerName = containerName[1:]
}
if containerName == name {
return &item, nil
}
}
return nil, errors.New("container not found")
}