-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathpushV2.go
145 lines (125 loc) · 3.4 KB
/
pushV2.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
// +build ignore
package main
import (
"bytes"
"encoding/json"
"fmt"
"io"
"io/ioutil"
"strings"
"github.com/docker/docker/pkg/tarsum"
"github.com/docker/docker/registry"
)
/*
Unused code, prepare the ground for full registry v2 when it's production ready
*/
func generateManifest(gitRepo *gitRepo, imageName, imageTag string) (*registry.ManifestData, error) {
branches, err := gitRepo.branch()
if err != nil {
return nil, err
}
var imageChecksums []string = make([]string, len(branches))
for _, br := range branches {
checksum := br.imageID()
sumTypeBytes, err := gitRepo.branchDescription(br)
if err != nil {
return nil, err
}
imageChecksums[br.number()] = string(sumTypeBytes) + ":" + checksum
}
manifest := ®istry.ManifestData{
Name: imageName,
Architecture: "amd64", //unclean but so far looks ok ...
Tag: imageTag,
SchemaVersion: 1,
FSLayers: make([]*registry.FSLayer, 0, 4),
}
for i, checksum := range imageChecksums {
if tarsum.VersionLabelForChecksum(checksum) != tarsum.Version1.String() {
//need to calculate the tarsum V1 for each layer ...
layerData, err := gitRepo.exportChangeSet(branches[i])
if err == ErrNoChange {
continue
}
if err != nil {
return nil, err
}
defer layerData.Close()
tarSum, err := tarsum.NewTarSum(layerData, true, tarsum.Version1)
if err != nil {
return nil, err
}
if _, err := io.Copy(ioutil.Discard, tarSum); err != nil {
return nil, err
}
checksum = tarSum.Sum(nil)
}
manifest.FSLayers = append(manifest.FSLayers, ®istry.FSLayer{BlobSum: checksum})
}
return manifest, nil
}
func (s *registrySession) pushRepositoryV2(imageName, imageTag, rootfs string) error {
if !isGitRepo(rootfs) {
return fmt.Errorf("%v not a git repository", rootfs)
}
gitRepo, _ := newGitRepo(rootfs)
endpoint, err := s.V2RegistryEndpoint(s.indexInfo)
if err != nil {
return err
}
auth, err := s.GetV2Authorization(endpoint, imageName, true)
if err != nil {
return err
}
fmt.Printf("Registry endpoint: %v\n", endpoint)
manifest, err := generateManifest(gitRepo, imageName, imageTag)
if err != nil {
return err
}
manifestBytes, err := json.MarshalIndent(manifest, "", " ")
if err != nil {
return err
}
branches, err := gitRepo.branch()
if err != nil {
return err
}
orderedBranches := make([]branch, len(branches))
for _, br := range branches {
orderedBranches[br.number()] = br
}
for i := len(manifest.FSLayers) - 1; i >= 0; i-- {
sumStr := manifest.FSLayers[i].BlobSum
sumParts := strings.SplitN(sumStr, ":", 2)
if len(sumParts) < 2 {
return fmt.Errorf("Invalid checksum: %s", sumStr)
}
manifestSum := sumParts[1]
// Call mount blob
exists, err := s.HeadV2ImageBlob(endpoint, imageName, sumParts[0], manifestSum, auth)
if err != nil {
return err
}
if !exists {
fmt.Println("Image doesn't exist")
layerData, err := gitRepo.exportChangeSet(orderedBranches[i])
if err != nil {
return err
}
defer layerData.Close()
fmt.Println(endpoint)
err = s.PutV2ImageBlob(endpoint, imageName, sumParts[0], sumParts[1], layerData, auth)
if err != nil {
return err
}
//todo push manifest
} else {
fmt.Println("Image already exists")
}
// push the manifest
if err := s.PutV2ImageManifest(endpoint, imageName, imageTag, bytes.NewReader([]byte(manifestBytes)), auth); err != nil {
return err
}
}
return nil
}