-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathdocker.go
156 lines (146 loc) · 4.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
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
152
153
154
155
156
package api
import (
"encoding/json"
"fmt"
"io"
"math/rand"
"os"
tm "github.com/buger/goterm"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/container"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/archive"
"golang.org/x/net/context"
)
type building struct {
Status string `json:"status"`
Stream string `json:"stream"`
Progress string `json:"progress"`
}
func showLoaderWhenBuildImage(Body io.ReadCloser) {
tm.Clear()
decode := json.NewDecoder(Body)
for decode.More() {
tm.MoveCursor(1, 1)
var Build building
err := decode.Decode(&Build)
if err != nil {
break
}
if Build.Status != "" {
tm.Printf("Building Image\nStatus: %s\nProgress: +%v", Build.Status, Build.Progress)
} else if len(Build.Stream) > 1 && Build.Stream[:4] == "Step" {
tm.Printf("%+v", Build.Stream)
}
tm.Flush()
}
}
func buildImage(ctx context.Context, cli *client.Client, imgDocker string, pathDockerImage string) error {
dockerFileTarReader, err := archive.TarWithOptions(pathDockerImage, &archive.TarOptions{})
if err != nil {
return err
}
builderResp, err := cli.ImageBuild(
ctx,
dockerFileTarReader,
types.ImageBuildOptions{
Context: dockerFileTarReader,
Dockerfile: imgDocker,
Remove: true,
ForceRemove: true,
Tags: []string{"previs"},
})
if err != nil {
return err
}
defer builderResp.Body.Close()
showLoaderWhenBuildImage(builderResp.Body)
return nil
}
func array_contains(arr []string, str string) bool {
for _, a := range arr {
if a == str {
return true
}
}
return false
}
var letters = []rune("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randSeq(n int) string {
b := make([]rune, n)
for i := range b {
b[i] = letters[rand.Intn(len(letters))]
}
return string(b)
}
func startContainer(ctx context.Context, cli *client.Client, imgDocker string, pathDockerImage string, envVar []string, services []string) (string, error) {
hostconfig := &container.HostConfig{}
if array_contains(services, "docker") {
hostconfig = &container.HostConfig{
Binds: []string{"/var/run/docker.sock:/var/run/docker.sock"},
}
}
containerName := randSeq(10) + "-previs"
respContainerCreater, err := cli.ContainerCreate(ctx, &container.Config{
Image: "previs",
Tty: true,
Env: envVar,
}, hostconfig, nil, containerName)
if err != nil {
return fmt.Sprintf("ContainerCreate failed with name '%s'", containerName), err
}
err = cli.ContainerStart(ctx, respContainerCreater.ID, types.ContainerStartOptions{})
if err != nil {
return "", err
}
statusChan, errChan := cli.ContainerWait(ctx, respContainerCreater.ID, container.WaitConditionNextExit)
select {
case errOnWaiting := <-errChan:
return "", errOnWaiting
case status := <-statusChan:
readerOutputLog, errOnLogs := cli.ContainerLogs(ctx, respContainerCreater.ID, types.ContainerLogsOptions{
ShowStderr: true,
ShowStdout: true,
})
if errOnLogs != nil {
return "", errOnLogs
}
defer readerOutputLog.Close()
io.Copy(os.Stdout, readerOutputLog)
err = CleanAll(ctx, cli, respContainerCreater.ID, imgDocker, pathDockerImage)
if err != nil {
return "", err
}
if status.StatusCode == 0 {
fmt.Printf("\nYour build has passed\n\n")
return respContainerCreater.ID, nil
}
fmt.Printf("\nYour build has failed for the container %s\n\nLOGS:\n", respContainerCreater.ID)
}
return respContainerCreater.ID, nil
}
// Start the pipeline of test: Build,Launch,Clean
func Start(imgDocker string, pathDockerImage string, envVar []string, services []string) error {
ctx := context.Background()
cli, err := client.NewClientWithOpts(client.WithVersion("1.38"))
if err != nil {
return err
}
err = buildImage(ctx, cli, imgDocker, pathDockerImage)
if err != nil {
errOnCleanImages := CleanProducedImages(ctx, cli)
if errOnCleanImages != nil {
return errOnCleanImages
}
return err
}
idContainer, err := startContainer(ctx, cli, imgDocker, pathDockerImage, envVar, services)
if err != nil {
errOnCleanCOntainer := CleanProducedContainer(ctx, cli, idContainer)
if errOnCleanCOntainer != nil {
return errOnCleanCOntainer
}
return err
}
return nil
}