forked from opencontainers/umoci
/
raw-runtime-config.go
174 lines (153 loc) · 5.36 KB
/
raw-runtime-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
/*
* 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"
"github.com/apex/log"
"github.com/openSUSE/umoci/oci/cas/dir"
"github.com/openSUSE/umoci/oci/casext"
"github.com/openSUSE/umoci/oci/layer"
"github.com/openSUSE/umoci/pkg/idtools"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/net/context"
)
var rawConfigCommand = cli.Command{
Name: "runtime-config",
Aliases: []string{"config"},
Usage: "generates an OCI runtime configuration for an image",
ArgsUsage: `--image <image-path>[:<tag>] [--rootfs <rootfs>] <config.json>
Where "<image-path>" is the path to the OCI image, "<tag>" is the name of the
tagged image to unpack (if not specified, defaults to "latest"), "<rootfs>" is
a rootfs to use as a supplementary "source of truth" for certain generation
operations and "<config.json>" is the destination to write the runtime
configuration to.
Note that the results of this may not agree with umoci-unpack(1) because the
--rootfs flag affects how certain properties are interpreted.`,
// unpack reads manifest information.
Category: "image",
Flags: []cli.Flag{
cli.StringSliceFlag{
Name: "uid-map",
Usage: "specifies a uid mapping to use when generating config",
},
cli.StringSliceFlag{
Name: "gid-map",
Usage: "specifies a gid mapping to use when generating config",
},
cli.BoolFlag{
Name: "rootless",
Usage: "generate rootless configuration",
},
cli.StringFlag{
Name: "rootfs",
Usage: "path to secondary source of truth (root filesystem)",
},
},
Action: rawConfig,
Before: func(ctx *cli.Context) error {
if ctx.NArg() != 1 {
return errors.Errorf("invalid number of positional arguments: expected <config.json>")
}
if ctx.Args().First() == "" {
return errors.Errorf("config.json path cannot be empty")
}
ctx.App.Metadata["config"] = ctx.Args().First()
return nil
},
}
func rawConfig(ctx *cli.Context) error {
imagePath := ctx.App.Metadata["--image-path"].(string)
fromName := ctx.App.Metadata["--image-tag"].(string)
configPath := ctx.App.Metadata["config"].(string)
var meta UmociMeta
meta.Version = UmociMetaVersion
// Parse map options.
// We need to set mappings if we're in rootless mode.
meta.MapOptions.Rootless = ctx.Bool("rootless")
if meta.MapOptions.Rootless {
if !ctx.IsSet("uid-map") {
ctx.Set("uid-map", fmt.Sprintf("%d:0:1", os.Geteuid()))
}
if !ctx.IsSet("gid-map") {
ctx.Set("gid-map", fmt.Sprintf("%d:0:1", os.Getegid()))
}
}
// Parse and set up the mapping options.
for _, uidmap := range ctx.StringSlice("uid-map") {
idMap, err := idtools.ParseMapping(uidmap)
if err != nil {
return errors.Wrapf(err, "failure parsing --uid-map %s: %s", uidmap)
}
meta.MapOptions.UIDMappings = append(meta.MapOptions.UIDMappings, idMap)
}
for _, gidmap := range ctx.StringSlice("gid-map") {
idMap, err := idtools.ParseMapping(gidmap)
if err != nil {
return errors.Wrapf(err, "failure parsing --gid-map %s: %s", gidmap)
}
meta.MapOptions.GIDMappings = append(meta.MapOptions.GIDMappings, idMap)
}
log.WithFields(log.Fields{
"map.uid": meta.MapOptions.UIDMappings,
"map.gid": meta.MapOptions.GIDMappings,
}).Debugf("parsed mappings")
// Get a reference to the CAS.
engine, err := dir.Open(imagePath)
if err != nil {
return errors.Wrap(err, "open CAS")
}
engineExt := casext.NewEngine(engine)
defer engine.Close()
fromDescriptorPaths, err := engineExt.ResolveReference(context.Background(), fromName)
if err != nil {
return errors.Wrap(err, "get descriptor")
}
if len(fromDescriptorPaths) != 1 {
// TODO: Handle this more nicely.
return errors.Errorf("tag is ambiguous: %s", fromName)
}
meta.From = fromDescriptorPaths[0]
manifestBlob, err := engineExt.FromDescriptor(context.Background(), meta.From.Descriptor())
if err != nil {
return errors.Wrap(err, "get manifest")
}
defer manifestBlob.Close()
if manifestBlob.MediaType != ispec.MediaTypeImageManifest {
return errors.Wrap(fmt.Errorf("descriptor does not point to ispec.MediaTypeImageManifest: not implemented: %s", manifestBlob.MediaType), "invalid --image tag")
}
// Get the manifest.
manifest, ok := manifestBlob.Data.(ispec.Manifest)
if !ok {
// Should _never_ be reached.
return errors.Errorf("[internal error] unknown manifest blob type: %s", manifestBlob.MediaType)
}
// Generate the configuration.
configFile, err := os.Create(configPath)
if err != nil {
return errors.Wrap(err, "opening config path")
}
defer configFile.Close()
// Write out the generated config.
log.Info("generating config.json")
if err := layer.UnpackRuntimeJSON(context.Background(), engineExt, configFile, ctx.String("rootfs"), manifest, &meta.MapOptions); err != nil {
return errors.Wrap(err, "generate config")
}
return nil
}