forked from buildpacks/pack
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rebase.go
249 lines (222 loc) · 7.02 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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
package pack
import (
"context"
"encoding/json"
"fmt"
"log"
"github.com/buildpack/pack/config"
dockercli "github.com/docker/docker/client"
"github.com/google/go-containerregistry/pkg/v1"
"github.com/google/go-containerregistry/pkg/v1/mutate"
"github.com/google/go-containerregistry/pkg/v1/remote"
"github.com/google/go-containerregistry/pkg/v1/types"
"github.com/pkg/errors"
)
type RebaseConfig struct {
RepoName string
Publish bool
Repo WritableStore
RepoImage v1.Image
OldBase v1.Image
NewBase v1.Image
}
type WritableStore interface {
Write(image v1.Image) error
}
type RebaseFactory struct {
Log *log.Logger
Docker Docker
Config *config.Config
Images Images
}
type RebaseFlags struct {
RepoName string
Publish bool
NoPull bool
}
func (f *RebaseFactory) RebaseConfigFromFlags(flags RebaseFlags) (RebaseConfig, error) {
if !flags.NoPull && !flags.Publish {
f.Log.Println("Pulling image", flags.RepoName)
err := f.Docker.PullImage(flags.RepoName)
if err != nil {
return RebaseConfig{}, fmt.Errorf(`failed to pull stack build image "%s": %s`, flags.RepoName, err)
}
}
stackID, err := f.imageLabel(flags.RepoName, "io.buildpacks.stack.id", !flags.Publish)
if err != nil {
return RebaseConfig{}, err
}
baseImageName, err := f.baseImageName(stackID, flags.RepoName)
if err != nil {
return RebaseConfig{}, err
}
if !flags.NoPull && !flags.Publish {
f.Log.Println("Pulling base image", baseImageName)
err := f.Docker.PullImage(baseImageName)
if err != nil {
return RebaseConfig{}, fmt.Errorf(`failed to pull stack build image "%s": %s`, baseImageName, err)
}
}
repoStore, err := f.Images.RepoStore(flags.RepoName, !flags.Publish)
if err != nil {
return RebaseConfig{}, fmt.Errorf(`failed to create repository store for image "%s": %s`, flags.RepoName, err)
}
f.Log.Println("Reading image", flags.RepoName)
repoImage, err := f.Images.ReadImage(flags.RepoName, !flags.Publish)
if err != nil {
return RebaseConfig{}, fmt.Errorf(`failed to read image "%s": %s`, flags.RepoName, err)
}
oldBase, err := f.fakeBaseImage(flags.RepoName, repoImage, !flags.Publish)
if err != nil {
return RebaseConfig{}, fmt.Errorf(`failed to read old base image "%s": %s`, baseImageName, err)
}
f.Log.Println("Reading new base image", baseImageName)
newBase, err := f.Images.ReadImage(baseImageName, !flags.Publish)
if err != nil {
return RebaseConfig{}, fmt.Errorf(`failed to read new base image "%s": %s`, baseImageName, err)
}
return RebaseConfig{
RepoName: flags.RepoName,
Publish: flags.Publish,
Repo: repoStore,
RepoImage: repoImage,
OldBase: oldBase,
NewBase: newBase,
}, nil
}
func (f *RebaseFactory) Rebase(cfg RebaseConfig) error {
newImage, err := mutate.Rebase(cfg.RepoImage, cfg.OldBase, cfg.NewBase, &mutate.RebaseOptions{})
if err != nil {
return err
}
// TODO : set runimage/sha on image metadata
if err := f.setRunImageSHA(newImage, cfg.NewBase); err != nil {
return err
}
h, err := newImage.Digest()
if err != nil {
return err
}
// TODO write image
if err := cfg.Repo.Write(newImage); err != nil {
return err
}
// TODO make sure hash is correct (I think it is currently wrong)
f.Log.Printf("Successfully replaced %s with %s\n", cfg.RepoName, h)
return nil
}
// TODO copied from create_builder.go
func (f *RebaseFactory) baseImageName(stackID, repoName string) (string, error) {
stack, err := f.Config.Get(stackID)
if err != nil {
return "", err
}
if len(stack.BuildImages) == 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.BuildImages)
}
// TODO copied from build.go
func (f *RebaseFactory) imageLabel(repoName, key string, useDaemon bool) (string, error) {
var labels map[string]string
if useDaemon {
i, _, err := f.Docker.ImageInspectWithRaw(context.Background(), repoName)
if dockercli.IsErrNotFound(err) {
return "", nil
} else if err != nil {
return "", errors.Wrap(err, "analyze read previous image config")
}
labels = i.Config.Labels
} else {
origImage, err := f.Images.ReadImage(repoName, false)
if err != nil || origImage == nil {
return "", err
}
config, err := origImage.ConfigFile()
if err != nil {
if remoteErr, ok := err.(*remote.Error); ok && len(remoteErr.Errors) > 0 {
switch remoteErr.Errors[0].Code {
case remote.UnauthorizedErrorCode, remote.ManifestUnknownErrorCode:
return "", nil
}
}
return "", errors.Wrapf(err, "access manifest: %s", repoName)
}
labels = config.Config.Labels
}
return labels[key], nil
}
func (f *RebaseFactory) setRunImageSHA(img, runImage v1.Image) error {
layers, err := runImage.Layers()
if err != nil {
return err
}
topSHA, err := layers[len(layers)-1].DiffID()
if err != nil {
return err
}
cfg, err := img.ConfigFile()
if err != nil {
return err
}
var metadata map[string]interface{}
if err = json.Unmarshal([]byte(cfg.Config.Labels["io.buildpacks.lifecycle.metadata"]), &metadata); err != nil {
return err
}
metadata["runimage"] = map[string]string{"sha": topSHA.String()}
b, err := json.Marshal(metadata)
if err != nil {
return err
}
cfg.Config.Labels["io.buildpacks.lifecycle.metadata"] = string(b)
return nil
}
func (f *RebaseFactory) fakeBaseImage(repoName string, repoImage v1.Image, useDaemon bool) (v1.Image, error) {
str, err := f.imageLabel(repoName, "io.buildpacks.lifecycle.metadata", useDaemon)
if err != nil {
return nil, err
}
var metadata struct {
RunImage struct {
SHA string `toml:"sha"`
} `toml:"runimage"`
}
if err := json.Unmarshal([]byte(str), &metadata); err != nil {
return nil, err
}
return &SubImage{img: repoImage, topSHA: metadata.RunImage.SHA}, nil
}
type SubImage struct {
img v1.Image
topSHA string
}
func (si *SubImage) Layers() ([]v1.Layer, error) {
all, err := si.img.Layers()
if err != nil {
return nil, err
}
for i, l := range all {
d, err := l.DiffID()
if err != nil {
return nil, err
}
if d.String() == si.topSHA {
return all[:i+1], nil
}
}
return nil, errors.New("could not find base layer in image")
}
func (si *SubImage) BlobSet() (map[v1.Hash]struct{}, error) { panic("Not Implemented") }
func (si *SubImage) MediaType() (types.MediaType, error) { panic("Not Implemented") }
func (si *SubImage) ConfigName() (v1.Hash, error) { panic("Not Implemented") }
func (si *SubImage) ConfigFile() (*v1.ConfigFile, error) { panic("Not Implemented") }
func (si *SubImage) RawConfigFile() ([]byte, error) { panic("Not Implemented") }
func (si *SubImage) Digest() (v1.Hash, error) { panic("Not Implemented") }
func (si *SubImage) Manifest() (*v1.Manifest, error) { panic("Not Implemented") }
func (si *SubImage) RawManifest() ([]byte, error) { panic("Not Implemented") }
func (si *SubImage) LayerByDigest(v1.Hash) (v1.Layer, error) { panic("Not Implemented") }
func (si *SubImage) LayerByDiffID(v1.Hash) (v1.Layer, error) { panic("Not Implemented") }