forked from opencontainers/umoci
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
301 lines (269 loc) · 8.65 KB
/
config.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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/*
* 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 (
"strings"
"time"
"github.com/apex/log"
"github.com/openSUSE/umoci/mutate"
"github.com/openSUSE/umoci/oci/cas"
igen "github.com/openSUSE/umoci/oci/generate"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/net/context"
)
// FIXME: We should also implement a raw mode that just does modifications of
// JSON blobs (allowing this all to be used outside of our build setup).
var configCommand = uxHistory(uxTag(cli.Command{
Name: "config",
Usage: "modifies the image configuration of an OCI image",
ArgsUsage: `--image <image-path>[:<tag>] [--tag <new-tag>]
Where "<image-path>" is the path to the OCI image, and "<tag>" is the name of
the tagged image from which the config modifications will be based (if not
specified, it defaults to "latest"). "<new-tag>" is the new reference name to
save the new image as, if this is not specified then umoci will replace the old
image.`,
// config modifies a particular image manifest.
Category: "image",
// Verify the metadata.
Before: func(ctx *cli.Context) error {
if _, ok := ctx.App.Metadata["--image-path"]; !ok {
return errors.Errorf("missing mandatory argument: --image")
}
if _, ok := ctx.App.Metadata["--image-tag"]; !ok {
return errors.Errorf("missing mandatory argument: --image")
}
return nil
},
Flags: []cli.Flag{
cli.StringFlag{Name: "config.user"},
cli.StringSliceFlag{Name: "config.exposedports"},
cli.StringSliceFlag{Name: "config.env"},
cli.StringSliceFlag{Name: "config.entrypoint"}, // FIXME: This interface is weird.
cli.StringSliceFlag{Name: "config.cmd"}, // FIXME: This interface is weird.
cli.StringSliceFlag{Name: "config.volume"},
cli.StringSliceFlag{Name: "config.label"},
cli.StringFlag{Name: "config.workingdir"},
cli.StringFlag{Name: "created"}, // FIXME: Implement TimeFlag.
cli.StringFlag{Name: "author"},
cli.StringFlag{Name: "architecture"},
cli.StringFlag{Name: "os"},
cli.StringSliceFlag{Name: "manifest.annotation"},
cli.StringSliceFlag{Name: "clear"},
},
Action: config,
}))
func toImage(config ispec.ImageConfig, meta mutate.Meta) ispec.Image {
return ispec.Image{
Config: config,
Created: meta.Created,
Author: meta.Author,
Architecture: meta.Architecture,
OS: meta.OS,
}
}
func fromImage(image ispec.Image) (ispec.ImageConfig, mutate.Meta) {
return image.Config, mutate.Meta{
Created: image.Created,
Author: image.Author,
Architecture: image.Architecture,
OS: image.OS,
}
}
// parseEnv splits a given environment variable (of the form name=value) into
// (name, value). An error is returned if there is no "=" in the line or if the
// name is empty.
func parseEnv(env string) (string, string, error) {
parts := strings.SplitN(env, "=", 2)
if len(parts) != 2 {
return "", "", errors.Errorf("environment variable must contain '=': %s", env)
}
name, value := parts[0], parts[1]
if name == "" {
return "", "", errors.Errorf("environment variable must have non-empty name: %s", env)
}
return name, value, nil
}
func config(ctx *cli.Context) error {
imagePath := ctx.App.Metadata["--image-path"].(string)
fromName := ctx.App.Metadata["--image-tag"].(string)
// By default we clobber the old tag.
tagName := fromName
if val, ok := ctx.App.Metadata["--tag"]; ok {
tagName = val.(string)
}
// Get a reference to the CAS.
engine, err := cas.Open(imagePath)
if err != nil {
return errors.Wrap(err, "open CAS")
}
defer engine.Close()
fromDescriptor, err := engine.GetReference(context.Background(), fromName)
if err != nil {
return errors.Wrap(err, "get from reference")
}
mutator, err := mutate.New(engine, *fromDescriptor)
if err != nil {
return errors.Wrap(err, "create mutator for manifest")
}
imageConfig, err := mutator.Config(context.Background())
if err != nil {
return errors.Wrap(err, "get base config")
}
imageMeta, err := mutator.Meta(context.Background())
if err != nil {
return errors.Wrap(err, "get base metadata")
}
annotations, err := mutator.Annotations(context.Background())
if err != nil {
return errors.Wrap(err, "get base annotations")
}
g, err := igen.NewFromImage(toImage(imageConfig, imageMeta))
if err != nil {
return errors.Wrap(err, "create new generator")
}
if ctx.IsSet("clear") {
for _, key := range ctx.StringSlice("clear") {
switch key {
case "config.labels":
g.ClearConfigLabels()
case "manifest.annotations":
annotations = nil
case "config.exposedports":
g.ClearConfigExposedPorts()
case "config.env":
g.ClearConfigEnv()
case "config.volume":
g.ClearConfigVolumes()
case "rootfs.diffids":
//g.ClearRootfsDiffIDs()
return errors.Errorf("--clear=rootfs.diffids is not safe")
default:
return errors.Errorf("unknown key to --clear: %s", key)
}
}
}
if ctx.IsSet("created") {
// How do we handle other formats?
created, err := time.Parse(igen.ISO8601, ctx.String("created"))
if err != nil {
return errors.Wrap(err, "parse --created")
}
g.SetCreated(created)
}
if ctx.IsSet("author") {
g.SetAuthor(ctx.String("author"))
}
if ctx.IsSet("architecture") {
g.SetArchitecture(ctx.String("architecture"))
}
if ctx.IsSet("os") {
g.SetOS(ctx.String("os"))
}
if ctx.IsSet("config.user") {
g.SetConfigUser(ctx.String("config.user"))
}
if ctx.IsSet("config.workingdir") {
g.SetConfigWorkingDir(ctx.String("config.workingdir"))
}
if ctx.IsSet("config.exposedports") {
for _, port := range ctx.StringSlice("config.exposedports") {
g.AddConfigExposedPort(port)
}
}
if ctx.IsSet("config.env") {
for _, env := range ctx.StringSlice("config.env") {
name, value, err := parseEnv(env)
if err != nil {
return err
}
g.AddConfigEnv(name, value)
}
}
// FIXME: This interface is weird.
if ctx.IsSet("config.entrypoint") {
g.SetConfigEntrypoint(ctx.StringSlice("config.entrypoint"))
}
// FIXME: This interface is weird.
if ctx.IsSet("config.cmd") {
g.SetConfigCmd(ctx.StringSlice("config.cmd"))
}
if ctx.IsSet("config.volume") {
for _, volume := range ctx.StringSlice("config.volume") {
g.AddConfigVolume(volume)
}
}
if ctx.IsSet("config.label") {
for _, label := range ctx.StringSlice("config.label") {
parts := strings.SplitN(label, "=", 2)
g.AddConfigLabel(parts[0], parts[1])
}
}
if ctx.IsSet("manifest.annotation") {
if annotations == nil {
annotations = map[string]string{}
}
for _, label := range ctx.StringSlice("manifest.annotation") {
parts := strings.SplitN(label, "=", 2)
annotations[parts[0]] = parts[1]
}
}
history := ispec.History{
Author: g.Author(),
Comment: "",
Created: time.Now().Format(igen.ISO8601),
CreatedBy: "umoci config",
EmptyLayer: true,
}
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 {
history.Created = val.(string)
}
if val, ok := ctx.App.Metadata["--history.created_by"]; ok {
history.CreatedBy = val.(string)
}
newConfig, newMeta := fromImage(g.Image())
if err := mutator.Set(context.Background(), newConfig, newMeta, annotations, history); err != nil {
return errors.Wrap(err, "set modified configuration")
}
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
}