This repository has been archived by the owner on Dec 20, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwhales.go
116 lines (100 loc) · 2.25 KB
/
whales.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
package main
import (
"sort"
"time"
"github.com/docker/engine-api/client"
"github.com/docker/engine-api/types"
"github.com/docker/engine-api/types/container"
"github.com/docker/engine-api/types/network"
"github.com/docker/go-connections/nat"
"golang.org/x/net/context"
)
type ContainerPs struct {
ID string `json:"containerId"`
Image string `json:"image"`
Status string `json:"status"`
SizeRw int64 `json:"sizeRw"`
SizeRootFs int64 `json:"sizeRootFs"`
}
type ContainerInfo struct {
ID string
Port string
Ports nat.PortMap
}
// Extract port as string from Ports
func portsToPort(ports nat.PortMap) string {
for _, v := range ports {
return v[0].HostPort
}
return ""
}
func listImages() []string {
var r []string
c, _ := client.NewEnvClient()
imgs, _ := c.ImageList(
context.Background(),
types.ImageListOptions{All: false},
)
for _, img := range imgs {
if img.RepoTags[0] != "<none>:<none>" {
r = append(r, img.RepoTags[0])
}
}
sort.Strings(r)
return r
}
func listContainers() []ContainerPs {
var r []ContainerPs
c, _ := client.NewEnvClient()
l, _ := c.ContainerList(
context.Background(),
types.ContainerListOptions{All: false},
)
for _, c := range l {
r = append(r, ContainerPs{
ID: c.ID,
Image: c.Image,
Status: c.Status,
SizeRw: c.SizeRw,
SizeRootFs: c.SizeRootFs})
}
return r
}
func runContainer(image string, memory int) (ContainerInfo, error) {
cl, _ := client.NewEnvClient()
mr := int64(memory) * 1024 * 1024
config := container.Config{
Image: image,
}
host := container.HostConfig{
PublishAllPorts: true,
Resources: container.Resources{
Memory: mr,
KernelMemory: 16 * 1024 * 1024,
},
}
ctx := context.Background()
c, err := cl.ContainerCreate(
ctx,
&config,
&host,
&network.NetworkingConfig{},
"",
)
if err != nil {
return ContainerInfo{}, err
}
cl.ContainerStart(ctx, c.ID)
ins, _ := cl.ContainerInspect(ctx, c.ID)
return ContainerInfo{
ins.ID, portsToPort(ins.NetworkSettings.Ports), ins.NetworkSettings.Ports,
}, nil
}
func killContainer(id string) error {
cl, _ := client.NewEnvClient()
return cl.ContainerKill(context.Background(), id, "SIGKILL")
}
func timeoutKill(id string, timeout int) {
time.Sleep(time.Second * time.Duration(timeout))
killContainer(id)
}