-
Notifications
You must be signed in to change notification settings - Fork 29
/
cli.go
422 lines (342 loc) · 9.94 KB
/
cli.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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
package cli
import (
"encoding/json"
"errors"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"path"
"path/filepath"
"reflect"
"strings"
"time"
"github.com/cheggaaa/pb"
"github.com/dogestry/dogestry/config"
"github.com/dogestry/dogestry/remote"
docker "github.com/fsouza/go-dockerclient"
homedir "github.com/mitchellh/go-homedir"
)
func newDockerClient(host string) (*docker.Client, error) {
var err error
var newClient *docker.Client
dockerCertPath := os.Getenv("DOCKER_CERT_PATH")
homeDir, _ := homedir.Dir()
dockerConfigDir := path.Join(homeDir, ".docker")
_, err = os.Stat(path.Join(dockerConfigDir, "cert.pem"))
certExists := err == nil
_, err = os.Stat(path.Join(dockerConfigDir, "ca.pem"))
caExists := err == nil
_, err = os.Stat(path.Join(dockerConfigDir, "key.pem"))
keyExists := err == nil
if dockerCertPath == "" && certExists && caExists && keyExists {
dockerCertPath = dockerConfigDir
}
if dockerCertPath != "" {
cert := path.Join(dockerCertPath, "cert.pem")
key := path.Join(dockerCertPath, "key.pem")
ca := path.Join(dockerCertPath, "ca.pem")
newClient, err = docker.NewTLSClient(host, cert, key, ca)
} else {
newClient, err = docker.NewClient(host)
}
if err != nil {
return nil, err
}
return newClient, err
}
func NewDogestryCli(cfg config.Config, hosts []string, tempDirRoot string) (*DogestryCli, error) {
var err error
dogestryCli := &DogestryCli{
Config: cfg,
err: os.Stderr,
DockerHost: cfg.Docker.Connection,
PullHosts: hosts,
TempDirRoot: tempDirRoot,
}
// Verify we were given a real dir - abort quickly and early on
if dogestryCli.TempDirRoot != "" {
fInfo, err := os.Stat(dogestryCli.TempDirRoot)
if err != nil {
log.Fatalf("Unable to verify temp dir: %v", err)
return nil, err
}
if !fInfo.IsDir() {
errMsg := fmt.Errorf("Temp dir %v is not a directory!", dogestryCli.TempDirRoot)
return nil, errMsg
}
}
dogestryCli.Client, err = newDockerClient(dogestryCli.DockerHost)
if err != nil {
log.Fatal(err)
return nil, err
}
if dogestryCli.PullHosts != nil && len(dogestryCli.PullHosts) > 0 {
var client *docker.Client
for _, host := range dogestryCli.PullHosts {
client, err = newDockerClient(host)
if err != nil {
log.Fatal(err)
return nil, err
}
dogestryCli.PullClients = append(dogestryCli.PullClients, client)
}
} else {
dogestryCli.PullHosts = []string{dogestryCli.DockerHost}
dogestryCli.PullClients = []*docker.Client{dogestryCli.Client}
}
return dogestryCli, nil
}
type DogestryCli struct {
Client *docker.Client
err io.Writer
TempDir string
TempDirRoot string
DockerHost string
Config config.Config
PullHosts []string
PullClients []*docker.Client
}
func (cli *DogestryCli) getMethod(name string) (func(...string) error, bool) {
methodName := "Cmd" + strings.ToUpper(name[:1]) + strings.ToLower(name[1:])
method := reflect.ValueOf(cli).MethodByName(methodName)
if !method.IsValid() {
return nil, false
}
return method.Interface().(func(...string) error), true
}
func (cli *DogestryCli) RunCmd(args ...string) error {
if len(args) > 0 {
method, exists := cli.getMethod(args[0])
if !exists {
fmt.Println("Error: Command not found:", args[0])
return cli.CmdHelp(args[1:]...)
}
return method(args[1:]...)
}
return cli.CmdHelp(args...)
}
func (cli *DogestryCli) Subcmd(name, signature, description string) *flag.FlagSet {
flags := flag.NewFlagSet(name, flag.ContinueOnError)
flags.Usage = func() {
fmt.Fprintf(cli.err, "\nUsage: dogestry %s %s\n\n%s\n\n", name, signature, description)
}
return flags
}
// CreateAndReturnTempDir creates and returns temporary work dir
// This dir is cleaned up on exit (if you call Cleanup())
func (cli *DogestryCli) CreateAndReturnTempDir() (string, error) {
if tempDir, err := ioutil.TempDir(cli.TempDirRoot, "dogestry"); err != nil {
return "", err
} else {
cli.TempDir = tempDir
}
return cli.TempDir, nil
}
// WorkDirGivenBaseDir creates temporary dir
func (cli *DogestryCli) WorkDirGivenBaseDir(basedir, suffix string) (string, error) {
suffix = strings.Replace(suffix, ":", "_", -1)
path := filepath.Join(basedir, suffix)
fmt.Printf("WorkDir: %v\n", path)
if err := os.MkdirAll(path, os.ModeDir|0700); err != nil {
return "", err
}
return path, nil
}
// WorkDir creates temporary dir
func (cli *DogestryCli) WorkDir(suffix string) (string, error) {
suffix = strings.Replace(suffix, ":", "_", -1)
basedir, err := cli.CreateAndReturnTempDir()
if err != nil {
return "", err
}
return cli.WorkDirGivenBaseDir(basedir, suffix)
}
// clean up the tempDir
func (cli *DogestryCli) Cleanup() {
if cli.TempDir != "" {
if err := os.RemoveAll(cli.TempDir); err != nil {
log.Println(err)
}
}
}
func (cli *DogestryCli) getLayerIdsToDownload(fromId remote.ID, imageRoot string, r remote.Remote, client *docker.Client) ([]remote.ID, error) {
toDownload := make([]remote.ID, 0)
err := r.WalkImages(fromId, func(id remote.ID, image docker.Image, err error) error {
fmt.Printf("Examining id '%s' on remote docker host...\n", id.Short())
if err != nil {
return err
}
_, err = client.InspectImage(string(id))
if err == docker.ErrNoSuchImage {
toDownload = append(toDownload, id)
return nil
} else if err != nil {
return err
} else {
fmt.Printf("Docker host already has id '%s', stop scanning.\n", id.Short())
return remote.BreakWalk
}
return nil
})
return toDownload, err
}
func (cli *DogestryCli) pullImage(fromId remote.ID, imageRoot string, r remote.Remote) error {
toDownload, err := cli.getLayerIdsToDownload(fromId, imageRoot, r, cli.Client)
if err != nil {
return err
}
for _, id := range toDownload {
downloadPath := filepath.Join(imageRoot, string(id))
fmt.Printf("Pulling image id '%s' to: %v\n", id.Short(), downloadPath)
err := r.PullImageId(id, downloadPath)
if err != nil {
return err
}
}
return nil
}
func (cli *DogestryCli) createRepositoriesJsonFile(image, imageRoot string, r remote.Remote) error {
repoName, repoTag := remote.NormaliseImageName(image)
id, err := r.ParseTag(repoName, repoTag)
if err != nil {
return err
} else if id == "" {
return nil
}
reposPath := filepath.Join(imageRoot, "repositories")
reposFile, err := os.Create(reposPath)
if err != nil {
return err
}
defer reposFile.Close()
repositories := map[string]Repository{}
repositories[repoName] = Repository{}
repositories[repoName][repoTag] = string(id)
return json.NewEncoder(reposFile).Encode(&repositories)
}
type Status struct {
Host string
Status string
Error string `json:",omitempty"`
}
// makeStatusJSON returns status JSON
func (cli *DogestryCli) makeStatusJSON(errMap map[string]error) ([]byte, error) {
var statusMap = make([]Status, len(cli.PullHosts))
var status Status
for i, host := range cli.PullHosts {
status.Host = host
if val, ok := errMap[host]; ok {
status.Status = "failed"
status.Error = val.Error()
} else {
status.Status = "ok"
}
statusMap[i] = status
}
return json.Marshal(statusMap)
}
func (cli *DogestryCli) outputStatus(errMap map[string]error) error {
result, err := cli.makeStatusJSON(errMap)
if err != nil {
return err
}
fmt.Println(string(result[:]))
return nil
}
// sendTar streams exported tarball into remote docker hosts
func (cli *DogestryCli) sendTar(imageRoot string) error {
type hostErrTuple struct {
host string
err error
}
type placeboProgressBar struct {
count int
bar *pb.ProgressBar
}
// Avoid displaying the progress bar if dogestry is running in server mode
if !cli.Config.ServerMode {
progressBar := &placeboProgressBar{1000, pb.StartNew(1000)}
progressBar.bar.ShowCounters = false
// Starts the placebo progress bar
go func(progressBar *placeboProgressBar) {
for {
progressBar.bar.Increment()
time.Sleep(time.Second)
}
}(progressBar)
}
tupleCh := make(chan hostErrTuple)
for i, client := range cli.PullClients {
host := cli.PullHosts[i]
go func(client *docker.Client, host string) {
cmd := exec.Command("tar", "cvf", "-", "-C", imageRoot, ".")
cmd.Env = os.Environ()
cmd.Dir = imageRoot
defer cmd.Wait()
stdout, err := cmd.StdoutPipe()
if err != nil {
tupleCh <- hostErrTuple{host, err}
return
}
if err := cmd.Start(); err != nil {
tupleCh <- hostErrTuple{host, err}
return
}
err = client.LoadImage(docker.LoadImageOptions{InputStream: stdout})
if err != nil {
tupleCh <- hostErrTuple{host, err}
return
}
tupleCh <- hostErrTuple{"", nil}
}(client, host)
}
uploadImageErrMap := make(map[string]error)
for range cli.PullClients {
tuple := <-tupleCh
if tuple.err != nil {
uploadImageErrMap[tuple.host] = tuple.err
}
}
close(tupleCh)
err := cli.outputStatus(uploadImageErrMap)
if len(uploadImageErrMap) > 0 {
err = errors.New("Error in sendTar")
}
return err
}
type DownloadMap map[remote.ID][]string
func (cli *DogestryCli) makeDownloadMap(r remote.Remote, id remote.ID, imageRoot string) (DownloadMap, error) {
var downloadMap = make(map[remote.ID][]string)
var err error
for i, pullHost := range cli.PullClients {
fmt.Printf("Connecting to remote docker host: %v\n", cli.PullHosts[i])
layers, err := cli.getLayerIdsToDownload(id, imageRoot, r, pullHost)
if err != nil {
return nil, err
}
for _, layer := range layers {
downloadMap[layer] = append(downloadMap[layer], cli.PullHosts[i])
}
}
return downloadMap, err
}
func (cli *DogestryCli) downloadImages(r remote.Remote, downloadMap DownloadMap, imageRoot string) error {
pullImagesErrMap := make(map[string]error)
for id, _ := range downloadMap {
downloadPath := filepath.Join(imageRoot, string(id))
fmt.Printf("Pulling image id '%s' to: %v\n", id.Short(), downloadPath)
err := r.PullImageId(id, downloadPath)
if err != nil {
pullImagesErrMap[downloadPath] = err
}
}
if len(pullImagesErrMap) > 0 {
fmt.Printf("Errors pulling images: %v\n", pullImagesErrMap)
return fmt.Errorf("Error downloading files from S3")
}
return nil
}