Skip to content

Commit

Permalink
Merge pull request #250 from KostyaSha/moreLogs
Browse files Browse the repository at this point in the history
MOre logging
  • Loading branch information
KostyaSha authored Mar 20, 2019
2 parents 9bb1435 + b6bc287 commit 0e3a99c
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.github.kostyasha.yad.connector.YADockerConnector;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import hudson.AbortException;
import hudson.EnvVars;
import hudson.FilePath;
import hudson.Launcher;
Expand All @@ -26,6 +25,7 @@
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.io.IOException;
import java.io.PrintStream;

import static java.util.Objects.isNull;
import static org.jenkinsci.plugins.cloudstats.CloudStatistics.ProvisioningListener.get;
Expand Down Expand Up @@ -76,6 +76,8 @@ public void setUp(Context context,
Launcher launcher,
TaskListener listener,
EnvVars initialEnvironment) throws IOException, InterruptedException {
PrintStream logger = listener.getLogger();
logger.println("Trying to setup env...");

final ProvisioningActivity.Id activityId = new ProvisioningActivity.Id(
run.getDisplayName(),
Expand All @@ -84,7 +86,7 @@ public void setUp(Context context,

try {
get().onStarted(activityId);
final String futureName = "yadp" + Integer.toString(activityId.getFingerprint());
final String futureName = "yadp" + activityId.getFingerprint();

final DockerSlaveSingle slave = new DockerSlaveSingle(futureName,
"Slave for " + run.getFullDisplayName(),
Expand All @@ -96,23 +98,27 @@ public void setUp(Context context,
Jenkins.getInstance().addNode(slave);
final Node futureNode = Jenkins.getInstance().getNode(futureName);
if (!(futureNode instanceof DockerSlaveSingle)) {
logger.println("Can't get node" + futureName);
throw new IllegalStateException("Can't get Node " + futureName);
}

final DockerSlaveSingle node = (DockerSlaveSingle) futureNode;
try {
final Computer toComputer = node.toComputer();
if (!(toComputer instanceof DockerComputerSingle)) {
logger.println("Can't get computer for " + node.getNodeName());
throw new IllegalStateException("Can't get computer for " + node.getNodeName());
}

final DockerComputerSingle computer = (DockerComputerSingle) toComputer;
computer.setRun(run);
computer.setListener(listener);
node.setListener(listener);
listener.getLogger().println("Getting launcher...");
logger.println("Getting launcher...");
((DelegatingComputerLauncher) computer.getLauncher()).getLauncher().launch(computer, listener);
} catch (Throwable e) {
logger.println("Failed to provision");
e.printStackTrace(logger);
LOG.error("fd", e);
CloudStatistics.ProvisioningListener.get().onFailure(node.getId(), e);
//terminate will do container cleanups and remove node from jenkins silently
Expand All @@ -124,8 +130,10 @@ public void setUp(Context context,
context.setDisposer(new DisposerImpl(futureName));

} catch (Descriptor.FormException | IOException e) {
logger.println("Failed to run slave");
e.printStackTrace(logger);
get().onFailure(activityId, e);
throw new AbortException("failed to run slave");
throw new IOException("failed to run slave", e);
}
}

Expand All @@ -143,8 +151,10 @@ public DisposerImpl(@Nonnull final String slaveName) {
@Override
public void tearDown(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskListener listener)
throws IOException, InterruptedException {
PrintStream logger = listener.getLogger();

LOG.info("Shutting down slave");
listener.getLogger().println("Shutting down slave '" + slaveName + "'.");
logger.println("Shutting down slave '" + slaveName + "'.");
final Node slaveNode = Jenkins.getInstance().getNode(slaveName);
if (isNull(slaveNode)) {
throw new IllegalStateException("Can't get node " + slaveName);
Expand All @@ -153,6 +163,8 @@ public void tearDown(Run<?, ?> run, FilePath workspace, Launcher launcher, TaskL
try {
node.terminate();
} catch (Throwable e) {
logger.println("Can't terminate node");
e.printStackTrace(logger);
LOG.error("Can't terminate node", e);
CloudStatistics.ProvisioningListener.get().onFailure(node.getId(), e);
}
Expand Down

0 comments on commit 0e3a99c

Please sign in to comment.