|
| 1 | +package cnab |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "fmt" |
| 6 | + "io/ioutil" |
| 7 | + "os" |
| 8 | + "strings" |
| 9 | + |
| 10 | + "github.com/deislabs/cnab-go/bundle" |
| 11 | + "github.com/docker/app/internal" |
| 12 | + "github.com/docker/app/internal/log" |
| 13 | + "github.com/docker/app/internal/packager" |
| 14 | + "github.com/docker/app/internal/store" |
| 15 | + appstore "github.com/docker/app/internal/store" |
| 16 | + "github.com/docker/cli/cli/command" |
| 17 | + "github.com/docker/cnab-to-oci/remotes" |
| 18 | + "github.com/docker/distribution/reference" |
| 19 | +) |
| 20 | + |
| 21 | +type nameKind uint |
| 22 | + |
| 23 | +const ( |
| 24 | + _ nameKind = iota |
| 25 | + nameKindEmpty |
| 26 | + nameKindFile |
| 27 | + nameKindDir |
| 28 | + nameKindReference |
| 29 | +) |
| 30 | + |
| 31 | +func getAppNameKind(name string) (string, nameKind) { |
| 32 | + if name == "" { |
| 33 | + return name, nameKindEmpty |
| 34 | + } |
| 35 | + // name can be a bundle.json or bundle.cnab file, or a dockerapp directory |
| 36 | + st, err := os.Stat(name) |
| 37 | + if os.IsNotExist(err) { |
| 38 | + // try with .dockerapp extension |
| 39 | + st, err = os.Stat(name + internal.AppExtension) |
| 40 | + if err == nil { |
| 41 | + name += internal.AppExtension |
| 42 | + } |
| 43 | + } |
| 44 | + if err != nil { |
| 45 | + return name, nameKindReference |
| 46 | + } |
| 47 | + if st.IsDir() { |
| 48 | + return name, nameKindDir |
| 49 | + } |
| 50 | + return name, nameKindFile |
| 51 | +} |
| 52 | + |
| 53 | +func extractAndLoadAppBasedBundle(dockerCli command.Cli, name string) (*bundle.Bundle, string, error) { |
| 54 | + app, err := packager.Extract(name) |
| 55 | + if err != nil { |
| 56 | + return nil, "", err |
| 57 | + } |
| 58 | + defer app.Cleanup() |
| 59 | + bndl, err := packager.MakeBundleFromApp(dockerCli, app, nil) |
| 60 | + return bndl, "", err |
| 61 | +} |
| 62 | + |
| 63 | +func loadBundleFromFile(filename string) (*bundle.Bundle, error) { |
| 64 | + b := &bundle.Bundle{} |
| 65 | + data, err := ioutil.ReadFile(filename) |
| 66 | + if err != nil { |
| 67 | + return b, err |
| 68 | + } |
| 69 | + return bundle.Unmarshal(data) |
| 70 | +} |
| 71 | + |
| 72 | +// ResolveBundle looks for a CNAB bundle which can be in a Docker App Package format or |
| 73 | +// a bundle stored locally or in the bundle store. It returns a built or found bundle, |
| 74 | +// a reference to the bundle if it is found in the bundlestore, and an error. |
| 75 | +func ResolveBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name string) (*bundle.Bundle, string, error) { |
| 76 | + // resolution logic: |
| 77 | + // - if there is a docker-app package in working directory, or an http:// / https:// prefix, use packager.Extract result |
| 78 | + // - the name has a .json or .cnab extension and refers to an existing file or web resource: load the bundle |
| 79 | + // - name matches a bundle name:version stored in the bundle store: use it |
| 80 | + // - pull the bundle from the registry and add it to the bundle store |
| 81 | + name, kind := getAppNameKind(name) |
| 82 | + switch kind { |
| 83 | + case nameKindFile: |
| 84 | + if strings.HasSuffix(name, internal.AppExtension) { |
| 85 | + return extractAndLoadAppBasedBundle(dockerCli, name) |
| 86 | + } |
| 87 | + bndl, err := loadBundleFromFile(name) |
| 88 | + return bndl, "", err |
| 89 | + case nameKindDir, nameKindEmpty: |
| 90 | + return extractAndLoadAppBasedBundle(dockerCli, name) |
| 91 | + case nameKindReference: |
| 92 | + bndl, tagRef, err := GetBundle(dockerCli, bundleStore, name) |
| 93 | + if err != nil { |
| 94 | + return nil, "", err |
| 95 | + } |
| 96 | + return bndl, tagRef.String(), err |
| 97 | + } |
| 98 | + return nil, "", fmt.Errorf("could not resolve bundle %q", name) |
| 99 | +} |
| 100 | + |
| 101 | +// GetBundle searches for the bundle locally and tries to pull it if not found |
| 102 | +func GetBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, name string) (*bundle.Bundle, reference.Reference, error) { |
| 103 | + ref, err := store.StringToRef(name) |
| 104 | + if err != nil { |
| 105 | + return nil, nil, err |
| 106 | + } |
| 107 | + bndl, err := bundleStore.Read(ref) |
| 108 | + if err != nil { |
| 109 | + fmt.Fprintf(dockerCli.Err(), "Unable to find application image %q locally\n", reference.FamiliarString(ref)) |
| 110 | + |
| 111 | + fmt.Fprintf(dockerCli.Out(), "Pulling from registry...\n") |
| 112 | + if named, ok := ref.(reference.Named); ok { |
| 113 | + bndl, err = PullBundle(dockerCli, bundleStore, named) |
| 114 | + if err != nil { |
| 115 | + return nil, nil, err |
| 116 | + } |
| 117 | + } |
| 118 | + } |
| 119 | + |
| 120 | + return bndl, ref, nil |
| 121 | +} |
| 122 | + |
| 123 | +// PullBundle pulls the bundle and stores it into the bundle store |
| 124 | +func PullBundle(dockerCli command.Cli, bundleStore appstore.BundleStore, tagRef reference.Named) (*bundle.Bundle, error) { |
| 125 | + insecureRegistries, err := internal.InsecureRegistriesFromEngine(dockerCli) |
| 126 | + if err != nil { |
| 127 | + return nil, fmt.Errorf("could not retrieve insecure registries: %v", err) |
| 128 | + } |
| 129 | + |
| 130 | + bndl, err := remotes.Pull(log.WithLogContext(context.Background()), reference.TagNameOnly(tagRef), remotes.CreateResolver(dockerCli.ConfigFile(), insecureRegistries...)) |
| 131 | + if err != nil { |
| 132 | + return nil, err |
| 133 | + } |
| 134 | + if _, err := bundleStore.Store(tagRef, bndl); err != nil { |
| 135 | + return nil, err |
| 136 | + } |
| 137 | + return bndl, nil |
| 138 | +} |
0 commit comments