-
Notifications
You must be signed in to change notification settings - Fork 8
/
push.go
105 lines (89 loc) · 2.54 KB
/
push.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
package main
import (
"fmt"
"io/ioutil"
"path"
"github.com/docker/docker/registry"
)
func (s *registrySession) pushRepository(imageName, imageTag, rootfs string) error {
if !isGitRepo(rootfs) {
return fmt.Errorf("%v not a git repository", rootfs)
}
gitRepo, _ := newGitRepo(rootfs)
branches, err := gitRepo.branch()
if err != nil {
return err
}
var imageIds []string = make([]string, len(branches))
for _, br := range branches {
imageIds[br.number()] = br.imageID()
}
fmt.Printf("Pushing %d layers:\n", len(imageIds))
//Push image index
var imageIndex []*registry.ImgData
for _, id := range imageIds {
imageIndex = append(imageIndex, ®istry.ImgData{ID: id, Tag: imageTag})
}
repoData, err := s.PushImageJSONIndex(imageName, imageIndex, false, nil)
if err != nil {
return err
}
ep := repoData.Endpoints[0]
//make sure existing branches are pushed
for i, imageId := range imageIds {
fmt.Printf("\t%v ... ", imageId)
if err := s.LookupRemoteImage(imageId, ep, repoData.Tokens); err == nil {
fmt.Printf("done (already pushed)\n")
} else {
err = s.pushImageLayer(gitRepo, branches[i], imageId, ep, repoData.Tokens)
if err != nil {
if err == registry.ErrAlreadyExists {
fmt.Printf("done (already pushed)\n")
} else {
return err
}
} else {
fmt.Printf("done\n")
}
}
//push tag
if err := s.PushRegistryTag(imageName, imageId, imageTag, ep, repoData.Tokens); err != nil {
return err
}
}
//Finalize push
if _, err = s.PushImageJSONIndex(imageName, imageIndex, true, repoData.Endpoints); err != nil {
return err
}
return nil
}
func (s *registrySession) pushImageLayer(gitRepo *gitRepo, br branch, imgID, ep string, token []string) error {
if _, err := gitRepo.checkout(br); err != nil {
return err
}
jsonRaw, err := ioutil.ReadFile(path.Join(gitRepo.Path, "json"))
if err != nil {
//if json is not found, this probably means that user pull the image using V2 registry
fmt.Printf("Hint: you can't push images pulled using the -v2 flag yet")
return err
}
imgData := ®istry.ImgData{
ID: imgID,
}
// Send the json
if err := s.PushImageJSONRegistry(imgData, jsonRaw, ep, token); err != nil {
return err
}
layerData, err := gitRepo.exportChangeSet(br)
if err != nil {
return err
}
defer layerData.Close()
checksum, checksumPayload, err := s.PushImageLayerRegistry(imgID, layerData, ep, token, jsonRaw)
if err != nil {
return err
}
imgData.Checksum = checksum
imgData.ChecksumPayload = checksumPayload
return s.PushImageChecksumRegistry(imgData, ep, token)
}