forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebase.go
76 lines (63 loc) · 1.88 KB
/
rebase.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
package pack
import (
"context"
"github.com/buildpacks/lifecycle"
"github.com/pkg/errors"
"github.com/buildpacks/pack/internal/builder"
"github.com/buildpacks/pack/internal/dist"
"github.com/buildpacks/pack/internal/style"
)
type RebaseOptions struct {
RepoName string
Publish bool
SkipPull bool
RunImage string
AdditionalMirrors map[string][]string
}
func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error {
imageRef, err := c.parseTagReference(opts.RepoName)
if err != nil {
return errors.Wrapf(err, "invalid image name '%s'", opts.RepoName)
}
appImage, err := c.imageFetcher.Fetch(ctx, opts.RepoName, !opts.Publish, !opts.SkipPull)
if err != nil {
return err
}
var md lifecycle.LayersMetadataCompat
if ok, err := dist.GetLabel(appImage, lifecycle.LayerMetadataLabel, &md); err != nil {
return err
} else if !ok {
return errors.Errorf("could not find label %s on image", style.Symbol(lifecycle.LayerMetadataLabel))
}
runImageName := c.resolveRunImage(
opts.RunImage,
imageRef.Context().RegistryStr(),
"",
builder.StackMetadata{
RunImage: builder.RunImageMetadata{
Image: md.Stack.RunImage.Image,
Mirrors: md.Stack.RunImage.Mirrors,
},
},
opts.AdditionalMirrors,
opts.Publish)
if runImageName == "" {
return errors.New("run image must be specified")
}
baseImage, err := c.imageFetcher.Fetch(ctx, runImageName, !opts.Publish, !opts.SkipPull)
if err != nil {
return err
}
c.logger.Infof("Rebasing %s on run image %s", style.Symbol(appImage.Name()), style.Symbol(baseImage.Name()))
rebaser := &lifecycle.Rebaser{Logger: c.logger}
err = rebaser.Rebase(appImage, baseImage, nil)
if err != nil {
return err
}
appImageIdentifier, err := appImage.Identifier()
if err != nil {
return err
}
c.logger.Infof("Rebased Image: %s", style.Symbol(appImageIdentifier.String()))
return nil
}