forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebase.go
113 lines (99 loc) · 2.58 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
112
113
package pack
import (
"encoding/json"
"fmt"
"log"
"github.com/buildpack/lifecycle"
"github.com/buildpack/pack/config"
"github.com/buildpack/pack/image"
)
type RebaseConfig struct {
Image image.Image
NewBaseImage image.Image
}
type RebaseFactory struct {
Log *log.Logger
Config *config.Config
ImageFactory ImageFactory
}
type RebaseFlags struct {
RepoName string
Publish bool
NoPull bool
}
func (f *RebaseFactory) RebaseConfigFromFlags(flags RebaseFlags) (RebaseConfig, error) {
var newImage func(string) (image.Image, error)
if flags.Publish {
newImage = f.ImageFactory.NewRemote
} else {
newImage = func(name string) (image.Image, error) {
return f.ImageFactory.NewLocal(name, !flags.NoPull)
}
}
image, err := newImage(flags.RepoName)
if err != nil {
return RebaseConfig{}, err
}
stackID, err := image.Label("io.buildpacks.stack.id")
if err != nil {
return RebaseConfig{}, err
}
baseImageName, err := f.runImageName(stackID, flags.RepoName)
if err != nil {
return RebaseConfig{}, err
}
baseImage, err := newImage(baseImageName)
if err != nil {
return RebaseConfig{}, err
}
return RebaseConfig{
Image: image,
NewBaseImage: baseImage,
}, nil
}
func (f *RebaseFactory) Rebase(cfg RebaseConfig) error {
label, err := cfg.Image.Label("io.buildpacks.lifecycle.metadata")
if err != nil {
return err
}
var metadata lifecycle.AppImageMetadata
if err := json.Unmarshal([]byte(label), &metadata); err != nil {
return err
}
if err := cfg.Image.Rebase(metadata.RunImage.TopLayer, cfg.NewBaseImage); err != nil {
return err
}
metadata.RunImage.SHA, err = cfg.NewBaseImage.Digest()
if err != nil {
return err
}
metadata.RunImage.TopLayer, err = cfg.NewBaseImage.TopLayer()
if err != nil {
return err
}
newLabel, err := json.Marshal(metadata)
if err := cfg.Image.SetLabel("io.buildpacks.lifecycle.metadata", string(newLabel)); err != nil {
return err
}
digest, err := cfg.Image.Save()
if err != nil {
return err
}
f.Log.Printf("Successfully replaced %s with %s\n", cfg.Image.Name(), digest)
return nil
}
// TODO copied from create_builder.go (called baseImage, and using baseImage (not run))
func (f *RebaseFactory) runImageName(stackID, repoName string) (string, error) {
stack, err := f.Config.Get(stackID)
if err != nil {
return "", err
}
if len(stack.RunImages) == 0 {
return "", fmt.Errorf(`Invalid stack: stack "%s" requies at least one build image`, stack.ID)
}
registry, err := config.Registry(repoName)
if err != nil {
return "", err
}
return config.ImageByRegistry(registry, stack.RunImages)
}