|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "context" |
| 6 | + "encoding/json" |
| 7 | + "fmt" |
| 8 | + "io/ioutil" |
| 9 | + |
| 10 | + "github.com/deis/duffle/pkg/bundle" |
| 11 | + "github.com/docker/app/internal/packager" |
| 12 | + "github.com/docker/app/types" |
| 13 | + "github.com/docker/app/types/metadata" |
| 14 | + "github.com/docker/cli/cli" |
| 15 | + "github.com/docker/cli/cli/command" |
| 16 | + "github.com/docker/distribution/reference" |
| 17 | + dockertypes "github.com/docker/docker/api/types" |
| 18 | + "github.com/docker/docker/pkg/jsonmessage" |
| 19 | + "github.com/pkg/errors" |
| 20 | + "github.com/spf13/cobra" |
| 21 | +) |
| 22 | + |
| 23 | +type bundleOptions struct { |
| 24 | + invocationImageName string |
| 25 | + namespace string |
| 26 | + out string |
| 27 | +} |
| 28 | + |
| 29 | +func bundleCmd(dockerCli command.Cli) *cobra.Command { |
| 30 | + var opts bundleOptions |
| 31 | + cmd := &cobra.Command{ |
| 32 | + Use: "bundle [<app-name>]", |
| 33 | + Short: "Create a CNAB invocation image and bundle.json for the application.", |
| 34 | + Args: cli.RequiresMaxArgs(1), |
| 35 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 36 | + return runBundle(dockerCli, firstOrEmpty(args), opts) |
| 37 | + }, |
| 38 | + } |
| 39 | + |
| 40 | + cmd.Flags().StringVarP(&opts.invocationImageName, "invocation-image", "i", "", "specify the name of invocation image to build") |
| 41 | + cmd.Flags().StringVar(&opts.namespace, "namespace", "", "namespace to use (default: namespace in metadata)") |
| 42 | + cmd.Flags().StringVarP(&opts.out, "out", "o", "bundle.json", "path to the output bundle.json (- for stdout)") |
| 43 | + return cmd |
| 44 | +} |
| 45 | + |
| 46 | +func runBundle(dockerCli command.Cli, appName string, opts bundleOptions) error { |
| 47 | + bundle, err := makeBundle(dockerCli, appName, opts.namespace, opts.invocationImageName) |
| 48 | + if err != nil { |
| 49 | + return err |
| 50 | + } |
| 51 | + if bundle == nil || len(bundle.InvocationImages) == 0 { |
| 52 | + return fmt.Errorf("failed to create bundle %q", appName) |
| 53 | + } |
| 54 | + fmt.Fprintf(dockerCli.Out(), "Invocation image %q successfully built\n", bundle.InvocationImages[0].Image) |
| 55 | + bundleBytes, err := json.MarshalIndent(bundle, "", "\t") |
| 56 | + if err != nil { |
| 57 | + return err |
| 58 | + } |
| 59 | + if opts.out == "-" { |
| 60 | + _, err = dockerCli.Out().Write(bundleBytes) |
| 61 | + return err |
| 62 | + } |
| 63 | + return ioutil.WriteFile(opts.out, bundleBytes, 0644) |
| 64 | +} |
| 65 | + |
| 66 | +func makeBundle(dockerCli command.Cli, appName, namespace, invocationImageName string) (*bundle.Bundle, error) { |
| 67 | + app, err := packager.Extract(appName) |
| 68 | + if err != nil { |
| 69 | + return nil, err |
| 70 | + } |
| 71 | + defer app.Cleanup() |
| 72 | + return makeBundleFromApp(dockerCli, app, namespace, invocationImageName) |
| 73 | +} |
| 74 | + |
| 75 | +func makeBundleFromApp(dockerCli command.Cli, app *types.App, namespace, invocationImageName string) (*bundle.Bundle, error) { |
| 76 | + meta := app.Metadata() |
| 77 | + invocationImageName, err := makeImageName(meta, namespace, invocationImageName, "-invoc") |
| 78 | + if err != nil { |
| 79 | + return nil, err |
| 80 | + } |
| 81 | + if _, err := makeImageName(app.Metadata(), namespace, "", ""); err != nil { |
| 82 | + return nil, err |
| 83 | + } |
| 84 | + |
| 85 | + buildContext := bytes.NewBuffer(nil) |
| 86 | + if err := packager.PackInvocationImageContext(app, buildContext); err != nil { |
| 87 | + return nil, err |
| 88 | + } |
| 89 | + |
| 90 | + buildResp, err := dockerCli.Client().ImageBuild(context.TODO(), buildContext, dockertypes.ImageBuildOptions{ |
| 91 | + Dockerfile: "Dockerfile", |
| 92 | + Tags: []string{invocationImageName}, |
| 93 | + }) |
| 94 | + if err != nil { |
| 95 | + return nil, err |
| 96 | + } |
| 97 | + defer buildResp.Body.Close() |
| 98 | + |
| 99 | + if err := jsonmessage.DisplayJSONMessagesStream(buildResp.Body, ioutil.Discard, 0, false, func(jsonmessage.JSONMessage) {}); err != nil { |
| 100 | + return nil, err |
| 101 | + } |
| 102 | + return packager.ToCNAB(app, invocationImageName), nil |
| 103 | +} |
| 104 | + |
| 105 | +func makeImageName(meta metadata.AppMetadata, namespace, name, suffix string) (string, error) { |
| 106 | + if name == "" { |
| 107 | + name = fmt.Sprintf("%s:%s%s", meta.Name, meta.Version, suffix) |
| 108 | + } |
| 109 | + if namespace == "" { |
| 110 | + namespace = meta.Namespace |
| 111 | + } |
| 112 | + if namespace != "" { |
| 113 | + name = fmt.Sprintf("%s/%s", namespace, name) |
| 114 | + } |
| 115 | + if _, err := reference.ParseNormalizedNamed(name); err != nil { |
| 116 | + return "", errors.Wrapf(err, "image name %q is invalid, please check namespace, name and version fields", name) |
| 117 | + } |
| 118 | + return name, nil |
| 119 | +} |
0 commit comments