-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdocker.go
132 lines (115 loc) · 2.63 KB
/
docker.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
package main
import (
"errors"
"os"
"time"
"archive/tar"
"bytes"
"fmt"
"github.com/fsouza/go-dockerclient"
"github.com/offers/dope/out"
"io"
"io/ioutil"
"strings"
)
func newDockerClient() (*docker.Client, error) {
endpoint := "unix:///var/run/docker.sock"
return docker.NewClient(endpoint)
}
func dockerPull(repo string, tag string) error {
client, err := newDockerClient()
if err != nil {
return err
}
opts := docker.PullImageOptions{
Repository: repo,
Tag: tag,
InactivityTimeout: time.Duration(30) * time.Second,
}
out.Printf("Pulling image %s:%s...\n", repo, tag)
return client.PullImage(opts, docker.AuthConfiguration{})
}
func dockerRmi(image string) error {
client, err := newDockerClient()
if err != nil {
return err
}
return client.RemoveImage(image)
}
func dockerGetDopeFile(repo string, tag string) ([]byte, error) {
exportFile, err := ioutil.TempFile("", "dope-docker-export")
if err != nil {
return []byte{}, err
}
defer exportFile.Close()
defer os.Remove(exportFile.Name())
client, err := newDockerClient()
if err != nil {
return []byte{}, err
}
image := fmt.Sprintf("%s:%s", repo, tag)
imageData, err := client.InspectImage(image)
if err != nil {
return []byte{}, err
}
exportOpts := docker.ExportImageOptions{
Name: imageData.ID,
OutputStream: exportFile,
}
out.Println("Searching docker image for .dope.json...")
err = client.ExportImage(exportOpts)
if err != nil {
return []byte{}, err
}
// Rewind to beginning of .tar for reading
exportFile.Seek(0, 0)
if err != nil {
return []byte{}, err
}
tr := tar.NewReader(exportFile)
// Iterate through the files in the archive.
for {
hdr, err := tr.Next()
if err == io.EOF {
break
}
if err != nil {
log.Fatal(err)
}
if strings.HasSuffix(hdr.Name, "layer.tar") {
buf := make([]byte, hdr.Size)
_, err := tr.Read(buf)
if err != nil {
return []byte{}, err
}
data, err := dockerGetFileFromLayer(".dope.json", buf)
if nil == err {
return data, nil
}
}
}
return []byte{}, errors.New(".dope.json not found in tar archive")
}
func dockerGetFileFromLayer(filename string, layer []byte) ([]byte, error) {
tr := tar.NewReader(bytes.NewReader(layer))
// Iterate through the files in the archive.
for {
hdr, err := tr.Next()
if err == io.EOF {
// end of tar archive
break
}
if err != nil {
log.Fatal(err)
}
if strings.HasSuffix(hdr.Name, filename) {
buf := make([]byte, hdr.Size)
_, err := tr.Read(buf)
if err != nil {
return []byte{}, err
}
return buf, nil
}
}
return []byte{}, errors.New(filename + " not found in layer")
}