-
Notifications
You must be signed in to change notification settings - Fork 0
/
push.go
111 lines (91 loc) · 3.25 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
// SPDX-License-Identifier: Apache-2.0
// SPDX-FileCopyrightText: 2021-Present The Jackal Authors
// Package zoci contains functions for interacting with Jackal packages stored in OCI registries.
package zoci
import (
"context"
"fmt"
"github.com/defenseunicorns/pkg/helpers"
"github.com/defenseunicorns/pkg/oci"
ocispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/racer159/jackal/src/pkg/layout"
"github.com/racer159/jackal/src/pkg/message"
"github.com/racer159/jackal/src/types"
"oras.land/oras-go/v2"
"oras.land/oras-go/v2/content/file"
)
// PublishPackage publishes the jackal package to the remote repository.
func (r *Remote) PublishPackage(ctx context.Context, pkg *types.JackalPackage, paths *layout.PackagePaths, concurrency int) error {
src, err := file.New(paths.Base)
if err != nil {
return err
}
defer src.Close()
r.Log().Info(fmt.Sprintf("Publishing package to %s", r.Repo().Reference))
spinner := message.NewProgressSpinner("")
defer spinner.Stop()
// Get all of the layers in the package
var descs []ocispec.Descriptor
for name, path := range paths.Files() {
spinner.Updatef("Preparing layer %s", helpers.First30last30(name))
mediaType := JackalLayerMediaTypeBlob
desc, err := src.Add(ctx, name, mediaType, path)
if err != nil {
return err
}
descs = append(descs, desc)
}
spinner.Successf("Prepared all layers")
copyOpts := r.GetDefaultCopyOpts()
copyOpts.Concurrency = concurrency
total := oci.SumDescsSize(descs)
annotations := annotationsFromMetadata(&pkg.Metadata)
// assumes referrers API is not supported since OCI artifact
// media type is not supported
r.Repo().SetReferrersCapability(false)
// push the manifest config
manifestConfigDesc, err := r.CreateAndPushManifestConfig(ctx, annotations, JackalConfigMediaType)
if err != nil {
return err
}
root, err := r.PackAndTagManifest(ctx, src, descs, manifestConfigDesc, annotations)
if err != nil {
return err
}
total += manifestConfigDesc.Size
progressBar := message.NewProgressBar(total, fmt.Sprintf("Publishing %s:%s", r.Repo().Reference.Repository, r.Repo().Reference.Reference))
defer progressBar.Stop()
r.SetProgressWriter(progressBar)
defer r.ClearProgressWriter()
publishedDesc, err := oras.Copy(ctx, src, root.Digest.String(), r.Repo(), "", copyOpts)
if err != nil {
return err
}
if err := r.UpdateIndex(ctx, r.Repo().Reference.Reference, publishedDesc); err != nil {
return err
}
progressBar.Successf("Published %s [%s]", r.Repo().Reference, JackalLayerMediaTypeBlob)
return nil
}
func annotationsFromMetadata(metadata *types.JackalMetadata) map[string]string {
annotations := map[string]string{
ocispec.AnnotationTitle: metadata.Name,
ocispec.AnnotationDescription: metadata.Description,
}
if url := metadata.URL; url != "" {
annotations[ocispec.AnnotationURL] = url
}
if authors := metadata.Authors; authors != "" {
annotations[ocispec.AnnotationAuthors] = authors
}
if documentation := metadata.Documentation; documentation != "" {
annotations[ocispec.AnnotationDocumentation] = documentation
}
if source := metadata.Source; source != "" {
annotations[ocispec.AnnotationSource] = source
}
if vendor := metadata.Vendor; vendor != "" {
annotations[ocispec.AnnotationVendor] = vendor
}
return annotations
}