-
Notifications
You must be signed in to change notification settings - Fork 2
/
docker.go
121 lines (104 loc) · 2.67 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
package main
import (
"context"
"strings"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/client"
"github.com/rivo/tview"
)
type DockerConfigKind string
const (
KIND_DOCKER_COMPOSE = "docker-compose"
KIND_DIRECTLY_CONFIGURE_DOCKER = "directly-configure-docker"
)
var dockerClient *client.Client
func getDockerClient() (*client.Client, error) {
if dockerClient == nil {
var err error
dockerClient, err = client.NewClientWithOpts(client.WithHostFromEnv(), client.WithAPIVersionNegotiation())
if err != nil {
return nil, err
}
}
return dockerClient, nil
}
func buildDockerComposeFile(menuItems []MenuItem) string {
dockerComposeFile := `version: '3'
services:
`
for _, item := range menuItems {
if !item.Config.IsConfigured() {
continue
}
dockerCompose, err := item.Config.ConfigureDocker(KIND_DOCKER_COMPOSE, nil)
if err != nil {
continue
}
for _, line := range strings.Split(dockerCompose, "\n") {
dockerComposeFile += "\t" + line + "\n"
}
}
return dockerComposeFile
}
func batchCreateDockerContainers(menuItems []MenuItem, form *tview.Form) []error {
var errors []error
for _, item := range menuItems {
if !item.Config.IsConfigured() {
continue
}
_, err := item.Config.ConfigureDocker(KIND_DIRECTLY_CONFIGURE_DOCKER, form)
if err != nil {
errors = append(errors, err)
}
}
return errors
}
func pullImageBlocking(imageName string, logView *tview.TextView) error {
client, err := getDockerClient()
if err != nil {
return err
}
ctx := context.Background()
out, err := client.ImagePull(ctx, imageName, image.PullOptions{})
if err != nil {
return err
}
// log the output
logView.Clear()
logView.ScrollToEnd()
logView.Write([]byte("Pulling image " + imageName + "\n"))
logView.ScrollToEnd()
buf := make([]byte, 1024)
for {
n, err := out.Read(buf)
if n > 0 {
logView.Write(buf[:n])
logView.ScrollToEnd()
}
if err != nil {
break
}
}
out.Close()
return nil
}
func createContainer(name string, containerConfig *container.Config, hostConfig *container.HostConfig, form *tview.Form) error {
client, err := getDockerClient()
if err != nil {
return err
}
logView := form.GetFormItemByLabel("").(*tview.TextView)
if err := pullImageBlocking(containerConfig.Image, logView); err != nil {
return err
}
out, err := client.ContainerCreate(context.Background(), containerConfig, hostConfig, nil, nil, name)
if err != nil {
return err
}
for _, warning := range out.Warnings {
logView.Write([]byte(warning + "\n"))
logView.ScrollToEnd()
}
return client.ContainerStart(context.Background(), out.ID, container.StartOptions{})
}