Skip to content

Commit

Permalink
Merge pull request #37 from jglick/tee-step
Browse files Browse the repository at this point in the history
Porting https://github.com/jglick/tee-step-plugin to pipeline-utility-steps
  • Loading branch information
rsandell authored Feb 12, 2018
2 parents 371cf8d + 5286faa commit 9bc6239
Show file tree
Hide file tree
Showing 4 changed files with 276 additions and 0 deletions.
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,13 @@
<version>${workflow-step-api.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.jenkins-ci.plugins.workflow</groupId>
<artifactId>workflow-support</artifactId>
<version>2.14</version>
<classifier>tests</classifier>
<scope>test</scope>
</dependency>
</dependencies>

<scm>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,159 @@
/*
* The MIT License
*
* Copyright 2017 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 org.jenkinsci.plugins.pipeline.utility.steps.fs;

import hudson.Extension;
import hudson.FilePath;
import hudson.console.ConsoleLogFilter;
import hudson.model.Run;
import hudson.remoting.RemoteOutputStream;
import hudson.remoting.VirtualChannel;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Serializable;
import java.nio.file.Files;
import java.nio.file.InvalidPathException;
import java.nio.file.StandardOpenOption;
import java.util.Collections;
import java.util.Set;
import jenkins.MasterToSlaveFileCallable;
import org.apache.commons.io.output.TeeOutputStream;
import org.jenkinsci.plugins.workflow.steps.BodyExecutionCallback;
import org.jenkinsci.plugins.workflow.steps.BodyInvoker;
import org.jenkinsci.plugins.workflow.steps.Step;
import org.jenkinsci.plugins.workflow.steps.StepContext;
import org.jenkinsci.plugins.workflow.steps.StepDescriptor;
import org.jenkinsci.plugins.workflow.steps.StepExecution;
import org.kohsuke.stapler.DataBoundConstructor;

public class TeeStep extends Step {

public final String file;

@DataBoundConstructor
public TeeStep(String file) {
this.file = file;
}

@Override
public StepExecution start(StepContext context) throws Exception {
return new Execution(context, file);
}

private static class Execution extends StepExecution {

private final String file;

Execution(StepContext context, String file) {
super(context);
this.file = file;
}

@Override
public boolean start() throws Exception {
FilePath f = getContext().get(FilePath.class).child(file);
getContext().newBodyInvoker().
withContext(BodyInvoker.mergeConsoleLogFilters(getContext().get(ConsoleLogFilter.class), new TeeFilter(f))).
withCallback(BodyExecutionCallback.wrap(getContext())).
start();
return false;
}

private static final long serialVersionUID = 1;

}

private static class TeeFilter extends ConsoleLogFilter implements Serializable {

private final FilePath f;

TeeFilter(FilePath f) {
this.f = f;
}

@SuppressWarnings("rawtypes")
@Override
public OutputStream decorateLogger(Run build, final OutputStream logger) throws IOException, InterruptedException {
return new TeeOutputStream(logger, append(f));
}

private static final long serialVersionUID = 1;

}

/** @see FilePath#write() */
private static OutputStream append(FilePath fp) throws IOException, InterruptedException {
if (fp.getChannel() == null) {
File f = new File(fp.getRemote()).getAbsoluteFile();
f.getParentFile().mkdirs();
try {
return Files.newOutputStream(f.toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND/*, StandardOpenOption.DSYNC*/);
} catch (InvalidPathException e) {
throw new IOException(e);
}
} else {
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();
f.getParentFile().mkdirs();
try {
return new RemoteOutputStream(Files.newOutputStream(f.toPath(), StandardOpenOption.CREATE, StandardOpenOption.APPEND/*, StandardOpenOption.DSYNC*/));
} catch (InvalidPathException e) {
throw new IOException(e);
}
}
});
}
}

@Extension
public static class DescriptorImpl extends StepDescriptor {

@Override
public Set<? extends Class<?>> getRequiredContext() {
return Collections.singleton(FilePath.class);
}

@Override
public String getFunctionName() {
return "tee";
}

@Override
public boolean takesImplicitBlockArgument() {
return true;
}

@Override
public String getDisplayName() {
return "Tee output to file";
}

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?xml version="1.0" encoding="UTF-8"?>
<!--
The MIT License
Copyright 2017 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.
-->

<?jelly escape-by-default='true'?>
<j:jelly xmlns:j="jelly:core" xmlns:f="/lib/form">
<f:entry name="file" title="File">
<f:textbox/>
</f:entry>
</j:jelly>
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
/*
* The MIT License
*
* Copyright 2017 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 org.jenkinsci.plugins.pipeline.utility.steps.fs;

import org.jenkinsci.plugins.workflow.cps.CpsFlowDefinition;
import org.jenkinsci.plugins.workflow.job.WorkflowJob;
import org.jenkinsci.plugins.workflow.job.WorkflowRun;
import org.jenkinsci.plugins.workflow.test.steps.SemaphoreStep;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.Rule;
import org.jvnet.hudson.test.BuildWatcher;
import org.jvnet.hudson.test.JenkinsRule;
import org.jvnet.hudson.test.RestartableJenkinsRule;

public class TeeStepTest {

@ClassRule
public static BuildWatcher buildWatcher = new BuildWatcher();

@Rule
public RestartableJenkinsRule rr = new RestartableJenkinsRule();

@Test
public void smokes() throws Exception {
rr.then(new RestartableJenkinsRule.Step() {
@Override
public void run(JenkinsRule r) throws Throwable {
r.createSlave("remote", null, null);
WorkflowJob p = r.createProject(WorkflowJob.class, "p");
p.setDefinition(new CpsFlowDefinition(
"node('remote') {\n" +
" dir($/" + r.jenkins.getWorkspaceFor(p) + "/$) {\n" + // remote FS gets blown away during restart, alas; need JenkinsRule utility for stable agent workspace
" tee('x.log') {\n" +
" echo 'first message'\n" +
" semaphore 'wait'\n" +
" echo 'second message'\n" +
" }\n" +
" echo(/got: ${readFile('x.log').trim().replace('\\n', ' ').replace('\\r', '')}/)\n" +
" }\n" +
"}", true));
WorkflowRun b = p.scheduleBuild2(0).waitForStart();
SemaphoreStep.waitForStart("wait/1", b);
}
});
rr.then(new RestartableJenkinsRule.Step() {
@Override
public void run(JenkinsRule r) throws Throwable {
SemaphoreStep.success("wait/1", null);
WorkflowRun b = r.jenkins.getItemByFullName("p", WorkflowJob.class).getBuildByNumber(1);
r.waitForCompletion(b);
r.assertLogContains("got: first message second message", b);
}
});
}

}

0 comments on commit 9bc6239

Please sign in to comment.