Search in sources :

Example 1 with UpgradeException

use of org.craftercms.studio.api.v2.exception.UpgradeException in project studio by craftercms.

the class DbVersionUpgradeOperation method execute.

@Override
public void execute(final String site) throws UpgradeException {
    try (Connection connection = getConnection();
        PreparedStatement statement = connection.prepareStatement(sqlCommand)) {
        statement.setString(1, nextVersion);
        int updated = statement.executeUpdate();
        connection.commit();
        if (updated != 1) {
            throw new UpgradeException("Error updating the db version");
        }
        logger.info("Database version updated to {0}", nextVersion);
    } catch (SQLException e) {
        throw new UpgradeException("Error updating the db version", e);
    }
}
Also used : UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) SQLException(java.sql.SQLException) Connection(java.sql.Connection) PreparedStatement(java.sql.PreparedStatement)

Example 2 with UpgradeException

use of org.craftercms.studio.api.v2.exception.UpgradeException in project studio by craftercms.

the class BlueprintsUpgradeOperation method execute.

@Override
public void execute(final String site) throws UpgradeException {
    String gitLockKey = SITE_SANDBOX_REPOSITORY_GIT_LOCK.replaceAll(PATTERN_SITE, site);
    generalLockService.lock(gitLockKey);
    try {
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Path globalConfigPath = helper.buildRepoPath(GitRepositories.GLOBAL);
        Path blueprintsPath = Paths.get(globalConfigPath.toAbsolutePath().toString(), studioConfiguration.getProperty(BLUE_PRINTS_PATH));
        String studioManifestLocation = servletContext.getRealPath(STUDIO_MANIFEST_LOCATION);
        String blueprintsManifestLocation = Paths.get(blueprintsPath.toAbsolutePath().toString(), "BLUEPRINTS.MF").toAbsolutePath().toString();
        boolean blueprintManifestExists = Files.exists(Paths.get(blueprintsManifestLocation));
        InputStream studioManifestStream = FileUtils.openInputStream(new File(studioManifestLocation));
        Manifest studioManifest = new Manifest(studioManifestStream);
        VersionInfo studioVersion = VersionInfo.getVersion(studioManifest);
        InputStream blueprintsManifestStream = null;
        Manifest blueprintsManifest = null;
        VersionInfo blueprintsVersion = null;
        if (blueprintManifestExists) {
            blueprintsManifestStream = FileUtils.openInputStream(new File(blueprintsManifestLocation));
            blueprintsManifest = new Manifest(blueprintsManifestStream);
            blueprintsVersion = VersionInfo.getVersion(blueprintsManifest);
        }
        if (!blueprintManifestExists || !StringUtils.equals(studioVersion.getPackageBuild(), blueprintsVersion.getPackageBuild()) || (StringUtils.equals(studioVersion.getPackageBuild(), blueprintsVersion.getPackageBuild()) && !StringUtils.equals(studioVersion.getPackageBuildDate(), blueprintsVersion.getPackageBuildDate()))) {
            String bootstrapBlueprintsFolderPath = servletContext.getRealPath(FILE_SEPARATOR + BOOTSTRAP_REPO_PATH + FILE_SEPARATOR + BOOTSTRAP_REPO_GLOBAL_PATH + FILE_SEPARATOR + studioConfiguration.getProperty(BLUE_PRINTS_PATH));
            File bootstrapBlueprintsFolder = new File(bootstrapBlueprintsFolderPath);
            File[] blueprintFolders = bootstrapBlueprintsFolder.listFiles(File::isDirectory);
            for (File blueprintFolder : blueprintFolders) {
                String blueprintName = blueprintFolder.getName();
                FileUtils.deleteDirectory(Paths.get(blueprintsPath.toAbsolutePath().toString(), blueprintName).toFile());
                TreeCopier tc = new TreeCopier(Paths.get(blueprintFolder.getAbsolutePath()), Paths.get(blueprintsPath.toAbsolutePath().toString(), blueprintName));
                EnumSet<FileVisitOption> opts = EnumSet.of(FileVisitOption.FOLLOW_LINKS);
                Files.walkFileTree(Paths.get(blueprintFolder.getAbsolutePath()), opts, Integer.MAX_VALUE, tc);
            }
            FileUtils.copyFile(Paths.get(studioManifestLocation).toFile(), Paths.get(globalConfigPath.toAbsolutePath().toString(), studioConfiguration.getProperty(BLUE_PRINTS_PATH), "BLUEPRINTS.MF").toFile());
        }
        Repository globalRepo = helper.getRepository(site, GitRepositories.GLOBAL);
        try (Git git = new Git(globalRepo)) {
            StatusCommand statusCommand = git.status();
            Status status = retryingRepositoryOperationFacade.call(statusCommand);
            if (status.hasUncommittedChanges() || !status.isClean()) {
                // Commit everything
                // TODO: Consider what to do with the commitId in the future
                AddCommand addCommand = git.add().addFilepattern(GIT_COMMIT_ALL_ITEMS);
                retryingRepositoryOperationFacade.call(addCommand);
                CommitCommand commitCommand = git.commit().setAll(true).setMessage(studioConfiguration.getProperty(REPO_BLUEPRINTS_UPDATED_COMMIT_MESSAGE));
                retryingRepositoryOperationFacade.call(commitCommand);
            }
        } catch (GitAPIException err) {
            logger.error("error creating initial commit for global configuration", err);
        }
    } catch (Exception e) {
        throw new UpgradeException("Error upgrading blueprints in the global repo", e);
    } finally {
        generalLockService.unlock(gitLockKey);
    }
}
Also used : Path(java.nio.file.Path) Status(org.eclipse.jgit.api.Status) InputStream(java.io.InputStream) FileVisitOption(java.nio.file.FileVisitOption) Manifest(java.util.jar.Manifest) StatusCommand(org.eclipse.jgit.api.StatusCommand) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) VersionInfo(org.craftercms.commons.monitoring.VersionInfo) Repository(org.eclipse.jgit.lib.Repository) Git(org.eclipse.jgit.api.Git) TreeCopier(org.craftercms.studio.impl.v1.repository.git.TreeCopier) CommitCommand(org.eclipse.jgit.api.CommitCommand) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) File(java.io.File) AddCommand(org.eclipse.jgit.api.AddCommand)

Example 3 with UpgradeException

use of org.craftercms.studio.api.v2.exception.UpgradeException in project studio by craftercms.

the class GlobalRepoUpgradeOperation method execute.

/**
 * {@inheritDoc}
 */
@Override
public void execute(final String site) throws UpgradeException {
    logger.debug("Upgrading global repo files");
    clusterGlobalRepoSyncTask.execute();
    for (Map.Entry<Resource, String> entry : files.entrySet()) {
        if (overwrite || !contentRepository.contentExists(site, entry.getValue())) {
            logger.debug("Upgrading global repo file: {0}", entry.getValue());
            try (InputStream is = entry.getKey().getInputStream()) {
                writeToRepo(site, entry.getValue(), is);
            } catch (IOException e) {
                throw new UpgradeException("Error while upgrading global repo file " + entry.getValue(), e);
            }
        } else {
            logger.debug("File {0} already exists in global repo, it will not be changed", entry.getValue());
        }
    }
}
Also used : UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) InputStream(java.io.InputStream) Resource(org.springframework.core.io.Resource) IOException(java.io.IOException) HashMap(java.util.HashMap) Map(java.util.Map)

Example 4 with UpgradeException

use of org.craftercms.studio.api.v2.exception.UpgradeException in project studio by craftercms.

the class AbstractPluginDescriptorUpgradeOperation method execute.

@Override
public void execute(final String site) throws UpgradeException {
    Path descriptorFile = getRepositoryPath(site).getParent().resolve(descriptorPath);
    if (Files.notExists(descriptorFile)) {
        logger.info("Plugin descriptor file not found for site {0}", site);
        return;
    }
    try (Reader reader = Files.newBufferedReader(descriptorFile)) {
        PluginDescriptor descriptor = descriptorReader.read(reader);
        if (descriptor.getDescriptorVersion().equals(descriptorVersion)) {
            logger.info("Plugin descriptor already update for site " + site);
            return;
        }
        logger.info("Updating plugin descriptor for site " + site);
        doPluginDescriptorUpdates(descriptor);
        descriptor.setDescriptorVersion(descriptorVersion);
        DumperOptions options = new DumperOptions();
        options.setDefaultFlowStyle(DumperOptions.FlowStyle.BLOCK);
        options.setPrettyFlow(true);
        Yaml yaml = new Yaml(new DisableClassLoadingConstructor(), new Representer() {

            @Override
            protected NodeTuple representJavaBeanProperty(final Object javaBean, final Property property, final Object propertyValue, final Tag customTag) {
                if (propertyValue != null) {
                    return super.representJavaBeanProperty(javaBean, property, propertyValue, customTag);
                }
                return null;
            }
        }, options);
        String content = yaml.dumpAsMap(descriptor);
        writeToRepo(site, descriptorPath, new ByteArrayInputStream(content.getBytes()));
        commitAllChanges(site);
    } catch (Exception e) {
        throw new UpgradeException("Plugin descriptor can't be read for site " + site);
    }
}
Also used : Path(java.nio.file.Path) PluginDescriptorReader(org.craftercms.commons.plugin.PluginDescriptorReader) Reader(java.io.Reader) Yaml(org.yaml.snakeyaml.Yaml) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) Representer(org.yaml.snakeyaml.representer.Representer) ByteArrayInputStream(java.io.ByteArrayInputStream) DumperOptions(org.yaml.snakeyaml.DumperOptions) Tag(org.yaml.snakeyaml.nodes.Tag) DisableClassLoadingConstructor(org.craftercms.commons.config.DisableClassLoadingConstructor) Property(org.yaml.snakeyaml.introspector.Property) NodeTuple(org.yaml.snakeyaml.nodes.NodeTuple)

Example 5 with UpgradeException

use of org.craftercms.studio.api.v2.exception.UpgradeException in project studio by craftercms.

the class AbstractContentTypeUpgradeOperation method shouldBeUpdated.

@Override
protected boolean shouldBeUpdated(final String site, final Path file) throws UpgradeException {
    logger.debug("Checking file {0} for site {1}", file, site);
    try {
        Document document = loadDocument(file);
        String contentTypeName = (String) select(document, contentTypeXpath, XPathConstants.STRING);
        if (CollectionUtils.isNotEmpty(includedContentTypes) && !includedContentTypes.contains(contentTypeName)) {
            logger.debug("File {0} of content-type {1} will not be updated", file, contentTypeName);
            return false;
        }
        if (StringUtils.isNotEmpty(formDefinitionXpath)) {
            Path formDefinition = getFormDefinition(site, contentTypeName);
            document = loadDocument(formDefinition);
            return (Boolean) select(document, formDefinitionXpath, XPathConstants.BOOLEAN);
        } else {
            return true;
        }
    } catch (ExecutionException e) {
        logger.error("Invalid XML file found at " + file, e);
        return false;
    } catch (Exception e) {
        throw new UpgradeException("Error parsing xml file " + file, e);
    }
}
Also used : Path(java.nio.file.Path) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) Document(org.w3c.dom.Document) ExecutionException(java.util.concurrent.ExecutionException) XPathExpressionException(javax.xml.xpath.XPathExpressionException) UpgradeException(org.craftercms.studio.api.v2.exception.UpgradeException) ExecutionException(java.util.concurrent.ExecutionException)

Aggregations

UpgradeException (org.craftercms.studio.api.v2.exception.UpgradeException)24 Path (java.nio.file.Path)10 IOException (java.io.IOException)8 SQLException (java.sql.SQLException)4 UpgradePipeline (org.craftercms.studio.api.v2.upgrade.UpgradePipeline)4 InputStream (java.io.InputStream)3 HashMap (java.util.HashMap)3 XPathExpressionException (javax.xml.xpath.XPathExpressionException)3 Git (org.eclipse.jgit.api.Git)3 File (java.io.File)2 Reader (java.io.Reader)2 Connection (java.sql.Connection)2 ExecutionException (java.util.concurrent.ExecutionException)2 Matcher (java.util.regex.Matcher)2 Transformer (javax.xml.transform.Transformer)2 StreamResult (javax.xml.transform.stream.StreamResult)2 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)2 CryptoException (org.craftercms.commons.crypto.CryptoException)2 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)2 UpgradeNotSupportedException (org.craftercms.studio.api.v2.exception.UpgradeNotSupportedException)2