forked from aws/amazon-ecs-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimage_app.go
More file actions
313 lines (264 loc) · 8.47 KB
/
image_app.go
File metadata and controls
313 lines (264 loc) · 8.47 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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
// Copyright 2015-2017 Amazon.com, Inc. or its affiliates. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License"). You may
// not use this file except in compliance with the License. A copy of the
// License is located at
//
// http://aws.amazon.com/apache2.0/
//
// or in the "license" file accompanying this file. This file 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 image
import (
"fmt"
"io"
"os"
"regexp"
"text/tabwriter"
"time"
ecrclient "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/ecr"
stsclient "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/aws/sts"
dockerclient "github.com/aws/amazon-ecs-cli/ecs-cli/modules/clients/docker"
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/commands/flags"
"github.com/aws/amazon-ecs-cli/ecs-cli/modules/config"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/ecr"
units "github.com/docker/go-units"
docker "github.com/fsouza/go-dockerclient"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
// const symbols and widths
const (
MinWidth = 20
TabWidth = 1
Padding = 3
PaddingChar = ' '
NumOfFlags = 0
PageSize = 100
// const formats
PushImageFormat = "ECR_REPOSITORY[:TAG]"
PullImageFormat = "ECR_REPOSITORY[:TAG|@DIGEST]"
ListImageFormat = "[ECR_REPOSITORY]"
)
// ImagePush does ecr login, tag image, and push image to ECR repository
func ImagePush(c *cli.Context) {
rdwr, err := config.NewReadWriter()
if err != nil {
logrus.Fatal("Error executing 'push': ", err)
}
commandConfig, err := config.NewCommandConfig(c, rdwr)
if err != nil {
logrus.Fatal("Error executing 'push': ", err)
}
dockerClient, err := dockerclient.NewClient()
if err != nil {
logrus.Fatal("Error executing 'push': ", err)
}
ecrClient := ecrclient.NewClient(commandConfig)
stsClient := stsclient.NewClient(commandConfig)
if err := pushImage(c, rdwr, dockerClient, ecrClient, stsClient); err != nil {
logrus.Fatal("Error executing 'push': ", err)
}
}
// ImagePull does ecr login and pulls from ECR repository
func ImagePull(c *cli.Context) {
rdwr, err := config.NewReadWriter()
if err != nil {
logrus.Fatal("Error executing 'pull': ", err)
}
commandConfig, err := config.NewCommandConfig(c, rdwr)
if err != nil {
logrus.Fatal("Error executing 'pull': ", err)
}
dockerClient, err := dockerclient.NewClient()
if err != nil {
logrus.Fatal("Error executing 'pull': ", err)
}
ecrClient := ecrclient.NewClient(commandConfig)
stsClient := stsclient.NewClient(commandConfig)
if err := pullImage(c, rdwr, dockerClient, ecrClient, stsClient); err != nil {
logrus.Fatal("Error executing 'pull': ", err)
}
}
// ImageList lists images up to 1000 items from ECR repository
func ImageList(c *cli.Context) {
rdwr, err := config.NewReadWriter()
if err != nil {
logrus.Fatal("Error executing 'images': ", err)
}
commandConfig, err := config.NewCommandConfig(c, rdwr)
if err != nil {
logrus.Fatal("Error executing 'images': ", err)
}
ecrClient := ecrclient.NewClient(commandConfig)
if err := getImages(c, rdwr, ecrClient); err != nil {
logrus.Fatal("Error executing 'images': ", err)
return
}
}
func pushImage(c *cli.Context, rdwr config.ReadWriter, dockerClient dockerclient.Client, ecrClient ecrclient.Client, stsClient stsclient.Client) error {
registryID := c.String(flags.RegistryIdFlag)
args := c.Args()
if len(args) != 1 {
return fmt.Errorf("ecs-cli push requires exactly 1 argument")
}
image := args[0]
registryURI, repository, tag, err := splitImageName(image, "[:]", PushImageFormat)
if err != nil {
return err
}
ecrAuth, err := getECRAuth(registryURI, registryID, stsClient, ecrClient)
if err != nil {
return err
}
repositoryURI := ecrAuth.Registry + "/" + repository
// Tag image to ECR uri
if registryURI == "" {
if err := dockerClient.TagImage(image, repositoryURI, tag); err != nil {
return err
}
}
// Check if repo exists, create if not present
if !ecrClient.RepositoryExists(repository) {
if _, err := ecrClient.CreateRepository(repository); err != nil {
return err
}
}
// Push Image to ECR
dockerAuth := docker.AuthConfiguration{
Username: ecrAuth.Username,
Password: ecrAuth.Password,
ServerAddress: ecrAuth.ProxyEndpoint,
}
err = dockerClient.PushImage(repositoryURI, tag, ecrAuth.Registry, dockerAuth)
return err
}
func pullImage(c *cli.Context, rdwr config.ReadWriter, dockerClient dockerclient.Client, ecrClient ecrclient.Client, stsClient stsclient.Client) error {
registryID := c.String(flags.RegistryIdFlag)
args := c.Args()
if len(args) != 1 {
return fmt.Errorf("ecs-cli pull requires exactly 1 argument")
}
image := args[0]
registryURI, repository, tag, err := splitImageName(image, "[:|@]", PullImageFormat)
if err != nil {
return err
}
ecrAuth, err := getECRAuth(registryURI, registryID, stsClient, ecrClient)
if err != nil {
return err
}
repositoryURI := ecrAuth.Registry + "/" + repository
dockerAuth := docker.AuthConfiguration{
Username: ecrAuth.Username,
Password: ecrAuth.Password,
ServerAddress: ecrAuth.ProxyEndpoint,
}
// Pull Image
err = dockerClient.PullImage(repositoryURI, tag, dockerAuth)
return err
}
type imageInfo struct {
RepositoryName string
Tag string
ImageDigest string
PushedAt string
Size string
}
func getImages(c *cli.Context, rdwr config.ReadWriter, ecrClient ecrclient.Client) error {
registryID := c.String(flags.RegistryIdFlag)
args := c.Args() // repository names
totalCount := 0
w := tabwriter.NewWriter(os.Stdout, MinWidth, TabWidth, Padding, PaddingChar, NumOfFlags)
err := ecrClient.GetImages(aws.StringSlice(args), getTagStatus(c), registryID, func(imageDetails []*ecr.ImageDetail) error {
// Prints all images in table
for _, image := range imageDetails {
info := imageInfo{
RepositoryName: aws.StringValue(image.RepositoryName),
ImageDigest: aws.StringValue(image.ImageDigest),
}
info.PushedAt = units.HumanDuration(time.Now().UTC().Sub(time.Unix(image.ImagePushedAt.Unix(), 0))) + " ago"
info.Size = units.HumanSizeWithPrecision(float64(aws.Int64Value(image.ImageSizeInBytes)), 3)
if len(image.ImageTags) == 0 {
info.Tag = "<none>"
listImagesContent(w, info, totalCount)
totalCount++
}
for _, tag := range image.ImageTags {
info.Tag = aws.StringValue(tag)
listImagesContent(w, info, totalCount)
totalCount++
}
}
return nil
})
w.Flush()
return err
}
func listImagesContent(w *tabwriter.Writer, info imageInfo, count int) {
if count%PageSize == 0 {
w.Flush()
fmt.Println()
printImageRow(w, imageInfo{
RepositoryName: "REPOSITORY NAME",
Tag: "TAG",
ImageDigest: "IMAGE DIGEST",
PushedAt: "PUSHED AT",
Size: "SIZE",
})
}
printImageRow(w, info)
}
func printImageRow(w io.Writer, info imageInfo) {
fmt.Fprintf(w, "%s\t%s\t%s\t%s\t%s\t\n",
info.RepositoryName,
info.Tag,
info.ImageDigest,
info.PushedAt,
info.Size,
)
}
func getTagStatus(c *cli.Context) string {
if c.Bool(flags.TaggedFlag) && c.Bool(flags.UntaggedFlag) {
return ""
}
if c.Bool(flags.TaggedFlag) {
return ecr.TagStatusTagged
}
if c.Bool(flags.UntaggedFlag) {
return ecr.TagStatusUntagged
}
return ""
}
func getRegistryID(registryID string, stsClient stsclient.Client) (string, error) {
if registryID == "" {
return stsClient.GetAWSAccountID()
}
return registryID, nil
}
func getECRAuth(registryURI string, registryID string,
stsClient stsclient.Client, ecrClient ecrclient.Client) (*ecrclient.Auth, error) {
if registryURI == "" {
id, err := getRegistryID(registryID, stsClient)
if err != nil {
return nil, err
}
return ecrClient.GetAuthorizationTokenByID(id)
}
return ecrClient.GetAuthorizationToken(registryURI)
}
func splitImageName(image string, seperatorRegExp string,
format string) (registry string, repository string, tag string, err error) {
re := regexp.MustCompile(
`^(?:((?:[a-zA-Z0-9][a-zA-Z0-9-_]*)\.dkr\.ecr\.[a-zA-Z0-9\-_]+\.amazonaws\.com(?:\.cn)?)/)?` + // repository uri (Optional)
`([0-9a-z\-_/]+)` + // repository
`(?:` + seperatorRegExp + `([0-9A-Za-z_.\-:]+))?$`) // tag (Optional)
matches := re.FindStringSubmatch(image)
if len(matches) == 0 {
return "", "", "", fmt.Errorf("Please specify the image name in the correct format [%s]", format)
}
return matches[1], matches[2], matches[3], nil
}