forked from opencontainers/umoci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
repack.go
224 lines (189 loc) · 6.65 KB
/
repack.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
/*
* umoci: Umoci Modifies Open Containers' Images
* Copyright (C) 2016, 2017 SUSE LLC.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package main
import (
"fmt"
"os"
"path/filepath"
"strings"
"time"
"github.com/apex/log"
"github.com/openSUSE/umoci"
"github.com/openSUSE/umoci/mutate"
"github.com/openSUSE/umoci/oci/cas"
igen "github.com/openSUSE/umoci/oci/config/generate"
"github.com/openSUSE/umoci/oci/layer"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/urfave/cli"
"github.com/vbatts/go-mtree"
"golang.org/x/net/context"
)
var repackCommand = uxHistory(cli.Command{
Name: "repack",
Usage: "repacks an OCI runtime bundle into a reference",
ArgsUsage: `--image <image-path>[:<new-tag>] <bundle>
Where "<image-path>" is the path to the OCI image, "<new-tag>" is the name of
the tag that the new image will be saved as (if not specified, defaults to
"latest"), and "<bundle>" is the bundle from which to generate the required
layers.
The "<image-path>" MUST be the same image that was used to create "<bundle>"
(using umoci-unpack(1)). Otherwise umoci will not be able to modify the
original manifest to add the diff layer.
All uid-map and gid-map settings are automatically loaded from the bundle
metadata (which is generated by umoci-unpack(1)) so if you unpacked an image
using a particular mapping then the same mapping will be used to generate the
new layer.
It should be noted that this is not the same as oci-create-layer because it
uses go-mtree to create diff layers from runtime bundles unpacked with
umoci-unpack(1). In addition, it modifies the image so that all of the relevant
manifest and configuration information uses the new diff atop the old manifest.`,
// repack creates a new image, with a given tag.
Category: "image",
Action: repack,
Before: func(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return errors.Errorf("invalid number of positional arguments: expected <bundle>")
}
if ctx.Args().First() == "" {
return errors.Errorf("bundle path cannot be empty")
}
ctx.App.Metadata["bundle"] = ctx.Args().First()
return nil
},
})
func repack(ctx *cli.Context) error {
imagePath := ctx.App.Metadata["--image-path"].(string)
tagName := ctx.App.Metadata["--image-tag"].(string)
bundlePath := ctx.App.Metadata["bundle"].(string)
// Read the metadata first.
meta, err := ReadBundleMeta(bundlePath)
if err != nil {
return errors.Wrap(err, "read umoci.json metadata")
}
log.WithFields(log.Fields{
"version": meta.Version,
"from": meta.From,
"map_options": meta.MapOptions,
}).Debugf("umoci: loaded UmociMeta metadata")
// FIXME: Implement support for manifest lists.
if meta.From.MediaType != ispec.MediaTypeImageManifest {
return errors.Wrap(fmt.Errorf("descriptor does not point to ispec.MediaTypeImageManifest: not implemented: %s", meta.From.MediaType), "invalid saved from descriptor")
}
// Get a reference to the CAS.
engine, err := cas.Open(imagePath)
if err != nil {
return errors.Wrap(err, "open CAS")
}
defer engine.Close()
// Create the mutator.
mutator, err := mutate.New(engine, meta.From)
if err != nil {
return errors.Wrap(err, "create mutator for base image")
}
mtreeName := strings.Replace(meta.From.Digest.String(), "sha256:", "sha256_", 1)
mtreePath := filepath.Join(bundlePath, mtreeName+".mtree")
fullRootfsPath := filepath.Join(bundlePath, layer.RootfsName)
log.WithFields(log.Fields{
"image": imagePath,
"bundle": bundlePath,
"rootfs": layer.RootfsName,
"mtree": mtreePath,
}).Debugf("umoci: repacking OCI image")
mfh, err := os.Open(mtreePath)
if err != nil {
return errors.Wrap(err, "open mtree")
}
defer mfh.Close()
spec, err := mtree.ParseSpec(mfh)
if err != nil {
return errors.Wrap(err, "parse mtree")
}
log.WithFields(log.Fields{
"keywords": MtreeKeywords,
}).Debugf("umoci: parsed mtree spec")
fsEval := umoci.DefaultFsEval
if meta.MapOptions.Rootless {
fsEval = umoci.RootlessFsEval
}
log.Info("computing filesystem diff ...")
diffs, err := mtree.Check(fullRootfsPath, spec, MtreeKeywords, fsEval)
if err != nil {
return errors.Wrap(err, "check mtree")
}
log.Info("... done")
log.WithFields(log.Fields{
"ndiff": len(diffs),
}).Debugf("umoci: checked mtree spec")
reader, err := layer.GenerateLayer(fullRootfsPath, diffs, &meta.MapOptions)
if err != nil {
return errors.Wrap(err, "generate diff layer")
}
defer reader.Close()
imageMeta, err := mutator.Meta(context.Background())
if err != nil {
return errors.Wrap(err, "get image metadata")
}
history := ispec.History{
Author: imageMeta.Author,
Comment: "",
Created: time.Now(),
CreatedBy: "umoci config", // XXX: Should we append argv to this?
EmptyLayer: false,
}
if val, ok := ctx.App.Metadata["--history.author"]; ok {
history.Author = val.(string)
}
if val, ok := ctx.App.Metadata["--history.comment"]; ok {
history.Comment = val.(string)
}
if val, ok := ctx.App.Metadata["--history.created"]; ok {
created, err := time.Parse(igen.ISO8601, val.(string))
if err != nil {
return errors.Wrap(err, "parsing --history.created")
}
history.Created = created
}
if val, ok := ctx.App.Metadata["--history.created_by"]; ok {
history.CreatedBy = val.(string)
}
// TODO: We should add a flag to allow for a new layer to be made
// non-distributable.
if err := mutator.Add(context.Background(), reader, history); err != nil {
return errors.Wrap(err, "add diff layer")
}
newDescriptor, err := mutator.Commit(context.Background())
if err != nil {
return errors.Wrap(err, "commit mutated image")
}
log.Infof("new image manifest created: %s", newDescriptor.Digest)
err = engine.PutReference(context.Background(), tagName, newDescriptor)
if err == cas.ErrClobber {
// We have to clobber a tag.
log.Warnf("clobbering existing tag: %s", tagName)
// Delete the old tag.
if err := engine.DeleteReference(context.Background(), tagName); err != nil {
return errors.Wrap(err, "delete old tag")
}
err = engine.PutReference(context.Background(), tagName, newDescriptor)
}
if err != nil {
return errors.Wrap(err, "add new tag")
}
log.Infof("created new tag for image manifest: %s", tagName)
return nil
}