/
manifest.go
387 lines (333 loc) · 9.1 KB
/
manifest.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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package main
import (
"archive/tar"
"compress/gzip"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"os"
"path"
"path/filepath"
"strings"
"github.com/appc/spec/aci"
"github.com/appc/spec/schema"
"github.com/appc/spec/schema/types"
)
var (
inputFile string
outputFile string
patchNocompress bool
patchOverwrite bool
patchReplace bool
patchManifestFile string
patchName string
patchExec string
patchUser string
patchGroup string
patchCaps string
patchMounts string
catPrettyPrint bool
cmdPatchManifest = &Command{
Name: "patch-manifest",
Description: `Copy an ACI and patch its manifest.`,
Summary: "Copy an ACI and patch its manifest (experimental)",
Usage: `
[--manifest=MANIFEST_FILE]
[--name=example.com/app]
[--exec="/app --debug"]
[--user=uid] [--group=gid]
[--capability=CAP_SYS_ADMIN,CAP_NET_ADMIN]
[--mounts=work,path=/opt,readOnly=true[:work2,...]]
[--replace]
INPUT_ACI_FILE
[OUTPUT_ACI_FILE]`,
Run: runPatchManifest,
}
cmdCatManifest = &Command{
Name: "cat-manifest",
Description: `Print the manifest from an ACI.`,
Summary: "Print the manifest from an ACI",
Usage: ` [--pretty-print] ACI_FILE`,
Run: runCatManifest,
}
)
func init() {
cmdPatchManifest.Flags.BoolVar(&patchOverwrite, "overwrite", false, "Overwrite target file if it already exists")
cmdPatchManifest.Flags.BoolVar(&patchNocompress, "no-compression", false, "Do not gzip-compress the produced ACI")
cmdPatchManifest.Flags.BoolVar(&patchReplace, "replace", false, "Replace the input file")
cmdPatchManifest.Flags.StringVar(&patchManifestFile, "manifest", "", "Replace image manifest with this file. Incompatible with other replace options.")
cmdPatchManifest.Flags.StringVar(&patchName, "name", "", "Replace name")
cmdPatchManifest.Flags.StringVar(&patchExec, "exec", "", "Replace the command line to launch the executable")
cmdPatchManifest.Flags.StringVar(&patchUser, "user", "", "Replace user")
cmdPatchManifest.Flags.StringVar(&patchGroup, "group", "", "Replace group")
cmdPatchManifest.Flags.StringVar(&patchCaps, "capability", "", "Replace capability")
cmdPatchManifest.Flags.StringVar(&patchMounts, "mounts", "", "Replace mount points")
cmdCatManifest.Flags.BoolVar(&catPrettyPrint, "pretty-print", false, "Print with better style")
}
func getIsolatorStr(name, value string) string {
return fmt.Sprintf(`
{
"name": "%s",
"value": { %s }
}`, name, value)
}
func patchManifest(im *schema.ImageManifest) error {
if patchName != "" {
name, err := types.NewACName(patchName)
if err != nil {
return err
}
im.Name = *name
}
if patchExec != "" {
im.App.Exec = strings.Split(patchExec, " ")
}
if patchUser != "" {
im.App.User = patchUser
}
if patchGroup != "" {
im.App.Group = patchGroup
}
if patchCaps != "" {
app := im.App
if app == nil {
return fmt.Errorf("no app in the manifest")
}
isolator := app.Isolators.GetByName(types.LinuxCapabilitiesRetainSetName)
if isolator != nil {
return fmt.Errorf("isolator already exists")
}
// Instantiate a Isolator with the content specified by the --capability
// parameter.
// TODO: Instead of creating a JSON and then unmarshalling it, the isolator
// should be instantiated directory. But it requires a constructor, see:
// https://github.com/appc/spec/issues/268
capsList := strings.Split(patchCaps, ",")
caps := fmt.Sprintf(`"set": ["%s"]`, strings.Join(capsList, `", "`))
isolatorStr := getIsolatorStr(types.LinuxCapabilitiesRetainSetName, caps)
isolator = &types.Isolator{}
err := isolator.UnmarshalJSON([]byte(isolatorStr))
if err != nil {
return err
}
app.Isolators = append(app.Isolators, *isolator)
}
if patchMounts != "" {
app := im.App
if app == nil {
return fmt.Errorf("no app in the manifest")
}
mounts := strings.Split(patchMounts, ":")
for _, m := range mounts {
mountPoint, err := types.MountPointFromString(m)
if err != nil {
return fmt.Errorf("cannot parse mount point %q", m)
}
app.MountPoints = append(app.MountPoints, *mountPoint)
}
}
return nil
}
// extractManifest iterates over the tar reader and locate the manifest. Once
// located, the manifest can be printed, replaced or patched.
func extractManifest(tr *tar.Reader, tw *tar.Writer, printManifest bool, newManifest []byte) error {
Tar:
for {
hdr, err := tr.Next()
switch err {
case io.EOF:
break Tar
case nil:
if filepath.Clean(hdr.Name) == aci.ManifestFile {
var new_bytes []byte
bytes, err := ioutil.ReadAll(tr)
if err != nil {
return err
}
if printManifest && !catPrettyPrint {
fmt.Println(string(bytes))
}
im := &schema.ImageManifest{}
err = im.UnmarshalJSON(bytes)
if err != nil {
return err
}
if printManifest && catPrettyPrint {
output, err := json.MarshalIndent(im, "", " ")
if err != nil {
return err
}
fmt.Println(string(output))
}
if tw == nil {
return nil
}
if len(newManifest) == 0 {
err = patchManifest(im)
if err != nil {
return err
}
new_bytes, err = im.MarshalJSON()
if err != nil {
return err
}
} else {
new_bytes = newManifest
}
hdr.Size = int64(len(new_bytes))
err = tw.WriteHeader(hdr)
if err != nil {
return err
}
_, err = tw.Write(new_bytes)
if err != nil {
return err
}
} else if tw != nil {
err := tw.WriteHeader(hdr)
if err != nil {
return err
}
_, err = io.Copy(tw, tr)
if err != nil {
return err
}
}
default:
return fmt.Errorf("error reading tarball: %v", err)
}
}
return nil
}
func runPatchManifest(args []string) (exit int) {
var fh *os.File
var err error
if patchReplace && patchOverwrite {
stderr("patch-manifest: Cannot use both --replace and --overwrite")
return 1
}
if !patchReplace && len(args) != 2 {
stderr("patch-manifest: Must provide input and output files (or use --replace)")
return 1
}
if patchReplace && len(args) != 1 {
stderr("patch-manifest: Must provide one file")
return 1
}
if patchManifestFile != "" && (patchName != "" || patchExec != "" || patchUser != "" || patchGroup != "" || patchCaps != "" || patchMounts != "") {
stderr("patch-manifest: --manifest is incompatible with other manifest editing options")
return 1
}
inputFile = args[0]
// Prepare output writer
if patchReplace {
fh, err = ioutil.TempFile(path.Dir(inputFile), ".actool-tmp."+path.Base(inputFile)+"-")
if err != nil {
stderr("patch-manifest: Cannot create temporary file: %v", err)
return 1
}
} else {
outputFile = args[1]
ext := filepath.Ext(outputFile)
if ext != schema.ACIExtension {
stderr("patch-manifest: Extension must be %s (given %s)", schema.ACIExtension, ext)
return 1
}
mode := os.O_CREATE | os.O_WRONLY
if patchOverwrite {
mode |= os.O_TRUNC
} else {
mode |= os.O_EXCL
}
fh, err = os.OpenFile(outputFile, mode, 0644)
if err != nil {
if os.IsExist(err) {
stderr("patch-manifest: Output file exists (try --overwrite)")
} else {
stderr("patch-manifest: Unable to open output %s: %v", outputFile, err)
}
return 1
}
}
var gw *gzip.Writer
var w io.WriteCloser = fh
if !patchNocompress {
gw = gzip.NewWriter(fh)
w = gw
}
tw := tar.NewWriter(w)
defer func() {
tw.Close()
if !patchNocompress {
gw.Close()
}
fh.Close()
if exit != 0 && !patchOverwrite {
os.Remove(fh.Name())
}
}()
// Prepare input reader
input, err := os.Open(inputFile)
if err != nil {
stderr("patch-manifest: Cannot open %s: %v", inputFile, err)
return 1
}
defer input.Close()
tr, err := aci.NewCompressedTarReader(input)
if err != nil {
stderr("patch-manifest: Cannot extract %s: %v", inputFile, err)
return 1
}
var newManifest []byte
if patchManifestFile != "" {
mr, err := os.Open(patchManifestFile)
if err != nil {
stderr("patch-manifest: Cannot open %s: %v", patchManifestFile, err)
return 1
}
defer input.Close()
newManifest, err = ioutil.ReadAll(mr)
if err != nil {
stderr("patch-manifest: Cannot read %s: %v", patchManifestFile, err)
return 1
}
}
err = extractManifest(tr, tw, false, newManifest)
if err != nil {
stderr("patch-manifest: Unable to read %s: %v", inputFile, err)
return 1
}
if patchReplace {
err = os.Rename(fh.Name(), inputFile)
if err != nil {
stderr("patch-manifest: Cannot rename %q to %q: %v", fh.Name, inputFile, err)
return 1
}
}
return
}
func runCatManifest(args []string) (exit int) {
if len(args) != 1 {
stderr("cat-manifest: Must provide one file")
return 1
}
inputFile = args[0]
input, err := os.Open(inputFile)
if err != nil {
stderr("cat-manifest: Cannot open %s: %v", inputFile, err)
return 1
}
defer input.Close()
tr, err := aci.NewCompressedTarReader(input)
if err != nil {
stderr("cat-manifest: Cannot extract %s: %v", inputFile, err)
return 1
}
err = extractManifest(tr, nil, true, nil)
if err != nil {
stderr("cat-manifest: Unable to read %s: %v", inputFile, err)
return 1
}
return
}