Search in sources :

Example 6 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException in project studio by craftercms.

the class GitContentRepository method pushToRemote.

@Override
public boolean pushToRemote(String siteId, String remoteName, String remoteBranch) throws ServiceLayerException, InvalidRemoteUrlException, CryptoException {
    logger.debug("Get remote data from database for remote " + remoteName + " and site " + siteId);
    Map<String, String> params = new HashMap<String, String>();
    params.put("siteId", siteId);
    params.put("remoteName", remoteName);
    RemoteRepository remoteRepository = remoteRepositoryDAO.getRemoteRepository(params);
    logger.debug("Prepare push command.");
    GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
    Repository repo = helper.getRepository(siteId, SANDBOX);
    try (Git git = new Git(repo)) {
        Iterable<PushResult> pushResultIterable = null;
        PushCommand pushCommand = git.push();
        logger.debug("Set remote " + remoteName);
        pushCommand.setRemote(remoteRepository.getRemoteName());
        logger.debug("Set branch to be " + remoteBranch);
        RefSpec r = new RefSpec();
        r = r.setSourceDestination(Constants.R_HEADS + repo.getBranch(), Constants.R_HEADS + remoteBranch);
        pushCommand.setRefSpecs(r);
        switch(remoteRepository.getAuthenticationType()) {
            case NONE:
                logger.debug("No authentication");
                pushResultIterable = pushCommand.call();
                break;
            case BASIC:
                logger.debug("Basic authentication");
                String hashedPassword = remoteRepository.getRemotePassword();
                String password = encryptor.decrypt(hashedPassword);
                pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteRepository.getRemoteUsername(), password));
                pushResultIterable = pushCommand.call();
                break;
            case TOKEN:
                logger.debug("Token based authentication");
                String hashedToken = remoteRepository.getRemoteToken();
                String remoteToken = encryptor.decrypt(hashedToken);
                pushCommand.setCredentialsProvider(new UsernamePasswordCredentialsProvider(remoteToken, EMPTY));
                pushResultIterable = pushCommand.call();
                break;
            case PRIVATE_KEY:
                logger.debug("Private key authentication");
                final Path tempKey = Files.createTempFile(UUID.randomUUID().toString(), ".tmp");
                String hashedPrivateKey = remoteRepository.getRemotePrivateKey();
                String privateKey = encryptor.decrypt(hashedPrivateKey);
                tempKey.toFile().deleteOnExit();
                pushCommand.setTransportConfigCallback(new TransportConfigCallback() {

                    @Override
                    public void configure(Transport transport) {
                        SshTransport sshTransport = (SshTransport) transport;
                        sshTransport.setSshSessionFactory(getSshSessionFactory(privateKey, tempKey));
                    }
                });
                pushResultIterable = retryingRepositoryOperationFacade.call(pushCommand);
                Files.delete(tempKey);
                break;
            default:
                throw new ServiceLayerException("Unsupported authentication type " + remoteRepository.getAuthenticationType());
        }
        boolean toRet = true;
        List<RemoteRefUpdate.Status> failure = Arrays.asList(REJECTED_NODELETE, REJECTED_NONFASTFORWARD, REJECTED_REMOTE_CHANGED, REJECTED_OTHER_REASON);
        for (PushResult pushResult : pushResultIterable) {
            Collection<RemoteRefUpdate> updates = pushResult.getRemoteUpdates();
            for (RemoteRefUpdate remoteRefUpdate : updates) {
                toRet = toRet && !failure.contains(remoteRefUpdate.getStatus());
                if (!toRet)
                    break;
            }
            if (!toRet)
                break;
        }
        return toRet;
    } catch (InvalidRemoteException e) {
        logger.error("Remote is invalid " + remoteName, e);
        throw new InvalidRemoteUrlException();
    } catch (IOException | JGitInternalException | GitAPIException | CryptoException e) {
        logger.error("Error while pushing to remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
        throw new ServiceLayerException("Error while pushing to remote " + remoteName + " branch " + remoteBranch + " for site " + siteId, e);
    }
}
Also used : HashMap(java.util.HashMap) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) RefSpec(org.eclipse.jgit.transport.RefSpec) TransportConfigCallback(org.eclipse.jgit.api.TransportConfigCallback) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) Path(java.nio.file.Path) Status(org.eclipse.jgit.api.Status) RemoteRefUpdate(org.eclipse.jgit.transport.RemoteRefUpdate) UsernamePasswordCredentialsProvider(org.eclipse.jgit.transport.UsernamePasswordCredentialsProvider) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) PushResult(org.eclipse.jgit.transport.PushResult) IOException(java.io.IOException) PushCommand(org.eclipse.jgit.api.PushCommand) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Git(org.eclipse.jgit.api.Git) InvalidRemoteException(org.eclipse.jgit.api.errors.InvalidRemoteException) JGitInternalException(org.eclipse.jgit.api.errors.JGitInternalException) SshTransport(org.eclipse.jgit.transport.SshTransport) Transport(org.eclipse.jgit.transport.Transport) CryptoException(org.craftercms.commons.crypto.CryptoException) SshTransport(org.eclipse.jgit.transport.SshTransport)

Example 7 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException in project studio by craftercms.

the class GitContentRepository method addRemote.

@Override
public boolean addRemote(String siteId, String remoteName, String remoteUrl, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey) throws InvalidRemoteUrlException, ServiceLayerException {
    boolean isValid = false;
    try {
        logger.debug("Add remote " + remoteName + " to the sandbox repo for the site " + siteId);
        GitRepositoryHelper helper = GitRepositoryHelper.getHelper(studioConfiguration, securityService, userServiceInternal, encryptor, generalLockService, retryingRepositoryOperationFacade);
        Repository repo = helper.getRepository(siteId, SANDBOX);
        try (Git git = new Git(repo)) {
            Config storedConfig = repo.getConfig();
            Set<String> remotes = storedConfig.getSubsections("remote");
            if (remotes.contains(remoteName)) {
                throw new RemoteAlreadyExistsException(remoteName);
            }
            RemoteAddCommand remoteAddCommand = git.remoteAdd();
            remoteAddCommand.setName(remoteName);
            remoteAddCommand.setUri(new URIish(remoteUrl));
            retryingRepositoryOperationFacade.call(remoteAddCommand);
            try {
                isValid = isRemoteValid(git, remoteName, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey);
            } finally {
                if (!isValid) {
                    RemoteRemoveCommand remoteRemoveCommand = git.remoteRemove();
                    remoteRemoveCommand.setRemoteName(remoteName);
                    retryingRepositoryOperationFacade.call(remoteRemoveCommand);
                    List<Ref> resultRemoteBranches = git.branchList().setListMode(ListBranchCommand.ListMode.REMOTE).call();
                    List<String> branchesToDelete = new ArrayList<String>();
                    for (Ref remoteBranchRef : resultRemoteBranches) {
                        if (remoteBranchRef.getName().startsWith(Constants.R_REMOTES + remoteName)) {
                            branchesToDelete.add(remoteBranchRef.getName());
                        }
                    }
                    if (CollectionUtils.isNotEmpty(branchesToDelete)) {
                        DeleteBranchCommand delBranch = git.branchDelete();
                        String[] array = new String[branchesToDelete.size()];
                        delBranch.setBranchNames(branchesToDelete.toArray(array));
                        delBranch.setForce(true);
                        retryingRepositoryOperationFacade.call(delBranch);
                    }
                }
            }
        } catch (URISyntaxException | ClassCastException e) {
            logger.error("Remote URL is invalid " + remoteUrl, e);
            throw new InvalidRemoteUrlException("Remote URL is invalid " + remoteUrl, e);
        } catch (GitAPIException | IOException e) {
            logger.error("Error while adding remote " + remoteName + " (url: " + remoteUrl + ") for site " + siteId, e);
            throw new ServiceLayerException("Error while adding remote " + remoteName + " (url: " + remoteUrl + ") for site " + siteId, e);
        }
        if (isValid) {
            insertRemoteToDb(siteId, remoteName, remoteUrl, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey);
        }
    } catch (CryptoException e) {
        throw new ServiceLayerException(e);
    }
    return isValid;
}
Also used : URIish(org.eclipse.jgit.transport.URIish) RemoteConfig(org.eclipse.jgit.transport.RemoteConfig) Config(org.eclipse.jgit.lib.Config) OpenSshConfig(org.eclipse.jgit.transport.OpenSshConfig) ServicesConfig(org.craftercms.studio.api.v1.service.configuration.ServicesConfig) RemoteRemoveCommand(org.eclipse.jgit.api.RemoteRemoveCommand) RemoteAddCommand(org.eclipse.jgit.api.RemoteAddCommand) ArrayList(java.util.ArrayList) URISyntaxException(java.net.URISyntaxException) InvalidRemoteUrlException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException) GitAPIException(org.eclipse.jgit.api.errors.GitAPIException) GitRepositoryHelper(org.craftercms.studio.api.v2.utils.GitRepositoryHelper) DeleteBranchCommand(org.eclipse.jgit.api.DeleteBranchCommand) ServiceLayerException(org.craftercms.studio.api.v1.exception.ServiceLayerException) IOException(java.io.IOException) RemoteAlreadyExistsException(org.craftercms.studio.api.v1.exception.repository.RemoteAlreadyExistsException) RemoteRepository(org.craftercms.studio.api.v2.dal.RemoteRepository) Repository(org.eclipse.jgit.lib.Repository) ContentRepository(org.craftercms.studio.api.v1.repository.ContentRepository) Ref(org.eclipse.jgit.lib.Ref) Git(org.eclipse.jgit.api.Git) CryptoException(org.craftercms.commons.crypto.CryptoException)

Example 8 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException in project studio by craftercms.

the class SiteServiceImpl method createSiteWithRemoteOption.

@Override
@ValidateParams
public void createSiteWithRemoteOption(@ValidateStringParam(name = "siteId", maxLength = 50, whitelistedPatterns = "[a-z0-9\\-]*") String siteId, @ValidateStringParam(name = "sandboxBranch") String sandboxBranch, @ValidateNoTagsParam(name = "description") String description, String blueprintName, @ValidateStringParam(name = "remoteName") String remoteName, @ValidateStringParam(name = "remoteUrl") String remoteUrl, String remoteBranch, boolean singleBranch, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey, @ValidateStringParam(name = "createOption") String createOption, Map<String, String> params, boolean createAsOrphan) throws ServiceLayerException, InvalidRemoteRepositoryException, InvalidRemoteRepositoryCredentialsException, RemoteRepositoryNotFoundException, RemoteRepositoryNotBareException, InvalidRemoteUrlException {
    if (exists(siteId)) {
        throw new SiteAlreadyExistsException();
    }
    logger.debug("Validate site entitlements");
    try {
        entitlementValidator.validateEntitlement(EntitlementType.SITE, 1);
    } catch (EntitlementException e) {
        throw new SiteCreationException("Unable to complete request due to entitlement limits. Please contact your " + "system administrator.", e);
    }
    switch(createOption) {
        case REMOTE_REPOSITORY_CREATE_OPTION_CLONE:
            logger.info("Clone from remote repository create option selected");
            createSiteCloneRemote(siteId, sandboxBranch, description, remoteName, remoteUrl, remoteBranch, singleBranch, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey, params, createAsOrphan);
            break;
        case REMOTE_REPOSITORY_CREATE_OPTION_PUSH:
            logger.info("Push to remote repository create option selected");
            createSitePushToRemote(siteId, sandboxBranch, description, blueprintName, remoteName, remoteUrl, remoteBranch, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey, params, createAsOrphan);
            break;
        default:
            logger.error("Invalid create option for create site using remote repository: " + createOption + "\nAvailable options: [" + REMOTE_REPOSITORY_CREATE_OPTION_CLONE + ", " + REMOTE_REPOSITORY_CREATE_OPTION_PUSH + "]");
            break;
    }
}
Also used : EntitlementException(org.craftercms.commons.entitlements.exception.EntitlementException) SiteAlreadyExistsException(org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException) ValidateParams(org.craftercms.commons.validation.annotations.param.ValidateParams)

Example 9 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException 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)

Example 10 with InvalidRemoteUrlException

use of org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException in project studio by craftercms.

the class SiteServiceImpl method createSitePushToRemote.

private void createSitePushToRemote(String siteId, String sandboxBranch, String description, String blueprintId, String remoteName, String remoteUrl, String remoteBranch, String authenticationType, String remoteUsername, String remotePassword, String remoteToken, String remotePrivateKey, Map<String, String> params, boolean createAsOrphan) throws ServiceLayerException {
    if (exists(siteId)) {
        throw new SiteAlreadyExistsException();
    }
    logger.debug("Get blueprint descriptor for " + blueprintId);
    PluginDescriptor descriptor = sitesServiceInternal.getBlueprintDescriptor(blueprintId);
    if (Objects.isNull(descriptor)) {
        throw new BlueprintNotFoundException();
    }
    logger.debug("Validate blueprint parameters");
    sitesServiceInternal.validateBlueprintParameters(descriptor, params);
    String blueprintLocation = sitesServiceInternal.getBlueprintLocation(blueprintId);
    String searchEngine = descriptor.getPlugin().getSearchEngine();
    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) deployer target, 2) git repo, 3) database, 4) kick deployer
    String siteUuid = UUID.randomUUID().toString();
    logger.info("Starting site creation process for site " + siteId + " from " + blueprintId + " blueprint.");
    // Create the site in the preview deployer
    try {
        logger.info("Creating Deployer targets for site " + siteId);
        deployer.createTargets(siteId, searchEngine);
    } catch (RestServiceException e) {
        String msg = "Error while creating site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId + ". The required Deployer targets couldn't be created";
        logger.error(msg, e);
        throw new DeployerTargetException(msg, e);
    }
    if (success) {
        try {
            logger.info("Creating site " + siteId + " from blueprint " + blueprintId);
            success = createSiteFromBlueprintGit(blueprintLocation, siteId, siteId, sandboxBranch, description, params);
            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);
            logger.info("Upgrading site");
            upgradeManager.upgradeSite(siteId);
        } catch (Exception e) {
            success = false;
            logger.error("Error while creating site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId + ". Rolling back...", e);
            contentRepository.deleteSite(siteId);
            try {
                deployer.deleteTargets(siteId);
            } catch (Exception ex) {
                logger.error("Error while rolling back/deleting site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId + ". This means the site's Deployer " + "targets are still present, but the site was not successfully created", e);
            }
            throw new SiteCreationException("Error while creating site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId, e);
        }
        if (success) {
            ZonedDateTime now = ZonedDateTime.now();
            String creator = securityService.getCurrentUser();
            try {
                logger.info("Pushing site " + siteId + " to remote repository " + remoteName + " (" + remoteUrl + ")");
                contentRepository.addRemote(siteId, remoteName, remoteUrl, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey);
                contentRepository.createSitePushToRemote(siteId, remoteName, remoteUrl, authenticationType, remoteUsername, remotePassword, remoteToken, remotePrivateKey, createAsOrphan);
            } catch (RemoteRepositoryNotFoundException | InvalidRemoteRepositoryException | InvalidRemoteRepositoryCredentialsException | RemoteRepositoryNotBareException | InvalidRemoteUrlException | ServiceLayerException e) {
                logger.error("Error while pushing site: " + siteId + " ID: " + siteId + " to remote repository " + remoteName + " (" + remoteUrl + ")", e);
                contentRepositoryV2.removeRemote(siteId, remoteName);
            }
            try {
                // Add default groups
                logger.info("Adding default groups for site " + siteId);
                addDefaultGroupsForNewSite(siteId);
                logger.debug("Adding audit logs.");
                String lastCommitId = contentRepositoryV2.getRepoLastCommitId(siteId);
                Map<String, String> createdFiles = contentRepositoryV2.getChangeSetPathsFromDelta(siteId, null, lastCommitId);
                insertCreateSiteAuditLog(siteId, siteId, createdFiles);
                insertAddRemoteAuditLog(siteId, remoteName);
                processCreatedFiles(siteId, createdFiles, creator, now, lastCommitId);
                contentRepositoryV2.insertGitLog(siteId, lastCommitId, 1, 1);
                updateLastCommitId(siteId, lastCommitId);
                updateLastVerifiedGitlogCommitId(siteId, lastCommitId);
                updateLastSyncedGitlogCommitId(siteId, lastCommitId);
                logger.info("Loading configuration for site " + siteId);
                reloadSiteConfiguration(siteId);
            } catch (Exception e) {
                success = false;
                logger.error("Error while creating site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId + ". Rolling back.", e);
                deleteSite(siteId);
                throw new SiteCreationException("Error while creating site: " + siteId + " ID: " + siteId + " from blueprint: " + blueprintId + ". Rolling back.");
            }
        }
    }
    if (success) {
        // Now that everything is created, we can sync the preview deployer with the new content
        logger.info("Sync all content to preview for site " + 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 : BlueprintNotFoundException(org.craftercms.studio.api.v1.exception.BlueprintNotFoundException) InvalidRemoteRepositoryCredentialsException(org.craftercms.studio.api.v1.exception.repository.InvalidRemoteRepositoryCredentialsException) DeployerTargetException(org.craftercms.studio.api.v1.exception.DeployerTargetException) SiteAlreadyExistsException(org.craftercms.studio.api.v1.exception.SiteAlreadyExistsException) 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) RestServiceException(org.craftercms.commons.rest.RestServiceException) PluginDescriptor(org.craftercms.commons.plugin.model.PluginDescriptor) ZonedDateTime(java.time.ZonedDateTime) SiteFeed(org.craftercms.studio.api.v1.dal.SiteFeed) RemoteRepositoryNotFoundException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotFoundException) SiteCreationException(org.craftercms.studio.api.v1.exception.SiteCreationException) RemoteRepositoryNotBareException(org.craftercms.studio.api.v1.exception.repository.RemoteRepositoryNotBareException)

Aggregations

ServiceLayerException (org.craftercms.studio.api.v1.exception.ServiceLayerException)15 InvalidRemoteUrlException (org.craftercms.studio.api.v1.exception.repository.InvalidRemoteUrlException)15 ContentRepository (org.craftercms.studio.api.v1.repository.ContentRepository)12 Git (org.eclipse.jgit.api.Git)12 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)12 Repository (org.eclipse.jgit.lib.Repository)12 IOException (java.io.IOException)11 CryptoException (org.craftercms.commons.crypto.CryptoException)10 RemoteRepository (org.craftercms.studio.api.v2.dal.RemoteRepository)10 SiteNotFoundException (org.craftercms.studio.api.v1.exception.SiteNotFoundException)9 URISyntaxException (java.net.URISyntaxException)8 RemoteAddCommand (org.eclipse.jgit.api.RemoteAddCommand)8 URIish (org.eclipse.jgit.transport.URIish)8 GitRepositoryHelper (org.craftercms.studio.api.v2.utils.GitRepositoryHelper)7 Config (org.eclipse.jgit.lib.Config)7 Path (java.nio.file.Path)6 SiteFeed (org.craftercms.studio.api.v1.dal.SiteFeed)6 InvalidRemoteException (org.eclipse.jgit.api.errors.InvalidRemoteException)5 HashMap (java.util.HashMap)4 PullCommand (org.eclipse.jgit.api.PullCommand)4