Skip to content

Commit

Permalink
ControllerToAgentFileCallable also seems helpful
Browse files Browse the repository at this point in the history
  • Loading branch information
jglick committed Nov 1, 2024
1 parent d7af175 commit 9f3cd33
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 41 deletions.
27 changes: 3 additions & 24 deletions core/src/main/java/hudson/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
import jenkins.MasterToSlaveFileCallable;
import jenkins.SlaveToMasterFileCallable;
import jenkins.agents.ControllerToAgentFileCallable;
import jenkins.model.Jenkins;
import jenkins.security.MasterToSlaveCallable;
import jenkins.util.ContextResettingExecutorService;
Expand Down Expand Up @@ -520,30 +520,14 @@ public int archive(final ArchiverFactory factory, OutputStream os, final DirScan
return act(new Archive(factory, out, scanner, verificationRoot, openOptions));
}

private static class Archive extends MasterToSlaveFileCallable<Integer> {
private final ArchiverFactory factory;
private final OutputStream out;
private final DirScanner scanner;
private final String verificationRoot;
private OpenOption[] openOptions;

Archive(ArchiverFactory factory, OutputStream out, DirScanner scanner, String verificationRoot, OpenOption... openOptions) {
this.factory = factory;
this.out = out;
this.scanner = scanner;
this.verificationRoot = verificationRoot;
this.openOptions = openOptions;
}

private record Archive(ArchiverFactory factory, OutputStream out, DirScanner scanner, String verificationRoot, OpenOption... openOptions) implements ControllerToAgentFileCallable<Integer> {
@Override
public Integer invoke(File f, VirtualChannel channel) throws IOException {
try (Archiver a = factory.create(out)) {
scanner.scan(f, ignoringTmpDirs(ignoringSymlinks(a, verificationRoot, openOptions), verificationRoot, openOptions));
return a.countEntries();
}
}

private static final long serialVersionUID = 1L;
}

public int archive(final ArchiverFactory factory, OutputStream os, final FileFilter filter) throws IOException, InterruptedException {
Expand Down Expand Up @@ -1185,12 +1169,7 @@ public void copyFrom(org.apache.commons.fileupload.FileItem file) throws IOExcep
/**
* Code that gets executed on the machine where the {@link FilePath} is local.
* Used to act on {@link FilePath}.
* <strong>Warning:</strong> implementations must be serializable, so prefer a static nested class to an inner class.
*
* <p>
* Subtypes would likely want to extend from either {@link MasterToSlaveCallable}
* or {@link SlaveToMasterFileCallable}.
*
* A typical implementation would be a {@code record} implementing {@link ControllerToAgentFileCallable}.
* @see FilePath#act(FileCallable)
*/
public interface FileCallable<T> extends Serializable, RoleSensitive {
Expand Down
22 changes: 6 additions & 16 deletions core/src/main/java/jenkins/MasterToSlaveFileCallable.java
Original file line number Diff line number Diff line change
@@ -1,26 +1,16 @@
package jenkins;

import hudson.FilePath.FileCallable;
import hudson.remoting.VirtualChannel;
import java.io.File;
import jenkins.security.Roles;
import jenkins.slaves.RemotingVersionInfo;
import org.jenkinsci.remoting.RoleChecker;
import jenkins.agents.ControllerToAgentFileCallable;

/**
* {@link FileCallable}s that are meant to be only used on the master.
*
* Note that the logic within {@link #invoke(File, VirtualChannel)} should use API of a minimum supported Remoting version.
* See {@link RemotingVersionInfo#getMinimumSupportedVersion()}.
*
* {@link FileCallable}s that could run on an agent.
* For new code, implement {@link ControllerToAgentFileCallable}
* which has the advantage that it can be used on {@code record}s.
* @since 1.587 / 1.580.1
* @param <T> the return type; note that this must either be defined in your plugin or included in the stock JEP-200 whitelist
* @param <T> the return type
*/
public abstract class MasterToSlaveFileCallable<T> implements FileCallable<T> {
@Override
public void checkRoles(RoleChecker checker) throws SecurityException {
checker.check(this, Roles.SLAVE);
}
public abstract class MasterToSlaveFileCallable<T> implements ControllerToAgentFileCallable<T> {

private static final long serialVersionUID = 1L;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
/*
* The MIT License
*
* Copyright 2024 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 jenkins.agents;

import hudson.FilePath;
import jenkins.security.Roles;
import org.jenkinsci.remoting.RoleChecker;

/**
* {@link FilePath.FileCallable} meant to be serialized then run on an agent.
* Like {@link ControllerToAgentCallable} this will typically be a {@link Record}.
* @param <T> the return type; note that this must either be defined in your plugin or included in the stock JEP-200 whitelist
* @since TODO
*/
public interface ControllerToAgentFileCallable<T> extends FilePath.FileCallable<T> {

@Override
default void checkRoles(RoleChecker checker) throws SecurityException {
checker.check(this, Roles.SLAVE);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* which has the advantage that it can be used on {@code record}s.
* @author Kohsuke Kawaguchi
* @since 1.587 / 1.580.1
* @param <V> the return type; note that this must either be defined in your plugin or included in the stock JEP-200 whitelist
* @param <V> the return type
*/
public abstract class MasterToSlaveCallable<V, T extends Throwable> implements ControllerToAgentCallable<V, T> {

Expand Down

0 comments on commit 9f3cd33

Please sign in to comment.