-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathmain.go
240 lines (200 loc) · 7.22 KB
/
main.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
/*
Copyright 2017 Matt Lavin <matt.lavin@gmail.com>
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package main
import (
"encoding/base64"
"fmt"
"github.com/alecthomas/kingpin"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/aws-sdk-go/service/ecr"
"github.com/docker/distribution/manifest/schema1"
"github.com/heroku/docker-registry-client/registry"
"io"
"io/ioutil"
"os"
"strings"
)
func moveLayerUsingFile(srcHub *registry.Registry, destHub *registry.Registry, srcRepo string, destRepo string, layer schema1.FSLayer, file *os.File) error {
layerDigest := layer.BlobSum
srcImageReader, err := srcHub.DownloadLayer(srcRepo, layerDigest)
if err != nil {
return fmt.Errorf("Failure while starting the download of an image layer. %v", err)
}
_, err = io.Copy(file, srcImageReader)
if err != nil {
return fmt.Errorf("Failure while copying the image layer to a temp file. %v", err)
}
srcImageReader.Close()
file.Sync()
imageReadStream, err := os.Open(file.Name())
if err != nil {
return fmt.Errorf("Failed to open temporary image layer for uploading. %v", err)
}
err = destHub.UploadLayer(destRepo, layerDigest, imageReadStream)
imageReadStream.Close()
if err != nil {
return fmt.Errorf("Failure while uploading the image. %v", err)
}
return nil
}
func migrateLayer(srcHub *registry.Registry, destHub *registry.Registry, srcRepo string, destRepo string, layer schema1.FSLayer) error {
fmt.Println("Checking if manifest layer exists in destination registery")
layerDigest := layer.BlobSum
hasLayer, err := destHub.HasLayer(destRepo, layerDigest)
if err != nil {
return fmt.Errorf("Failure while checking if the destination registry contained an image layer. %v", err)
}
if !hasLayer {
fmt.Println("Need to upload layer", layerDigest, "to the destination")
tempFile, err := ioutil.TempFile("", "docker-image")
if err != nil {
return fmt.Errorf("Failure while creating temporary file for an image layer download. %v", err)
}
err = moveLayerUsingFile(srcHub, destHub, srcRepo, destRepo, layer, tempFile)
removeErr := os.Remove(tempFile.Name())
if removeErr != nil {
// Print the error but don't fail the whole migration just because of a leaked temp file
fmt.Printf("Failed to remove image layer temp file %s. %v", tempFile.Name(), removeErr)
}
return err
} else {
fmt.Println("Layer already exists in the destination")
return nil
}
}
type RepositoryArguments struct {
RegistryURL *string
Repository *string
Tag *string
}
func buildRegistryArguments(argPrefix string, argDescription string) RepositoryArguments {
registryURLName := fmt.Sprintf("%s-url", argPrefix)
registryURLDescription := fmt.Sprintf("URL of %s registry", argDescription)
registryURLArg := kingpin.Flag(registryURLName, registryURLDescription).String()
repositoryName := fmt.Sprintf("%s-repo", argPrefix)
repositoryDescription := fmt.Sprintf("Name of the %s repository", argDescription)
repositoryArg := kingpin.Flag(repositoryName, repositoryDescription).String()
tagName := fmt.Sprintf("%s-tag", argPrefix)
tagDescription := fmt.Sprintf("Name of the %s tag", argDescription)
tagArg := kingpin.Flag(tagName, tagDescription).String()
return RepositoryArguments{
RegistryURL: registryURLArg,
Repository: repositoryArg,
Tag: tagArg,
}
}
func connectToRegistry(args RepositoryArguments) (*registry.Registry, error) {
origUrl := *args.RegistryURL
url := origUrl
username := ""
password := ""
if strings.HasPrefix(url, "ecr:") {
registryId := strings.TrimPrefix(url, "ecr:")
sess, err := session.NewSession()
if err != nil {
return nil, fmt.Errorf("Failed to create new AWS SDK session. %v", err)
}
svc := ecr.New(sess)
params := &ecr.GetAuthorizationTokenInput{
RegistryIds: []*string{
aws.String(registryId), // Required
},
}
resp, err := svc.GetAuthorizationToken(params)
if err != nil {
return nil, fmt.Errorf("Failed to get ECR authorization token for registry %s. %v", registryId, err)
}
decoded, err := base64.StdEncoding.DecodeString(*resp.AuthorizationData[0].AuthorizationToken)
if err != nil {
return nil, fmt.Errorf("Failed to decode base64 encoded authorization data for ECR registry %s. %v", registryId, err)
}
parts := strings.Split(string(decoded), ":")
url = *resp.AuthorizationData[0].ProxyEndpoint
username = parts[0]
password = parts[1]
}
registry, err := registry.New(url, username, password)
if err != nil {
return nil, fmt.Errorf("Failed to create registry connection for %s. %v", origUrl, err)
}
err = registry.Ping()
if err != nil {
return nil, fmt.Errorf("Failed to ping registry %s as a connection test. %v", origUrl, err)
}
return registry, nil
}
func main() {
exitCode := 0
defer func() {
os.Exit(exitCode)
}()
srcArgs := buildRegistryArguments("src", "source")
destArgs := buildRegistryArguments("dest", "destination")
repoArg := kingpin.Flag("repo", "The repository in the source and the destination. Values provided by --src-repo or --dest-tag will override this value").String()
tagArg := kingpin.Flag("tag", "The tag name in the source and the destination. Values provided by --src-tag or --dest-tag will override this value").Default("latest").String()
kingpin.Parse()
if *srcArgs.Repository == "" {
srcArgs.Repository = repoArg
}
if *destArgs.Repository == "" {
destArgs.Repository = repoArg
}
if *srcArgs.Tag == "" {
srcArgs.Tag = tagArg
}
if *destArgs.Tag == "" {
destArgs.Tag = tagArg
}
if *srcArgs.Repository == "" {
fmt.Printf("A source repository name is required either with --src-repo or --repo")
exitCode = -1
return
}
if *destArgs.Repository == "" {
fmt.Printf("A destination repository name is required either with --dest-repo or --repo")
exitCode = -1
return
}
srcHub, err := connectToRegistry(srcArgs)
if err != nil {
fmt.Printf("Failed to establish a connection to the source registry. %v", err)
exitCode = -1
return
}
destHub, err := connectToRegistry(destArgs)
if err != nil {
fmt.Printf("Failed to establish a connection to the destination registry. %v", err)
exitCode = -1
return
}
manifest, err := srcHub.Manifest(*srcArgs.Repository, *srcArgs.Tag)
if err != nil {
fmt.Printf("Failed to fetch the manifest for %s/%s:%s. %v", srcHub.URL, *srcArgs.Repository, *srcArgs.Tag, err)
exitCode = -1
return
}
for _, layer := range manifest.FSLayers {
err := migrateLayer(srcHub, destHub, *srcArgs.Repository, *destArgs.Repository, layer)
if err != nil {
fmt.Printf("Failed to migrate image layer. %v", err)
exitCode = -1
return
}
}
err = destHub.PutManifest(*destArgs.Repository, *destArgs.Tag, manifest)
if err != nil {
fmt.Printf("Failed to upload manifest to %s/%s:%s. %v", destHub.URL, *destArgs.Repository, *destArgs.Tag, err)
exitCode = -1
}
}