Search in sources :

Example 1 with EnvironmentUnit

use of com.axway.ats.environment.EnvironmentUnit in project ats-framework by Axway.

the class EnvironmentConfigurator method apply.

@Override
public void apply() throws ConfigurationException {
    for (Properties dbProperties : dbPropertiesList) {
        int dbConfigurationIndex = -1;
        if (dbProperties.get(DB_CONFIGURATION_INDEX) != null) {
            dbConfigurationIndex = (Integer) dbProperties.get(DB_CONFIGURATION_INDEX);
        }
        ComponentRepository componentRepository = ComponentRepository.getInstance();
        ComponentEnvironment componentEnvironment;
        try {
            componentEnvironment = componentRepository.getComponentEnvironment(component);
        } catch (NoSuchComponentException nsce) {
            throw new ConfigurationException("Error changing environment configuration. CAUSE: " + nsce.getMessage());
        }
        int foundDbConfigurationIndex = 0;
        for (EnvironmentUnit environmentUnit : componentEnvironment.getEnvironmentUnits()) {
            if (environmentUnit instanceof DatabaseEnvironmentUnit) {
                if (foundDbConfigurationIndex == dbConfigurationIndex) {
                    DatabaseEnvironmentUnit dbEnvironmentUnit = (DatabaseEnvironmentUnit) environmentUnit;
                    DbConnection dbConnection = dbEnvironmentUnit.getDbConnection();
                    // the database port is kept in a list of custom properties
                    Map<String, Object> customProperties = dbConnection.getCustomProperties();
                    // get the new configuration properties
                    String newDbHost = chooseNewProperty(dbProperties.getProperty(DB_HOST), dbConnection.getHost());
                    String newDbName = chooseNewProperty(dbProperties.getProperty(DB_NAME), dbConnection.getDb());
                    String newDbUserName = chooseNewProperty(dbProperties.getProperty(DB_USER_NAME), dbConnection.getUser());
                    String newDbUserPassword = chooseNewProperty(dbProperties.getProperty(DB_USER_PASSWORD), dbConnection.getPassword());
                    Object newDbPort = chooseDbPort(dbProperties.get(DB_PORT), customProperties.get(DbKeys.PORT_KEY));
                    // create a new connection object
                    customProperties.put(DbKeys.PORT_KEY, newDbPort);
                    DbConnection newDbConnection = DatabaseProviderFactory.createDbConnection(dbConnection.getDbType(), newDbHost, newDbName, newDbUserName, newDbUserPassword, customProperties);
                    // apply the changes
                    dbEnvironmentUnit.setDbConnection(newDbConnection);
                    log.info("Database configuration for index " + dbConfigurationIndex + " is changed. DbConnection: " + newDbConnection.getDescription());
                    return;
                } else {
                    // still searching the exact database configuration
                    foundDbConfigurationIndex++;
                }
            }
        }
        throw new ConfigurationException("Database configuration with index " + dbConfigurationIndex + " is not available");
    }
}
Also used : ComponentEnvironment(com.axway.ats.agent.core.ComponentEnvironment) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) EnvironmentUnit(com.axway.ats.environment.EnvironmentUnit) ComponentRepository(com.axway.ats.agent.core.ComponentRepository) Properties(java.util.Properties) NoSuchComponentException(com.axway.ats.agent.core.exceptions.NoSuchComponentException) DbConnection(com.axway.ats.core.dbaccess.DbConnection)

Example 2 with EnvironmentUnit

use of com.axway.ats.environment.EnvironmentUnit in project ats-framework by Axway.

the class ConfigurationParser method parseEnvironment.

/**
     * Parse the environment node in the Agent descriptor
     *
     * @param environmentNode the environment node
     * @throws AgentException on error
     */
private void parseEnvironment(Node environmentNode) throws AgentException {
    NodeList environmentChildNodes = environmentNode.getChildNodes();
    //read the environment name
    String environmentName = parseEnvironmentName(environmentNode);
    //read the backup folder
    String backupFolder = parseBackupFolder(environmentNode);
    //now read the individual entries in the environment
    //these can be databases, files, processes, etc.
    List<EnvironmentUnit> environmentUnits = new ArrayList<EnvironmentUnit>();
    for (int i = 0; i < environmentChildNodes.getLength(); i++) {
        Node individualEnvironmentNode = environmentChildNodes.item(i);
        if (individualEnvironmentNode.getNodeName().equals(DATABASE)) {
            environmentUnits.add(parseDbEnvironment(individualEnvironmentNode, backupFolder));
        } else if (individualEnvironmentNode.getNodeName().equals(FILE)) {
            environmentUnits.add(parseFileEnvironment(individualEnvironmentNode, backupFolder));
        } else if (individualEnvironmentNode.getNodeName().equals(DIRECTORY)) {
            environmentUnits.add(parseDirectoryEnvironment(individualEnvironmentNode, backupFolder));
        }
    }
    environments.add(new ComponentEnvironment(componentName, environmentName, environmentUnits, backupFolder));
}
Also used : NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) ArrayList(java.util.ArrayList) FileEnvironmentUnit(com.axway.ats.environment.file.FileEnvironmentUnit) EnvironmentUnit(com.axway.ats.environment.EnvironmentUnit) DatabaseEnvironmentUnit(com.axway.ats.environment.database.DatabaseEnvironmentUnit) DirectoryEnvironmentUnit(com.axway.ats.environment.file.DirectoryEnvironmentUnit)

Example 3 with EnvironmentUnit

use of com.axway.ats.environment.EnvironmentUnit in project ats-framework by Axway.

the class ComponentEnvironment method backupOnlyIfNotAlreadyDone.

public void backupOnlyIfNotAlreadyDone() throws AgentException {
    try {
        File backupDir = new File(backupFolder);
        String[] fileList = backupDir.list();
        if (backupDir.isDirectory() && fileList != null && fileList.length > 0) {
            log.info("Backup directory '" + backupDir.getAbsolutePath() + "' already exists and the backup will be skipped.");
        } else if (backupDir.exists() && !backupDir.isDirectory()) {
            throw new AgentException("Could not create backup directory '" + backupDir.getAbsolutePath() + "'. File with this name already exists.");
        } else {
            log.info("Creating backup for component " + componentName);
            for (EnvironmentUnit environmentUnit : environmentUnits) {
                environmentUnit.backup();
            }
        }
    } catch (EnvironmentCleanupException ece) {
        throw new AgentException("Could not backup environment for component '" + componentName + "'" + recurseCauses(ece), ece);
    }
}
Also used : AgentException(com.axway.ats.agent.core.exceptions.AgentException) EnvironmentCleanupException(com.axway.ats.environment.EnvironmentCleanupException) 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)

Example 4 with EnvironmentUnit

use of com.axway.ats.environment.EnvironmentUnit in project ats-framework by Axway.

the class ComponentEnvironment method restore.

public void restore(String folderPath) throws AgentException {
    // All additional actions are implicitly added to the Additional Actions Queue Instance
    try {
        for (EnvironmentUnit environmentUnit : environmentUnits) {
            environmentUnit.setTempBackupDir(folderPath);
            environmentUnit.restore();
        }
    } catch (EnvironmentCleanupException ece) {
        throw new AgentException("Could not restore environment for component '" + this.componentName + "'" + recurseCauses(ece), ece);
    }
    // Now execute the additional actions and clean the queue
    try {
        AdditionalActionsQueue.getInstance().flushAllActions();
    } catch (EnvironmentCleanupException ece) {
        throw new AgentException("Could not restore environment for component '" + this.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)

Example 5 with EnvironmentUnit

use of com.axway.ats.environment.EnvironmentUnit in project ats-framework by Axway.

the class Test_DirectoryEnvironmentUnit method restore_aNewFileMustBeDeleted.

// TODO - investigate failing error. It is just on CI machine, only when run from Jenkins
@Ignore
@Test
public void restore_aNewFileMustBeDeleted() throws EnvironmentCleanupException, IOException {
    EnvironmentUnit dirEnvUnit = new DirectoryEnvironmentUnit(restoreDirPath, backupDirPath, backupDirName);
    Assert.assertTrue(dirEnvUnit.restore());
    new File(restoreDirPath + "newFile.txt").createNewFile();
    // now the restore is not needed
    Assert.assertTrue(dirEnvUnit.restore());
}
Also used : EnvironmentUnit(com.axway.ats.environment.EnvironmentUnit) File(java.io.File) Ignore(org.junit.Ignore) BaseTest(com.axway.ats.environment.BaseTest) Test(org.junit.Test)

Aggregations

EnvironmentUnit (com.axway.ats.environment.EnvironmentUnit)18 Test (org.junit.Test)11 DatabaseEnvironmentUnit (com.axway.ats.environment.database.DatabaseEnvironmentUnit)10 BaseTest (com.axway.ats.environment.BaseTest)8 DirectoryEnvironmentUnit (com.axway.ats.environment.file.DirectoryEnvironmentUnit)8 FileEnvironmentUnit (com.axway.ats.environment.file.FileEnvironmentUnit)8 File (java.io.File)8 AgentException (com.axway.ats.agent.core.exceptions.AgentException)3 EnvironmentCleanupException (com.axway.ats.environment.EnvironmentCleanupException)3 InputStream (java.io.InputStream)3 ArrayList (java.util.ArrayList)3 Ignore (org.junit.Ignore)3 ComponentEnvironment (com.axway.ats.agent.core.ComponentEnvironment)2 Component (com.axway.ats.agent.core.Component)1 ComponentRepository (com.axway.ats.agent.core.ComponentRepository)1 NoSuchComponentException (com.axway.ats.agent.core.exceptions.NoSuchComponentException)1 DbConnection (com.axway.ats.core.dbaccess.DbConnection)1 DbConnMySQL (com.axway.ats.core.dbaccess.mysql.DbConnMySQL)1 SystemProcessAction (com.axway.ats.environment.process.SystemProcessAction)1 Date (java.util.Date)1