forked from pachyderm/pachyderm
/
images.go
119 lines (115 loc) · 3.25 KB
/
images.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
package images
import (
"fmt"
"io"
"time"
docker "github.com/fsouza/go-dockerclient"
"github.com/pachyderm/pachyderm/src/server/pkg/deploy/assets"
)
// Export a tarball of the images needed by a deployment.
func Export(opts *assets.AssetOpts, out io.Writer) error {
client, err := docker.NewClientFromEnv()
if err != nil {
return err
}
authConfigs, err := docker.NewAuthConfigurationsFromDockerCfg()
if err != nil {
return fmt.Errorf("error parsing auth: %s, try running `docker login`", err.Error())
}
if len(authConfigs.Configs) == 0 {
return fmt.Errorf("didn't find any valid auth configurations")
}
images := assets.Images(opts)
for _, image := range images {
repository, tag := docker.ParseRepositoryTag(image)
pulled := false
var loopErr []error
for registry, authConfig := range authConfigs.Configs {
if err := client.PullImage(
docker.PullImageOptions{
Repository: repository,
Tag: tag,
InactivityTimeout: 5 * time.Second,
},
authConfig,
); err != nil {
loopErr = append(loopErr, fmt.Errorf("error pulling from %s: %v", registry, err))
continue
}
pulled = true
break
}
if !pulled {
errStr := ""
for _, err := range loopErr {
errStr += err.Error() + "\n"
}
return fmt.Errorf("errors pulling image %s:%s:\n%s", repository, tag, errStr)
}
}
return client.ExportImages(docker.ExportImagesOptions{
Names: images,
OutputStream: out,
})
}
// Import a tarball of the images needed by a deployment such as the one
// created by Export and push those images to the registry specific in opts.
func Import(opts *assets.AssetOpts, in io.Reader) error {
client, err := docker.NewClientFromEnv()
if err != nil {
return err
}
authConfigs, err := docker.NewAuthConfigurationsFromDockerCfg()
if err != nil {
return fmt.Errorf("error parsing auth: %s, try running `docker login`", err.Error())
}
if len(authConfigs.Configs) == 0 {
return fmt.Errorf("didn't find any valid auth configurations")
}
if err := client.LoadImage(docker.LoadImageOptions{
InputStream: in,
}); err != nil {
return err
}
registry := opts.Registry
opts.Registry = "" // pretend we're using default images so we can get targets to tag
images := assets.Images(opts)
opts.Registry = registry
for _, image := range images {
repository, tag := docker.ParseRepositoryTag(image)
registryRepo := assets.AddRegistry(opts.Registry, repository)
if err := client.TagImage(image, docker.TagImageOptions{
Repo: registryRepo,
Tag: tag,
},
); err != nil {
return fmt.Errorf("error tagging image: %v", err)
}
pushed := false
var loopErr []error
for registry, authConfig := range authConfigs.Configs {
if err := client.PushImage(
docker.PushImageOptions{
Name: registryRepo,
Tag: tag,
Registry: opts.Registry,
InactivityTimeout: 5 * time.Second,
},
authConfig,
); err != nil {
loopErr = append(loopErr, fmt.Errorf("error pushing to %s: %v", registry, err))
continue
}
pushed = true
break
}
if !pushed {
errStr := ""
for _, err := range loopErr {
errStr += err.Error() + "\n"
}
return fmt.Errorf("errors pushing image %s:%s:\n%s", registryRepo, tag, errStr)
}
}
return nil
}