Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Store entire bundle, generate relocation map #65

Merged
merged 1 commit into from
Sep 12, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Gopkg.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 17 additions & 10 deletions cmd/cnab-to-oci/fixup.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package main

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
Expand All @@ -12,14 +11,17 @@ import (
"github.com/docker/cli/cli/config"
"github.com/docker/cnab-to-oci/remotes"
"github.com/docker/distribution/reference"
"github.com/docker/go/canonical/json"
"github.com/spf13/cobra"
)

type fixupOptions struct {
input string
output string
bundle string
relocationMap string
targetRef string
insecureRegistries []string
autoUpdateBundle bool
}

func fixupCmd() *cobra.Command {
Expand All @@ -34,9 +36,11 @@ func fixupCmd() *cobra.Command {
return runFixup(opts)
},
}
cmd.Flags().StringVarP(&opts.output, "output", "o", "fixed-bundle.json", "specify the output file")
cmd.Flags().StringVar(&opts.bundle, "bundle", "fixed-bundle.json", "fixed bundle output file (- to print on standard output)")
cmd.Flags().StringVar(&opts.relocationMap, "relocation-map", "relocation-map.json", "relocation map output file (- to print on standard output)")
cmd.Flags().StringVarP(&opts.targetRef, "target", "t", "", "reference where the bundle will be pushed")
cmd.Flags().StringSliceVar(&opts.insecureRegistries, "insecure-registries", nil, "Use plain HTTP for those registries")
cmd.Flags().BoolVar(&opts.autoUpdateBundle, "auto-update-bundle", false, "Updates the bundle image properties with the one resolved on the registry")
return cmd
}

Expand All @@ -53,18 +57,21 @@ func runFixup(opts fixupOptions) error {
if err != nil {
return err
}
if err := remotes.FixupBundle(context.Background(), &b, ref, createResolver(opts.insecureRegistries), remotes.WithEventCallback(displayEvent)); err != nil {
return err

fixupOptions := []remotes.FixupOption{
remotes.WithEventCallback(displayEvent),
}
bundleJSON, err = json.MarshalIndent(b, "", "\t")
if opts.autoUpdateBundle {
fixupOptions = append(fixupOptions, remotes.WithAutoBundleUpdate())
}
relocationMap, err := remotes.FixupBundle(context.Background(), &b, ref, createResolver(opts.insecureRegistries), fixupOptions...)
if err != nil {
return err
}
if opts.output == "-" {
fmt.Fprintln(os.Stdout, string(bundleJSON))
return nil
if err := writeOutput(opts.bundle, b); err != nil {
return err
}
return ioutil.WriteFile(opts.output, bundleJSON, 0644)
return writeOutput(opts.relocationMap, relocationMap)
}

func displayEvent(ev remotes.FixupEvent) {
Expand Down
24 changes: 17 additions & 7 deletions cmd/cnab-to-oci/pull.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ package main

import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"

"github.com/docker/cnab-to-oci/remotes"
"github.com/docker/distribution/reference"
"github.com/docker/go/canonical/json"
"github.com/spf13/cobra"
)

type pullOptions struct {
output string
bundle string
relocationMap string
targetRef string
insecureRegistries []string
}
Expand All @@ -30,7 +31,8 @@ func pullCmd() *cobra.Command {
},
}

cmd.Flags().StringVarP(&opts.output, "output", "o", "pulled.json", "output file")
cmd.Flags().StringVar(&opts.bundle, "bundle", "pulled.json", "bundle output file (- to print on standard output)")
cmd.Flags().StringVar(&opts.relocationMap, "relocation-map", "relocation-map.json", "relocation map output file (- to print on standard output)")
cmd.Flags().StringSliceVar(&opts.insecureRegistries, "insecure-registries", nil, "Use plain HTTP for those registries")
return cmd
}
Expand All @@ -40,17 +42,25 @@ func runPull(opts pullOptions) error {
if err != nil {
return err
}
b, err := remotes.Pull(context.Background(), ref, createResolver(opts.insecureRegistries))

b, relocationMap, err := remotes.Pull(context.Background(), ref, createResolver(opts.insecureRegistries))
if err != nil {
return err
}
bytes, err := json.MarshalIndent(b, "", "\t")
if err := writeOutput(opts.bundle, b); err != nil {
return err
}
return writeOutput(opts.relocationMap, relocationMap)
}

func writeOutput(file string, data interface{}) error {
bytes, err := json.MarshalCanonical(data)
if err != nil {
return err
}
if opts.output == "-" {
if file == "-" {
fmt.Fprintln(os.Stdout, string(bytes))
return nil
}
return ioutil.WriteFile(opts.output, bytes, 0644)
return ioutil.WriteFile(file, bytes, 0644)
}
15 changes: 12 additions & 3 deletions cmd/cnab-to-oci/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ type pushOptions struct {
allowFallbacks bool
invocationPlatforms []string
componentPlatforms []string
autoUpdateBundle bool
}

func pushCmd() *cobra.Command {
Expand All @@ -42,6 +43,8 @@ func pushCmd() *cobra.Command {
cmd.Flags().BoolVar(&opts.allowFallbacks, "allow-fallbacks", true, "Enable automatic compatibility fallbacks for registries without support for custom media type, or OCI manifests")
cmd.Flags().StringSliceVar(&opts.invocationPlatforms, "invocation-platforms", nil, "Platforms to push (for multi-arch invocation images)")
cmd.Flags().StringSliceVar(&opts.componentPlatforms, "component-platforms", nil, "Platforms to push (for multi-arch component images)")
cmd.Flags().BoolVar(&opts.autoUpdateBundle, "auto-update-bundle", false, "Updates the bundle image properties with the one resolved on the registry")

return cmd
}

Expand All @@ -60,13 +63,19 @@ func runPush(opts pushOptions) error {
return err
}

err = remotes.FixupBundle(context.Background(), &b, ref, resolver, remotes.WithEventCallback(displayEvent),
fixupOptions := []remotes.FixupOption{
remotes.WithEventCallback(displayEvent),
remotes.WithInvocationImagePlatforms(opts.invocationPlatforms),
remotes.WithComponentImagePlatforms(opts.componentPlatforms))
remotes.WithComponentImagePlatforms(opts.componentPlatforms),
}
if opts.autoUpdateBundle {
fixupOptions = append(fixupOptions, remotes.WithAutoBundleUpdate())
}
relocationMap, err := remotes.FixupBundle(context.Background(), &b, ref, resolver, fixupOptions...)
if err != nil {
return err
}
d, err := remotes.Push(context.Background(), &b, ref, resolver, opts.allowFallbacks)
d, err := remotes.Push(context.Background(), &b, relocationMap, ref, resolver, opts.allowFallbacks)
if err != nil {
return err
}
Expand Down
Loading