下面列出了com.amazonaws.services.s3.model.SSEAwsKeyManagementParams#hudson.remoting.VirtualChannel 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@Override
public Report invoke(final File workspace, final VirtualChannel channel) {
Report report = new Report();
report.logInfo("Searching for all files in '%s' that match the pattern '%s'",
workspace.getAbsolutePath(), filePattern);
String[] fileNames = new FileFinder(filePattern, StringUtils.EMPTY, followSymbolicLinks).find(workspace);
if (fileNames.length == 0) {
report.logError("No files found for pattern '%s'. Configuration error?", filePattern);
}
else {
report.logInfo("-> found %s", plural(fileNames.length, "file"));
scanFiles(workspace, fileNames, report);
}
return report;
}
@Override
public Integer invoke(File dir, VirtualChannel channel) throws IOException, InterruptedException {
String canonicalZip = zipFile.getRemote();
if (overwrite && !Files.deleteIfExists(Paths.get(canonicalZip))) {
throw new IOException("Failed to delete " + canonicalZip);
}
Archiver archiver = ArchiverFactory.ZIP.create(zipFile.write());
FileSet fs = Util.createFileSet(dir, glob);
DirectoryScanner scanner = fs.getDirectoryScanner(new org.apache.tools.ant.Project());
try {
for (String path : scanner.getIncludedFiles()) {
File toArchive = new File(dir, path).getCanonicalFile();
if (!toArchive.getPath().equals(canonicalZip)) {
archiver.visit(toArchive, path);
}
}
} finally {
archiver.close();
}
return archiver.countEntries();
}
/** @see FilePath#write() */
private static OutputStream append(FilePath fp, OutputStream stream) throws IOException, InterruptedException {
if (stream == null) {
return fp.act(new MasterToSlaveFileCallable<OutputStream>() {
private static final long serialVersionUID = 1L;
@Override
public OutputStream invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
f = f.getAbsoluteFile();
if (!f.getParentFile().exists() && !f.getParentFile().mkdirs()) {
throw new IOException("Failed to create directory " + f.getParentFile());
}
try {
return new RemoteOutputStream(Files.newOutputStream(f.toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND/*, StandardOpenOption.DSYNC*/));
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
});
}
return stream;
}
public File[] invoke(File f, VirtualChannel channel) {
File[] listFiles = {};
Path pathAuthenticationScriptsDir = Paths.get(zapDefaultDir, NAME_SCRIPTS_DIR_ZAP, NAME_AUTHENTICATION_SCRIPTS_DIR_ZAP);
if(Files.isDirectory(pathAuthenticationScriptsDir)) {
File zapAuthenticationScriptsDir = pathAuthenticationScriptsDir.toFile() ;
// create new filename filter (the filter returns true as all the extensions are accepted)
FilenameFilter policyFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
return true;
}
};
// returns pathnames for files and directory
listFiles = zapAuthenticationScriptsDir.listFiles(policyFilter);
}
return listFiles;
}
public List<String> invoke(File f, VirtualChannel channel) throws IOException {
PrintStream llog = taskListener.getLogger();
llog.println("Creating connection to docker daemon...");
try (DockerClient client = connector.getClient()) {
BuildImageCmd buildImageCmd = client.buildImageCmd();
buildImage.fillSettings(buildImageCmd);
llog.println("Pulling image ");
String imageId = buildImageCmd.exec(new MyBuildImageResultCallback(llog))
.awaitImageId();
llog.println(imageId);
llog.println("Image tagging during build isn't support atm, no tags applied.");
} catch (Exception ex) {
LOG.error("Can't get client", ex);
throw new IOException("Can't get docker client", ex);
}
return null;
}
/**
* Makes the key materials available locally and returns {@link KeyMaterialFactory} that gives you the parameters
* needed to access it.
*
* @deprecated Call {@link #newKeyMaterialFactory(Run, VirtualChannel)}
*/
@Deprecated
public KeyMaterialFactory newKeyMaterialFactory(@Nonnull Item context, @Nonnull VirtualChannel target) throws IOException, InterruptedException {
// as a build step, your access to credentials are constrained by what the build
// can access, hence Jenkins.getAuthentication()
DockerServerCredentials creds=null;
if (credentialsId!=null) {
List<DomainRequirement> domainRequirements = URIRequirementBuilder.fromUri(getUri()).build();
domainRequirements.add(new DockerServerDomainRequirement());
creds = CredentialsMatchers.firstOrNull(
CredentialsProvider.lookupCredentials(
DockerServerCredentials.class, context, Jenkins.getAuthentication(),
domainRequirements),
CredentialsMatchers.withId(credentialsId)
);
}
// the directory needs to be outside workspace to avoid prying eyes
FilePath dotDocker = dotDocker(target);
dotDocker.mkdirs();
// ServerKeyMaterialFactory.materialize creates a random subdir if one is needed:
return newKeyMaterialFactory(dotDocker, creds);
}
/**
* Makes the key materials available locally and returns {@link KeyMaterialFactory} that gives you the parameters
* needed to access it.
*/
public KeyMaterialFactory newKeyMaterialFactory(@Nonnull Run context, @Nonnull VirtualChannel target) throws IOException, InterruptedException {
DockerServerCredentials creds=null;
if (credentialsId!=null) {
List<DomainRequirement> domainRequirements = URIRequirementBuilder.fromUri(getUri()).build();
domainRequirements.add(new DockerServerDomainRequirement());
creds = CredentialsProvider.findCredentialById(credentialsId, DockerServerCredentials.class, context,
domainRequirements);
}
// the directory needs to be outside workspace to avoid prying eyes
FilePath dotDocker = dotDocker(target);
dotDocker.mkdirs();
// ServerKeyMaterialFactory.materialize creates a random subdir if one is needed:
return newKeyMaterialFactory(dotDocker, creds);
}
public TestResult invoke(File ws, VirtualChannel channel) throws IOException {
final long nowSlave = System.currentTimeMillis();
FileSet fs = Util.createFileSet(ws, testResults);
DirectoryScanner ds = fs.getDirectoryScanner();
TestResult result = null;
String[] files = ds.getIncludedFiles();
if (files.length > 0) {
result = new TestResult(buildTime + (nowSlave - nowMaster), ds, keepLongStdio, pipelineTestDetails);
result.tally();
} else {
if (this.allowEmptyResults) {
result = new TestResult();
} else {
// no test result. Most likely a configuration
// error or fatal problem
throw new AbortException(Messages.JUnitResultArchiver_NoTestReportFound());
}
}
return result;
}
/**
* Construct a FlakyTestResultAction object with Run and BuildListener
*
* @param build this build
* @param listener listener of this build
*/
public FlakyTestResultAction(AbstractBuild build, Launcher launcher, TaskListener listener) throws IOException, InterruptedException {
this.build = build;
// TODO consider the possibility that there is >1 such action
AbstractTestResultAction action = build.getAction(AbstractTestResultAction.class);
if (action != null) {
Object latestResult = action.getResult();
if (latestResult != null && latestResult instanceof TestResult) {
VirtualChannel channel = launcher.getChannel();
if(channel == null) {
throw new InterruptedException("Could not get channel to run a program remotely.");
}
FlakyTestResult flakyTestResult = channel.call(new FlakyTestResultCollector((TestResult) latestResult));
flakyTestResult.freeze(action, build);
FlakyRunStats stats = new FlakyRunStats(flakyTestResult.getTestFlakyStatsMap());
setFlakyRunStats(stats, listener);
}
} else {
logger.log(Level.WARNING, "No test result found, please publish junit report first");
}
}
public boolean perform() throws Exception {
FilePath rootFileObject = new FilePath(this.workspace, config.getRootObject());
final DeployerContext
deployerContext = new DeployerContext(config, rootFileObject, listener);
final VirtualChannel channel = launcher.getChannel();
if (null == channel)
throw new IllegalStateException("Null Channel (?)");
final Future<Boolean>
booleanFuture =
channel.callAsync(new SlaveDeployerCallable(deployerContext));
return booleanFuture.get();
}
public GitClient invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
if (listener == null) listener = TaskListener.NULL;
if (env == null) env = new EnvVars();
if (Main.isUnitTest && System.getProperty(Git.class.getName() + ".mockClient") != null) {
return initMockClient(System.getProperty(Git.class.getName() + ".mockClient"),
exe, env, f, listener);
}
if (exe == null || JGitTool.MAGIC_EXENAME.equalsIgnoreCase(exe)) {
return new JGitAPIImpl(f, listener);
}
if (JGitApacheTool.MAGIC_EXENAME.equalsIgnoreCase(exe)) {
final PreemptiveAuthHttpClientConnectionFactory factory = new PreemptiveAuthHttpClientConnectionFactory();
return new JGitAPIImpl(f, listener, factory);
}
// Ensure we return a backward compatible GitAPI, even API only claim to provide a GitClient
return new GitAPI(exe, f, listener, env);
}
@Test
public void test_clone_refspecs() throws Exception {
List<RefSpec> refspecs = Arrays.asList(
new RefSpec("+refs/heads/master:refs/remotes/origin/master"),
new RefSpec("+refs/heads/1.4.x:refs/remotes/origin/1.4.x")
);
testGitClient.clone_().url(workspace.localMirror()).refspecs(refspecs).repositoryName("origin").execute();
testGitClient.withRepository((Repository workRepo, VirtualChannel channel) -> {
String[] fetchRefSpecs = workRepo.getConfig().getStringList(ConfigConstants.CONFIG_REMOTE_SECTION, Constants.DEFAULT_REMOTE_NAME, "fetch");
assertThat(fetchRefSpecs.length, is(2));
assertThat(fetchRefSpecs[0], is("+refs/heads/master:refs/remotes/origin/master"));
assertThat(fetchRefSpecs[1], is("+refs/heads/1.4.x:refs/remotes/origin/1.4.x"));
return null;
});
Set<Branch> remoteBranches = testGitClient.getRemoteBranches();
assertBranchesExist(remoteBranches, "origin/master");
assertBranchesExist(remoteBranches, "origin/1.4.x");
assertThat(remoteBranches.size(), is(2));
}
@Override
public Report invoke(final File workspace, final VirtualChannel channel) {
Report report = new Report();
report.logInfo("Searching for files in workspace '%s' that match the include pattern '%s' and exclude pattern '%s'",
workspace, includePattern, excludePattern);
FileFinder fileFinder = new FileFinder(includePattern, excludePattern);
String[] fileNames = fileFinder.find(workspace);
report.logInfo("-> found %d files that will be scanned", fileNames.length);
Path root = workspace.toPath();
TaskScanner scanner = createTaskScanner();
report.logInfo(scanner.getTaskTags());
report.logInfo("Scanning all %d files for open tasks", fileNames.length);
for (String fileName : fileNames) {
report.addAll(scanner.scan(root.resolve(fileName), getCharset()));
if (Thread.interrupted()) {
throw new ParsingCanceledException();
}
}
report.logInfo("Found a total of %d open tasks", report.size());
Map<String, Integer> countPerType = report.getPropertyCount(Issue::getType);
for (Entry<String, Integer> entry : countPerType.entrySet()) {
report.logInfo("-> %s: %d open tasks", entry.getKey(), entry.getValue());
}
return report;
}
@Override
public AnnotatedReport invoke(final File workspace, final VirtualChannel channel) throws InterruptedException {
resolvePaths(originalReport);
resolveModuleNames(originalReport, workspace);
resolvePackageNames(originalReport);
Report filtered = filter(originalReport, filters, id);
createFingerprints(filtered);
FileLocations fileLocations = new ReportLocations().toFileLocations(filtered);
return new AnnotatedReport(id, filtered,
blame(filtered, fileLocations),
mineRepository(filtered, fileLocations));
}
/**
* Returns a {@link VirtualChannel} to the agent where this step has been executed.
*
* @return the channel
* @throws IOException
* if the computer could be be resolved
* @throws InterruptedException
* if the user canceled the run
*/
protected Optional<VirtualChannel> getChannel() throws IOException, InterruptedException {
Computer computer = getContext().get(Computer.class);
if (computer == null) {
return Optional.empty();
}
return Optional.ofNullable(computer.getChannel());
}
protected VirtualChannel getChannel() {
final VirtualChannel channel = getLauncher().getChannel();
if (channel == null) {
throw new IllegalArgumentException(
"Unable to get the channel, Perhaps you forgot to surround the code with a step that provides this, such as: node, dockerNode");
}
return channel;
}
/**
* Upload from slave
*/
@Override
public Integer invoke(final TransferManager transferManager, File base, VirtualChannel channel) throws IOException, InterruptedException {
if(!base.exists()) return 0;
final AtomicInteger count = new AtomicInteger(0);
final Uploads uploads = new Uploads();
final Map<String, S3ObjectSummary> summaries = lookupExistingCacheEntries(transferManager.getAmazonS3Client());
// Find files to upload that match scan
scanner.scan(base, new FileVisitor() {
@Override
public void visit(File f, String relativePath) throws IOException {
if (f.isFile()) {
String key = pathPrefix + "/" + relativePath;
S3ObjectSummary summary = summaries.get(key);
if (summary == null || f.lastModified() > summary.getLastModified().getTime()) {
final ObjectMetadata metadata = buildMetadata(f);
uploads.startUploading(transferManager, f, IOUtils.toBufferedInputStream(FileUtils.openInputStream(f)), new Destination(bucketName, key), metadata);
if (uploads.count() > 20) {
waitForUploads(count, uploads);
}
}
}
}
});
// Wait for each file to complete before returning
waitForUploads(count, uploads);
return uploads.count();
}
/**
* Override this if you don't want a transfer manager
*/
@Override
public T invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
TransferManager transferManager = new TransferManager(helper.client());
try {
return invoke(transferManager, f, channel);
} finally {
transferManager.shutdownNow();
}
}
/**
* Download to executor
*/
@Override
public Integer invoke(TransferManager transferManager, File base, VirtualChannel channel) throws IOException, InterruptedException {
if(!base.exists()) {
if (!base.mkdirs()) {
throw new IOException("Failed to create directory : " + base);
}
}
int totalCount;
Downloads downloads = new Downloads();
ObjectListing objectListing = null;
do {
objectListing = transferManager.getAmazonS3Client().listObjects(new ListObjectsRequest()
.withBucketName(bucketName)
.withPrefix(pathPrefix)
.withMarker(objectListing != null ? objectListing.getNextMarker() : null));
for (S3ObjectSummary summary : objectListing.getObjectSummaries()) {
downloads.startDownload(transferManager, base, pathPrefix, summary);
}
} while (objectListing.getNextMarker() != null);
// Grab # of files copied
totalCount = downloads.count();
// Finish the asynchronous downloading process
downloads.finishDownloading();
return totalCount;
}
@Override
public Void invoke(File localFile, VirtualChannel channel) throws IOException, InterruptedException {
TransferManager mgr = TransferManagerBuilder.standard()
.withS3Client(AWSClientFactory.create(this.amazonS3ClientOptions.createAmazonS3ClientBuilder(), this.envVars))
.build();
if (this.path == null || this.path.isEmpty() || this.path.endsWith("/")) {
try {
final MultipleFileDownload fileDownload = mgr.downloadDirectory(this.bucket, this.path, localFile);
fileDownload.waitForCompletion();
RemoteDownloader.this.taskListener.getLogger().println("Finished: " + fileDownload.getDescription());
}
finally {
mgr.shutdownNow();
}
return null;
} else {
try {
final Download download = mgr.download(this.bucket, this.path, localFile);
download.addProgressListener((ProgressListener) progressEvent -> {
if (progressEvent.getEventType() == ProgressEventType.TRANSFER_COMPLETED_EVENT) {
RemoteDownloader.this.taskListener.getLogger().println("Finished: " + download.getDescription());
}
});
download.waitForCompletion();
}
finally {
mgr.shutdownNow();
}
return null;
}
}
@Override
public Boolean invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
PrintStream logger = listener.getLogger();
try (ZipFile zip = new ZipFile(f)) {
logger.print("Checking ");
logger.print(zip.size());
logger.print(" zipped entries in ");
logger.println(f.getAbsolutePath());
Checksum checksum = new CRC32();
byte[] buffer = new byte[4096];
Enumeration<? extends ZipEntry> entries = zip.entries();
while (entries.hasMoreElements()) {
checksum.reset();
ZipEntry entry = entries.nextElement();
if (!entry.isDirectory()) {
try (InputStream inputStream = zip.getInputStream(entry)) {
int length;
while ((length = IOUtils.read(inputStream, buffer)) > 0) {
checksum.update(buffer, 0, length);
}
if (checksum.getValue() != entry.getCrc()) {
listener.error("Checksum error in : " + f.getAbsolutePath() + ":" + entry.getName());
return false;
}
}
}
}
return true;
} catch (ZipException e) {
listener.error("Error validating zip file: " + e.getMessage());
return false;
} finally {
logger.flush();
}
}
@Override
public String invoke(File file, VirtualChannel virtualChannel) throws IOException, InterruptedException {
if (file.exists() && file.isFile()) {
try {
return sha1(file);
} catch (NoSuchAlgorithmException e) {
throw new IOException(e.getMessage(), e);
}
} else {
return null;
}
}
@Override
public synchronized void onOnline(Computer c, TaskListener listener) throws IOException, InterruptedException {
VirtualChannel ch = c.getChannel();
if (ch instanceof Channel) {
channels.add((Channel)ch);
}
}
@Override
public Void invoke(final File workspace, final VirtualChannel channel) throws InterruptedException {
clearWorkspaceIfSelected(workspace, listener);
final AWSClients awsClients = awsClientFactory.getAwsClient(
model.getAwsAccessKey(),
model.getAwsSecretKey(),
model.getProxyHost(),
model.getProxyPort(),
model.getRegion(),
pluginUserAgentPrefix);
final AWSCodePipelineJobCredentialsProvider credentialsProvider = new AWSCodePipelineJobCredentialsProvider(
job.getId(), awsClients.getCodePipelineClient());
final AmazonS3 s3Client = awsClients.getS3Client(credentialsProvider);
for (final Artifact artifact : job.getData().getInputArtifacts()) {
final S3Object sessionObject = getS3Object(s3Client, artifact);
model.setCompressionType(ExtractionTools.getCompressionType(sessionObject, listener));
final String downloadedFileName = Paths.get(sessionObject.getKey()).getFileName().toString();
try {
downloadAndExtract(sessionObject, workspace, downloadedFileName, listener);
} catch (final Exception ex) {
final String error = "Failed to acquire artifacts: " + ex.getMessage();
LoggingHelper.log(listener, error);
LoggingHelper.log(listener, ex);
throw new InterruptedException(error);
}
}
return null;
}
public File[] invoke(File f, VirtualChannel channel) {
File[] listFiles = {};
Path pathPolicyDir = Paths.get(zapDefaultDir, NAME_POLICIES_DIR_ZAP);
if(Files.isDirectory(pathPolicyDir)) {
File zapPolicyDir = new File(zapDefaultDir, NAME_POLICIES_DIR_ZAP);
// create new filename filter (get only file with FILE_POLICY_EXTENSION extension)
FilenameFilter policyFilter = new FilenameFilter() {
@Override
public boolean accept(File dir, String name) {
if (name.lastIndexOf('.') > 0) {
// get last index for '.' char
int lastIndex = name.lastIndexOf('.');
// get extension
String str = name.substring(lastIndex);
// match path name extension
if (str.equals(FILE_POLICY_EXTENSION)) {
return true;
}
}
return false;
}
};
// returns pathnames for files and directory
listFiles = zapPolicyDir.listFiles(policyFilter);
}
return listFiles;
}
public String invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
File fileCopiedDir = new File(zapDefaultDir, ZAProxy.NAME_POLICIES_DIR_ZAP);
File fileCopied = new File(fileCopiedDir, copyFilename);
FileUtils.writeStringToFile(fileCopied, data);
stringForLogger += "[" + fileCopied.getAbsolutePath() + "]";
return stringForLogger;
}
public DockerImageComboStepResponse invoke(File f, VirtualChannel channel) throws IOException {
PrintStream llog = taskListener.getLogger();
llog.println("Creating connection to docker daemon...");
try (DockerClient client = connector.getClient()) {
return invoke(client);
} catch (Exception ex) {
Throwables.propagate(ex);
}
return null;
}
@Override
public Void invoke(File f, VirtualChannel channel) throws IOException, InterruptedException {
PrintStream llog = taskListener.getLogger();
llog.println("Creating connection to docker daemon...");
try (DockerClient client = connector.getClient()) {
return invoke(client);
} catch (Exception ex) {
Throwables.propagate(ex);
}
return null;
}
/**
* @deprecated Call {@link #newKeyMaterialFactory(Run, FilePath, Launcher, EnvVars, TaskListener, String)}
*/
@Deprecated
public KeyMaterialFactory newKeyMaterialFactory(@CheckForNull Item context, @Nonnull VirtualChannel target, @CheckForNull Launcher launcher, @Nonnull TaskListener listener) throws IOException, InterruptedException {
if (credentialsId == null) {
return KeyMaterialFactory.NULL; // nothing needed to be done
}
DockerRegistryToken token = getToken(context);
if (token == null) {
throw new AbortException("Could not find credentials matching " + credentialsId);
}
return token.newKeyMaterialFactory(getEffectiveUrl(), target, launcher, listener);
}
@Before
public void setup() throws Exception {
// fake launcher for the docker login invocation
FakeLauncher faker = new FakeLauncher() {
@Override
public Proc onLaunch(final ProcStarter p) throws IOException {
return new FinishedProc(0);
}
};
PretendSlave slave = j.createPretendSlave(faker);
// VirtualChannel channel = slave.getChannel();
// FreeStyleProject project = j.createFreeStyleProject();
TaskListener listener = TaskListener.NULL;
Launcher launcher = slave.createLauncher(listener);
launcher = new Launcher.DecoratedLauncher(launcher) {
@Override
public VirtualChannel getChannel() {
return new LocalChannel(null) {
@Override
public <V, T extends Throwable> V call(final Callable<V, T> callable) throws T {
// ugly as hell, but we need a way to mock fetching the home directory
return (V) new FilePath(tempFolder.getRoot());
}
};
}
};
URL endpoint = new DockerRegistryEndpoint(null, null).getEffectiveUrl();
EnvVars env = new EnvVars();
String dockerExecutable = DockerTool.getExecutable(null, null, listener, env);
factory = new RegistryKeyMaterialFactory("username", "password", endpoint, launcher, env, listener,
dockerExecutable).contextualize(new KeyMaterialContext(new FilePath(tempFolder.newFolder())));
}