-
Notifications
You must be signed in to change notification settings - Fork 444
/
main.go
355 lines (310 loc) · 11 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
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
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
package main
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"github.com/docker/docker/api/types"
"github.com/docker/docker/client"
"github.com/google/go-github/v47/github"
"golang.org/x/oauth2"
"gopkg.in/alecthomas/kingpin.v2"
"gopkg.in/yaml.v3"
"io"
"os"
"regexp"
"strings"
"text/template"
)
var resultTpl = `
{{ if .Success }}
**转换完成**
^^^bash
{{ if .Registry }}
docker login -u{{ .RegistryUser }} {{ .Registry }}
{{ end }}
#原镜像
{{ .OriginImageName }}
#转换后镜像
{{ .TargetImageName }}
#下载并重命名镜像
docker pull {{ .TargetImageName }} {{ if .Platform }} --platform {{ .Platform }} {{ end }}
docker tag {{ .TargetImageName }} {{ index (split .OriginImageName "@") 0 }}
docker images | grep $(echo {{ .OriginImageName }} |awk -F':' '{print $1}')
^^^
{{ else }}
**转换失败**
详见 [构建任务](https://github.com/{{ .GhUser }}/{{ .Repo }}/actions/runs/{{ .RunId }})
{{ end }}
`
func main() {
ctx := context.Background()
var (
ghToken = kingpin.Flag("github.token", "Github token.").Short('t').String()
ghUser = kingpin.Flag("github.user", "Github Owner.").Short('u').String()
ghRepo = kingpin.Flag("github.repo", "Github Repo.").Short('p').String()
registry = kingpin.Flag("docker.registry", "Docker Registry.").Short('r').Default("").String()
registryNamespace = kingpin.Flag("docker.namespace", "Docker Registry Namespace.").Short('n').String()
registryUserName = kingpin.Flag("docker.user", "Docker Registry User.").Short('a').String()
registryPassword = kingpin.Flag("docker.secret", "Docker Registry Password.").Short('s').String()
runId = kingpin.Flag("github.run_id", "Github Run Id.").Short('i').String()
)
kingpin.HelpFlag.Short('h')
kingpin.Parse()
config := &Config{
GhToken: *ghToken,
GhUser: *ghUser,
Repo: *ghRepo,
Registry: *registry,
RegistryNamespace: *registryNamespace,
RegistryUserName: *registryUserName,
RegistryPassword: *registryPassword,
RunId: *runId,
Rules: map[string]string{
"^gcr.io": "",
"^docker.io": "docker",
"^k8s.gcr.io": "google-containers",
"^registry.k8s.io": "google-containers",
"^quay.io": "quay",
"^ghcr.io": "ghcr",
},
}
rulesFile, err := os.ReadFile("rules.yaml")
if err == nil {
rules := make(map[string]string)
err2 := yaml.Unmarshal(rulesFile, &rules)
if err2 == nil {
config.Rules = rules
}
}
ts := oauth2.StaticTokenSource(
&oauth2.Token{AccessToken: config.GhToken},
)
tc := oauth2.NewClient(ctx, ts)
cli := github.NewClient(tc)
issues, err := getIssues(cli, ctx, config)
if err != nil {
fmt.Println("查询 Issues 报错,", err.Error())
os.Exit(-1)
}
if len(issues) == 0 {
fmt.Println("暂无需要搬运的镜像")
os.Exit(0)
}
// 可以用协程,但是懒得写
issue := issues[0]
fmt.Println("添加 构建进展 Comment")
commentIssues(issue, cli, ctx, "[构建进展](https://github.com/"+config.GhUser+"/"+config.Repo+"/actions/runs/"+config.RunId+")")
err, originImageName, targetImageName, platform := mirrorByIssues(issue, config)
if err != nil {
commentErr := commentIssues(issue, cli, ctx, err.Error())
if commentErr != nil {
fmt.Println("提交 comment 报错", commentErr)
}
}
result := struct {
Success bool
Registry string
RegistryUser string
OriginImageName string
TargetImageName string
Platform string
GhUser string
Repo string
RunId string
}{
Success: err == nil,
Registry: config.Registry,
RegistryUser: config.RegistryUserName,
OriginImageName: originImageName,
TargetImageName: targetImageName,
Platform: platform,
GhUser: *ghUser,
Repo: *ghRepo,
RunId: *runId,
}
var buf bytes.Buffer
funcmap := template.FuncMap{
"split": strings.Split,
}
tmpl, err := template.New("result").Funcs(funcmap).Parse(resultTpl)
err = tmpl.Execute(&buf, &result)
fmt.Println("添加 转换结果 Comment")
res := buf.String()
commentIssues(issue, cli, ctx, strings.ReplaceAll(res, "^", "`"))
fmt.Println("添加 转换结果 Label")
var labels []string
if len(platform) > 0 {
labels = append(labels, "platform")
}
issuesAddLabels(issue, cli, ctx, result.Success, labels)
fmt.Println("关闭 Issues")
issuesClose(issue, cli, ctx)
}
func issuesClose(issues *github.Issue, cli *github.Client, ctx context.Context) {
names := strings.Split(*issues.RepositoryURL, "/")
state := "closed"
cli.Issues.Edit(ctx, names[len(names)-2], names[len(names)-1], issues.GetNumber(), &github.IssueRequest{
State: &state,
})
}
func issuesAddLabels(issues *github.Issue, cli *github.Client, ctx context.Context, success bool, labels []string) {
names := strings.Split(*issues.RepositoryURL, "/")
label := "success"
if !success {
label = "failed"
}
if labels == nil {
labels = []string{label}
} else {
labels = append(labels, label)
}
cli.Issues.AddLabelsToIssue(ctx, names[len(names)-2], names[len(names)-1], issues.GetNumber(), labels)
}
func commentIssues(issues *github.Issue, cli *github.Client, ctx context.Context, comment string) error {
names := strings.Split(*issues.RepositoryURL, "/")
_, _, err := cli.Issues.CreateComment(ctx, names[len(names)-2], names[len(names)-1], issues.GetNumber(), &github.IssueComment{
Body: &comment,
})
return err
}
func mirrorByIssues(issues *github.Issue, config *Config) (err error, originImageName string, targetImageName string, platform string) {
// 去掉前缀 [PORTER] 整体去除前后空格
originImageName = strings.TrimSpace(strings.Replace(*issues.Title, "[PORTER]", "", 1))
names := strings.Split(originImageName, "|")
originImageName = names[0]
if len(names) > 1 {
platform = names[1]
}
if strings.Index(originImageName, ".") > strings.Index(originImageName, "/") {
originImageName = "docker.io/" + originImageName
}
targetImageName = originImageName
if strings.ContainsAny(originImageName, "@") {
targetImageName = strings.Split(originImageName, "@")[0]
}
registrys := []string{}
for k, v := range config.Rules {
targetImageName = regexp.MustCompile(k).ReplaceAllString(targetImageName, v)
registrys = append(registrys, k)
}
if strings.EqualFold(targetImageName, originImageName) {
return errors.New("@" + *issues.GetUser().Login + " 暂不支持同步" + originImageName + ",目前仅支持同步 `" + strings.Join(registrys, " ,") + "`镜像"), originImageName, targetImageName, platform
}
targetImageName = strings.ReplaceAll(targetImageName, "/", ".")
if len(config.RegistryNamespace) > 0 {
targetImageName = config.RegistryNamespace + "/" + targetImageName
}
if len(config.Registry) > 0 {
targetImageName = config.Registry + "/" + targetImageName
}
fmt.Println("source:", originImageName, " , target:", targetImageName)
//execCmd("docker", "login", config.Registry, "-u", config.RegistryUserName, "-p", config.RegistryPassword)
cli, ctx, err := dockerLogin(config)
if err != nil {
return errors.New("@" + config.GhUser + " ,docker login 报错 `" + err.Error() + "`"), originImageName, targetImageName, platform
}
//execCmd("docker", "pull", originImageName)
err = dockerPull(originImageName, platform, cli, ctx)
if err != nil {
return errors.New("@" + *issues.GetUser().Login + " ,docker pull 报错 `" + err.Error() + "`"), originImageName, targetImageName, platform
}
//execCmd("docker", "tag", originImageName, targetImageName)
err = dockerTag(originImageName, targetImageName, cli, ctx)
if err != nil {
return errors.New("@" + *issues.GetUser().Login + " ,docker tag 报错 `" + err.Error() + "`"), originImageName, targetImageName, platform
}
//execCmd("docker", "push", targetImageName)
err = dockerPush(targetImageName, platform, cli, ctx, config)
if err != nil {
return errors.New("@" + *issues.GetUser().Login + " ,docker push 报错 `" + err.Error() + "`"), originImageName, targetImageName, platform
}
return nil, originImageName, targetImageName, platform
}
func dockerLogin(config *Config) (*client.Client, context.Context, error) {
cli, err := client.NewClientWithOpts(client.FromEnv, client.WithAPIVersionNegotiation())
if err != nil {
return nil, nil, err
}
fmt.Println("docker login, server: ", config.Registry, " user: ", config.RegistryUserName, ", password: ***")
authConfig := types.AuthConfig{
Username: config.RegistryUserName,
Password: config.RegistryPassword,
ServerAddress: config.Registry,
}
ctx := context.Background()
_, err = cli.RegistryLogin(ctx, authConfig)
if err != nil {
return nil, nil, err
}
return cli, ctx, nil
}
func dockerPull(originImageName string, platform string, cli *client.Client, ctx context.Context) error {
fmt.Println("docker pull ", originImageName)
pullOut, err := cli.ImagePull(ctx, originImageName, types.ImagePullOptions{
Platform: platform,
})
if err != nil {
return err
}
defer pullOut.Close()
io.Copy(os.Stdout, pullOut)
return nil
}
func dockerTag(originImageName string, targetImageName string, cli *client.Client, ctx context.Context) error {
fmt.Println("docker tag ", originImageName, " ", targetImageName)
err := cli.ImageTag(ctx, originImageName, targetImageName)
return err
}
func dockerPush(targetImageName string, platform string, cli *client.Client, ctx context.Context, config *Config) error {
fmt.Println("docker push ", targetImageName)
authConfig := types.AuthConfig{
Username: config.RegistryUserName,
Password: config.RegistryPassword,
}
if len(config.Registry) > 0 {
authConfig.ServerAddress = config.Registry
}
encodedJSON, err := json.Marshal(authConfig)
if err != nil {
return err
}
authStr := base64.URLEncoding.EncodeToString(encodedJSON)
pushOut, err := cli.ImagePush(ctx, targetImageName, types.ImagePushOptions{
RegistryAuth: authStr,
Platform: platform,
})
if err != nil {
return err
}
defer pushOut.Close()
io.Copy(os.Stdout, pushOut)
return nil
}
type Config struct {
GhToken string `yaml:"gh_token"`
GhUser string `yaml:"gh_user"`
Repo string `yaml:"repo"`
Registry string `yaml:"registry"`
RegistryNamespace string `yaml:"registry_namespace"`
RegistryUserName string `yaml:"registry_user_name"`
RegistryPassword string `yaml:"registry_password"`
Rules map[string]string `yaml:"rules"`
RunId string `yaml:"run_id"`
}
func getIssues(cli *github.Client, ctx context.Context, config *Config) ([]*github.Issue, error) {
issues, _, err := cli.Issues.ListByRepo(ctx, config.GhUser, config.Repo, &github.IssueListByRepoOptions{
//State: "closed",
State: "open",
Labels: []string{"porter"},
Sort: "created",
Direction: "desc",
// 防止被滥用,每次最多只能拉20条,虽然可以递归,但是没必要。
//ListOptions: github.ListOptions{Page: 1, PerPage: 20},
// 考虑了下,每次还是只允许转一个吧
ListOptions: github.ListOptions{Page: 1, PerPage: 1},
})
return issues, err
}