@@ -2,20 +2,23 @@ package cnab
22
33import (
44 "bytes"
5+ "encoding/json"
56 "io"
67 "os"
78 "strings"
89
10+ "github.com/pkg/errors"
11+
912 "github.com/docker/app/internal/cliopts"
10- store2 "github.com/docker/app/internal/store"
13+ "github.com/docker/app/internal/store"
1114
1215 "github.com/deislabs/cnab-go/claim"
1316 "github.com/deislabs/cnab-go/driver"
1417 dockerDriver "github.com/deislabs/cnab-go/driver/docker"
1518 "github.com/docker/app/internal"
1619 "github.com/docker/cli/cli/command"
1720 "github.com/docker/cli/cli/context/docker"
18- "github.com/docker/cli/cli/context/store"
21+ cliContext "github.com/docker/cli/cli/context/store"
1922 "github.com/docker/docker/api/types/container"
2023 "github.com/docker/docker/api/types/mount"
2124)
@@ -39,7 +42,7 @@ func RequiredClaimBindMount(c claim.Claim, dockerCli command.Cli) (BindMount, er
3942
4043// RequiredBindMount Returns the path required to bind mount when running
4144// the invocation image.
42- func RequiredBindMount (targetContextName string , targetOrchestrator string , s store .Store ) (BindMount , error ) {
45+ func RequiredBindMount (targetContextName string , targetOrchestrator string , s cliContext .Store ) (BindMount , error ) {
4346 if targetOrchestrator == "kubernetes" {
4447 return BindMount {}, nil
4548 }
@@ -119,7 +122,7 @@ func prepareDriver(dockerCli command.Cli, bindMount BindMount, stdout io.Writer)
119122 return d , errBuf
120123}
121124
122- func SetupDriver (installation * store2 .Installation , dockerCli command.Cli , opts * cliopts.InstallerContextOptions , stdout io.Writer ) (driver.Driver , * bytes.Buffer , error ) {
125+ func SetupDriver (installation * store .Installation , dockerCli command.Cli , opts * cliopts.InstallerContextOptions , stdout io.Writer ) (driver.Driver , * bytes.Buffer , error ) {
123126 dockerCli , err := opts .SetInstallerContext (dockerCli )
124127 if err != nil {
125128 return nil , nil , err
@@ -131,3 +134,31 @@ func SetupDriver(installation *store2.Installation, dockerCli command.Cli, opts
131134 driverImpl , errBuf := prepareDriver (dockerCli , bind , stdout )
132135 return driverImpl , errBuf , nil
133136}
137+
138+ func WithRelocationMap (installation * store.Installation ) func (op * driver.Operation ) error {
139+ return func (op * driver.Operation ) error {
140+ if err := addRelocationMapToFiles (op , installation ); err != nil {
141+ return err
142+ }
143+ relocateInvocationImage (op , installation )
144+ return nil
145+ }
146+ }
147+
148+ func addRelocationMapToFiles (op * driver.Operation , installation * store.Installation ) error {
149+ data , err := json .Marshal (installation .RelocationMap )
150+ if err != nil {
151+ return errors .Wrap (err , "could not marshal relocation map" )
152+ }
153+ op .Files ["/cnab/app/relocation-mapping.json" ] = string (data )
154+
155+ return nil
156+ }
157+
158+ func relocateInvocationImage (op * driver.Operation , installation * store.Installation ) {
159+ invocImage := op .Image
160+ if relocatedImage , ok := installation .RelocationMap [invocImage .Image ]; ok {
161+ invocImage .Image = relocatedImage
162+ op .Image = invocImage
163+ }
164+ }
0 commit comments