Search in sources :

Example 1 with InvalidRequestException

use of com.aws.greengrass.deployment.exceptions.InvalidRequestException in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentService method copyRecipesAndArtifacts.

@SuppressWarnings("PMD.ExceptionAsFlowControl")
private void copyRecipesAndArtifacts(Deployment deployment) throws InvalidRequestException, IOException {
    try {
        LocalOverrideRequest localOverrideRequest = SerializerFactory.getFailSafeJsonObjectMapper().readValue(deployment.getDeploymentDocument(), LocalOverrideRequest.class);
        if (!Utils.isEmpty(localOverrideRequest.getRecipeDirectoryPath())) {
            Path recipeDirectoryPath = Paths.get(localOverrideRequest.getRecipeDirectoryPath());
            copyRecipesToComponentStore(recipeDirectoryPath);
        }
        if (!Utils.isEmpty(localOverrideRequest.getArtifactsDirectoryPath())) {
            Path kernelArtifactsDirectoryPath = kernel.getNucleusPaths().componentStorePath().resolve(ComponentStore.ARTIFACT_DIRECTORY);
            Path artifactsDirectoryPath = Paths.get(localOverrideRequest.getArtifactsDirectoryPath());
            try {
                Utils.copyFolderRecursively(artifactsDirectoryPath, kernelArtifactsDirectoryPath, (Path src, Path dst) -> {
                    // existing component and then do the deployment to make it work.
                    if (PlatformResolver.isWindows) {
                        try {
                            if (Files.exists(dst) && FileUtils.contentEquals(src.toFile(), dst.toFile())) {
                                return false;
                            }
                        } catch (IOException e) {
                            logger.atError().log("Unable to determine if files are equal", e);
                            return true;
                        }
                    }
                    return true;
                }, StandardCopyOption.REPLACE_EXISTING);
            } catch (IOException e) {
                throw new IOException(String.format("Unable to copy artifacts from  %s due to: %s", artifactsDirectoryPath, e.getMessage()), e);
            }
        }
    } catch (JsonProcessingException e) {
        throw new InvalidRequestException("Unable to parse the deployment request - Invalid JSON", e);
    }
}
Also used : Path(java.nio.file.Path) LocalOverrideRequest(com.aws.greengrass.deployment.model.LocalOverrideRequest) InvalidRequestException(com.aws.greengrass.deployment.exceptions.InvalidRequestException) IOException(java.io.IOException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException)

Example 2 with InvalidRequestException

use of com.aws.greengrass.deployment.exceptions.InvalidRequestException in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentService method parseAndValidateJobDocument.

private DeploymentDocument parseAndValidateJobDocument(Deployment deployment) throws InvalidRequestException {
    String jobDocumentString = deployment.getDeploymentDocument();
    if (Utils.isEmpty(jobDocumentString)) {
        throw new InvalidRequestException("Job document cannot be empty");
    }
    DeploymentDocument document;
    try {
        switch(deployment.getDeploymentType()) {
            case LOCAL:
                LocalOverrideRequest localOverrideRequest = SerializerFactory.getFailSafeJsonObjectMapper().readValue(jobDocumentString, LocalOverrideRequest.class);
                Map<String, String> rootComponents = new HashMap<>();
                Set<String> rootComponentsInRequestedGroup = new HashSet<>();
                config.lookupTopics(GROUP_TO_ROOT_COMPONENTS_TOPICS, localOverrideRequest.getGroupName() == null ? LOCAL_DEPLOYMENT_GROUP_NAME : THING_GROUP_RESOURCE_NAME_PREFIX + localOverrideRequest.getGroupName()).forEach(t -> rootComponentsInRequestedGroup.add(t.getName()));
                if (!Utils.isEmpty(rootComponentsInRequestedGroup)) {
                    rootComponentsInRequestedGroup.forEach(c -> {
                        Topics serviceTopic = kernel.findServiceTopic(c);
                        if (serviceTopic != null) {
                            String version = Coerce.toString(serviceTopic.find(VERSION_CONFIG_KEY));
                            rootComponents.put(c, version);
                        }
                    });
                }
                document = DeploymentDocumentConverter.convertFromLocalOverrideRequestAndRoot(localOverrideRequest, rootComponents);
                break;
            case IOT_JOBS:
            case SHADOW:
                // Note: This is the data contract that gets sending down from FCS::CreateDeployment
                // Configuration is really a bad name choice as it is too generic but we can change it later
                // since it is only a internal model
                Configuration configuration = SerializerFactory.getFailSafeJsonObjectMapper().readValue(jobDocumentString, Configuration.class);
                document = DeploymentDocumentConverter.convertFromDeploymentConfiguration(configuration);
                break;
            default:
                throw new IllegalArgumentException("Invalid deployment type: " + deployment.getDeploymentType());
        }
    } catch (JsonProcessingException | IllegalArgumentException e) {
        throw new InvalidRequestException("Unable to parse the job document", e);
    }
    deployment.setDeploymentDocumentObj(document);
    return document;
}
Also used : Topics(com.aws.greengrass.config.Topics) Configuration(com.amazon.aws.iot.greengrass.configuration.common.Configuration) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) LocalOverrideRequest(com.aws.greengrass.deployment.model.LocalOverrideRequest) InvalidRequestException(com.aws.greengrass.deployment.exceptions.InvalidRequestException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) HashSet(java.util.HashSet)

Example 3 with InvalidRequestException

use of com.aws.greengrass.deployment.exceptions.InvalidRequestException in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentService method createNewDeployment.

private void createNewDeployment(Deployment deployment) {
    logger.atInfo().kv(DEPLOYMENT_ID_LOG_KEY, deployment.getId()).kv("DeploymentType", deployment.getDeploymentType().toString()).log("Received deployment in the queue");
    DeploymentTask deploymentTask;
    boolean cancellable = true;
    if (DEFAULT.equals(deployment.getDeploymentStage())) {
        deploymentTask = createDefaultNewDeployment(deployment);
    } else {
        deploymentTask = createKernelUpdateDeployment(deployment);
        cancellable = false;
        if (DeploymentType.IOT_JOBS.equals(deployment.getDeploymentType())) {
            // Keep track of IoT jobs for de-duplication
            IotJobsHelper.getLatestQueuedJobs().addProcessedJob(deployment.getId());
        }
    }
    if (deploymentTask == null) {
        return;
    }
    deploymentStatusKeeper.persistAndPublishDeploymentStatus(deployment.getId(), deployment.getDeploymentType(), JobStatus.IN_PROGRESS.toString(), new HashMap<>());
    if (DEFAULT.equals(deployment.getDeploymentStage())) {
        try {
            context.get(KernelAlternatives.class).cleanupLaunchDirectoryLinks();
            deploymentDirectoryManager.createNewDeploymentDirectory(deployment.getDeploymentDocumentObj().getDeploymentId());
            deploymentDirectoryManager.writeDeploymentMetadata(deployment);
        } catch (IOException ioException) {
            logger.atError().log("Unable to create deployment directory", ioException);
            updateDeploymentResultAsFailed(deployment, deploymentTask, true, new DeploymentTaskFailureException(ioException));
            return;
        }
        List<String> requiredCapabilities = deployment.getDeploymentDocumentObj().getRequiredCapabilities();
        if (requiredCapabilities != null && !requiredCapabilities.isEmpty()) {
            List<String> missingCapabilities = requiredCapabilities.stream().filter(reqCapabilities -> !kernel.getSupportedCapabilities().contains(reqCapabilities)).collect(Collectors.toList());
            if (!missingCapabilities.isEmpty()) {
                updateDeploymentResultAsFailed(deployment, deploymentTask, false, new MissingRequiredCapabilitiesException("The current nucleus version doesn't support one " + "or more capabilities that are required by this deployment: " + String.join(", ", missingCapabilities)));
                return;
            }
        }
        if (DeploymentType.LOCAL.equals(deployment.getDeploymentType())) {
            try {
                copyRecipesAndArtifacts(deployment);
            } catch (InvalidRequestException | IOException e) {
                logger.atError().log("Error copying recipes and artifacts", e);
                updateDeploymentResultAsFailed(deployment, deploymentTask, false, e);
                return;
            }
        }
    }
    Future<DeploymentResult> process = executorService.submit(deploymentTask);
    logger.atInfo().kv("deployment", deployment.getId()).log("Started deployment execution");
    currentDeploymentTaskMetadata = new DeploymentTaskMetadata(deploymentTask, process, deployment.getId(), deployment.getDeploymentType(), new AtomicInteger(1), deployment.getDeploymentDocumentObj(), cancellable);
}
Also used : PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) SerializerFactory.getRecipeSerializer(com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializer) SerializerFactory.getRecipeSerializerJson(com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializerJson) DeploymentDocumentConverter(com.aws.greengrass.deployment.converter.DeploymentDocumentConverter) InvalidRequestException(com.aws.greengrass.deployment.exceptions.InvalidRequestException) THING_GROUP_RESOURCE_NAME_PREFIX(com.aws.greengrass.deployment.converter.DeploymentDocumentConverter.THING_GROUP_RESOURCE_NAME_PREFIX) Deployment(com.aws.greengrass.deployment.model.Deployment) DeploymentTask(com.aws.greengrass.deployment.model.DeploymentTask) Future(java.util.concurrent.Future) DEVICE_DEPLOYMENT_GROUP_NAME_PREFIX(com.aws.greengrass.deployment.DefaultDeploymentTask.DEVICE_DEPLOYMENT_GROUP_NAME_PREFIX) State(com.aws.greengrass.dependency.State) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) DEFAULT(com.aws.greengrass.deployment.model.Deployment.DeploymentStage.DEFAULT) Duration(java.time.Duration) Map(java.util.Map) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) Path(java.nio.file.Path) DependencyResolver(com.aws.greengrass.componentmanager.DependencyResolver) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) Node(com.aws.greengrass.config.Node) CancellationException(java.util.concurrent.CancellationException) LocalOverrideRequest(com.aws.greengrass.deployment.model.LocalOverrideRequest) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) LOCAL_DEPLOYMENT_GROUP_NAME(com.aws.greengrass.deployment.converter.DeploymentDocumentConverter.LOCAL_DEPLOYMENT_GROUP_NAME) Set(java.util.Set) Collectors(java.util.stream.Collectors) Topics(com.aws.greengrass.config.Topics) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) List(java.util.List) Stream(java.util.stream.Stream) DeploymentTaskFailureException(com.aws.greengrass.deployment.exceptions.DeploymentTaskFailureException) DEPLOYMENT_ID_LOG_KEY(com.aws.greengrass.deployment.DeploymentConfigMerger.DEPLOYMENT_ID_LOG_KEY) ImplementsService(com.aws.greengrass.dependency.ImplementsService) ComponentStore(com.aws.greengrass.componentmanager.ComponentStore) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) SerializerFactory(com.aws.greengrass.util.SerializerFactory) Setter(lombok.Setter) VERSION_CONFIG_KEY(com.aws.greengrass.componentmanager.KernelConfigResolver.VERSION_CONFIG_KEY) Getter(lombok.Getter) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) Configuration(com.amazon.aws.iot.greengrass.configuration.common.Configuration) UpdateSystemPolicyService(com.aws.greengrass.lifecyclemanager.UpdateSystemPolicyService) Coerce(com.aws.greengrass.util.Coerce) StandardCopyOption(java.nio.file.StandardCopyOption) HashSet(java.util.HashSet) Inject(javax.inject.Inject) ComponentManager(com.aws.greengrass.componentmanager.ComponentManager) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) Context(com.aws.greengrass.dependency.Context) ComponentRecipe(com.amazon.aws.iot.greengrass.component.common.ComponentRecipe) JobStatus(software.amazon.awssdk.iot.iotjobs.model.JobStatus) PlatformResolver(com.aws.greengrass.config.PlatformResolver) ExecutorService(java.util.concurrent.ExecutorService) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException) DeploymentType(com.aws.greengrass.deployment.model.Deployment.DeploymentType) Files(java.nio.file.Files) MissingRequiredCapabilitiesException(com.aws.greengrass.deployment.exceptions.MissingRequiredCapabilitiesException) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) FileUtils(org.apache.commons.io.FileUtils) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Topic(com.aws.greengrass.config.Topic) AtomicLong(java.util.concurrent.atomic.AtomicLong) Utils(com.aws.greengrass.util.Utils) DeploymentTaskMetadata(com.aws.greengrass.deployment.model.DeploymentTaskMetadata) Paths(java.nio.file.Paths) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) FAILED_ROLLBACK_NOT_REQUESTED(com.aws.greengrass.deployment.model.DeploymentResult.DeploymentStatus.FAILED_ROLLBACK_NOT_REQUESTED) DeploymentStatus(com.aws.greengrass.deployment.model.DeploymentResult.DeploymentStatus) KernelConfigResolver(com.aws.greengrass.componentmanager.KernelConfigResolver) Logger(com.aws.greengrass.logging.api.Logger) IOException(java.io.IOException) DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) KernelAlternatives(com.aws.greengrass.lifecyclemanager.KernelAlternatives) MissingRequiredCapabilitiesException(com.aws.greengrass.deployment.exceptions.MissingRequiredCapabilitiesException) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) InvalidRequestException(com.aws.greengrass.deployment.exceptions.InvalidRequestException) DeploymentTaskMetadata(com.aws.greengrass.deployment.model.DeploymentTaskMetadata) DeploymentTaskFailureException(com.aws.greengrass.deployment.exceptions.DeploymentTaskFailureException) DeploymentTask(com.aws.greengrass.deployment.model.DeploymentTask)

Aggregations

InvalidRequestException (com.aws.greengrass.deployment.exceptions.InvalidRequestException)3 LocalOverrideRequest (com.aws.greengrass.deployment.model.LocalOverrideRequest)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)3 Configuration (com.amazon.aws.iot.greengrass.configuration.common.Configuration)2 Topics (com.aws.greengrass.config.Topics)2 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 ConcurrentHashMap (java.util.concurrent.ConcurrentHashMap)2 ComponentRecipe (com.amazon.aws.iot.greengrass.component.common.ComponentRecipe)1 SerializerFactory.getRecipeSerializer (com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializer)1 SerializerFactory.getRecipeSerializerJson (com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializerJson)1 ComponentManager (com.aws.greengrass.componentmanager.ComponentManager)1 ComponentStore (com.aws.greengrass.componentmanager.ComponentStore)1 DependencyResolver (com.aws.greengrass.componentmanager.DependencyResolver)1 KernelConfigResolver (com.aws.greengrass.componentmanager.KernelConfigResolver)1 VERSION_CONFIG_KEY (com.aws.greengrass.componentmanager.KernelConfigResolver.VERSION_CONFIG_KEY)1 PackageLoadingException (com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)1 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)1 Node (com.aws.greengrass.config.Node)1