-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcopyimageto.go
213 lines (182 loc) · 6.48 KB
/
copyimageto.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
/*
* Tencent is pleased to support the open source community by making BK-CI 蓝鲸持续集成平台 available.
*
* Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
*
* BK-CI 蓝鲸持续集成平台 is licensed under the MIT license.
*
* A copy of the MIT License is included in this file.
*
*
* Terms of the MIT License:
* ---------------------------------------------------
* Permission is hereby granted, free of charge, to any person obtaining a copy of this
* software and associated documentation
* files (the "Software"), to deal in the Software without restriction, including
* without limitation the rights to use,copy,
* modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies
* or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
* LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
* NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
rice "github.com/GeertJohan/go.rice"
"github.com/ci-plugins/DockerBuildPush/api"
"github.com/ci-plugins/DockerBuildPush/log"
"github.com/syyongx/php2go"
)
func unzipBinExeFileToDisk(binFilePath string, filename string) {
if php2go.IsFile(binFilePath) {
//文件已经存在
log.Info(binFilePath + "文件已经存在,先进行删除,再次解压")
php2go.Delete(binFilePath)
//return
}
conf := rice.Config{
LocateOrder: []rice.LocateMethod{rice.LocateEmbedded, rice.LocateAppended, rice.LocateFS},
}
log.Warn("使用go rice对嵌入资源bin_file进行解压")
box, err := conf.FindBox("bin_file")
if err != nil {
log.Warn(fmt.Sprintf("打开 rice.Box: %s %s失败,请确认源代码目录存在,并且执行过go rice打包\n", filename, err.Error()))
}
log.Warn("读取" + filename + "和写入")
skopeoBytes, err := box.Bytes(filename)
if err != nil {
log.Warn(fmt.Sprintf("没找到%s文件 byteSlice: %s\n", filename, err.Error()))
}
err = os.MkdirAll(filepath.Dir(binFilePath), 0644)
err = ioutil.WriteFile(binFilePath, skopeoBytes, 0644)
if err != nil {
log.Warn(fmt.Sprintf("写入skopeo文件失败: %s\n", err.Error()))
}
err = os.Chmod(binFilePath, 755)
}
func copyImageTo(srcUser string, srcPass string, dstUser string, dstPass string,
srcImageUrl string, dstImageUrl string) {
addHostsToFile()
var b strings.Builder
b.WriteString("/usr/local/bin/skopeo ")
b.WriteString(" --insecure-policy ")
b.WriteString(" --debug ")
b.WriteString(" copy ")
b.WriteString(" --dest-tls-verify=false ")
b.WriteString(" --src-tls-verify=false ")
if srcUser == "" || len(srcUser) <= 1 {
b.WriteString(" --src-no-creds ")
} else {
if srcUser != "" && srcPass != "" {
b.WriteString(" --src-username " + srcUser + " ")
b.WriteString(" --src-password " + srcPass + " ")
} else {
if srcUser != "" && srcPass == "" {
b.WriteString(" --src-username " + srcUser + " ")
} else {
//do nothing
}
}
}
if dstUser == "" || len(dstUser) <= 1 {
b.WriteString(" --dest-no-creds ")
} else {
if dstUser != "" && dstPass != "" {
b.WriteString(" --dest-username " + dstUser + " ")
b.WriteString(" --dest-password " + dstPass + " ")
} else {
if dstUser != "" && dstPass == "" {
b.WriteString(" --dest-username " + dstUser + " ")
} else {
//do nothing
}
}
}
if srcImageUrl != "" {
b.WriteString(" docker://" + srcImageUrl + " ")
}
if dstImageUrl != "" {
b.WriteString(" docker://" + dstImageUrl + " ")
}
//log.Debug("debug log , run shell command:"+b.String())
log.Info("开始复制镜像源 " + srcImageUrl + " 到目标 " + dstImageUrl)
err := exeCommandStdout(b.String())
if err != nil {
log.Error("复制镜像发生错误,一般可能是网络抖动问题建议重试,或者检查用户名密码授权是否正确或过期。")
//os.Exit(1)
api.FinishBuildWithError(api.StatusError,
"复制镜像发生错误,一般可能是网络抖动问题建议重试,或者检查用户名密码授权是否正确或过期。",
2, api.UserError)
}
log.Info("镜像复制完成")
}
func getUserAndPass(targetTicketId string, inputType string) (string, string) {
username := ""
password := ""
if targetTicketId != "" {
certs := MyGetCertificate(targetTicketId)
if len(certs) > 0 {
value, ok := certs["password"]
if ok {
password = value
//fmt.Printf(value)
} else {
}
valueu, ok := certs["username"]
if ok {
username = valueu
//fmt.Printf(value)
} else {
log.Warn("你选择的蓝盾凭证内容为空,或者选择的不是多行密码类型凭证.")
}
}
} else {
if inputType == "src" {
username = api.GetInputParam("srcRegUsername")
password = api.GetInputParam("srcRegPassword")
} else if inputType == "dst" {
username = api.GetInputParam("targetRegUsername")
password = api.GetInputParam("targetRegPassword")
} else {
log.Warn("从蓝盾读取凭证失败,可能的原因,1.所选凭证你没有权限,2.网络故障,3.凭证命名不存在")
}
}
return username, password
}
// CopyImageTo 从一个仓库复制一个镜像到另一个resgistry
func CopyImageTo() {
log.Info("插件开始执行CopyImageTo......")
targetImage := api.GetInputParam("targetImage")
targetTicketId := api.GetInputParam("targetTicketId")
targetImageTag := api.GetInputParam("targetImageTag")
srcImage := api.GetInputParam("srcImage")
srcTicketId := api.GetInputParam("srcTicketId")
srcImageTag := api.GetInputParam("srcImageTag")
srcUser, srcPass := getUserAndPass(srcTicketId, "src")
dstUser, dstPass := getUserAndPass(targetTicketId, "dst")
srcImageUrl := srcImage + ":" + srcImageTag
skopeoPath := "/usr/local/bin/skopeo"
unzipBinExeFileToDisk(skopeoPath, "skopeo")
if targetImageTag == "" {
targetImageTag = "latest"
}
tagLines := removeCRLF(targetImageTag)
for _, v := range tagLines {
if v != "" {
dstImageUrl := targetImage + ":" + v
copyImageTo(srcUser, srcPass, dstUser, dstPass, srcImageUrl, dstImageUrl)
}
}
}