forked from rancher/rancher
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
190 lines (154 loc) · 3.78 KB
/
main.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
package main
import (
"fmt"
"log"
"os"
"sort"
"strings"
"github.com/rancher/norman/types/convert"
"github.com/rancher/types/apis/management.cattle.io/v3"
"github.com/rancher/types/image"
)
func main() {
if err := run(os.Args[1:]...); err != nil {
log.Fatal(err)
}
}
func run(images ...string) error {
targetImages, err := collectionImages(v3.K8sVersionToRKESystemImages, v3.ToolsSystemImages)
if err != nil {
return err
}
for _, i := range images {
targetImages = append(targetImages, image.Mirror(i))
}
err = imagesText(targetImages)
if err != nil {
return err
}
err = mirrorScript(targetImages)
if err != nil {
return err
}
err = saveScript(targetImages)
if err != nil {
return err
}
return loadScript(targetImages)
}
func loadScript(targetImages []string) error {
log.Println("Creating rancher-load-images.sh")
load, err := os.Create("rancher-load-images.sh")
if err != nil {
return err
}
defer load.Close()
load.Chmod(0755)
fmt.Fprintln(load, `#!/bin/sh
if [ -z "$1" ]; then
echo Usage: $0 [REGISTRY]
exit 1
fi
set -e -x
REGISTRY=$1
docker load --input rancher-images.tar.gz`)
fmt.Fprint(load, "\n\n")
for _, saveImage := range saveImages(targetImages) {
fmt.Fprintf(load, "docker tag %s ${REGISTRY}/%s\n", saveImage, saveImage)
fmt.Fprintf(load, "docker push ${REGISTRY}/%s\n", saveImage)
}
return nil
}
func saveImages(targetImages []string) []string {
var saveImages []string
for _, targetImage := range targetImages {
_, ok := image.Mirrors[targetImage]
if !ok {
continue
}
saveImages = append(saveImages, targetImage)
}
return saveImages
}
func saveScript(targetImages []string) error {
log.Println("Creating rancher-save-images.sh")
save, err := os.Create("rancher-save-images.sh")
if err != nil {
return err
}
defer save.Close()
save.Chmod(0755)
fmt.Fprintf(save, "#!/bin/sh\nset -e -x\n\n")
for _, targetImage := range saveImages(targetImages) {
fmt.Fprintf(save, "docker pull %s\n", targetImage)
}
fmt.Fprintf(save, "docker save %s | gzip -c > rancher-images.tar.gz\n",
strings.Join(saveImages(targetImages), " "))
return nil
}
func imagesText(targetImages []string) error {
log.Println("Creating rancher-images.txt")
save, err := os.Create("rancher-images.txt")
if err != nil {
return err
}
defer save.Close()
save.Chmod(0755)
for _, image := range saveImages(targetImages) {
log.Println("Image:", image)
fmt.Fprintln(save, image)
}
return nil
}
func mirrorScript(targetImages []string) error {
log.Println("Creating rancher-mirror-to-rancher-org.sh")
mirror, err := os.Create("rancher-mirror-to-rancher-org.sh")
if err != nil {
return err
}
defer mirror.Close()
mirror.Chmod(0755)
fmt.Fprintf(mirror, "#!/bin/sh\nset -e -x\n\n")
var saveImages []string
for _, targetImage := range targetImages {
srcImage, ok := image.Mirrors[targetImage]
if !ok {
continue
}
saveImages = append(saveImages, targetImage)
fmt.Fprintf(mirror, "docker pull %s\n", srcImage)
if targetImage != srcImage {
fmt.Fprintf(mirror, "docker tag %s %s\n", srcImage, targetImage)
fmt.Fprintf(mirror, "docker push %s\n", targetImage)
}
}
return nil
}
func collectionImages(objs ...interface{}) ([]string, error) {
images := map[string]bool{}
for _, obj := range objs {
data := map[string]interface{}{}
if err := convert.ToObj(obj, &data); err != nil {
return nil, err
}
findStrings(data, images)
}
var result []string
for k := range images {
result = append(result, k)
}
sort.Slice(result, func(i, j int) bool {
return result[i] < result[j]
})
return result, nil
}
func findStrings(obj map[string]interface{}, found map[string]bool) {
for _, v := range obj {
switch t := v.(type) {
case string:
found[t] = true
case map[string]interface{}:
findStrings(t, found)
}
}
}