forked from containers/buildah
/
config.go
203 lines (194 loc) · 5.05 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
package main
import (
"strings"
"github.com/mattn/go-shellwords"
"github.com/pkg/errors"
"github.com/projectatomic/buildah"
"github.com/sirupsen/logrus"
"github.com/urfave/cli"
)
const (
// DefaultCreatedBy is the default description of how an image layer
// was created that we use when adding to an image's history.
DefaultCreatedBy = "manual edits"
)
var (
configFlags = []cli.Flag{
cli.StringSliceFlag{
Name: "annotation, a",
Usage: "add `annotation` e.g. annotation=value, for the target image",
},
cli.StringFlag{
Name: "arch",
Usage: "set `architecture` of the target image",
},
cli.StringFlag{
Name: "author",
Usage: "set image author contact `information`",
},
cli.StringFlag{
Name: "cmd",
Usage: "sets the default `command` to run for containers based on the image",
},
cli.StringFlag{
Name: "created-by",
Usage: "add `description` of how the image was created",
Value: DefaultCreatedBy,
},
cli.StringFlag{
Name: "entrypoint",
Usage: "set `entry point` for containers based on image",
},
cli.StringSliceFlag{
Name: "env, e",
Usage: "add `environment variable` to be set when running containers based on image",
},
cli.StringSliceFlag{
Name: "label, l",
Usage: "add image configuration `label` e.g. label=value",
},
cli.StringFlag{
Name: "os",
Usage: "set `operating system` of the target image",
},
cli.StringSliceFlag{
Name: "port, p",
Usage: "add `port` to expose when running containers based on image",
},
cli.StringFlag{
Name: "shell",
Usage: "add `shell` to run in containers",
},
cli.StringFlag{
Name: "user, u",
Usage: "set default `user` to run inside containers based on image",
},
cli.StringSliceFlag{
Name: "volume, v",
Usage: "add default `volume` path to be created for containers based on image",
},
cli.StringFlag{
Name: "workingdir",
Usage: "set working `directory` for containers based on image",
},
}
configDescription = "Modifies the configuration values which will be saved to the image"
configCommand = cli.Command{
Name: "config",
Usage: "Update image configuration settings",
Description: configDescription,
Flags: configFlags,
Action: configCmd,
ArgsUsage: "CONTAINER-NAME-OR-ID",
SkipArgReorder: true,
}
)
func updateConfig(builder *buildah.Builder, c *cli.Context) {
if c.IsSet("author") {
builder.SetMaintainer(c.String("author"))
}
if c.IsSet("created-by") {
builder.SetCreatedBy(c.String("created-by"))
}
if c.IsSet("arch") {
builder.SetArchitecture(c.String("arch"))
}
if c.IsSet("os") {
builder.SetOS(c.String("os"))
}
if c.IsSet("user") {
builder.SetUser(c.String("user"))
}
if c.IsSet("shell") {
shellSpec, err := shellwords.Parse(c.String("shell"))
if err != nil {
logrus.Errorf("error parsing --shell %q: %v", c.String("shell"), err)
} else {
builder.SetShell(shellSpec)
}
}
if c.IsSet("port") || c.IsSet("p") {
for _, portSpec := range c.StringSlice("port") {
builder.SetPort(portSpec)
}
}
if c.IsSet("env") || c.IsSet("e") {
for _, envSpec := range c.StringSlice("env") {
env := strings.SplitN(envSpec, "=", 2)
if len(env) > 1 {
builder.SetEnv(env[0], env[1])
} else {
builder.UnsetEnv(env[0])
}
}
}
if c.IsSet("entrypoint") {
entrypointSpec, err := shellwords.Parse(c.String("entrypoint"))
if err != nil {
logrus.Errorf("error parsing --entrypoint %q: %v", c.String("entrypoint"), err)
} else {
builder.SetEntrypoint(entrypointSpec)
}
}
if c.IsSet("cmd") {
cmdSpec, err := shellwords.Parse(c.String("cmd"))
if err != nil {
logrus.Errorf("error parsing --cmd %q: %v", c.String("cmd"), err)
} else {
builder.SetCmd(cmdSpec)
}
}
if c.IsSet("volume") {
if volSpec := c.StringSlice("volume"); len(volSpec) > 0 {
for _, spec := range volSpec {
builder.AddVolume(spec)
}
}
}
if c.IsSet("label") || c.IsSet("l") {
for _, labelSpec := range c.StringSlice("label") {
label := strings.SplitN(labelSpec, "=", 2)
if len(label) > 1 {
builder.SetLabel(label[0], label[1])
} else {
builder.UnsetLabel(label[0])
}
}
}
if c.IsSet("workingdir") {
builder.SetWorkDir(c.String("workingdir"))
}
if c.IsSet("annotation") || c.IsSet("a") {
for _, annotationSpec := range c.StringSlice("annotation") {
annotation := strings.SplitN(annotationSpec, "=", 2)
if len(annotation) > 1 {
builder.SetAnnotation(annotation[0], annotation[1])
} else {
builder.UnsetAnnotation(annotation[0])
}
}
}
}
func configCmd(c *cli.Context) error {
args := c.Args()
if len(args) == 0 {
return errors.Errorf("container ID must be specified")
}
if len(args) > 1 {
return errors.Errorf("too many arguments specified")
}
name := args[0]
if err := validateFlags(c, configFlags); err != nil {
return err
}
store, err := getStore(c)
if err != nil {
return err
}
builder, err := openBuilder(store, name)
if err != nil {
return errors.Wrapf(err, "error reading build container %q", name)
}
updateConfig(builder, c)
return builder.Save()
}