-
Notifications
You must be signed in to change notification settings - Fork 5
/
save.go
77 lines (63 loc) · 1.28 KB
/
save.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
package main
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"github.com/docker/docker/client"
"github.com/hightouchio/injecto/tar"
)
var (
blacklist = []string{
"dev",
"etc/hostname",
"etc/hosts",
"etc/motd",
"etc/modules-load.d",
"etc/mtab",
"etc/resolv.conf",
"media",
"mnt",
"sys",
"tmp",
}
)
type manifestEntry struct {
Layers []string
}
func save(cli *client.Client, dir, image string) error {
fmt.Printf("saving %s\n", image)
reader, err := cli.ImageSave(context.Background(), []string{image})
if err != nil {
return err
}
saveDir, err := ioutil.TempDir("", "")
if err != nil {
return err
}
if err := tar.Extract(reader, saveDir, blacklist); err != nil {
return err
}
manifestBytes, err := ioutil.ReadFile(path.Join(saveDir, "manifest.json"))
if err != nil {
return err
}
var manifest []manifestEntry
if err := json.Unmarshal(manifestBytes, &manifest); err != nil {
return err
}
for i, layer := range manifest[0].Layers {
filename := path.Join(saveDir, layer)
layerFile, err := os.Open(filename)
if err != nil {
return err
}
fmt.Printf("extracting layer [%d/%d]\n", i+1, len(manifest[0].Layers))
if err := tar.Extract(layerFile, dir, blacklist); err != nil {
return err
}
}
return nil
}