forked from coreos/fedora-coreos-pipeline
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlibcloud.groovy
More file actions
356 lines (342 loc) · 14.6 KB
/
libcloud.groovy
File metadata and controls
356 lines (342 loc) · 14.6 KB
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
356
// Replicate artifacts in various clouds
def replicate_to_clouds(pipecfg, basearch, buildID, stream) {
def meta = readJSON(text: shwrapCapture("""
cosa meta --build=${buildID} --arch=${basearch} --dump
"""))
def replicators = [:]
def builders = [:]
def credentials
def stream_info = pipecfg.streams[stream]
credentials = [file(variable: "ALIYUN_IMAGE_UPLOAD_CONFIG",
credentialsId: "aliyun-image-upload-config")]
if (meta.aliyun) {
def creds = credentials
replicators["☁️ 🔄:aliyun"] = {
withCredentials(creds) {
def c = pipecfg.clouds.aliyun
def extraArgs = []
if (c.public) {
extraArgs += "--public"
}
shwrap("""
coreos-assembler aliyun-replicate \
--build=${buildID} \
--arch=${basearch} \
--config=\${ALIYUN_IMAGE_UPLOAD_CONFIG} \
${extraArgs.join(' ')}
""")
}
}
}
// For AWS we need to consider the primary AWS partition and the
// GovCloud partition. Define a closure here that we'll call for both.
def awsReplicateClosure = { config, credentialId, winli = false ->
def creds = [file(variable: "AWS_CONFIG_FILE", credentialsId: credentialId)]
withCredentials(creds) {
def c = config
def extraArgs = []
if (winli) {
extraArgs += "--winli"
}
shwrap("""
cosa aws-replicate \
--log-level=INFO \
--build=${buildID} \
--arch=${basearch} \
--source-region=${c.primary_region} \
--credentials-file=\${AWS_CONFIG_FILE} \
${extraArgs.join(' ')}
""")
}
}
// A closure to build the aws-winli AMI. This will be called before replicating to
// all regions, if the option is set for the current stream. `cosa aws-replicate`
// will handle both traditional AMIs and aws-winli AMIs if present in the metadata.
// aws-winli is only supported on x86_64.
def awsWinLIBuildClosure = { config, credentialId ->
def creds = [file(variable: "AWS_CONFIG_FILE", credentialsId: credentialId)]
withCredentials(creds) {
utils.syncCredentialsIfInRemoteSession(["AWS_CONFIG_FILE"])
def c = config
def extraArgs = []
if (c.grant_users) {
extraArgs += c.grant_users.collect{"--grant-user=${it}"}
extraArgs += c.grant_users.collect{"--grant-user-snapshot=${it}"}
}
if (c.tags) {
extraArgs += c.tags.collect { "--tags=${it}" }
}
if (c.public) {
extraArgs += "--public"
}
shwrap("""
cosa imageupload-aws \
--upload \
--winli \
--winli-billing-product ${c.winli_billing_code}\
--arch=${basearch} \
--build=${buildID} \
--region=${c.primary_region} \
--credentials-file=\${AWS_CONFIG_FILE} \
${extraArgs.join(' ')}
""")
}
}
if (meta.amis) {
credentials = [file(variable: "UNUSED", credentialsId: "aws-build-upload-config")]
if (pipecfg.clouds?.aws &&
utils.credentialsExist(credentials)) {
replicators["☁️ 🔄:aws"] = {
awsReplicateClosure.call(pipecfg.clouds.aws,
"aws-build-upload-config")
}
// aws-winli is only supported on x86_64
if ((basearch == "x86_64") && (stream_info.create_and_replicate_winli_ami)) {
builders["☁️ 🔨:aws-winli"] = {
awsWinLIBuildClosure.call(pipecfg.clouds.aws, "aws-build-upload-config")
}
replicators["☁️ 🔄:aws-winli"] = {
awsReplicateClosure.call(pipecfg.clouds.aws,
"aws-build-upload-config", true)
}
}
}
credentials = [file(variable: "UNUSED", credentialsId: "aws-govcloud-image-upload-config")]
if (pipecfg.clouds?.aws?.govcloud &&
utils.credentialsExist(credentials)) {
replicators["☁️ 🔄:aws:govcloud"] = {
awsReplicateClosure.call(pipecfg.clouds.aws.govcloud,
"aws-govcloud-image-upload-config")
}
}
}
credentials = [file(variable: "POWERVS_IMAGE_UPLOAD_CONFIG",
credentialsId: "powervs-image-upload-config")]
if (meta.powervs) {
def creds = credentials
replicators["☁️ 🔄:powervs"] = {
withCredentials(creds) {
def c = pipecfg.clouds.powervs
// for powervs in RHCOS the images are uploaded to a bucket in each
// region that is uniquely named with the region as a suffix
// i.e. `rhcos-powervs-images-us-east`
def regions = []
if (c.regions) {
regions = c.regions.join(" ")
}
shwrap("""
cosa powervs-replicate \
--cloud-object-storage ${c.cloud_object_storage_service_instance} \
--build ${buildID} \
--arch=${basearch} \
--bucket-prefix ${c.bucket} \
--regions ${regions} \
--credentials-file \${POWERVS_IMAGE_UPLOAD_CONFIG}
""")
}
}
}
parallel builders
parallel replicators
}
// Upload artifacts to clouds
def upload_to_clouds(pipecfg, basearch, buildID, stream) {
// In 4.19+ we switched the command to upload to clouds to
// `cosa imageupload-<cloud>`
// https://github.com/coreos/coreos-assembler/pull/4074
def image_upload_cmd = "imageupload"
if (shwrapRc("cosa shell -- test -e /usr/lib/coreos-assembler/cmd-imageupload-aws") != 0) {
image_upload_cmd = "buildextend"
}
// Get a list of the artifacts that are currently built.
def images_json = readJSON(text: shwrapCapture("""
cosa meta --build=${buildID} --arch=${basearch} --get-value images
"""))
def artifacts = images_json.keySet()
// Define an uploader closure for each artifact/cloud that we
// support uploading to. Only add the closure to the map if the
// artifact exists and we have credentials in our Jenkins env for
// uploading to that cloud.
def uploaders = [:]
def credentials
credentials = [file(variable: "ALIYUN_IMAGE_UPLOAD_CONFIG",
credentialsId: "aliyun-image-upload-config")]
if (pipecfg.clouds?.aliyun &&
artifacts.contains("aliyun") &&
utils.credentialsExist(credentials)) {
def creds = credentials
uploaders["☁️ ⬆️ :aliyun"] = {
withCredentials(creds) {
utils.syncCredentialsIfInRemoteSession(["ALIYUN_IMAGE_UPLOAD_CONFIG"])
def c = pipecfg.clouds.aliyun
def extraArgs = []
if (c.public) {
extraArgs += "--public"
}
shwrap("""
cosa ${image_upload_cmd}-aliyun \
--upload \
--arch=${basearch} \
--build=${buildID} \
--region=${c.primary_region} \
--bucket=${c.bucket} \
--config=\${ALIYUN_IMAGE_UPLOAD_CONFIG} \
${extraArgs.join(' ')}
""")
}
}
}
// For AWS we need to consider the primary AWS partition and the
// GovCloud partition. Define a closure here that we'll call for both.
def awsUploadClosure = { config, credentialId ->
def creds = [file(variable: "AWS_CONFIG_FILE", credentialsId: credentialId)]
withCredentials(creds) {
utils.syncCredentialsIfInRemoteSession(["AWS_CONFIG_FILE"])
def c = config
def extraArgs = []
if (c.grant_users) {
extraArgs += c.grant_users.collect{"--grant-user=${it}"}
extraArgs += c.grant_users.collect{"--grant-user-snapshot=${it}"}
}
if (c.tags) {
extraArgs += c.tags.collect { "--tags=${it}" }
}
if (c.public) {
extraArgs += "--public"
}
shwrap("""
cosa ${image_upload_cmd}-aws \
--upload \
--arch=${basearch} \
--build=${buildID} \
--region=${c.primary_region} \
--bucket=s3://${c.bucket} \
--credentials-file=\${AWS_CONFIG_FILE} \
${extraArgs.join(' ')}
""")
}
}
if (artifacts.contains("aws")) {
credentials = [file(variable: "UNUSED", credentialsId: "aws-build-upload-config")]
if (pipecfg.clouds?.aws &&
utils.credentialsExist(credentials)) {
uploaders["☁️ ⬆️ :aws"] = {
awsUploadClosure.call(pipecfg.clouds.aws,
"aws-build-upload-config")
}
}
credentials = [file(variable: "UNUSED", credentialsId: "aws-govcloud-image-upload-config")]
if (pipecfg.clouds?.aws?.govcloud &&
utils.credentialsExist(credentials)) {
uploaders["☁️ ⬆️ :aws:govcloud"] = {
awsUploadClosure.call(pipecfg.clouds.aws.govcloud,
"aws-govcloud-image-upload-config")
}
}
}
credentials = [file(variable: 'AZURE_IMAGE_UPLOAD_CONFIG',
credentialsId: 'azure-image-upload-config')]
if (pipecfg.clouds?.azure &&
artifacts.contains("azure") &&
utils.credentialsExist(credentials)) {
def creds = credentials
uploaders["☁️ ⬆️ :azure"] = {
withCredentials(creds) {
utils.syncCredentialsIfInRemoteSession(["AZURE_IMAGE_UPLOAD_CONFIG"])
def c = pipecfg.clouds.azure
shwrap("""cosa ${image_upload_cmd}-azure \
--upload \
--credentials \${AZURE_IMAGE_UPLOAD_CONFIG} \
--build=${buildID} \
--resource-group ${c.resource_group} \
--storage-account ${c.storage_account} \
--container=${c.storage_container}
""")
}
}
}
credentials = [file(variable: "GCP_IMAGE_UPLOAD_CONFIG",
credentialsId: "gcp-image-upload-config")]
if (pipecfg.clouds?.gcp &&
artifacts.contains("gcp") &&
utils.credentialsExist(credentials)) {
def creds = credentials
uploaders["☁️ ⬆️ :gcp"] = {
withCredentials(creds) {
utils.syncCredentialsIfInRemoteSession(["GCP_IMAGE_UPLOAD_CONFIG"])
def c = pipecfg.clouds.gcp
def extraArgs = []
if (c.family?."${basearch}") {
// If there is an image family then we set it on image creation
// and also start the image in a deprecated state, which will be
// un-deprecated in the release job.
extraArgs += "--family=" + utils.substituteStr(c.family."${basearch}", [STREAM: stream])
extraArgs += "--deprecated"
}
// Apply image description if provided
if (c.description) {
def description = utils.substituteStr(c.description, [
BUILDID: buildID,
STREAM: stream,
BASEARCH: basearch,
DATE: shwrapCapture("date +%Y-%m-%d")])
extraArgs += "--description=\"${description}\""
}
// Apply specified licenses to the created image
if (c.licenses) {
for (license in c.licenses) {
extraArgs += "--license=" + utils.substituteStr(license, [STREAM: stream])
}
}
// Mark the image as public if requested
if (c.public) {
extraArgs += "--public"
}
shwrap("""
# pick up the project to use from the config
gcp_project=\$(jq -r .project_id \${GCP_IMAGE_UPLOAD_CONFIG})
cosa ${image_upload_cmd}-gcp \
--log-level=INFO \
--build=${buildID} \
--arch=${basearch} \
--upload \
--project=\${gcp_project} \
--bucket gs://${c.bucket} \
--json \${GCP_IMAGE_UPLOAD_CONFIG} \
${extraArgs.join(' ')}
""")
}
}
}
credentials = [file(variable: "POWERVS_IMAGE_UPLOAD_CONFIG",
credentialsId: "powervs-image-upload-config")]
if (pipecfg.clouds?.powervs &&
artifacts.contains("powervs") &&
utils.credentialsExist(credentials)) {
def creds = credentials
uploaders["☁️ ⬆️ :powervs"] = {
withCredentials(creds) {
utils.syncCredentialsIfInRemoteSession(["POWERVS_IMAGE_UPLOAD_CONFIG"])
def c = pipecfg.clouds.powervs
// for powervs in RHCOS the images are uploaded to a bucket in each
// region that is uniquely named with the region as a suffix
// i.e. `rhcos-powervs-images-us-east`
def bucket = "${c.bucket}-${c.primary_region}"
shwrap("""cosa ${image_upload_cmd}-powervs \
--upload \
--cloud-object-storage ${c.cloud_object_storage_service_instance} \
--bucket ${bucket} \
--region ${c.primary_region} \
--credentials-file \${POWERVS_IMAGE_UPLOAD_CONFIG} \
--build ${buildID}
""");
}
}
}
// Run the resulting set of uploaders in parallel
// It shouldn't take more than 45 minutes.
timeout(time: 45, unit: 'MINUTES') {
parallel uploaders
}
}
return this