use of com.axway.ats.core.atsconfig.exceptions.AtsManagerException in project ats-framework by Axway.
the class AtsInfrastructureManager method installAgent.
/**
* Install the ATS Agent by alias
*
* @param agentAlias the agent alias declared in the configuration
* @throws AtsManagerException
*/
public ApplicationStatus installAgent(String agentAlias) throws AtsManagerException {
AgentInfo agentInfo = getAgentInfo(agentAlias);
log.info(TOP_LEVEL_ACTION_PREFIX + "Now we will try to install " + agentInfo.getDescription());
String agentZip = projectConfiguration.getSourceProject().getAgentZip();
if (StringUtils.isNullOrEmpty(agentZip)) {
throw new AtsManagerException("The agent zip source file is not specified in the configuration");
}
// extract agent.zip to a temporary local directory
String agentFolder = IoUtils.normalizeDirPath(extractAgentZip(agentZip));
JschSftpClient sftpClient = new JschSftpClient();
try {
// upload clean agent
log.info("Upload clean " + agentInfo.getDescription());
sftpClient.connect(agentInfo.getSystemUser(), agentInfo.getSystemPassword(), agentInfo.getHost(), agentInfo.getSSHPort(), agentInfo.getSSHPrivateKey(), agentInfo.getSSHPrivateKeyPassword());
if (sftpClient.isRemoteFileOrDirectoryExisting(agentInfo.getSftpHome())) {
sftpClient.purgeRemoteDirectoryContents(agentInfo.getSftpHome());
}
sftpClient.uploadDirectory(agentFolder, agentInfo.getSftpHome(), true);
// upload custom agent dependencies
log.info("Upload custom agent dependencies");
for (PathInfo pathInfo : agentInfo.getPaths()) {
if (pathInfo.isFile()) {
if (!sftpClient.isRemoteFileOrDirectoryExisting(pathInfo.getSftpPath())) {
String fileName = IoUtils.getFileName(pathInfo.getSftpPath());
String filePath = projectConfiguration.getSourceProject().findFile(fileName);
if (filePath == null) {
log.warn("File '" + fileName + "' can't be found in the source project libraries," + " so it can't be uploaded to " + agentInfo.getDescription());
continue;
}
if (!new File(filePath).exists()) {
log.warn("Local file '" + filePath + "' does not exist on the local system," + " so it can't be uploaded to " + agentInfo.getDescription());
continue;
}
int lastSlashIdx = pathInfo.getSftpPath().lastIndexOf('/');
if (lastSlashIdx > 0) {
sftpClient.makeRemoteDirectories(pathInfo.getSftpPath().substring(0, lastSlashIdx));
}
sftpClient.uploadFile(filePath, pathInfo.getSftpPath());
}
} else {
log.warn("Uploading directories into ATS agent is still not supported");
}
}
// make agent start file to be executable
makeScriptsExecutable(agentInfo);
// execute post install shell command, if any
executePostActionShellCommand(agentInfo, "install", agentInfo.getPostInstallShellCommand());
log.info(TOP_LEVEL_ACTION_PREFIX + "Successfully installed " + agentInfo.getDescription());
return ApplicationStatus.STOPPED;
} finally {
sftpClient.disconnect();
}
}
use of com.axway.ats.core.atsconfig.exceptions.AtsManagerException in project ats-framework by Axway.
the class AtsInfrastructureManager method downloadAgentZip.
private String downloadAgentZip(String agentZipUrl, String tempPath) throws AtsManagerException {
File downloadedFile = new File(tempPath + "agent.zip");
downloadedFile.deleteOnExit();
log.info("Download ATS agent from " + agentZipUrl + " into " + downloadedFile);
BufferedInputStream bis = null;
BufferedOutputStream bos = null;
try {
URL url = new URL(agentZipUrl);
URLConnection conn = url.openConnection();
bis = new BufferedInputStream(conn.getInputStream());
bos = new BufferedOutputStream(new FileOutputStream(downloadedFile));
int inByte;
while ((inByte = bis.read()) != -1) {
bos.write(inByte);
}
return downloadedFile.getPath();
} catch (Exception e) {
throw new AtsManagerException("Error downloading agent ZIP file from " + agentZipUrl, e);
} finally {
IoUtils.closeStream(bis);
IoUtils.closeStream(bos);
}
}
use of com.axway.ats.core.atsconfig.exceptions.AtsManagerException in project ats-framework by Axway.
the class AtsInfrastructureManager method executePostActionShellCommand.
/**
* Execute post install/upgrade(full) shell command, if any
*
* @param agentInfo agent information
* @throws AtsManagerException
*/
private void executePostActionShellCommand(AgentInfo agentInfo, String actionName, String shellCommand) throws AtsManagerException {
if (shellCommand != null) {
log.info("Executing post '" + actionName + "' shell command: " + shellCommand);
JschSshClient sshClient = new JschSshClient();
try {
sshClient.connect(agentInfo.getSystemUser(), agentInfo.getSystemPassword(), agentInfo.getHost(), agentInfo.getSSHPort(), agentInfo.getSSHPrivateKey(), agentInfo.getSSHPrivateKeyPassword());
int exitCode = sshClient.execute(shellCommand, true);
if (exitCode != 0) {
throw new AtsManagerException("Unable to execute the post '" + actionName + "' shell command '" + shellCommand + "' on agent '" + agentInfo.getAlias() + "'. The error output is" + (StringUtils.isNullOrEmpty(sshClient.getErrorOutput()) ? " empty." : ":\n" + sshClient.getErrorOutput()));
}
log.info("The output of shell command \"" + shellCommand + "\" is" + (StringUtils.isNullOrEmpty(sshClient.getStandardOutput()) ? " empty." : ":\n" + sshClient.getStandardOutput()));
} finally {
sshClient.disconnect();
}
}
}
use of com.axway.ats.core.atsconfig.exceptions.AtsManagerException in project ats-framework by Axway.
the class AtsInfrastructureManager method extractAgentZip.
/**
* Extract the agent zip file in a temporary directory and return its location
*
* @param agentZipPath agent zip file path
* @return the directory where the zip file is unzipped
* @throws AtsManagerException
*/
private String extractAgentZip(String agentZipPath) throws AtsManagerException {
// the temp path contains the thread name to have uniqueness when running action on more than
// one agent at same time
final String tempPath = IoUtils.normalizeDirPath(AtsSystemProperties.SYSTEM_USER_TEMP_DIR + "/ats_tmp/") + Thread.currentThread().getName() + "_";
if (agentZipPath.toLowerCase().startsWith("http://")) {
// download from HTTP URL
agentZipPath = downloadAgentZip(agentZipPath, tempPath);
}
File agentZip = new File(agentZipPath);
if (!agentZip.exists()) {
throw new AtsManagerException("The agent ZIP file doesn't exist '" + agentZipPath + "'");
}
String agentFolderName = tempPath + "agent_" + String.valueOf(agentZip.lastModified());
File agentFolder = new File(agentFolderName);
if (!agentFolder.exists()) {
try {
log.info("Unzip " + agentZipPath + " into " + agentFolderName);
IoUtils.unzip(agentZipPath, agentFolderName, true);
} catch (IOException ioe) {
throw new AtsManagerException("Unable to unzip the agent ZIP file '" + agentZipPath + "' to the temporary created directory '" + agentFolderName + "'", ioe);
}
}
return agentFolderName;
}
use of com.axway.ats.core.atsconfig.exceptions.AtsManagerException in project ats-framework by Axway.
the class ApplicationController method start.
@Override
public ApplicationStatus start(boolean isTopLevelAction) throws AtsManagerException {
JschSshClient sshClient = new JschSshClient();
try {
// it must be stopped, before we try to start it
ApplicationStatus status = getStatus(sshClient, false);
if (status != ApplicationStatus.STOPPED) {
log.error((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + "We will not try to start " + anyApplicationInfo.description + " as it is currently " + status.name());
return status;
}
// connect
String command = anyApplicationInfo.getStartCommand();
log.info((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + "Now we will try to start " + anyApplicationInfo.description + " with: " + command);
sshClient.connect(anyApplicationInfo.systemUser, anyApplicationInfo.systemPassword, anyApplicationInfo.host, anyApplicationInfo.sshPort);
// execute shell command
int commandExitCode = sshClient.execute(command, true);
// check exit code
// check if there is something in STD ERR
boolean isStartedOk = false;
if (commandExitCode == 0 && StringUtils.isNullOrEmpty(sshClient.getErrorOutput())) {
String stdoutSearchToken = anyApplicationInfo.getStartCommandStdOutSearchToken();
if (stdoutSearchToken != null) {
// we confirm the success by a token in STD OUT
if (Pattern.compile(stdoutSearchToken, Pattern.DOTALL).matcher(sshClient.getStandardOutput()).matches()) {
isStartedOk = true;
} else {
log.error("Execution of '" + command + "' completed with exit code " + commandExitCode + " and empty STD ERR, but we did not get the expected '" + stdoutSearchToken + "' in STD OUT");
}
} else {
// we confirm the success by checking the status again
if (getStatus(sshClient, false) == ApplicationStatus.STARTED) {
isStartedOk = true;
}
}
}
if (isStartedOk) {
log.info((isTopLevelAction ? TOP_LEVEL_ACTION_PREFIX : "") + anyApplicationInfo.description + " is successfully started");
executePostActionShellCommand(anyApplicationInfo, "START", anyApplicationInfo.getPostStartShellCommand());
return ApplicationStatus.STARTED;
} else {
throw new AtsManagerException("Can't start " + anyApplicationInfo.description + "\n" + sshClient.getLastCommandExecutionResult());
}
} finally {
sshClient.disconnect();
}
}
Aggregations