forked from opencontainers/umoci
/
new.go
140 lines (115 loc) · 3.89 KB
/
new.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
/*
* umoci: Umoci Modifies Open Containers' Images
* Copyright (C) 2016, 2017, 2018 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 (
"runtime"
"time"
"github.com/apex/log"
"github.com/openSUSE/umoci/oci/cas/dir"
"github.com/openSUSE/umoci/oci/casext"
igen "github.com/openSUSE/umoci/oci/config/generate"
imeta "github.com/opencontainers/image-spec/specs-go"
ispec "github.com/opencontainers/image-spec/specs-go/v1"
"github.com/pkg/errors"
"github.com/urfave/cli"
"golang.org/x/net/context"
)
var newCommand = cli.Command{
Name: "new",
Usage: "creates a blank tagged OCI image",
ArgsUsage: `--image <image-path>:<new-tag>
Where "<image-path>" is the path to the OCI image, and "<new-tag>" is the name
of the tag for the empty manifest.
Once you create a new image with umoci-new(1) you can directly use the image
with umoci-unpack(1), umoci-repack(1), and umoci-config(1) to modify the new
manifest as you see fit. This allows you to create entirely new images without
needing a base image to start from.`,
// new modifies an image layout.
Category: "image",
Action: newImage,
}
func newImage(ctx *cli.Context) error {
imagePath := ctx.App.Metadata["--image-path"].(string)
tagName := ctx.App.Metadata["--image-tag"].(string)
// 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()
// Create a new manifest.
log.WithFields(log.Fields{
"tag": tagName,
}).Debugf("creating new manifest")
// Create a new image config.
g := igen.New()
createTime := time.Now()
// Set all of the defaults we need.
g.SetCreated(createTime)
g.SetOS(runtime.GOOS)
g.SetArchitecture(runtime.GOARCH)
g.ClearHistory()
// Make sure we have no diffids.
g.SetRootfsType("layers")
g.ClearRootfsDiffIDs()
// Update config and create a new blob for it.
config := g.Image()
configDigest, configSize, err := engineExt.PutBlobJSON(context.Background(), config)
if err != nil {
return errors.Wrap(err, "put config blob")
}
log.WithFields(log.Fields{
"digest": configDigest,
"size": configSize,
}).Debugf("umoci: added new config")
// Create a new manifest that just points to the config and has an
// empty layer set. FIXME: Implement ManifestList support.
manifest := ispec.Manifest{
Versioned: imeta.Versioned{
SchemaVersion: 2, // FIXME: This is hardcoded at the moment.
},
Config: ispec.Descriptor{
MediaType: ispec.MediaTypeImageConfig,
Digest: configDigest,
Size: configSize,
},
Layers: []ispec.Descriptor{},
}
manifestDigest, manifestSize, err := engineExt.PutBlobJSON(context.Background(), manifest)
if err != nil {
return errors.Wrap(err, "put manifest blob")
}
log.WithFields(log.Fields{
"digest": manifestDigest,
"size": manifestSize,
}).Debugf("umoci: added new manifest")
// Now create a new reference, and either add it to the engine or spew it
// to stdout.
descriptor := ispec.Descriptor{
// FIXME: Support manifest lists.
MediaType: ispec.MediaTypeImageManifest,
Digest: manifestDigest,
Size: manifestSize,
}
log.Infof("new image manifest created: %s", descriptor.Digest)
if err := engineExt.UpdateReference(context.Background(), tagName, descriptor); err != nil {
return errors.Wrap(err, "add new tag")
}
log.Infof("created new tag for image manifest: %s", tagName)
return nil
}