String command = execCommand.toString();
LOGGER.info("Exec " + command);
// Open an execution channel that supports SSH agent forwarding
Channel channel = sess.openChannel("exec");
((ChannelExec) channel).setCommand(command);
channel.connect();
// Wait for the channel to close
while (true) {
if (channel.isClosed()) {
break;
}
try {
Thread.sleep(1000);
} catch (Exception ee) {
}
}
int result = channel.getExitStatus();
if (result != 0) {
LOGGER.warning("Download of slave.jar failed. Return code = " + result);
throw new IOException(
"Download of slave.jar failed. Return code = "
+ result);
}
channel.disconnect();
// Execute the slave.jar to establish a connection
// Make sure to enable SSH agent forwarding
logger.println("Executing slave jar to make connection...");
final Channel slaveChannel = sess.openChannel("exec");
String sshWrapperPath = "/usr/libexec/openshift/cartridges/jenkins/bin/git_ssh_wrapper.sh";
((ChannelExec) slaveChannel).setEnv("GIT_SSH", sshWrapperPath);
((ChannelExec) slaveChannel).setAgentForwarding(true);
String jarCachePath=System.getenv("JENKINS_JAR_CACHE_PATH");
if (jarCachePath==null) {
jarCachePath="$OPENSHIFT_DATA_DIR/.jenkins/cache/jars";
}
//jar-cache parameter needed for jenkins 1.540+
((ChannelExec) slaveChannel)
.setCommand("java -jar $OPENSHIFT_DATA_DIR/jenkins/slave.jar -jar-cache "+jarCachePath);
InputStream serverOutput = slaveChannel.getInputStream();
OutputStream clientInput = slaveChannel.getOutputStream();
slaveChannel.connect();
if (slaveChannel.isClosed()) {
LOGGER.severe("Slave connection terminated early with exit = "
+ channel.getExitStatus());
}
computer.setChannel(serverOutput, clientInput, taskListener,
new Listener() {
public void onClosed(hudson.remoting.Channel channel,
IOException cause) {
slaveChannel.disconnect();
sess.disconnect();
}
});
LOGGER.info("Slave connected.");