forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebase.go
111 lines (93 loc) · 2.69 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
package pack
import (
"context"
"encoding/json"
"github.com/buildpack/imgutil"
"github.com/buildpack/lifecycle/metadata"
"github.com/pkg/errors"
"github.com/buildpack/pack/config"
"github.com/buildpack/pack/style"
)
type RebaseOptions struct {
RepoName string
Publish bool
SkipPull bool
RunImage string
}
func (c *Client) Rebase(ctx context.Context, opts RebaseOptions) error {
appImage, err := c.imageFetcher.Fetch(ctx, opts.RepoName, !opts.Publish, !opts.SkipPull)
if err != nil {
return err
}
runImageName, err := c.getRunImageName(ctx, opts, appImage)
if err != nil {
return err
}
baseImage, err := c.imageFetcher.Fetch(ctx, runImageName, !opts.Publish, !opts.SkipPull)
if err != nil {
return err
}
label, err := appImage.Label(metadata.AppMetadataLabel)
if err != nil {
return err
}
var md metadata.AppImageMetadata
if err := json.Unmarshal([]byte(label), &md); err != nil {
return err
}
c.logger.Infof("Rebasing %s on run image %s", style.Symbol(appImage.Name()), style.Symbol(baseImage.Name()))
if err := appImage.Rebase(md.RunImage.TopLayer, baseImage); err != nil {
return err
}
md.RunImage.SHA, err = baseImage.Digest()
if err != nil {
return err
}
md.RunImage.TopLayer, err = baseImage.TopLayer()
if err != nil {
return err
}
newLabel, err := json.Marshal(md)
if err := appImage.SetLabel(metadata.AppMetadataLabel, string(newLabel)); err != nil {
return err
}
sha, err := appImage.Save()
if err != nil {
return err
}
c.logger.Infof("New sha: %s", style.Symbol(sha))
return nil
}
func (c *Client) getRunImageName(ctx context.Context, opts RebaseOptions, appImage imgutil.Image) (string, error) {
var runImageName string
if opts.RunImage != "" {
runImageName = opts.RunImage
} else {
contents, err := appImage.Label(metadata.AppMetadataLabel)
if err != nil {
return "", err
}
var appImageMetadata metadata.AppImageMetadata
if err := json.Unmarshal([]byte(contents), &appImageMetadata); err != nil {
return "", err
}
registry, err := config.Registry(opts.RepoName)
if err != nil {
return "", errors.Wrapf(err, "parsing registry from reference '%s'", opts.RepoName)
}
var mirrors []string
if localRunImage := c.config.GetRunImage(appImageMetadata.Stack.RunImage.Image); localRunImage != nil {
mirrors = localRunImage.Mirrors
}
mirrors = append(mirrors, appImageMetadata.Stack.RunImage.Image)
mirrors = append(mirrors, appImageMetadata.Stack.RunImage.Mirrors...)
runImageName, err = config.ImageByRegistry(registry, mirrors)
if err != nil {
return "", errors.Wrapf(err, "find image by registry")
}
}
if runImageName == "" {
return "", errors.New("run image must be specified")
}
return runImageName, nil
}