Search in sources :

Example 1 with PluginDescriptor

use of org.craftercms.commons.plugin.model.PluginDescriptor in project commons by craftercms.

the class PluginDescriptorReaderImplTest method testParameters.

@Test
public void testParameters() throws IOException, PluginException {
    Parameter param = new Parameter();
    param.setLabel("Optional Key");
    param.setName("optionalKey");
    param.setRequired(false);
    param.setType(Parameter.Type.PASSWORD);
    try (InputStream is = descriptorParams.getInputStream()) {
        PluginDescriptor descriptor = reader.read(is);
        assertNull(descriptor.getBlueprint());
        assertNotNull(descriptor.getPlugin());
        Plugin plugin = descriptor.getPlugin();
        assertNotNull(plugin.getParameters());
        assertEquals(3, plugin.getParameters().size());
        assertEquals("AWS Access Key", plugin.getParameters().get(0).getLabel());
        assertEquals("awsAccessKey", plugin.getParameters().get(0).getName());
        assertEquals(param, plugin.getParameters().get(2));
    }
}
Also used : PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) InputStream(java.io.InputStream) Parameter(org.craftercms.commons.plugin.model.Parameter) Plugin(org.craftercms.commons.plugin.model.Plugin) Test(org.junit.Test)

Example 2 with PluginDescriptor

use of org.craftercms.commons.plugin.model.PluginDescriptor in project commons by craftercms.

the class PluginDescriptorReaderImplTest method testV1.

@Test
public void testV1() throws IOException, PluginException {
    try (InputStream is = descriptorV1.getInputStream()) {
        PluginDescriptor descriptor = reader.read(is);
        assertEquals("1", descriptor.getDescriptorVersion());
        assertNotNull(descriptor.getBlueprint());
        assertNull(descriptor.getPlugin());
        BlueprintDescriptor.Blueprint blueprint = descriptor.getBlueprint();
        assertEquals("Website Editorial Blueprint", blueprint.getName());
    }
}
Also used : PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) InputStream(java.io.InputStream) BlueprintDescriptor(org.craftercms.commons.plugin.model.BlueprintDescriptor) Test(org.junit.Test)

Example 3 with PluginDescriptor

use of org.craftercms.commons.plugin.model.PluginDescriptor in project studio by craftercms.

the class SitesServiceInternalImpl method getBlueprintLocation.

@Override
public String getBlueprintLocation(String blueprintId) {
    RepositoryItem[] blueprintsFolders = getBlueprintsFolders();
    for (RepositoryItem folder : blueprintsFolders) {
        if (folder.isFolder) {
            Path descriptorPath = getBlueprintPath(folder);
            PluginDescriptor descriptor = loadDescriptor(folder);
            if (descriptor != null && descriptor.getPlugin().getId().equals(blueprintId)) {
                return descriptorPath.getParent().toAbsolutePath().toString();
            }
        }
    }
    return StringUtils.EMPTY;
}
Also used : RepositoryItem(org.craftercms.studio.api.v1.repository.RepositoryItem) Path(java.nio.file.Path) PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor)

Example 4 with PluginDescriptor

use of org.craftercms.commons.plugin.model.PluginDescriptor 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 PluginDescriptor

use of org.craftercms.commons.plugin.model.PluginDescriptor in project studio by craftercms.

the class SiteServiceImpl method createSiteCloneRemote.

@SuppressWarnings("deprecation")
private void createSiteCloneRemote(String siteId, String sandboxBranch, String description, String remoteName, String remoteUrl, String remoteBranch, boolean singleBranch, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey, Map<String, String> params, boolean createAsOrphan) throws ServiceLayerException, InvalidRemoteRepositoryException, InvalidRemoteRepositoryCredentialsException, RemoteRepositoryNotFoundException, InvalidRemoteUrlException {
    boolean success = true;
    // We must fail site creation if any of the site creations steps fail and rollback
    // For example: Create site => create Deployer Target (fail) = fail
    // and rollback the whole thing.
    // What we need to do for site creation and the order of execution:
    // 1) git repo, 2) deployer target, 3) database, 4) kick deployer
    String siteUuid = UUID.randomUUID().toString();
    try {
        // create site by cloning remote git repo
        logger.info("Creating site " + siteId + " by cloning remote repository " + remoteName + " (" + remoteUrl + ")");
        success = contentRepositoryV2.createSiteCloneRemote(siteId, sandboxBranch, remoteName, remoteUrl, remoteBranch, singleBranch, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey, params, createAsOrphan);
    } catch (InvalidRemoteRepositoryException | InvalidRemoteRepositoryCredentialsException | RemoteRepositoryNotFoundException | InvalidRemoteUrlException | ServiceLayerException e) {
        contentRepository.deleteSite(siteId);
        logger.error("Error while creating site: " + siteId + " ID: " + siteId + " as clone from " + "remote repository: " + remoteName + " (" + remoteUrl + "). Rolling back.", e);
        throw e;
    }
    if (!success) {
        contentRepository.removeRemoteRepositoriesForSite(siteId);
        contentRepository.deleteSite(siteId);
        throw new ServiceLayerException("Failed to create site: " + siteId + " ID: " + siteId + " as clone from " + "remote repository: " + remoteName + " (" + remoteUrl + ")");
    }
    // try to get the search engine from the blueprint descriptor file
    String searchEngine = studioConfiguration.getProperty(StudioConfiguration.PREVIEW_SEARCH_ENGINE);
    PluginDescriptor descriptor = sitesServiceInternal.getSiteBlueprintDescriptor(siteId);
    if (Objects.nonNull(descriptor) && Objects.nonNull(descriptor.getPlugin())) {
        searchEngine = descriptor.getPlugin().getSearchEngine();
        logger.info("Using search engine {0} from plugin descriptor", searchEngine);
    } else if (Objects.nonNull(descriptor) && Objects.nonNull(descriptor.getBlueprint())) {
        searchEngine = descriptor.getBlueprint().getSearchEngine();
        logger.info("Using search engine {0} from blueprint descriptor", searchEngine);
    } else {
        logger.info("Missing descriptor, using default search engine {0}", searchEngine);
    }
    if (success) {
        // Create the site in the preview deployer
        try {
            logger.info("Creating Deployer targets for site " + siteId);
            deployer.createTargets(siteId, searchEngine);
        } catch (Exception e) {
            logger.error("Error while creating site: " + siteId + " ID: " + siteId + " as clone from" + " remote repository: " + remoteName + " (" + remoteUrl + "). Rolling back...", e);
            contentRepositoryV2.removeRemote(siteId, remoteName);
            boolean deleted = contentRepository.deleteSite(siteId);
            if (!deleted) {
                logger.error("Error while rolling back site: " + siteId);
            }
            throw new DeployerTargetException("Error while creating site: " + siteId + " ID: " + siteId + " as clone from remote repository: " + remoteName + " (" + remoteUrl + "). The required Deployer targets couldn't " + "be created", e);
        }
    }
    if (success) {
        ZonedDateTime now = ZonedDateTime.now();
        String creator = securityService.getCurrentUser();
        try {
            logger.debug("Adding site UUID.");
            addSiteUuidFile(siteId, siteUuid);
            // insert database records
            logger.info("Adding site record to database for site " + siteId);
            SiteFeed siteFeed = new SiteFeed();
            siteFeed.setName(siteId);
            siteFeed.setSiteId(siteId);
            siteFeed.setSiteUuid(siteUuid);
            siteFeed.setDescription(description);
            siteFeed.setPublishingStatus(READY);
            siteFeed.setPublishingStatusMessage(studioConfiguration.getProperty(JOB_DEPLOY_CONTENT_TO_ENVIRONMENT_STATUS_MESSAGE_DEFAULT));
            siteFeed.setSandboxBranch(sandboxBranch);
            siteFeed.setSearchEngine(searchEngine);
            siteFeedMapper.createSite(siteFeed);
            upgradeManager.upgradeSite(siteId);
            // Add default groups
            logger.info("Adding default groups for site " + siteId);
            addDefaultGroupsForNewSite(siteId);
            String lastCommitId = contentRepositoryV2.getRepoLastCommitId(siteId);
            String firstCommitId = contentRepositoryV2.getRepoFirstCommitId(siteId);
            long startGetChangeSetCreatedFilesMark = logger.isDebugEnabled() ? System.currentTimeMillis() : 0;
            Map<String, String> createdFiles = contentRepositoryV2.getChangeSetPathsFromDelta(siteId, null, lastCommitId);
            if (logger.isDebugEnabled()) {
                logger.debug("Get change set created files finished in " + (System.currentTimeMillis() - startGetChangeSetCreatedFilesMark) + " milliseconds");
            }
            insertCreateSiteAuditLog(siteId, siteId, createdFiles);
            contentRepositoryV2.insertGitLog(siteId, firstCommitId, 1, 1);
            processCreatedFiles(siteId, createdFiles, creator, now, lastCommitId);
            updateLastCommitId(siteId, lastCommitId);
            updateLastVerifiedGitlogCommitId(siteId, lastCommitId);
            updateLastSyncedGitlogCommitId(siteId, firstCommitId);
            logger.info("Loading configuration for site " + siteId);
            reloadSiteConfiguration(siteId);
        } catch (Exception e) {
            success = false;
            logger.error("Error while creating site: " + siteId + " ID: " + siteId + " as clone from " + "remote repository: " + remoteName + " (" + remoteUrl + "). Rolling back.", e);
            deleteSite(siteId);
            throw new SiteCreationException("Error while creating site: " + siteId + " ID: " + siteId + " as clone from remote repository: " + remoteName + " (" + remoteUrl + "). Rolling back.");
        }
    }
    if (success) {
        // Now that everything is created, we can sync the preview deployer with the new content
        logger.info("Sync all site content to preview for " + siteId);
        try {
            deploymentService.syncAllContentToPreview(siteId, true);
        } catch (ServiceLayerException e) {
            logger.error("Error while syncing site: " + siteId + " ID: " + siteId + " to preview. Site was " + "successfully created otherwise. Ignoring.", e);
            throw new SiteCreationException("Error while syncing site: " + siteId + " ID: " + siteId + " to preview. Site was successfully created, but it won't be preview-able until the " + "Preview Deployer is reachable.");
        }
        setSiteState(siteId, STATE_CREATED);
    } else {
        throw new SiteCreationException("Error while creating site: " + siteId + " ID: " + siteId + ".");
    }
    logger.info("Finished creating site " + siteId);
}
Also used : InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) DeployerTargetException(org.craftercms.studio.api.v1.exception.DeployerTargetException) InvalidRemoteRepositoryException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) SiteConfigNotFoundException(org.craftercms.studio.api.v1.service.site.SiteConfigNotFoundException) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException) MissingPluginParameterException(org.craftercms.studio.api.v2.exception.MissingPluginParameterException) IOException(java.io.IOException) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) UserNotFoundException(org.craftercms.studio.api.v1.exception.security.UserNotFoundException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) DeployerTargetException(org.craftercms.studio.api.v1.exception.DeployerTargetException) RestServiceException(org.craftercms.commons.rest.RestServiceException) SiteAlreadyExistsException(org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException) GroupAlreadyExistsException(org.craftercms.studio.api.v1.exception.security.GroupAlreadyExistsException) DocumentException(org.dom4j.DocumentException) EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) BlueprintNotFoundException(org.craftercms.studio.api.v1.exception.BlueprintNotFoundException) SiteNotFoundException(org.craftercms.studio.api.v1.exception.SiteNotFoundException) CryptoException(org.craftercms.commons.crypto.CryptoException) RemoteRepositoryNotBareException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotBareException) InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) InvalidRemoteRepositoryException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException) PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) ZonedDateTime(java.time.ZonedDateTime) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException)

Aggregations

PluginDescriptor (org.craftercms.commons.plugin.model.PluginDescriptor)11 ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)4 InvalidRemoteRepositoryCredentialsException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException)4 InvalidRemoteRepositoryException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryException)4 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)4 RemoteRepositoryNotBareException (org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotBareException)4 RemoteRepositoryNotFoundException (org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException)4 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 ZonedDateTime (java.time.ZonedDateTime)3 CryptoException (org.craftercms.commons.crypto.CryptoException)3 EntitlementException (org.craftercms.commons.entitlements.exception.EntitlementException)3 RestServiceException (org.craftercms.commons.rest.RestServiceException)3 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)3 BlueprintNotFoundException (org.craftercms.studio.api.v1.exception.BlueprintNotFoundException)3 DeployerTargetException (org.craftercms.studio.api.v1.exception.DeployerTargetException)3 SiteAlreadyExistsException (org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException)3 SiteCreationException (org.craftercms.studio.api.v1.exception.SiteCreationException)3 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)3 GroupAlreadyExistsException (org.craftercms.studio.api.v1.exception.security.GroupAlreadyExistsException)3