-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathclient.go
More file actions
159 lines (141 loc) · 4.17 KB
/
client.go
File metadata and controls
159 lines (141 loc) · 4.17 KB
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
package docker
import (
"bufio"
"context"
"encoding/base64"
"encoding/json"
"fmt"
"io"
"os"
"time"
"github.com/docker/docker/api/types"
"github.com/docker/docker/api/types/image"
"github.com/docker/docker/api/types/registry"
"github.com/docker/docker/client"
"github.com/moby/go-archive"
)
type BuildOptions struct {
QuayImgExp string
BuildTimeLimitSeconds uint32
}
func DefaultBuildOptions() *BuildOptions {
return &BuildOptions{
QuayImgExp: "never",
BuildTimeLimitSeconds: 60 * 10, // 10 minutes
}
}
type CEClient struct {
Client DockerClient
}
func NewCEClient() (*CEClient, error) {
container_cli, err := client.NewClientWithOpts(client.FromEnv)
if err != nil {
fmt.Println(client.IsErrConnectionFailed(err))
return nil, fmt.Errorf("error while creating Docker Client (%w)", err)
}
return &CEClient{
Client: container_cli,
}, nil
}
func (ce CEClient) Build(filepath string, name string, tags []string, archetype string, build_options *BuildOptions) error {
image_tag := name + ":" + tags[0]
quay_img_exp_value := map[string]string{
"quay.expires-after": build_options.QuayImgExp,
}
ctx, cancel := context.WithTimeout(context.Background(),
time.Second*time.Duration(build_options.BuildTimeLimitSeconds))
defer cancel()
tar, err := archive.TarWithOptions(filepath, &archive.TarOptions{})
if err != nil {
return fmt.Errorf("error archiving %s (%w)", filepath, err)
}
opts := types.ImageBuildOptions{
Dockerfile: "Dockerfile",
Tags: []string{image_tag},
Labels: quay_img_exp_value,
Platform: archetype,
}
res, err := ce.Client.ImageBuild(ctx, tar, opts)
if err != nil {
return fmt.Errorf("error building %s (%w)", name, err)
}
if res.Body != nil {
err := Show(res.Body, os.Stdout)
if err != nil {
return fmt.Errorf("error for %s found by container engine during build (%w)", name, err)
}
err = res.Body.Close()
if err != nil {
return fmt.Errorf("error closing image build response (%w)", err)
}
}
return nil
}
func Show(rd io.Reader, writer io.Writer) error {
var lastLine string
var nextLine string
scanner := bufio.NewScanner(rd)
line := &StreamLine{}
for scanner.Scan() {
lastLine = scanner.Text()
nextLine = scanner.Text()
err := json.Unmarshal([]byte(nextLine), &line)
if err != nil {
return fmt.Errorf("error unmarshalling jsons stream %s (%w)", lastLine, err)
}
if _, err := writer.Write([]byte(line.Stream)); err != nil {
return fmt.Errorf("error writing json stream (%w)", err)
}
line = &StreamLine{}
}
errLine := &ErrorLine{}
err := json.Unmarshal([]byte(lastLine), errLine)
if err != nil {
return fmt.Errorf(
"error unmarshalling error details from jsons stream producer %s (%w)",
lastLine, err)
}
if errLine.Error != "" {
return fmt.Errorf("error details from jsons stream producer (%s)", errLine.Error)
}
if err := scanner.Err(); err != nil {
return fmt.Errorf("error scanning jsons stream (%w)", err)
}
return nil
}
func (ce CEClient) Tag(image_tag string, destination string) error {
ctx, cancel := context.WithTimeout(context.Background(), time.Second*120)
defer cancel()
err := ce.Client.ImageTag(ctx, image_tag, destination)
if err != nil {
return fmt.Errorf("error tagging %s (%w)", destination, err)
}
return nil
}
func (ce CEClient) Push(destination string, username string, password string, registry_address string) error {
authConfig := registry.AuthConfig{
Username: username,
Password: password,
ServerAddress: registry_address,
}
authConfigBytes, _ := json.Marshal(authConfig)
authConfigEncoded := base64.URLEncoding.EncodeToString(authConfigBytes)
opts := image.PushOptions{RegistryAuth: authConfigEncoded}
ctx, cancel := context.WithTimeout(context.Background(), time.Second*120)
defer cancel()
rdr, err := ce.Client.ImagePush(ctx, destination, opts)
if err != nil {
return fmt.Errorf("error pushing %s (%w)", destination, err)
}
if rdr != nil {
err := Show(rdr, os.Stdout)
if err != nil {
return fmt.Errorf("error for %s found by container engine during push (%w)", destination, err)
}
err = rdr.Close()
if err != nil {
return fmt.Errorf("error closing image push reader (%w)", err)
}
}
return nil
}