forked from d2iq-archive/cake-builder
-
Notifications
You must be signed in to change notification settings - Fork 0
/
docker_client.go
185 lines (150 loc) · 4.78 KB
/
docker_client.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
package main
import (
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"log"
"os"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
"github.com/heroku/docker-registry-client/registry"
"github.com/jhoonb/archivex"
)
type DockerClient interface {
Tags(repository string, imageName string) (tags []string, err error)
ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error)
ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error)
}
type ExternalDockerClient struct {
AuthConfig AuthConfig
Client *client.Client
Registry *registry.Registry
}
func (client *ExternalDockerClient) Tags(repository string, imageName string) (tags []string, err error) {
return client.Registry.Tags(imageName)
}
func (client *ExternalDockerClient) ImageBuild(ctx context.Context, buildContext io.Reader, options types.ImageBuildOptions) (types.ImageBuildResponse, error) {
return client.Client.ImageBuild(context.Background(), buildContext, options)
}
func (client *ExternalDockerClient) ImagePush(ctx context.Context, image string, options types.ImagePushOptions) (io.ReadCloser, error) {
return client.Client.ImagePush(context.Background(), image, options)
}
func NewExternalDockerClient(authConfig AuthConfig) *ExternalDockerClient {
dockerClient := ExternalDockerClient{
AuthConfig: authConfig,
}
dockerRegistry, err := registry.New(authConfig.DockerRegistryUrl, authConfig.Username, authConfig.Password)
if err != nil {
log.Fatal(err)
}
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithVersion("1.39"))
if err != nil {
log.Fatal(err)
}
dockerClient.Client = cli
dockerClient.Registry = dockerRegistry
return &dockerClient
}
func imageExists(dockerClient DockerClient, image *Image, config BuildConfig) (bool, error) {
tags, err := dockerClient.Tags(config.AuthConfig.DockerRegistryUrl, image.getFullName())
if err != nil {
return false, fmt.Errorf("unable to retrieve tags for %s: %v", image.getFullName(), err)
}
for _, tag := range tags {
if tag == image.getStableTag(config) {
log.Printf("Found Image %s with tag: %s", image.getFullName(), image.getStableTag(config))
return true, nil
}
}
return false, nil
}
func buildImage(dockerClient DockerClient, image *Image, config BuildConfig) error {
imageConfig := image.ImageConfig
tmpDir := os.TempDir()
buildContextTarName := fmt.Sprintf("%s/%s_%s_context.tar", tmpDir, imageConfig.Repository, imageConfig.Name)
tar := new(archivex.TarFile)
defer tar.Close()
err := tar.Create(buildContextTarName)
if err != nil {
return err
}
err = tar.AddAll(config.BaseDir, false)
if err != nil {
return err
}
dockerBuildContext, err := os.Open(buildContextTarName)
if err != nil {
return err
}
defer dockerBuildContext.Close()
log.Printf("Building image with tags: %s", image.getDockerTags(config))
base64Auth, err := base64Auth(config)
if err != nil {
return err
}
authConfigs := make(map[string]types.AuthConfig)
authConfigs[config.AuthConfig.DockerRegistryUrl] = types.AuthConfig{
Auth: base64Auth,
}
options := types.ImageBuildOptions{
Dockerfile: image.Dockerfile,
Tags: image.getDockerTags(config),
AuthConfigs: authConfigs,
}
response, err := dockerClient.ImageBuild(context.Background(), dockerBuildContext, options)
if err != nil {
return err
}
err = handleOutput(response.Body)
if err != nil {
return err
}
return nil
}
func pushImage(dockerClient DockerClient, image *Image, config BuildConfig) error {
base64Auth, err := base64Auth(config)
if err != nil {
return err
}
pushOptions := types.ImagePushOptions{
RegistryAuth: base64Auth,
}
for _, tag := range image.getDockerTags(config) {
log.Printf("Pushing image with tag: %s", tag)
out, err := dockerClient.ImagePush(context.Background(), tag, pushOptions)
if err != nil {
return fmt.Errorf("error while pushing image %s: %v", tag, err)
}
err = handleOutput(out)
if err != nil {
return err
}
}
return nil
}
func handleOutput(reader io.ReadCloser) error {
termFd, isTerm := term.GetFdInfo(os.Stderr)
err := jsonmessage.DisplayJSONMessagesStream(reader, os.Stderr, termFd, isTerm, func(message jsonmessage.JSONMessage) {
log.Println(string(*message.Aux))
})
if err != nil {
return fmt.Errorf("error response from Docker daemon: %v", err)
}
defer reader.Close()
return nil
}
func base64Auth(config BuildConfig) (string, error) {
authConfig := types.AuthConfig{
Username: config.AuthConfig.Username,
Password: config.AuthConfig.Password,
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
return "", err
}
return base64.URLEncoding.EncodeToString(encodedJSON), nil
}