-
Notifications
You must be signed in to change notification settings - Fork 5
/
client.go
271 lines (242 loc) · 7.17 KB
/
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
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package client
import (
"io/ioutil"
"log"
"fmt"
"os"
"github.com/docker/distribution"
"github.com/docker/distribution/digest"
"github.com/docker/distribution/manifest"
manifestV1 "github.com/docker/distribution/manifest/schema1"
"github.com/vbaksa/promoter/progressbar"
"github.com/docker/libtrust"
"github.com/dustin/go-humanize"
"github.com/heroku/docker-registry-client/registry"
"gopkg.in/cheggaaa/pb.v1"
)
//Promote holds single image promotions structure
type Promote struct {
SrcRegistry string
SrcImage string
SrcImageTag string
SrcUsername string
SrcPassword string
SrcInsecure bool
DestRegistry string
DestImage string
DestImageTag string
DestUsername string
DestPassword string
DestInsecure bool
Debug bool
}
//PromoteImage executes single image promotion
func (pr *Promote) PromoteImage() {
if !pr.Debug {
log.SetOutput(ioutil.Discard)
}
fmt.Println("Connecting to Source registry")
var srcHub *registry.Registry
var destHub *registry.Registry
var err error
if pr.SrcInsecure {
srcHub, err = registry.NewInsecure(pr.SrcRegistry, pr.SrcUsername, pr.SrcPassword)
} else {
srcHub, err = registry.New(pr.SrcRegistry, pr.SrcUsername, pr.SrcPassword)
}
if err != nil {
fmt.Println("Cannot connect to registry: " + pr.SrcRegistry)
fmt.Println("Connection error: " + err.Error())
os.Exit(1)
} else {
fmt.Println("Connected to Source registry")
}
if pr.DestInsecure {
destHub, err = registry.NewInsecure(pr.DestRegistry, pr.DestUsername, pr.DestPassword)
} else {
destHub, err = registry.New(pr.DestRegistry, pr.DestUsername, pr.DestPassword)
}
fmt.Println("Connecting to Destination registry")
if err != nil {
fmt.Println("Cannot connect to registry: " + pr.DestRegistry)
fmt.Println("Connection error: " + err.Error())
os.Exit(1)
} else {
fmt.Println("Connected to Destination registry")
}
srcManifest, err := srcHub.Manifest(pr.SrcImage, pr.SrcImageTag)
if err != nil {
fmt.Println("Failed to download Source Image manifest. Error: " + err.Error())
os.Exit(1)
}
srcLayers := srcManifest.FSLayers
fmt.Println("Optimising upload...")
uploadLayer := pr.layerExists(destHub, srcHub, srcLayers)
totalDownloadSize := pr.getTotalDownloadSize(srcHub, uploadLayer)
if len(uploadLayer) > 0 {
fmt.Println()
fmt.Printf("Going to upload around %s of layer data. Expected network bandwidth: %s \n", humanize.Bytes(uint64(totalDownloadSize)), humanize.Bytes(uint64(totalDownloadSize*2)))
fmt.Println()
fmt.Println()
fmt.Println("Uploading layers")
fmt.Println()
done := make(chan bool)
var totalReader = make(chan int64)
for _, layer := range uploadLayer {
//srcHub.DownloadLayer(src)
go func(layer digest.Digest) {
pr.uploadLayer(destHub, srcHub, layer, &totalReader)
done <- true
}(layer)
}
bar := pb.New64(totalDownloadSize * 2).SetUnits(pb.U_BYTES)
bar.Start()
go func() {
for {
t := <-totalReader
bar.Add64(t * 2)
}
}()
for i := 0; i < len(uploadLayer); i++ {
<-done
}
bar.Finish()
fmt.Println("Finished uploading layers")
}
fmt.Println("Generating Signing Key...")
key, err := libtrust.GenerateECP256PrivateKey()
if err != nil {
fmt.Println("Error occurred while generating Image Key")
fmt.Println("Error: " + err.Error())
os.Exit(1)
}
fmt.Println("Signing Image Manifest...")
destManifest := srcManifest
destManifest.Name = pr.DestImage
destManifest.Tag = pr.DestImageTag
manifest := &manifestV1.Manifest{
Versioned: manifest.Versioned{
SchemaVersion: 1,
},
Name: pr.DestImage,
Tag: pr.DestImageTag,
Architecture: destManifest.Architecture,
FSLayers: destManifest.FSLayers,
History: destManifest.History,
}
signedManifest, err := manifestV1.Sign(manifest, key)
if err != nil {
fmt.Println("Error occurred while Signing Image Manifest")
fmt.Println("Error: " + err.Error())
os.Exit(1)
}
fmt.Println("Submitting Image Manifest")
err = destHub.PutManifest(pr.DestImage, pr.DestImageTag, signedManifest)
if err != nil {
fmt.Println("Manifest update error: " + err.Error())
os.Exit(1)
}
fmt.Println("Push Complete")
os.Exit(0)
}
type layerCheckResult struct {
Err error
Exists *existingLayer
Missing *missingLayer
}
type missingLayer struct {
Blob digest.Digest
}
type existingLayer struct {
Descriptor distribution.Descriptor
}
func (pr *Promote) getTotalDownloadSize(srcHub *registry.Registry, uploadLayer []digest.Digest) int64 {
result := make(chan distribution.Descriptor)
for _, layer := range uploadLayer {
go func(layer digest.Digest) {
l, err := srcHub.LayerMetadata(pr.SrcImage, layer)
if err != nil {
fmt.Println("Error while inspecting layer: " + layer)
os.Exit(1)
}
result <- l
}(layer)
}
var total int64
for i := 0; i < len(uploadLayer); i++ {
r := <-result
total = total + r.Size
}
return total
}
func (pr *Promote) layerExists(destHub *registry.Registry, srcHub *registry.Registry, srcLayers []manifestV1.FSLayer) []digest.Digest {
//Layers array returned by function
results := make([]digest.Digest, 0)
//How much space we will save by skipping already existing layers
var totalSaved int64
//Temporary result channel
result := make(chan *layerCheckResult)
// check each layer on remote hub
for _, layer := range srcLayers {
go func(layer manifestV1.FSLayer, result chan *layerCheckResult) {
layerMetada, err := destHub.LayerMetadata(pr.DestImage, layer.BlobSum)
if err != nil {
// Layer does not exist
// fmt.Println("Layer does not exist: " + layer.BlobSum)
checkResult := &layerCheckResult{
Err: err,
Missing: &missingLayer{
Blob: layer.BlobSum,
},
}
result <- checkResult
} else {
// Layer exists
fmt.Println("Layer already exists on Remote Registry: " + layerMetada.Digest)
checkResult := &layerCheckResult{
Err: nil,
Exists: &existingLayer{
Descriptor: layerMetada,
},
}
result <- checkResult
}
}(layer, result)
}
// Wait for result (each layer check)
for i := 0; i < len(srcLayers); i++ {
res := <-result
// If we got result about missing layer on remote registry, else check result about existing layer
if res.Missing != nil {
//iterate over existing array and make sure that there is no duplicate layers
var e = false
for _, r := range results {
if r == res.Missing.Blob {
e = true
}
}
//does not exist on array
if !e {
results = append(results, res.Missing.Blob)
}
} else {
totalSaved = totalSaved + res.Exists.Descriptor.Size
}
}
fmt.Println()
if totalSaved > 100 {
fmt.Printf("Some layers already exist on Remote Registry. Skipping around %s of layer data. Total network bandwidth saved: %s \n", humanize.Bytes(uint64(totalSaved)), humanize.Bytes(uint64(totalSaved*2)))
}
fmt.Println()
return results
}
func (pr *Promote) uploadLayer(destHub *registry.Registry, srcHub *registry.Registry, layer digest.Digest, totalReader *chan int64) {
reader, err := srcHub.DownloadLayer(pr.SrcImage, layer)
defer reader.Close()
rd := &progressbar.PassThru{ReadCloser: reader, Total: totalReader}
destHub.UploadLayer(pr.DestImage, layer, rd)
if err != nil {
fmt.Println("Error occurred while uploading layer: " + layer)
os.Exit(1)
}
}