-
Notifications
You must be signed in to change notification settings - Fork 57
/
DownloadStep.java
519 lines (452 loc) · 17.7 KB
/
DownloadStep.java
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
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
/*
* Copyright 2017 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.google.jenkins.plugins.storage;
import static com.google.common.base.Preconditions.checkNotNull;
import com.google.api.client.googleapis.media.MediaHttpDownloader;
import com.google.api.services.storage.Storage;
import com.google.api.services.storage.model.Objects;
import com.google.api.services.storage.model.StorageObject;
import com.google.jenkins.plugins.credentials.domains.RequiresDomain;
import com.google.jenkins.plugins.credentials.oauth.GoogleRobotCredentials;
import com.google.jenkins.plugins.storage.util.BucketPath;
import com.google.jenkins.plugins.storage.util.RetryStorageOperation;
import com.google.jenkins.plugins.storage.util.RetryStorageOperation.Operation;
import com.google.jenkins.plugins.storage.util.RetryStorageOperation.RepeatOperation;
import com.google.jenkins.plugins.storage.util.StorageUtil;
import com.google.jenkins.plugins.util.Executor;
import com.google.jenkins.plugins.util.ExecutorException;
import hudson.AbortException;
import hudson.Extension;
import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractProject;
import hudson.model.Run;
import hudson.model.TaskListener;
import hudson.remoting.Callable;
import hudson.tasks.BuildStepDescriptor;
import hudson.tasks.BuildStepMonitor;
import hudson.tasks.Builder;
import hudson.util.FormValidation;
import java.io.IOException;
import java.io.InputStream;
import java.io.Serializable;
import java.security.GeneralSecurityException;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import java.util.logging.Logger;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import jenkins.model.Jenkins;
import jenkins.tasks.SimpleBuildStep;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.jenkinsci.Symbol;
import org.jenkinsci.remoting.RoleChecker;
import org.kohsuke.stapler.DataBoundConstructor;
import org.kohsuke.stapler.DataBoundSetter;
import org.kohsuke.stapler.QueryParameter;
import org.kohsuke.stapler.StaplerRequest;
/** A step to allow Download from Google Cloud Storage as a Build step and in pipeline. */
@RequiresDomain(value = StorageScopeRequirement.class)
public class DownloadStep extends Builder implements SimpleBuildStep, Serializable {
private static final long serialVersionUID = 1;
private static final Logger logger = Logger.getLogger(DownloadStep.class.getName());
private final String credentialsId;
private final String bucketUri;
private final String localDirectory;
private String pathPrefix;
/** The module to use for providing dependencies. */
private final transient UploadModule module;
/**
* DataBoundConstructor for DownloadStep.
*
* @param credentialsId The unique ID for the credentials we are using to authenticate with GCS.
* @param bucketUri Name of the GCS bucket. e.g. gs://MY_BUCKET_NAME
* @param localDirectory Path of the local directory in Jenkins to download the file to.
*/
@DataBoundConstructor
public DownloadStep(String credentialsId, String bucketUri, String localDirectory) {
this(credentialsId, bucketUri, localDirectory, null);
}
/**
* Constructor for DownloadStep.
*
* @param credentialsId The unique ID for the credentials we are using to authenticate with GCS.
* @param bucketUri Name of the GCS bucket. e.g. gs://MY_BUCKET_NAME
* @param localDirectory Path of the local directory in Jenkins to download the file to.
* @param module An {@link UploadModule} to use for execution.
*/
public DownloadStep(
String credentialsId,
String bucketUri,
String localDirectory,
@Nullable UploadModule module) {
if (module != null) {
this.module = module;
} else {
this.module = new UploadModule();
}
this.bucketUri = bucketUri;
this.credentialsId = credentialsId;
this.localDirectory = localDirectory;
}
/**
* @return The bucket uri specified by the user, which potentially contains unresolved symbols,
* such as $JOB_NAME and $BUILD_NUMBER.
*/
public String getBucketUri() {
return bucketUri;
}
/**
* @return The local directory in the Jenkins workspace that will receive the files. This might
* contain unresolved symbols, such as $JOB_NAME and $BUILD_NUMBER.
*/
public String getLocalDirectory() {
return localDirectory;
}
/**
* @param pathPrefix The path prefix that will be stripped from downloaded files. May be null if
* no path prefix needs to be stripped.
* <p>Filenames that do not start with this prefix will not be modified. Trailing slash is
* automatically added if it is missing.
*/
@DataBoundSetter
public void setPathPrefix(@Nullable String pathPrefix) {
if (pathPrefix != null && !pathPrefix.endsWith("/")) {
pathPrefix += "/";
}
this.pathPrefix = pathPrefix;
}
/**
* @return The path prefix that will be stripped from downloaded files. May be null if no path
* prefix needs to be stripped.
* <p>Filenames that do not start with this prefix will not be modified. Trailing slash is
* automatically added if it is missing.
*/
@Nullable
public String getPathPrefix() {
return pathPrefix;
}
/** @return The UploadModule used for providing dependencies. */
protected synchronized UploadModule getModule() {
if (this.module == null) {
return new UploadModule();
}
return this.module;
}
/** @return The unique ID for the credentials we are using to authenticate with GCS. */
public String getCredentialsId() {
return credentialsId;
}
/** @return The credentials we are using to authenticate with GCS. */
public GoogleRobotCredentials getCredentials() {
return GoogleRobotCredentials.getById(getCredentialsId());
}
/** {@inheritDoc} */
@Override
public BuildStepMonitor getRequiredMonitorService() {
return BuildStepMonitor.NONE;
}
/**
* The main entry point of this extension. Downloads the resolved GCS objects from the resolved
* GCS bucket to a local directory.
*
* @param run Current job being run.
* @param workspace Workspace of node running the job.
* @param launcher {@link Launcher} for this job.
* @param listener Listener for events of this job.
* @throws IOException If there was an issue parsing the bucket URI.
* @throws InterruptedException If there was an issue initiating downloads at workspace or
* expanding variables in the pathPrefix.
*/
@Override
public void perform(
@Nonnull Run<?, ?> run,
@Nonnull FilePath workspace,
@Nonnull Launcher launcher,
@Nonnull TaskListener listener)
throws IOException, InterruptedException {
try {
String version = getModule().getVersion();
String path = StorageUtil.replaceMacro(getBucketUri(), run, listener);
BucketPath bucketPath = new BucketPath(path);
if (bucketPath.error()) {
throw new IOException("Invalid bucket path: " + getBucketUri());
}
String dirName = StorageUtil.replaceMacro(getLocalDirectory(), run, listener);
FilePath dirPath = workspace.child(dirName);
List<StorageObjectId> objects = resolveBucketPath(bucketPath, getCredentials(), version);
listener
.getLogger()
.println(getModule().prefix(Messages.Download_FoundForPattern(objects.size(), path)));
// TODO(agoulti): add a download report.
String resolvedPrefix = StorageUtil.replaceMacro(pathPrefix, run, listener);
initiateDownloadsAtWorkspace(
getCredentials(), run, objects, dirPath, listener, version, resolvedPrefix);
} catch (ExecutorException e) {
throw new IOException(Messages.Download_DownloadException(), e);
}
}
private void performDownloadWithRetry(
final Executor executor,
final Storage service,
final StorageObjectId obj,
final FilePath localName,
final UploadModule module,
final TaskListener listener)
throws IOException, InterruptedException, ExecutorException {
Operation a =
new Operation() {
public void act() throws IOException, InterruptedException, ExecutorException {
listener
.getLogger()
.println(module.prefix(Messages.Download_Downloading(obj.getName(), localName)));
Storage.Objects.Get getObject = service.objects().get(obj.getBucket(), obj.getName());
MediaHttpDownloader downloader = getObject.getMediaHttpDownloader();
if (downloader != null) {
downloader.setDirectDownloadEnabled(true);
}
InputStream is = module.executeMediaAsInputStream(getObject);
localName.copyFrom(is);
}
};
RetryStorageOperation.performRequestWithRetry(executor, a, module.getInsertRetryCount());
}
private void performDownloads(
final GoogleRobotCredentials credentials,
final FilePath localDir,
final List<StorageObjectId> objs,
final TaskListener listener,
final String version,
final String resolvedPrefix)
throws IOException {
RepeatOperation<IOException> a =
new RepeatOperation<IOException>() {
private Queue<StorageObjectId> objects = new LinkedList<StorageObjectId>(objs);
Executor executor = getModule().newExecutor();
Storage service;
public void initCredentials() throws IOException {
service = getModule().getStorageService(credentials, version);
}
public boolean moreWork() {
return !objects.isEmpty();
}
public void act() throws IOException, InterruptedException, ExecutorException {
StorageObjectId obj = objects.peek();
String addPath = StorageUtil.getStrippedFilename(obj.getName(), resolvedPrefix);
FilePath localName = localDir.withSuffix("/" + addPath);
performDownloadWithRetry(executor, service, obj, localName, getModule(), listener);
objects.remove();
}
};
try {
RetryStorageOperation.performRequestWithReinitCredentials(a);
} catch (ExecutorException e) {
throw new IOException(Messages.Download_DownloadException(), e);
} catch (InterruptedException e) {
throw new IOException(Messages.Download_DownloadException(), e);
}
}
private void initiateDownloadsAtWorkspace(
final GoogleRobotCredentials credentials,
final Run run,
final List<StorageObjectId> objects,
final FilePath localDir,
final TaskListener listener,
final String version,
final String resolvedPrefix)
throws IOException, InterruptedException {
try {
// Use remotable credential to access the storage service from the
// remote machine.
final GoogleRobotCredentials remoteCredentials =
checkNotNull(credentials).forRemote(getModule().getRequirement());
localDir.act(
new Callable<Void, IOException>() {
@Override
public Void call() throws IOException {
performDownloads(
remoteCredentials, localDir, objects, listener, version, resolvedPrefix);
return (Void) null;
}
@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
// We know by definition that this is the correct role;
// the callable exists only in this method context.
}
});
} catch (GeneralSecurityException e) {
throw new IOException(Messages.AbstractUpload_RemoteCredentialError(), e);
}
}
/** A class to store StorageObject information in a serializable manner. */
protected static class StorageObjectId implements Serializable {
public StorageObjectId(StorageObject obj) {
this.bucket = obj.getBucket();
this.name = obj.getName();
}
public String getBucket() {
return bucket;
}
public String getName() {
return name;
}
private final String bucket;
private final String name;
}
/**
* Split the string on wildcards ("*").
*
* <p>String.split removes trailing empty strings, for example, "a", "a*" and "a**" and would
* produce the same result, so that method is not suitable.
*
* @param uri URI supplied to be split.
* @return URI split by "*" wildcard.
* @throws AbortException If there is more than one wild card character in the provided string.
*/
public static String[] split(String uri) throws AbortException {
int occurs = StringUtils.countMatches(uri, "*");
if (occurs == 0) {
return new String[] {uri};
}
if (occurs > 1) {
throw new AbortException(Messages.Download_UnsupportedMultipleAsterisks(uri));
}
int index = uri.indexOf('*');
return new String[] {uri.substring(0, index), uri.substring(index + 1)};
}
/** Verifies that the given path is supported within current limitations */
private static void verifySupported(BucketPath path) throws AbortException {
if (path.getBucket().contains("*")) {
throw new AbortException(Messages.Download_UnsupportedAsteriskInBucket(path.getBucket()));
}
String[] pieces = split(path.getObject());
if (pieces.length == 2) {
if (pieces[1].contains("/")) {
throw new AbortException(Messages.Download_UnsupportedDirSuffix(path.getObject()));
}
}
}
/**
* Take the bucket path and return a list of objects in the cloud that match it.
*
* <p>This will eventually handle wildcards, but for now is limited to directly specifying the
* object.
*/
private List<StorageObjectId> resolveBucketPath(
BucketPath bucketPath, GoogleRobotCredentials credentials, String version)
throws IOException, ExecutorException {
Storage service = getModule().getStorageService(credentials, version);
Executor executor = getModule().newExecutor();
List<StorageObjectId> result = new ArrayList<StorageObjectId>();
verifySupported(bucketPath);
// Allow a single asterisk in the object name for now.
// Let the behavior be consistent with
// https://cloud.google.com/storage/docs/gsutil/addlhelp/WildcardNames
//
// Support for richer constructs will be added as needed.
String[] pieces = split(bucketPath.getObject());
if (pieces.length == 1) {
// No wildcards. Do simple lookup
Storage.Objects.Get obj =
service.objects().get(bucketPath.getBucket(), bucketPath.getObject());
result.add(new StorageObjectId(executor.execute(obj)));
return result;
}
// Single wildcard, of the form pre/fix/log_*_some.txt
String bucketPathPrefix = pieces[0];
String bucketPathSuffix = pieces[1];
String pageToken = "";
do {
Storage.Objects.List list =
service
.objects()
.list(bucketPath.getBucket())
.setPrefix(bucketPathPrefix)
.setDelimiter("/");
if (pageToken.length() > 0) {
list.setPageToken(pageToken);
}
Objects objects = executor.execute(list);
pageToken = objects.getNextPageToken();
// Collect the items that match the suffix
for (StorageObject o : objects.getItems()) {
if (o.getName().endsWith(bucketPathSuffix)) {
result.add(new StorageObjectId(o));
}
}
} while (pageToken != null && pageToken.length() > 0);
return result;
}
/** {@inheritDoc} */
public DescriptorImpl getDescriptor() {
return (DescriptorImpl) checkNotNull(Jenkins.get()).getDescriptor(getClass());
}
/** Descriptor for the DownloadStep */
@Extension
@Symbol("googleStorageDownload")
public static class DescriptorImpl extends BuildStepDescriptor<Builder> {
private UploadModule module;
/** @return Module for the DescriptorImpl. */
public synchronized UploadModule getModule() {
if (this.module == null) {
return new UploadModule();
}
return this.module;
}
/** Constructor for {@link DownloadStep}'s DescriptorImpl. */
public DescriptorImpl() {
this.module = new UploadModule();
}
/** {@inheritDoc} */
@Override
public String getDisplayName() {
return Messages.Download_BuildStepDisplayName();
}
/** {@inheritDoc} */
@Override
public boolean isApplicable(Class<? extends AbstractProject> jobType) {
return true;
}
/** {@inheritDoc} */
@Override
public Builder newInstance(StaplerRequest req, JSONObject formData) throws FormException {
if (Boolean.FALSE.equals(formData.remove("stripPathPrefix"))) {
formData.remove("pathPrefix");
}
return super.newInstance(req, formData);
}
/**
* This callback validates the {@code bucketNameWithVars} input field's values.
*
* @param bucketUri Name of the GCS bucket. e.g. gs://MY_BUCKET_NAME
* @return Valid form validation result or error message if invalid.
*/
public FormValidation doCheckBucketUri(@QueryParameter final String bucketUri) {
try {
BucketPath path = new BucketPath(bucketUri);
verifySupported(path);
} catch (AbortException e) {
return FormValidation.error(e.getMessage());
} catch (IllegalArgumentException e) {
return FormValidation.error(e.getMessage());
}
return ClassicUpload.DescriptorImpl.staticDoCheckBucket(bucketUri);
}
}
}