Search in sources :

Example 1 with DeployerException

use of org.craftercms.deployer.api.exceptions.DeployerException in project deployer by craftercms.

the class FindAndReplaceProcessor method doMainProcess.

/**
 * {@inheritDoc}
 */
@Override
protected ChangeSet doMainProcess(Deployment deployment, ProcessorExecution execution, ChangeSet filteredChangeSet, ChangeSet originalChangeSet) throws DeployerException {
    logger.info("Performing find & replace. Pattern '{}' will be replaced with '{}'...", textPattern, replacement);
    for (String file : ListUtils.union(filteredChangeSet.getCreatedFiles(), filteredChangeSet.getUpdatedFiles())) {
        try {
            Path path = Paths.get(localRepoUrl, file);
            String content = new String(Files.readAllBytes(path));
            String updated = content.replaceAll(textPattern, replacement);
            if (StringUtils.equals(content, updated)) {
                logger.debug("No matches found for file {}", file);
            } else {
                logger.debug("Writing changes to file {}", file);
                Files.write(path, updated.getBytes());
            }
        } catch (Exception e) {
            throw new DeployerException("Error performing find and replace on file " + file, e);
        }
    }
    return null;
}
Also used : Path(java.nio.file.Path) DeployerException(org.craftercms.deployer.api.exceptions.DeployerException) DeployerException(org.craftercms.deployer.api.exceptions.DeployerException) ConfigurationException(org.craftercms.commons.config.ConfigurationException)

Example 2 with DeployerException

use of org.craftercms.deployer.api.exceptions.DeployerException in project deployer by craftercms.

the class HttpMethodCallProcessor method doMainProcess.

@Override
protected ChangeSet doMainProcess(Deployment deployment, ProcessorExecution execution, ChangeSet filteredChangeSet, ChangeSet originalChangeSet) throws DeployerException {
    HttpUriRequest request = createRequest();
    logger.info("Executing request {}...", request);
    try (CloseableHttpResponse response = httpClient.execute(request)) {
        int status = response.getStatusLine().getStatusCode();
        HttpEntity entity = response.getEntity();
        String body = entity != null ? EntityUtils.toString(entity) : null;
        if (StringUtils.isEmpty(body)) {
            body = "empty";
        }
        if (status >= 200 && status < 300) {
            logger.info("Successful response for request {}: status = {}, body = {}", request, status, body);
            execution.setStatusDetails("Successful response for request " + request + ": status = " + status);
        } else {
            logger.error("Error response for request {}: status = {}, body = {}", request, status, body);
            execution.setStatusDetails("Error response for request " + request + ": status = " + status);
            execution.endExecution(Deployment.Status.FAILURE);
        }
    } catch (IOException e) {
        throw new DeployerException("IO error on HTTP request " + request, e);
    }
    return null;
}
Also used : HttpEntity(org.apache.http.HttpEntity) DeployerException(org.craftercms.deployer.api.exceptions.DeployerException) IOException(java.io.IOException)

Example 3 with DeployerException

use of org.craftercms.deployer.api.exceptions.DeployerException in project deployer by craftercms.

the class ScriptProcessor method doMainProcess.

@Override
protected ChangeSet doMainProcess(Deployment deployment, ProcessorExecution execution, ChangeSet filteredChangeSet, ChangeSet originalChangeSet) throws DeployerException {
    if (sandboxInterceptor != null) {
        sandboxInterceptor.register();
    }
    try {
        Binding binding = new Binding();
        binding.setVariable(SCRIPT_VAR_LOGGER, logger);
        binding.setVariable(SCRIPT_VAR_APP_CTX, applicationContext);
        binding.setVariable(SCRIPT_VAR_DEPLOYMENT, deployment);
        binding.setVariable(SCRIPT_VAR_EXECUTION, execution);
        binding.setVariable(SCRIPT_VAR_FILTERED_CHANGE_SET, filteredChangeSet);
        binding.setVariable(SCRIPT_VAR_ORIGINAL_CHANGE_SET, originalChangeSet);
        logger.info("Starting execution of script {}", scriptPath);
        Object result = scriptEngine.run(scriptPath, binding);
        logger.info("Completed execution of script {}", scriptPath);
        if (result != null && !ChangeSet.class.isAssignableFrom(result.getClass())) {
            throw new DeployerException("Incompatible type " + result.getClass().getName() + " returned by script " + scriptPath);
        }
        return (ChangeSet) result;
    } catch (Throwable e) {
        throw new DeployerException("Error executing script " + scriptPath, e);
    } finally {
        if (sandboxInterceptor != null) {
            sandboxInterceptor.unregister();
        }
    }
}
Also used : Binding(groovy.lang.Binding) DeployerException(org.craftercms.deployer.api.exceptions.DeployerException) ChangeSet(org.craftercms.deployer.api.ChangeSet)

Example 4 with DeployerException

use of org.craftercms.deployer.api.exceptions.DeployerException in project deployer by craftercms.

the class CloudFrontInvalidationProcessor method doMainProcess.

/**
 * {@inheritDoc}
 */
@Override
@SuppressWarnings("unchecked")
protected ChangeSet doMainProcess(Deployment deployment, ProcessorExecution execution, ChangeSet filteredChangeSet, ChangeSet originalChangeSet) throws DeployerException {
    logger.info("Performing Cloudfront invalidation...");
    AmazonCloudFront client = buildClient();
    List<String> changedFiles = ListUtils.union(filteredChangeSet.getUpdatedFiles(), filteredChangeSet.getDeletedFiles());
    if (CollectionUtils.isNotEmpty(changedFiles)) {
        changedFiles = changedFiles.stream().map(f -> UriUtils.encodePath(f, StandardCharsets.UTF_8)).collect(Collectors.toList());
        Paths paths = new Paths().withItems(changedFiles).withQuantity(changedFiles.size());
        logger.info("Will invalidate {} files", changedFiles.size());
        for (String distribution : distributions) {
            try {
                String caller = UUID.randomUUID().toString();
                logger.info("Creating invalidation for distribution {} with reference {}", distribution, caller);
                InvalidationBatch batch = new InvalidationBatch().withPaths(paths).withCallerReference(caller);
                CreateInvalidationRequest request = new CreateInvalidationRequest(distribution, batch);
                CreateInvalidationResult result = client.createInvalidation(request);
                logger.info("Created invalidation {} for distribution {}", result.getInvalidation().getId(), distribution);
            } catch (Exception e) {
                throw new DeployerException("Error invalidating changed files for distribution " + distribution, e);
            }
        }
    } else {
        logger.info("No actual files that need to be invalidated");
    }
    return null;
}
Also used : InvalidationBatch(com.amazonaws.services.cloudfront.model.InvalidationBatch) CreateInvalidationResult(com.amazonaws.services.cloudfront.model.CreateInvalidationResult) DeployerException(org.craftercms.deployer.api.exceptions.DeployerException) AmazonCloudFront(com.amazonaws.services.cloudfront.AmazonCloudFront) Paths(com.amazonaws.services.cloudfront.model.Paths) CreateInvalidationRequest(com.amazonaws.services.cloudfront.model.CreateInvalidationRequest) DeployerException(org.craftercms.deployer.api.exceptions.DeployerException) ConfigurationException(org.craftercms.commons.config.ConfigurationException)

Example 5 with DeployerException

use of org.craftercms.deployer.api.exceptions.DeployerException in project deployer by craftercms.

the class CommandLineProcessor method executeProcess.

protected void executeProcess(String site, String operation, String file) throws DeployerException {
    List<String> fullCommand = new LinkedList<>();
    Collections.addAll(fullCommand, command.split("\\s"));
    if (isNoneEmpty(site, operation, file)) {
        fullCommand.add(site);
        fullCommand.add(operation);
        fullCommand.add(file);
    }
    ProcessBuilder processBuilder = new ProcessBuilder(fullCommand);
    if (StringUtils.isNotEmpty(workingDir)) {
        processBuilder.directory(new File(workingDir));
    }
    processBuilder.redirectErrorStream(true);
    logger.info("Executing command: {}", command);
    try {
        Process process = processBuilder.start();
        process.waitFor(processTimeoutSecs, TimeUnit.SECONDS);
        try (BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()))) {
            String str;
            while ((str = reader.readLine()) != null) {
                logger.info("PROCESS OUTPUT: {}", str);
            }
        }
        logger.info("Process finished with exit code {}", process.exitValue());
    } catch (IOException | InterruptedException e) {
        throw new DeployerException("Error while executing command", e);
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) BufferedReader(java.io.BufferedReader) DeployerException(org.craftercms.deployer.api.exceptions.DeployerException) IOException(java.io.IOException) File(java.io.File) LinkedList(java.util.LinkedList)

Aggregations

DeployerException (org.craftercms.deployer.api.exceptions.DeployerException)31 ConfigurationException (org.craftercms.commons.config.ConfigurationException)12 IOException (java.io.IOException)11 File (java.io.File)6 AmazonS3 (com.amazonaws.services.s3.AmazonS3)4 GitAPIException (org.eclipse.jgit.api.errors.GitAPIException)4 AmazonS3Exception (com.amazonaws.services.s3.model.AmazonS3Exception)3 DeleteObjectsRequest (com.amazonaws.services.s3.model.DeleteObjectsRequest)3 Path (java.nio.file.Path)3 ArrayList (java.util.ArrayList)3 ChangeSet (org.craftercms.deployer.api.ChangeSet)3 Stack (com.amazonaws.services.cloudformation.model.Stack)2 DeleteObjectsResult (com.amazonaws.services.s3.model.DeleteObjectsResult)2 UnknownHostException (java.net.UnknownHostException)2 List (java.util.List)2 Collectors (java.util.stream.Collectors)2 Configuration (org.apache.commons.configuration2.Configuration)2 HierarchicalConfiguration (org.apache.commons.configuration2.HierarchicalConfiguration)2 Deployment (org.craftercms.deployer.api.Deployment)2 SearchException (org.craftercms.search.commons.exception.SearchException)2