use of com.thoughtworks.go.agent.common.AgentBootstrapperBackwardCompatibility in project gocd by gocd.
the class AgentLauncherImpl method doLaunch.
private Integer doLaunch(AgentLaunchDescriptor descriptor) {
Thread shutdownHook = null;
try {
int returnValue;
if (!lockFile.tryLock()) {
return IRRECOVERABLE_ERROR;
}
shutdownHook = registerShutdownHook();
Map context = descriptor.context();
AgentBootstrapperBackwardCompatibility backwardCompatibility = backwardCompatibility(context);
ServerUrlGenerator urlGenerator = backwardCompatibility.getUrlGenerator();
File rootCertFile = backwardCompatibility.rootCertFile();
SslVerificationMode sslVerificationMode = backwardCompatibility.sslVerificationMode();
ServerBinaryDownloader launcherDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode);
if (launcherDownloader.downloadIfNecessary(DownloadableFile.LAUNCHER)) {
return LAUNCHER_NOT_UP_TO_DATE;
}
ServerBinaryDownloader agentDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode);
agentDownloader.downloadIfNecessary(DownloadableFile.AGENT);
returnValue = agentProcessParentRunner.run(getLauncherVersion(), launcherDownloader.getMd5(), urlGenerator, System.getenv(), context);
try {
// Sleep a bit so that if there are problems we don't spin
Thread.sleep(1000);
} catch (InterruptedException e) {
return returnValue;
}
return returnValue;
} catch (Exception e) {
LOG.error("Launch encountered an unknown exception", e);
return UNKNOWN_EXCEPTION_OCCURRED;
} finally {
removeShutDownHook(shutdownHook);
lockFile.delete();
}
}
use of com.thoughtworks.go.agent.common.AgentBootstrapperBackwardCompatibility in project gocd by gocd.
the class AgentProcessParentImpl method agentInvocationCommand.
private String[] agentInvocationCommand(String agentMD5, String launcherMd5, String agentPluginsZipMd5, String tfsImplMd5, Map<String, String> env, Map context, // the port is kept for backward compatibility to ensure that old bootstrappers are able to launch new agents
@Deprecated String sslPort) {
AgentBootstrapperBackwardCompatibility backwardCompatibility = backwardCompatibility(context);
String startupArgsString = env.get(AGENT_STARTUP_ARGS);
List<String> commandSnippets = new ArrayList<>();
commandSnippets.add(javaCmd());
if (!isEmpty(startupArgsString)) {
String[] startupArgs = startupArgsString.split(" ");
for (String startupArg : startupArgs) {
String decodedStartupArg = startupArg.trim().replace("%20", " ");
if (!isEmpty(decodedStartupArg)) {
commandSnippets.add(decodedStartupArg);
}
}
}
commandSnippets.add(property(GoConstants.AGENT_PLUGINS_MD5, agentPluginsZipMd5));
commandSnippets.add(property(GoConstants.AGENT_JAR_MD5, agentMD5));
commandSnippets.add(property(GoConstants.GIVEN_AGENT_LAUNCHER_JAR_MD5, launcherMd5));
commandSnippets.add(property(GoConstants.TFS_IMPL_MD5, tfsImplMd5));
commandSnippets.add("-jar");
commandSnippets.add(Downloader.AGENT_BINARY);
commandSnippets.add("-serverUrl");
commandSnippets.add(backwardCompatibility.sslServerUrl(sslPort));
if (backwardCompatibility.sslVerificationMode() != null) {
commandSnippets.add("-sslVerificationMode");
commandSnippets.add(backwardCompatibility.sslVerificationMode().toString());
}
if (backwardCompatibility.rootCertFileAsString() != null) {
commandSnippets.add("-rootCertFile");
commandSnippets.add(backwardCompatibility.rootCertFileAsString());
}
return commandSnippets.toArray(new String[] {});
}
use of com.thoughtworks.go.agent.common.AgentBootstrapperBackwardCompatibility in project gocd by gocd.
the class AgentLauncherImpl method launch.
public int launch(AgentLaunchDescriptor descriptor) {
Thread shutdownHook = null;
try {
LoggingHelper.configureLoggerIfNoneExists("go-agent-launcher.log", "go-agent-launcher-log4j.properties");
int returnValue;
if (!lockFile.tryLock()) {
return IRRECOVERABLE_ERROR;
}
shutdownHook = registerShutdownHook();
Map context = descriptor.context();
AgentBootstrapperBackwardCompatibility backwardCompatibility = backwardCompatibility(context);
ServerUrlGenerator urlGenerator = backwardCompatibility.getUrlGenerator();
File rootCertFile = backwardCompatibility.rootCertFile();
SslVerificationMode sslVerificationMode = backwardCompatibility.sslVerificationMode();
ServerBinaryDownloader launcherDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode);
if (launcherDownloader.downloadIfNecessary(DownloadableFile.LAUNCHER)) {
return LAUNCHER_NOT_UP_TO_DATE;
}
ServerBinaryDownloader agentDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode);
agentDownloader.downloadIfNecessary(DownloadableFile.AGENT);
returnValue = agentProcessParentRunner.run(getLauncherVersion(), launcherDownloader.getMd5(), urlGenerator, System.getenv(), context);
try {
// Sleep a bit so that if there are problems we don't spin
Thread.sleep(1000);
} catch (InterruptedException e) {
return returnValue;
}
return returnValue;
} catch (Exception e) {
LOG.error("Launch encountered an unknown exception", e);
return UNKNOWN_EXCEPTION_OCCURRED;
} finally {
removeShutDownHook(shutdownHook);
lockFile.delete();
}
}
use of com.thoughtworks.go.agent.common.AgentBootstrapperBackwardCompatibility in project gocd by gocd.
the class AgentProcessParentImpl method run.
public int run(String launcherVersion, String launcherMd5, ServerUrlGenerator urlGenerator, Map<String, String> env, Map context) {
int exitValue = 0;
LOG.info("Agent is version: {}", CurrentGoCDVersion.getInstance().fullVersion());
String[] command = new String[] {};
try {
AgentBootstrapperBackwardCompatibility backwardCompatibility = backwardCompatibility(context);
File rootCertFile = backwardCompatibility.rootCertFile();
SslVerificationMode sslVerificationMode = backwardCompatibility.sslVerificationMode();
ServerBinaryDownloader agentDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode);
agentDownloader.downloadIfNecessary(DownloadableFile.AGENT);
ServerBinaryDownloader pluginZipDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode);
pluginZipDownloader.downloadIfNecessary(DownloadableFile.AGENT_PLUGINS);
ServerBinaryDownloader tfsImplDownloader = new ServerBinaryDownloader(urlGenerator, rootCertFile, sslVerificationMode);
tfsImplDownloader.downloadIfNecessary(DownloadableFile.TFS_IMPL);
command = agentInvocationCommand(agentDownloader.getMd5(), launcherMd5, pluginZipDownloader.getMd5(), tfsImplDownloader.getMd5(), env, context, agentDownloader.getSslPort());
LOG.info("Launching Agent with command: {}", join(command, " "));
Process agent = invoke(command);
// The next lines prevent the child process from blocking on Windows
AgentOutputAppender agentOutputAppenderForStdErr = new AgentOutputAppender(GO_AGENT_STDERR_LOG);
AgentOutputAppender agentOutputAppenderForStdOut = new AgentOutputAppender(GO_AGENT_STDOUT_LOG);
if (new SystemEnvironment().consoleOutToStdout()) {
agentOutputAppenderForStdErr.writeTo(AgentOutputAppender.Outstream.STDERR);
agentOutputAppenderForStdOut.writeTo(AgentOutputAppender.Outstream.STDOUT);
}
agent.getOutputStream().close();
AgentConsoleLogThread stdErrThd = new AgentConsoleLogThread(agent.getErrorStream(), agentOutputAppenderForStdErr);
stdErrThd.start();
AgentConsoleLogThread stdOutThd = new AgentConsoleLogThread(agent.getInputStream(), agentOutputAppenderForStdOut);
stdOutThd.start();
Shutdown shutdownHook = new Shutdown(agent);
Runtime.getRuntime().addShutdownHook(shutdownHook);
try {
exitValue = agent.waitFor();
} catch (InterruptedException ie) {
LOG.error("Agent was interrupted. Terminating agent and respawning. {}", ie.toString());
agent.destroy();
} finally {
removeShutdownHook(shutdownHook);
stdErrThd.stopAndJoin();
stdOutThd.stopAndJoin();
}
} catch (Exception e) {
LOG.error("Exception while executing command: {} - {}", join(command, " "), e.toString());
exitValue = EXCEPTION_OCCURRED;
}
return exitValue;
}
Aggregations