Skip to content

Commit

Permalink
Remove need for A2C callables
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick authored and daniel-beck committed Nov 3, 2021
1 parent 88c16cc commit 8d962b6
Show file tree
Hide file tree
Showing 7 changed files with 124 additions and 5 deletions.
36 changes: 32 additions & 4 deletions src/main/java/hudson/maven/MavenBuild.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
package hudson.maven;

import hudson.EnvVars;
import hudson.ExtensionList;
import hudson.FilePath;
import hudson.Launcher;
import hudson.Util;
Expand Down Expand Up @@ -90,9 +91,11 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.atomic.AtomicBoolean;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import javax.annotation.CheckForNull;

Expand Down Expand Up @@ -465,24 +468,44 @@ public final void executeAsync(BuildCallable<?,?> program) throws IOException {
throw new AssertionError();
}

private FilePath transferArea() {
FilePath ws = getWorkspace();
Channel mavenChannel = Channel.current();
if (mavenChannel != null) {
ws = new FilePath(mavenChannel, ws.getRemote());
}
return WorkspaceList.tempDir(ws).child("maven-reporters");
}

private void copyFromTransferArea(FilePath transferAreaSubdir, File target, BuildListener listener, boolean restricted) throws IOException, InterruptedException {
if (transferAreaSubdir.isDirectory()) {
listener.getLogger().println("Copying " + transferAreaSubdir + " to " + target);
if (restricted) {
transferAreaSubdir.copyRecursiveTo(ExtensionList.lookup(MavenReporterDescriptor.class).stream().map(MavenReporterDescriptor::reportedFilePattern).filter(Objects::nonNull).collect(Collectors.joining(",")), new FilePath(target));
} else {
transferAreaSubdir.copyRecursiveTo(new FilePath(target));
}
}
}

public FilePath getRootDir() {
return new FilePath(MavenBuild.this.getRootDir());
return transferArea().child("build");
}

public FilePath getProjectRootDir() {
return new FilePath(MavenBuild.this.getParent().getRootDir());
return transferArea().child("project");
}

public FilePath getModuleSetRootDir() {
return new FilePath(MavenBuild.this.getParent().getParent().getRootDir());
return transferArea().child("moduleset");
}

/**
* @deprecated Does not work with {@link ArtifactManager}.
*/
@Deprecated
public FilePath getArtifactsDir() {
return new FilePath(MavenBuild.this.getArtifactsDir());
return transferArea().child("artifacts");
}

@Override public void queueArchiving(String artifactPath, String artifact) {
Expand Down Expand Up @@ -519,6 +542,11 @@ void performArchiving(Launcher launcher, BuildListener listener) throws IOExcept
am.archive(f.getParent(), launcher, listener, Collections.singletonMap(e.getKey(), f.getName()));
}

copyFromTransferArea(getRootDir(), MavenBuild.this.getRootDir(), listener, true);
copyFromTransferArea(getProjectRootDir(), MavenBuild.this.getParent().getRootDir(), listener, true);
copyFromTransferArea(getModuleSetRootDir(), MavenBuild.this.getParent().getParent().getRootDir(), listener, true);
copyFromTransferArea(getArtifactsDir(), MavenBuild.this.getArtifactsDir(), listener, false);

if (false) {
long duration = System.currentTimeMillis()-startTime;
listener.getLogger().println("[JENKINS] Archiving took "+ Util.getTimeSpanString(duration));
Expand Down
5 changes: 4 additions & 1 deletion src/main/java/hudson/maven/MavenBuildProxy.java
Original file line number Diff line number Diff line change
Expand Up @@ -80,17 +80,20 @@ public interface MavenBuildProxy {
/**
* Root directory of the build.
*
* @see MavenBuild#getRootDir()
* @see MavenBuild#getRootDir()
* @see MavenReporterDescriptor#reportedFilePattern
*/
FilePath getRootDir();

/**
* Root directory of the parent of this build.
* @see MavenReporterDescriptor#reportedFilePattern
*/
FilePath getProjectRootDir();

/**
* Root directory of the owner {@link MavenModuleSet}
* @see MavenReporterDescriptor#reportedFilePattern
*/
FilePath getModuleSetRootDir();

Expand Down
14 changes: 14 additions & 0 deletions src/main/java/hudson/maven/MavenReporterDescriptor.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
*/
package hudson.maven;

import edu.umd.cs.findbugs.annotations.CheckForNull;
import hudson.maven.reporters.MavenArtifactArchiver;
import hudson.model.Descriptor;
import hudson.model.Describable;
Expand Down Expand Up @@ -104,4 +105,17 @@ public static Collection<MavenReporterDescriptor> all() {
// use getDescriptorList and not getExtensionList to pick up legacy instances
return Jenkins.get().getDescriptorList(MavenReporter.class);
}

/**
* File patterns this reporter might create when running inside the Maven JVM.
* Applies to usages from {@link MavenReporter#postBuild} and similar callbacks
* of {@link MavenBuildProxy#getRootDir}, {@link MavenBuildProxy#getProjectRootDir}, and {@link MavenBuildProxy#getModuleSetRootDir}.
* (But not {@link MavenBuildProxy#getArtifactsDir}, which should not be used anyway.)
* @return an Ant-style file pattern of files we expect to copy, or null if not applicable (default)
*/
@CheckForNull
public String reportedFilePattern() {
return null;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public String getDisplayName() {
public MavenJavadocArchiver newAutoInstance(MavenModule module) {
return new MavenJavadocArchiver();
}

@Override
public String reportedFilePattern() {
return "javadoc/";
}

}

private static final long serialVersionUID = 1L;
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/hudson/maven/reporters/MavenSiteArchiver.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,12 @@ public String getDisplayName() {
public MavenSiteArchiver newAutoInstance(MavenModule module) {
return new MavenSiteArchiver();
}

@Override
public String reportedFilePattern() {
return "site/";
}

}

private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,12 @@ public String getDisplayName() {
public MavenTestJavadocArchiver newAutoInstance(MavenModule module) {
return new MavenTestJavadocArchiver();
}

@Override
public String reportedFilePattern() {
return "test-javadoc/";
}

}

private static final long serialVersionUID = 1L;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
/*
* The MIT License
*
* Copyright 2021 CloudBees, Inc.
*
* 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 hudson.maven.reporters;

import edu.umd.cs.findbugs.annotations.Nullable;
import hudson.Extension;
import hudson.FilePath;
import hudson.remoting.Callable;
import hudson.remoting.ChannelBuilder;
import jenkins.security.ChannelConfigurator;
import jenkins.util.JenkinsJVM;
import org.jenkinsci.remoting.CallableDecorator;

/**
* Prevents {@link FilePath#act} (or {@link FilePath#actAsync}) from running in a controller → agent direction during tests, to make sure we do not rely on them.
*/
public class BlockSlaveToMasterFileCallable extends CallableDecorator {

@Override public <V, T extends Throwable> Callable<V, T> userRequest(Callable<V, T> op, Callable<V, T> stem) {
if (op.getClass().getName().equals("hudson.FilePath$FileCallableWrapper") && JenkinsJVM.isJenkinsJVM()) {
throw new SecurityException("blocked");
}
return stem;
}

@Extension public static class ChannelConfiguratorImpl extends ChannelConfigurator {

@Override public void onChannelBuilding(ChannelBuilder builder, @Nullable Object context) {
builder.with(new BlockSlaveToMasterFileCallable());
}

}

}

0 comments on commit 8d962b6

Please sign in to comment.