/
push.go
299 lines (273 loc) · 8.39 KB
/
push.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
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
package commands
import (
"bytes"
"context"
"fmt"
"io"
"io/ioutil"
"os"
"strings"
"github.com/containerd/containerd/platforms"
"github.com/deislabs/cnab-go/bundle"
"github.com/docker/app/types/metadata"
"github.com/docker/cli/cli"
"github.com/docker/cli/cli/command"
"github.com/docker/cnab-to-oci/remotes"
"github.com/docker/distribution/reference"
"github.com/docker/docker/api/types"
"github.com/docker/docker/pkg/jsonmessage"
"github.com/docker/docker/pkg/term"
"github.com/docker/docker/registry"
"github.com/morikuni/aec"
"github.com/pkg/errors"
"github.com/spf13/cobra"
)
type pushOptions struct {
registry registryOptions
tag string
platforms []string
}
func pushCmd(dockerCli command.Cli) *cobra.Command {
var opts pushOptions
cmd := &cobra.Command{
Use: "push [APP_NAME] --tag TARGET_REFERENCE [OPTIONS]",
Short: "Push an application package to a registry",
Example: `$ docker app push myapp --tag myrepo/myapp:mytag`,
Args: cli.RequiresMaxArgs(1),
RunE: func(cmd *cobra.Command, args []string) error {
return runPush(dockerCli, firstOrEmpty(args), opts)
},
}
flags := cmd.Flags()
flags.StringVarP(&opts.tag, "tag", "t", "", "Target registry reference (default: <name>:<version> from metadata)")
flags.StringSliceVar(&opts.platforms, "platform", nil, "For multi-arch service images, only push the specified platforms")
opts.registry.addFlags(flags)
return cmd
}
func runPush(dockerCli command.Cli, name string, opts pushOptions) error {
defer muteDockerCli(dockerCli)()
bundleStore, err := prepareBundleStore()
if err != nil {
return err
}
bndl, _, err := resolveBundle(dockerCli, bundleStore, name, false, nil)
if err != nil {
return err
}
if err := bndl.Validate(); err != nil {
return err
}
retag, err := shouldRetagInvocationImage(metadata.FromBundle(bndl), bndl, opts.tag)
if err != nil {
return err
}
if retag.shouldRetag {
err := retagInvocationImage(dockerCli, bndl, retag.invocationImageRef.String())
if err != nil {
return err
}
}
// pushing invocation image
repoInfo, err := registry.ParseRepositoryInfo(retag.invocationImageRef)
if err != nil {
return err
}
encodedAuth, err := command.EncodeAuthToBase64(command.ResolveAuthConfig(context.Background(), dockerCli, repoInfo.Index))
if err != nil {
return err
}
reader, err := dockerCli.Client().ImagePush(context.Background(), retag.invocationImageRef.String(), types.ImagePushOptions{
RegistryAuth: encodedAuth,
})
if err != nil {
return errors.Wrapf(err, "starting push of %q", retag.invocationImageRef.String())
}
defer reader.Close()
if err = jsonmessage.DisplayJSONMessagesStream(reader, ioutil.Discard, 0, false, nil); err != nil {
return errors.Wrapf(err, "pushing to %q", retag.invocationImageRef.String())
}
resolverConfig := remotes.NewResolverConfigFromDockerConfigFile(dockerCli.ConfigFile(), opts.registry.insecureRegistries...)
var display fixupDisplay = &plainDisplay{out: os.Stdout}
if term.IsTerminal(os.Stdout.Fd()) {
display = &interactiveDisplay{out: os.Stdout}
}
fixupOptions := []remotes.FixupOption{
remotes.WithEventCallback(display.onEvent),
}
if len(opts.platforms) > 0 {
fixupOptions = append(fixupOptions, remotes.WithComponentImagePlatforms(opts.platforms))
}
// bundle fixup
err = remotes.FixupBundle(context.Background(), bndl, retag.cnabRef, resolverConfig, fixupOptions...)
if err != nil {
return errors.Wrapf(err, "fixing up %q for push", retag.cnabRef)
}
// push bundle manifest
descriptor, err := remotes.Push(context.Background(), bndl, retag.cnabRef, resolverConfig.Resolver, true)
if err != nil {
return errors.Wrapf(err, "pushing to %q", retag.cnabRef)
}
fmt.Printf("Successfully pushed bundle to %s. Digest is %s.\n", retag.cnabRef.String(), descriptor.Digest)
return nil
}
func retagInvocationImage(dockerCli command.Cli, bndl *bundle.Bundle, newName string) error {
err := dockerCli.Client().ImageTag(context.Background(), bndl.InvocationImages[0].Image, newName)
if err != nil {
return err
}
bndl.InvocationImages[0].Image = newName
return nil
}
type retagResult struct {
shouldRetag bool
cnabRef reference.Named
invocationImageRef reference.Named
}
func shouldRetagInvocationImage(meta metadata.AppMetadata, bndl *bundle.Bundle, tagOverride string) (retagResult, error) {
imgName := tagOverride
var err error
if imgName == "" {
imgName, err = makeCNABImageName(meta.Name, meta.Version, "")
if err != nil {
return retagResult{}, err
}
}
cnabRef, err := reference.ParseNormalizedNamed(imgName)
if err != nil {
return retagResult{}, errors.Wrap(err, imgName)
}
if _, digested := cnabRef.(reference.Digested); digested {
return retagResult{}, errors.Errorf("%s: can't push to a digested reference", cnabRef)
}
cnabRef = reference.TagNameOnly(cnabRef)
expectedInvocationImageRef, err := reference.ParseNormalizedNamed(reference.TagNameOnly(cnabRef).String() + "-invoc")
if err != nil {
return retagResult{}, errors.Wrap(err, reference.TagNameOnly(cnabRef).String()+"-invoc")
}
currentInvocationImageRef, err := reference.ParseNormalizedNamed(bndl.InvocationImages[0].Image)
if err != nil {
return retagResult{}, errors.Wrap(err, bndl.InvocationImages[0].Image)
}
return retagResult{
cnabRef: cnabRef,
invocationImageRef: expectedInvocationImageRef,
shouldRetag: expectedInvocationImageRef.String() != currentInvocationImageRef.String(),
}, nil
}
type fixupDisplay interface {
onEvent(remotes.FixupEvent)
}
type interactiveDisplay struct {
out io.Writer
previousLineCount int
images []interactiveImageState
}
func (r *interactiveDisplay) onEvent(ev remotes.FixupEvent) {
out := bytes.NewBuffer(nil)
for i := 0; i < r.previousLineCount; i++ {
fmt.Fprint(out, aec.NewBuilder(aec.Up(1), aec.EraseLine(aec.EraseModes.All)).ANSI)
}
switch ev.EventType {
case remotes.FixupEventTypeCopyImageStart:
r.images = append(r.images, interactiveImageState{name: ev.SourceImage})
case remotes.FixupEventTypeCopyImageEnd:
r.images[r.imageIndex(ev.SourceImage)].done = true
case remotes.FixupEventTypeProgress:
r.images[r.imageIndex(ev.SourceImage)].onProgress(ev.Progress)
}
r.previousLineCount = 0
for _, s := range r.images {
r.previousLineCount += s.print(out)
}
r.out.Write(out.Bytes()) //nolint:errcheck // nothing much we can do with an error to write to output.
}
func (r *interactiveDisplay) imageIndex(name string) int {
for ix, state := range r.images {
if state.name == name {
return ix
}
}
return 0
}
type interactiveImageState struct {
name string
progress remotes.ProgressSnapshot
done bool
}
func (s *interactiveImageState) onProgress(p remotes.ProgressSnapshot) {
s.progress = p
}
func (s *interactiveImageState) print(out io.Writer) int {
if s.done {
fmt.Fprint(out, aec.Apply(s.name, aec.BlueF))
} else {
fmt.Fprint(out, s.name)
}
fmt.Fprint(out, "\n")
lineCount := 1
for _, p := range s.progress.Roots {
lineCount += printDescriptorProgress(out, &p, 1)
}
return lineCount
}
func printDescriptorProgress(out io.Writer, p *remotes.DescriptorProgressSnapshot, depth int) int {
fmt.Fprint(out, strings.Repeat(" ", depth))
name := p.MediaType
if p.Platform != nil {
name = platforms.Format(*p.Platform)
}
if len(p.Children) == 0 {
name = fmt.Sprintf("%s...: %s", p.Digest.String()[:15], p.Action)
}
doneCount := 0
for _, c := range p.Children {
if c.Done {
doneCount++
}
}
display := name
if len(p.Children) > 0 {
display = fmt.Sprintf("%s [%d/%d] (%s...)", name, doneCount, len(p.Children), p.Digest.String()[:15])
}
if p.Done {
display = aec.Apply(display, aec.BlueF)
}
if hasError(p) {
display = aec.Apply(display, aec.RedF)
}
fmt.Fprintln(out, display)
lineCount := 1
if p.Done {
return lineCount
}
for _, c := range p.Children {
lineCount += printDescriptorProgress(out, &c, depth+1)
}
return lineCount
}
func hasError(p *remotes.DescriptorProgressSnapshot) bool {
if p.Error != nil {
return true
}
for _, c := range p.Children {
if hasError(&c) {
return true
}
}
return false
}
type plainDisplay struct {
out io.Writer
}
func (r *plainDisplay) onEvent(ev remotes.FixupEvent) {
switch ev.EventType {
case remotes.FixupEventTypeCopyImageStart:
fmt.Fprintf(r.out, "Handling image %s...", ev.SourceImage)
case remotes.FixupEventTypeCopyImageEnd:
if ev.Error != nil {
fmt.Fprintf(r.out, "\nFailure: %s\n", ev.Error)
} else {
fmt.Fprint(r.out, " done!\n")
}
}
}