-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathimage.go
245 lines (220 loc) · 7.11 KB
/
image.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
package meli
import (
"archive/tar"
"bufio"
"bytes"
"context"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path/filepath"
"strings"
"sync"
"github.com/docker/docker/api/types"
"github.com/pkg/errors"
)
// PullDockerImage pulls a docker from a registry via docker daemon
func PullDockerImage(ctx context.Context, cli APIclient, dc *DockerContainer) error {
imageName := dc.ComposeService.Image
result, _ := AuthInfo.Load("dockerhub")
if strings.Contains(imageName, "quay") {
result, _ = AuthInfo.Load("quay")
}
GetRegistryAuth := result.(map[string]string)["RegistryAuth"]
imagePullResp, err := cli.ImagePull(
ctx,
imageName,
types.ImagePullOptions{RegistryAuth: GetRegistryAuth})
if err != nil {
return errors.Wrapf(err, "unable to pull image %v", imageName)
}
var imgProg imageProgress
scanner := bufio.NewScanner(imagePullResp)
for scanner.Scan() {
_ = json.Unmarshal(scanner.Bytes(), &imgProg)
fmt.Fprintln(dc.LogMedium, dc.ServiceName, "::", imgProg.Status, imgProg.Progress)
}
if err := scanner.Err(); err != nil {
fmt.Println(" :unable to log output for image", imageName, err)
}
imagePullResp.Close()
return nil
}
func walkFnClosure(src string, tw *tar.Writer, buf *bytes.Buffer) filepath.WalkFunc {
return func(path string, info os.FileInfo, err error) error {
if err != nil {
// todo: maybe we should return nil
return err
}
tarHeader, err := tar.FileInfoHeader(info, info.Name())
if err != nil {
return err
}
// update the name to correctly reflect the desired destination when untaring
// https://medium.com/@skdomino/taring-untaring-files-in-go-6b07cf56bc07
tarHeader.Name = strings.TrimPrefix(strings.Replace(path, src, "", -1), string(filepath.Separator))
if src == "." {
// see: issues/74
tarHeader.Name = strings.TrimPrefix(path, string(filepath.Separator))
}
err = tw.WriteHeader(tarHeader)
if err != nil {
return err
}
// return on directories since there will be no content to tar
if info.Mode().IsDir() {
return nil
}
// return on non-regular files since there will be no content to tar
if !info.Mode().IsRegular() {
// non regular files are like symlinks etc; https://golang.org/src/os/types.go?h=ModeSymlink#L49
return nil
}
// open files for taring
f, err := os.Open(path)
if err != nil {
return err
}
defer f.Close()
tr := io.TeeReader(f, tw)
_, err = poolReadFrom(tr)
if err != nil {
return err
}
return nil
}
}
// this is taken from io.util
var blackHolePool = sync.Pool{
New: func() interface{} {
// TODO: change this size accordingly
// we could find the size of the file we want to tar
// then pass that in as the size. That way we will
// always create a right sized slice and not have to incure cost of slice regrowth(if any)
b := make([]byte, 512)
return &b
},
}
// this is taken from io.util
func poolReadFrom(r io.Reader) (n int64, err error) {
bufp := blackHolePool.Get().(*[]byte)
// reset the buffer since it may contain data from a previous round
// see issues/118
for i := range *bufp {
(*bufp)[i] = 0
}
readSize := 0
for {
readSize, err = r.Read(*bufp)
n += int64(readSize)
if err != nil {
blackHolePool.Put(bufp)
if err == io.EOF {
return n, nil
}
return
}
}
}
// BuildDockerImage builds a docker image via docker daemon
func BuildDockerImage(ctx context.Context, cli APIclient, dc *DockerContainer) (string, error) {
// TODO: I dont like the way we are handling paths here.
// look at dirWithComposeFile in container.go
dockerFile := dc.ComposeService.Build.Dockerfile
if dockerFile == "" {
dockerFile = "Dockerfile"
}
dirWithComposeFile := filepath.Dir(dc.DockerComposeFile)
dirWithComposeFileAbs, err := filepath.Abs(dirWithComposeFile)
if err != nil {
return "", errors.Wrapf(err, "unable to get absolute path of %v", dirWithComposeFile)
}
userContext := filepath.Dir(dc.ComposeService.Build.Context + "/")
userContextAbs := filepath.Join(dirWithComposeFileAbs, userContext)
if filepath.IsAbs(userContext) {
// For user Contexts that are absolute paths,
// do NOT join them with anything. They should be used as is.
userContextAbs = userContext
}
if userContextAbs == "/" {
// ie: dc.ComposeService.Build.Context=="" because user didn't provide any
userContextAbs = dirWithComposeFile
}
dockerFilePath, err := filepath.Abs(
filepath.Join(userContextAbs, dockerFile))
if err != nil {
return "", errors.Wrapf(err, "unable to get path to Dockerfile %v", dockerFile)
}
dockerFileReader, err := os.Open(dockerFilePath)
if err != nil {
return "", errors.Wrapf(err, "unable to open Dockerfile %v", dockerFilePath)
}
readDockerFile, err := ioutil.ReadAll(dockerFileReader)
if err != nil {
return "", errors.Wrapf(err, "unable to read dockerfile %v", dockerFile)
}
imageName := "meli_" + strings.ToLower(dc.ServiceName)
splitDockerfile := strings.Split(string(readDockerFile), " ")
splitImageName := strings.Split(splitDockerfile[1], "\n")
imgFromDockerfile := splitImageName[0]
result, _ := AuthInfo.Load("dockerhub")
if strings.Contains(imgFromDockerfile, "quay") {
result, _ = AuthInfo.Load("quay")
}
authInfo := result.(map[string]string)
registryURL := authInfo["registryURL"]
username := authInfo["username"]
password := authInfo["password"]
AuthConfigs := make(map[string]types.AuthConfig)
AuthConfigs[registryURL] = types.AuthConfig{Username: username, Password: password}
buf := new(bytes.Buffer)
tw := tar.NewWriter(buf)
defer tw.Close()
/*
Context is either a path to a directory containing a Dockerfile, or a url to a git repository.
When the value supplied is a relative path, it is interpreted as relative to the location of the Compose file.
This directory is also the build context that is sent to the Docker daemon.
- https://docs.docker.com/compose/compose-file/#context
*/
UserProvidedContextPath := filepath.Dir(userContextAbs + "/")
err = filepath.Walk(UserProvidedContextPath, walkFnClosure(UserProvidedContextPath, tw, buf))
if err != nil {
return "", errors.Wrapf(err, "unable to walk user provided context path %v", UserProvidedContextPath)
}
dockerFileTarReader := bytes.NewReader(buf.Bytes())
imageBuildResponse, err := cli.ImageBuild(
ctx,
dockerFileTarReader,
types.ImageBuildOptions{
//PullParent: true,
//Squash: true, currently only supported in experimenta mode
Tags: []string{imageName},
Remove: true, //remove intermediary containers after build
NoCache: dc.Rebuild,
SuppressOutput: false,
Dockerfile: dockerFile,
Context: dockerFileTarReader,
AuthConfigs: AuthConfigs})
if err != nil {
return "", errors.Wrapf(err, "unable to build docker image %v for service %v", imageName, dc.ServiceName)
}
var imgProg imageProgress
scanner := bufio.NewScanner(imageBuildResponse.Body)
for scanner.Scan() {
_ = json.Unmarshal(scanner.Bytes(), &imgProg)
fmt.Fprint(
dc.LogMedium,
dc.ServiceName,
"::",
imgProg.Status,
imgProg.Progress,
imgProg.Stream)
}
if err := scanner.Err(); err != nil {
fmt.Println(" :unable to log output for image", imageName, err)
}
imageBuildResponse.Body.Close()
return imageName, nil
}