Search in sources :

Example 1 with BackupFailedException

use of com.devonfw.cobigen.impl.exceptions.BackupFailedException in project cobigen by devonfw.

the class AbstractConfigurationUpgrader method createBackup.

/**
 * Creates a backup of the given file. If ignoreFailedBackup is set to <code>true</code>, the backup will silently log
 * a failed backup and return successfully.
 *
 * @param file to be backed up
 * @param backupPolicy the {@link BackupPolicy} to choose if a backup is necessary or not. It will throw a
 *        {@link CobiGenRuntimeException} if a backup was enforced but the creation of the backup failed.
 * @throws BackupFailedException if the backup could not be created
 */
private void createBackup(Path file, BackupPolicy backupPolicy) {
    for (int i = 0; ; i++) {
        Pattern p = Pattern.compile("(.+\\.)([^\\.]+)");
        Matcher matcher = p.matcher(file.getFileName().toString());
        String backupFilename;
        if (matcher.matches()) {
            backupFilename = matcher.group(1) + "bak" + (i == 0 ? "" : i) + "." + matcher.group(2);
        } else {
            backupFilename = file.getFileName().toString().concat(".bak" + (i == 0 ? "" : i));
        }
        Path backupPath = file.resolveSibling(backupFilename);
        try {
            Files.copy(file, backupPath, StandardCopyOption.COPY_ATTRIBUTES);
            LOG.info("Backup of templates configuration file created ('{}').", backupPath.toUri());
            break;
        } catch (FileAlreadyExistsException e) {
            continue;
        } catch (UnsupportedOperationException | IOException | SecurityException e) {
            switch(backupPolicy) {
                case NO_BACKUP:
                    // nothing to do because backup was not needed anyway
                    break;
                case BACKUP_IF_POSSIBLE:
                    LOG.warn("Could not write backup of the configuration file ('{}').", backupPath.toUri());
                    break;
                case ENFORCE_BACKUP:
                    throw new BackupFailedException("Upgrade failed. Not possible to create the backup in '" + backupPath.toUri() + "' before upgrading the configuration.", e);
            }
            break;
        }
    }
}
Also used : Path(java.nio.file.Path) Pattern(java.util.regex.Pattern) FileAlreadyExistsException(java.nio.file.FileAlreadyExistsException) Matcher(java.util.regex.Matcher) BackupFailedException(com.devonfw.cobigen.impl.exceptions.BackupFailedException) IOException(java.io.IOException)

Example 2 with BackupFailedException

use of com.devonfw.cobigen.impl.exceptions.BackupFailedException in project cobigen by devonfw.

the class HealthCheckImpl method upgradeTemplatesConfiguration.

@Override
public HealthCheckReport upgradeTemplatesConfiguration(Path templatesConfigurationFolder, BackupPolicy backupPolicy) {
    LOG.info("Upgrade of the templates configuration in '{}' triggered.", templatesConfigurationFolder);
    System.out.println(templatesConfigurationFolder.toString());
    TemplateConfigurationUpgrader templateConfigurationUpgrader = new TemplateConfigurationUpgrader();
    try {
        templateConfigurationUpgrader.upgradeConfigurationToLatestVersion(templatesConfigurationFolder, backupPolicy);
        LOG.info("Upgrade finished successfully.");
    } catch (BackupFailedException e) {
        this.healthCheckReport.addError(e);
        if (containsAnyExceptionOfClass(BackupFailedException.class)) {
            templateConfigurationUpgrader.upgradeConfigurationToLatestVersion(templatesConfigurationFolder, BackupPolicy.NO_BACKUP);
            LOG.info("Upgrade finished successfully but without backup.");
        } else {
            this.healthCheckReport.addError(new CobiGenRuntimeException("Upgrade aborted"));
            LOG.info("Upgrade aborted.");
        }
    }
    return this.healthCheckReport;
}
Also used : TemplateConfigurationUpgrader(com.devonfw.cobigen.impl.config.upgrade.TemplateConfigurationUpgrader) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) BackupFailedException(com.devonfw.cobigen.impl.exceptions.BackupFailedException)

Example 3 with BackupFailedException

use of com.devonfw.cobigen.impl.exceptions.BackupFailedException in project cobigen by devonfw.

the class HealthCheckImpl method upgradeAllConfigurations.

@Override
public HealthCheckReport upgradeAllConfigurations(Path contextConfigurationPath, BackupPolicy backupPolicy) {
    try {
        upgradeContextConfiguration(contextConfigurationPath, backupPolicy);
    } catch (BackupFailedException e) {
        upgradeContextConfiguration(contextConfigurationPath, BackupPolicy.NO_BACKUP);
    }
    ContextConfiguration contextConfiguration = new ContextConfiguration(contextConfigurationPath);
    List<String> expectedTemplatesConfigurations = new ArrayList<>();
    Set<String> hasConfiguration = Sets.newHashSet();
    Map<String, Path> upgradeableConfigurations = this.healthCheckReport.getUpgradeableConfigurations();
    for (Trigger t : contextConfiguration.getTriggers()) {
        expectedTemplatesConfigurations.add(t.getTemplateFolder());
        hasConfiguration.add(t.getTemplateFolder());
    }
    this.healthCheckReport.setHasConfiguration(hasConfiguration);
    upgradeableConfigurations.put("TempOne", contextConfigurationPath.resolve("TempOne"));
    this.healthCheckReport.setUpgradeableConfigurations(upgradeableConfigurations);
    if (expectedTemplatesConfigurations.containsAll(this.healthCheckReport.getHasConfiguration())) {
        for (final String key : expectedTemplatesConfigurations) {
            if (this.healthCheckReport.getUpgradeableConfigurations().containsKey(key)) {
                upgradeTemplatesConfiguration(this.healthCheckReport.getUpgradeableConfigurations().get(key), backupPolicy);
            }
        }
    } else {
        LOG.error("Expected template configuration does not equal the actual template configuration");
        throw new CobiGenRuntimeException("Update of the templates configuration was not successful, please retry");
    }
    return this.healthCheckReport;
}
Also used : Path(java.nio.file.Path) Trigger(com.devonfw.cobigen.impl.config.entity.Trigger) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) BackupFailedException(com.devonfw.cobigen.impl.exceptions.BackupFailedException) ArrayList(java.util.ArrayList) ContextConfiguration(com.devonfw.cobigen.impl.config.ContextConfiguration)

Example 4 with BackupFailedException

use of com.devonfw.cobigen.impl.exceptions.BackupFailedException in project cobigen by devonfw.

the class AbstractConfigurationUpgrader method upgradeConfigurationToLatestVersion.

/**
 * Upgrades the configuration to the latest supported version.
 *
 * @param configurationRoot the root folder containing the configuration with the specified
 *        {@link #configurationFilename}. See {@link #AbstractConfigurationUpgrader(Enum, Class, String)} for more
 *        information.
 * @param backupPolicy the {@link BackupPolicy} to choose if a backup is necessary or not.
 * @return if manual adoptions has to be performed after upgrading
 * @throws BackupFailedException if the backup could not be created
 */
public boolean upgradeConfigurationToLatestVersion(Path configurationRoot, BackupPolicy backupPolicy) {
    boolean manualAdoptionsNecessary = false;
    VERSIONS_TYPE currentVersion = resolveLatestCompatibleSchemaVersion(configurationRoot);
    Path configurationFile = configurationRoot.resolve(this.configurationFilename);
    if (currentVersion == null) {
        throw new InvalidConfigurationException(configurationFile.toUri().toString(), StringUtils.capitalize(this.configurationName) + " does not match any current or legacy schema definitions.");
    } else {
        VERSIONS_TYPE latestVersion = this.versions[this.versions.length - 1];
        // increasing iteration of versions
        for (int i = 0; i < this.versions.length; i++) {
            if (currentVersion == latestVersion) {
                // already up to date
                break;
            }
            if (currentVersion == this.versions[i]) {
                LOG.info("Upgrading {} '{}' from version {} to {}...", this.configurationName, configurationFile.toUri(), this.versions[i], this.versions[i + 1]);
                Object rootNode;
                try {
                    Class<?> jaxbConfigurationClass = getJaxbConfigurationClass(this.versions[i]);
                    rootNode = unmarshallConfiguration(configurationFile, this.versions[i], jaxbConfigurationClass);
                    createBackup(configurationFile, backupPolicy);
                    // NotYetSupportedException
                    ConfigurationUpgradeResult result = performNextUpgradeStep(this.versions[i], rootNode);
                    manualAdoptionsNecessary |= result.areManualAdoptionsNecessary();
                    try (OutputStream out = Files.newOutputStream(configurationFile)) {
                        JAXB.marshal(result.getResultConfigurationJaxbRootNode(), out);
                    }
                    // implicitly check upgrade step
                    currentVersion = resolveLatestCompatibleSchemaVersion(configurationRoot);
                } catch (NotYetSupportedException | BackupFailedException e) {
                    throw e;
                } catch (Throwable e) {
                    throw new CobiGenRuntimeException("An unexpected exception occurred while upgrading the " + this.configurationName + " from version '" + this.versions[i] + "' to '" + this.versions[i + 1] + "'.", e);
                }
                // if CobiGen does not understand the upgraded file... throw exception
                if (currentVersion == null) {
                    throw new CobiGenRuntimeException("An error occurred while upgrading " + this.configurationName + " from version " + this.versions[i] + " to " + this.versions[i + 1] + ".");
                }
            }
        }
    }
    return manualAdoptionsNecessary;
}
Also used : Path(java.nio.file.Path) CobiGenRuntimeException(com.devonfw.cobigen.api.exception.CobiGenRuntimeException) BackupFailedException(com.devonfw.cobigen.impl.exceptions.BackupFailedException) OutputStream(java.io.OutputStream) NotYetSupportedException(com.devonfw.cobigen.api.exception.NotYetSupportedException) InvalidConfigurationException(com.devonfw.cobigen.api.exception.InvalidConfigurationException)

Aggregations

BackupFailedException (com.devonfw.cobigen.impl.exceptions.BackupFailedException)4 CobiGenRuntimeException (com.devonfw.cobigen.api.exception.CobiGenRuntimeException)3 Path (java.nio.file.Path)3 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 NotYetSupportedException (com.devonfw.cobigen.api.exception.NotYetSupportedException)1 ContextConfiguration (com.devonfw.cobigen.impl.config.ContextConfiguration)1 Trigger (com.devonfw.cobigen.impl.config.entity.Trigger)1 TemplateConfigurationUpgrader (com.devonfw.cobigen.impl.config.upgrade.TemplateConfigurationUpgrader)1 IOException (java.io.IOException)1 OutputStream (java.io.OutputStream)1 FileAlreadyExistsException (java.nio.file.FileAlreadyExistsException)1 ArrayList (java.util.ArrayList)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1