use of com.axway.ats.environment.EnvironmentCleanupException in project ats-framework by Axway.
the class ComponentEnvironment method backup.
public void backup(String folderPath) throws AgentException {
log.info("Backuping environment for component " + componentName);
try {
String currentBackupFolder = backupFolder;
if (folderPath != null) {
currentBackupFolder = folderPath;
}
//if the current backup folder already exists and is not empty, we will rename it in order to have
//a clean backup and to save the previous backup data
File backupDir = new File(currentBackupFolder);
if (backupDir.isDirectory() && backupDir.list().length > 0) {
String backupFolderPath = currentBackupFolder;
if (currentBackupFolder.endsWith("/") || currentBackupFolder.endsWith("\\")) {
backupFolderPath = currentBackupFolder.substring(0, currentBackupFolder.length() - 1);
}
backupFolderPath = backupFolderPath + BACKUP_DATE_FORMATTER.format(new Date());
backupFolderPath = IoUtils.normalizeDirPath(backupFolderPath);
log.info("In order to have a clean backup, we'll rename the current backup folder: " + backupFolderPath);
backupDir.renameTo(new File(backupFolderPath));
}
for (EnvironmentUnit environmentUnit : environmentUnits) {
environmentUnit.setTempBackupDir(folderPath);
environmentUnit.backup();
}
} catch (EnvironmentCleanupException ece) {
throw new AgentException("Could not backup environment for component " + componentName + recurseCauses(ece), ece);
}
}
use of com.axway.ats.environment.EnvironmentCleanupException in project ats-framework by Axway.
the class DirectoryEnvironmentUnit method restoreAllFilesInDirectory.
private void restoreAllFilesInDirectory(File originalDir, Set<String> fileAndDirectoryPaths) throws EnvironmentCleanupException {
File[] files = originalDir.listFiles();
if (files != null) {
for (File file : files) {
if (file.isDirectory()) {
restoreAllFilesInDirectory(file, fileAndDirectoryPaths);
} else {
String backupFilePath = IoUtils.normalizeFilePath(getFileCanonicalPath(file)).replace(origDirName, getBackupDir());
if (fileAndDirectoryPaths.contains(backupFilePath)) {
boolean fileRestored = new FileEnvironmentUnit(getFileCanonicalPath(file), IoUtils.getFilePath(backupFilePath), file.getName()).restore();
updateRestoredFlag(fileRestored);
// remove from the backup files and directories index
fileAndDirectoryPaths.remove(backupFilePath);
} else {
// the file is new and missing from the backup directory => delete it
if (file.delete()) {
updateRestoredFlag(true);
log.info("File " + getFileCanonicalPath(file) + " is deleted.");
} else {
throw new EnvironmentCleanupException("File " + getFileCanonicalPath(file) + " must be removed, but the delete operation fails.");
}
}
}
}
}
// we have to delete all new created directories, which are missing in the backup directory
String backupDirPath = IoUtils.normalizeDirPath(getFileCanonicalPath(originalDir)).replace(origDirName, getBackupDir());
if (fileAndDirectoryPaths.contains(backupDirPath)) {
// remove from the backup files and directories index
fileAndDirectoryPaths.remove(backupDirPath);
} else if (!backupDirPath.equals(getBackupDir())) {
if (originalDir.delete()) {
updateRestoredFlag(true);
log.info("Directory " + getFileCanonicalPath(originalDir) + " is deleted.");
} else {
// TODO delete
log.error("Directory " + getFileCanonicalPath(originalDir) + " must be removed, but the delete operation fails. Details follow");
log.error("Exists: " + originalDir.exists());
log.error("Is directory: " + originalDir.isDirectory());
log.error("Is file: " + originalDir.isFile());
throw new EnvironmentCleanupException("Directory " + getFileCanonicalPath(originalDir) + " must be removed, but the delete operation fails.");
}
}
}
use of com.axway.ats.environment.EnvironmentCleanupException in project ats-framework by Axway.
the class SystemProcessAction method executeAction.
@Override
protected void executeAction() throws EnvironmentCleanupException {
try {
log.debug("Executing additional action with shell command '" + shellCommand + "'");
LocalProcessExecutor processExecutor = new LocalProcessExecutor(HostUtils.LOCAL_HOST_IPv4, shellCommand);
processExecutor.execute();
} catch (Exception e) {
throw new EnvironmentCleanupException("Could not execute command '" + shellCommand + "'", e);
}
}
use of com.axway.ats.environment.EnvironmentCleanupException in project ats-framework by Axway.
the class DirectoryEnvironmentUnit method backupAllFilesInDirectory.
private void backupAllFilesInDirectory(File origDir) throws EnvironmentCleanupException {
File[] files = origDir.listFiles();
if (files == null) {
throw new EnvironmentCleanupException("No such directory '" + origDir + "'.");
}
for (File file : files) {
if (file.isDirectory()) {
backupAllFilesInDirectory(file);
} else {
String backupFileName = IoUtils.normalizeFilePath(getFileCanonicalPath(file));
// important that case matches incl. drive letter so origDirName and backupDir
// should also come after getCanonicalPath() invocation
backupFileName = backupFileName.replace(origDirName, getBackupDir());
new FileEnvironmentUnit(getFileCanonicalPath(file), IoUtils.getFilePath(backupFileName), file.getName()).backup();
}
}
// if the directory is empty - create the new empty directory in the backup folder
if (files.length == 0) {
String backupDir = IoUtils.normalizeDirPath(getFileCanonicalPath(origDir)).replace(origDirName, getBackupDir());
new File(backupDir).mkdirs();
}
}
Aggregations