-
Notifications
You must be signed in to change notification settings - Fork 0
/
satchel.go
250 lines (195 loc) · 5.29 KB
/
satchel.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
package main
import (
"context"
"flag"
"fmt"
"os"
"github.com/pelletier/go-toml"
"compress/gzip"
"docker.io/go-docker"
"docker.io/go-docker/api/types"
"io"
"log"
"text/template"
)
const manifest_name = "satchel-manifest.toml"
const loadScriptName = "load-images.sh"
const loadScriptTemplate = `#!/bin/bash
repository=${1}
imageFile={{ .OutputFile }}
if [ "$repository" == "" ]; then
echo "Repository not specified"
exit 1
fi
echo Loading Images from ${imageFile}
docker load < ${imageFile}
{{ range $key, $value := .Descriptor.Images }}
echo "Tagging {{ .Repository }}:{{ .Tag }} -> ${repository}/{{ .Repository }}:{{ .Tag }}"
docker tag {{ .Repository }}:{{ .Tag }} ${repository}/{{ .Repository }}:{{ .Tag }}
{{ end }}
{{ range $key, $value := .Descriptor.Images }}
echo "Pushing ${repository}/{{ .Repository }}:{{ .Tag }}"
docker push ${repository}/{{ .Repository }}:{{ .Tag }}
{{ end }}
`
const usage = `
satchel pulls and packs docker images between private registries
Flags:
`
type Descriptor struct {
Images []Image `toml:"image"`
}
type Image struct {
Registry string `toml:"registry"`
Repository string `toml:"repository"`
Tag string `toml:"tag"`
Public bool `toml:"public"`
}
func (i Image) ImageName() string {
registry := i.Registry
if registry != "" {
registry = registry + "/"
}
return fmt.Sprintf("%s%s:%s", registry, i.Repository, i.Tag)
}
func (i Image) ImageNameNoRegistry() string {
return fmt.Sprintf("%s:%s", i.Repository, i.Tag)
}
var inputFile string
var outputFile string
var includePublic bool
func main() {
flag.Usage = func() {
fmt.Fprintf(os.Stderr, usage)
flag.PrintDefaults()
os.Exit(2)
}
flag.StringVar(&inputFile, "in", "satchel.toml", "Input TOML manifest to use")
flag.StringVar(&outputFile, "out", "satchel-images.tgz", "Name of archive file to generate")
flag.BoolVar(&includePublic, "public", false, "Include public images in the archive")
flag.Parse()
validate()
descriptor := loadDescriptor()
pullImages(descriptor.Images)
tagImages(descriptor.Images)
saveImages(descriptor.Images)
generateScript(*descriptor)
}
func validate() {
if _, err := os.Stat(inputFile); err != nil {
log.Fatalf("Input file '%v' not found", inputFile)
}
}
func loadDescriptor() *Descriptor {
tree, err := toml.LoadFile(inputFile)
if err != nil {
log.Fatalf("Error Loading File %v: %v", inputFile, err)
}
descriptor := &Descriptor{}
tree.Unmarshal(descriptor)
for i := range descriptor.Images {
if descriptor.Images[i].Tag == "" {
descriptor.Images[i].Tag = "latest"
}
}
return descriptor
}
func pullImages(images []Image) {
cli := newDockerClient()
for _, image := range images {
if image.Public && !includePublic {
continue
}
log.Printf("Pulling Image %s\n", image.ImageName())
_, err := cli.ImagePull(context.Background(), image.ImageName(), types.ImagePullOptions{})
if err != nil {
log.Fatal(err)
}
}
}
func tagImages(images []Image) {
cli := newDockerClient()
for _, image := range images {
if image.Public && !includePublic {
continue
}
src := image.ImageName()
dest := image.ImageNameNoRegistry()
log.Printf("Tagging Image %s -> %s", src, dest)
cli.ImageTag(context.Background(), src, dest)
}
}
func saveImages(images []Image) {
cli := newDockerClient()
imageSummaries, err := cli.ImageList(context.Background(), types.ImageListOptions{})
if err != nil {
log.Fatalf("Error Getting Docker Image List: %v", err)
}
imageIds := findImageIds(images, imageSummaries)
readerCloser, err := cli.ImageSave(context.Background(), imageIds)
if err != nil {
log.Fatalf("Error saving images: %v", err)
}
log.Printf("Writing images to %v", outputFile)
outFile, err := os.Create(outputFile)
if err != nil {
log.Fatalf("Error Creating Image File %v: %v", outputFile, err)
}
gzipWriter, err := gzip.NewWriterLevel(outFile, gzip.BestCompression)
if err != nil {
log.Fatalf("Error Creating Compressed Archive %v: %v", outputFile, err)
}
defer gzipWriter.Close()
defer readerCloser.Close()
_, err = io.Copy(gzipWriter, readerCloser)
}
func generateScript(descriptor Descriptor) {
t, err := template.New("script").Parse(loadScriptTemplate)
if err != nil {
log.Fatal(err)
}
log.Printf("Writing load script '%s'", loadScriptName)
f, err := os.OpenFile(loadScriptName, os.O_WRONLY|os.O_CREATE|os.O_TRUNC, 0700)
defer f.Close()
data := map[string]interface{}{
"OutputFile": outputFile,
"Descriptor": descriptor,
}
err = t.Execute(f, data)
if err != nil {
log.Fatalf("Error generating load script: %v", err)
}
}
func newDockerClient() *docker.Client {
cli, err := docker.NewEnvClient()
if err != nil {
log.Fatal(err)
}
return cli
}
func findImageIds(images []Image, imageSummaries []types.ImageSummary) []string {
var imageIds []string
for _, imageSummary := range imageSummaries {
if imageSummary.ParentID == "" {
for _, image := range images {
if image.Public && !includePublic {
continue
}
// Record image ID if it's a root image and has a correct tag
if containsTag(image.ImageNameNoRegistry(), imageSummary.RepoTags) {
imageIds = append(imageIds, imageSummary.ID)
break
}
}
}
}
return imageIds
}
func containsTag(tag string, tags []string) bool {
for _, searchTag := range tags {
if searchTag == tag {
return true
}
}
return false
}