-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpull.go
121 lines (98 loc) · 2.72 KB
/
pull.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
package main
import (
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"github.com/docker/docker/pkg/archive"
)
const (
MAX_DL_CONCURRENCY = 7
ONE_MB = 1000000
)
//krgo pull image -r rootfs
//download a flattened docker image from the V1 registry
func (s *registrySession) pullImage(imageName, imageTag, rootfsDest string) error {
return s.downloadImage(imageName, imageTag, rootfsDest, false)
}
//krgo pull image -r rootfs -g
//download a docker image from the V1 registry putting each layer in a git branch "on top of each other"
func (s *registrySession) pullRepository(imageName, imageTag, rootfsDest string) error {
return s.downloadImage(imageName, imageTag, rootfsDest, true)
}
//pulling using V1 registry
func (s *registrySession) downloadImage(imageName, imageTag, rootfsDest string, gitLayering bool) error {
repoData, err := s.GetRepositoryData(imageName)
if err != nil {
return err
}
fmt.Printf("Registry endpoint: %v\n", repoData.Endpoints)
tagsList, err := s.GetRemoteTags(repoData.Endpoints, imageName, repoData.Tokens)
if err != nil {
return err
}
imageId := tagsList[imageTag]
fmt.Printf("Image ID: %v\n", imageId)
//Download image history
var imageHistory []string
for _, ep := range repoData.Endpoints {
imageHistory, err = s.GetRemoteHistory(imageId, ep, repoData.Tokens)
if err == nil {
break
}
}
if err != nil {
return err
}
err = os.MkdirAll(rootfsDest, 0700)
if err != nil {
return err
}
var gitRepo *gitRepo
if gitLayering {
if gitRepo, err = newGitRepo(rootfsDest); err != nil {
return err
}
}
queue := NewQueue(MAX_DL_CONCURRENCY)
fmt.Printf("Pulling %d layers:\n", len(imageHistory))
for i := len(imageHistory) - 1; i >= 0; i-- {
layerId := imageHistory[i]
job := NewPullingJob(s, repoData, layerId)
queue.Enqueue(job)
}
<-queue.DoneChan
fmt.Printf("Downloading layers:\n")
cpt := 0
for i := len(imageHistory) - 1; i >= 0; i-- {
//for each layers
layerID := imageHistory[i]
if gitLayering {
//create a git branch
if _, err = gitRepo.checkoutB(newBranch(cpt, layerID)); err != nil {
return err
}
}
//download and untar the layer
job := queue.CompletedJobWithID(layerID).(*PullingJob)
fmt.Printf("\t%s (%.2f MB) ... ", layerID, float64(job.LayerSize)/ONE_MB)
_, err = archive.ApplyLayer(rootfsDest, job.LayerData)
job.LayerData.Close()
if err != nil {
return err
}
ioutil.WriteFile(path.Join(rootfsDest, "json"), job.LayerInfo, 0644)
if gitLayering {
ioutil.WriteFile(path.Join(rootfsDest, "layersize"), []byte(strconv.Itoa(job.LayerSize)), 0644)
}
if gitLayering {
if _, err = gitRepo.addAllAndCommit("adding layer " + layerID); err != nil {
return err
}
}
cpt++
fmt.Printf("done\n")
}
return nil
}