Search in sources :

Example 6 with EnvironmentCleanupException

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);
    }
}
Also used : EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException) AgentException(com.axway.ats.agent.core.exceptions.AgentException) EnvironmentUnit(com.axway.ats.environment.EnvironmentUnit) FileEnvironmentUnit(com.axway.ats.environment.file.FileEnvironmentUnit) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DirectoryEnvironmentUnit(com.axway.ats.environment.file.DirectoryEnvironmentUnit) File(java.io.File) Date(java.util.Date)

Example 7 with EnvironmentCleanupException

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.");
        }
    }
}
Also used : EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException) File(java.io.File)

Example 8 with EnvironmentCleanupException

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);
    }
}
Also used : EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException) LocalProcessExecutor(com.axway.ats.core.process.LocalProcessExecutor) EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException)

Example 9 with EnvironmentCleanupException

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();
    }
}
Also used : EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException) File(java.io.File)

Aggregations

EnvironmentCleanupException (com.axway.ats.environment.EnvironmentCleanupException)9 File (java.io.File)7 AgentException (com.axway.ats.agent.core.exceptions.AgentException)3 EnvironmentUnit (com.axway.ats.environment.EnvironmentUnit)3 DatabaseEnvironmentUnit (com.axway.ats.environment.database.DatabaseEnvironmentUnit)3 DirectoryEnvironmentUnit (com.axway.ats.environment.file.DirectoryEnvironmentUnit)3 FileEnvironmentUnit (com.axway.ats.environment.file.FileEnvironmentUnit)3 FileNotFoundException (java.io.FileNotFoundException)2 IOException (java.io.IOException)2 PublicAtsApi (com.axway.ats.common.PublicAtsApi)1 LocalProcessExecutor (com.axway.ats.core.process.LocalProcessExecutor)1 Date (java.util.Date)1