-
Notifications
You must be signed in to change notification settings - Fork 19
/
main.go
153 lines (133 loc) · 3.75 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
/*
* Copyright 2017-2018 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 main
import (
"context"
"fmt"
"os"
"strconv"
"text/tabwriter"
"time"
"github.com/awslabs/amazon-ecr-containerd-resolver/ecr"
"github.com/containerd/containerd"
"github.com/containerd/containerd/images"
"github.com/containerd/containerd/log"
"github.com/containerd/containerd/namespaces"
"github.com/containerd/containerd/pkg/progress"
"github.com/containerd/containerd/remotes"
"github.com/containerd/containerd/remotes/docker"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/sirupsen/logrus"
"golang.org/x/sync/errgroup"
)
const (
// Default to no debug logging.
defaultEnableDebug = 0
)
func main() {
ctx := namespaces.NamespaceFromEnv(context.Background())
//logrus.SetLevel(logrus.DebugLevel)
if len(os.Args) < 2 {
log.G(ctx).Fatal("Must provide image to push as argument")
}
ref := os.Args[1]
local := ""
if len(os.Args) > 2 {
local = os.Args[2]
} else {
local = ref
}
enableDebug := defaultEnableDebug
parseEnvInt(ctx, "ECR_PUSH_DEBUG", &enableDebug)
if enableDebug == 1 {
log.L.Logger.SetLevel(logrus.TraceLevel)
}
client, err := containerd.New("/run/containerd/containerd.sock")
if err != nil {
log.G(ctx).WithError(err).Fatal("Failed to connect to containerd")
}
defer client.Close()
tracker := docker.NewInMemoryTracker()
resolver, err := ecr.NewResolver(ecr.WithTracker(tracker))
if err != nil {
log.G(ctx).WithError(err).Fatal("Failed to create resolver")
}
img, err := client.ImageService().Get(ctx, local)
if err != nil {
fmt.Println(err)
os.Exit(1)
}
ongoing := newPushJobs(tracker)
eg, ctx := errgroup.WithContext(ctx)
eg.Go(func() error {
log.G(ctx).WithField("local", local).WithField("ref", ref).Info("Pushing to Amazon ECR")
desc := img.Target
jobHandler := images.HandlerFunc(func(ctx context.Context, desc ocispec.Descriptor) ([]ocispec.Descriptor, error) {
ongoing.add(remotes.MakeRefKey(ctx, desc))
return nil, nil
})
return client.Push(ctx, ref, desc,
containerd.WithResolver(resolver),
containerd.WithImageHandler(jobHandler))
})
errs := make(chan error)
go func() {
defer close(errs)
errs <- eg.Wait()
}()
err = displayUploadProgress(ctx, ongoing, errs)
if err != nil {
log.G(ctx).WithError(err).WithField("ref", ref).Fatal("Failed to push")
}
log.G(ctx).WithField("ref", ref).Info("Pushed successfully!")
}
func displayUploadProgress(ctx context.Context, ongoing *pushjobs, errs chan error) error {
var (
ticker = time.NewTicker(100 * time.Millisecond)
fw = progress.NewWriter(os.Stdout)
start = time.Now()
done bool
)
defer ticker.Stop()
for {
select {
case <-ticker.C:
fw.Flush()
tw := tabwriter.NewWriter(fw, 1, 8, 1, ' ', 0)
display(tw, ongoing.status(), start)
tw.Flush()
if done {
fw.Flush()
return nil
}
case err := <-errs:
if err != nil {
return err
}
done = true
case <-ctx.Done():
done = true // allow ui to update once more
}
}
}
func parseEnvInt(ctx context.Context, varname string, val *int) {
if varval := os.Getenv(varname); varval != "" {
parsed, err := strconv.Atoi(varval)
if err != nil {
log.G(ctx).WithError(err).Fatalf("Failed to parse %s", varname)
}
*val = parsed
}
}