Search in sources :

Example 1 with DeploymentType

use of com.aws.greengrass.deployment.model.Deployment.DeploymentType in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentStatusKeeper method publishPersistedStatusUpdates.

/**
 * Invokes the call-backs with persisted deployment status updates for deployments with specified type.
 * This is called by IotJobsHelper/MqttJobsHelper when connection is re-established to update cloud of all
 * all deployments the device performed when offline
 *
 * @param type deployment type
 */
public void publishPersistedStatusUpdates(DeploymentType type) {
    synchronized (type) {
        Topics processedDeployments = getProcessedDeployments();
        ArrayList<Topics> deployments = new ArrayList<>();
        processedDeployments.forEach(node -> {
            Topics deploymentDetails = (Topics) node;
            DeploymentType deploymentType = Coerce.toEnum(DeploymentType.class, deploymentDetails.find(DEPLOYMENT_TYPE_KEY_NAME));
            if (Objects.equals(deploymentType, type)) {
                deployments.add(deploymentDetails);
            }
        });
        // Topics are stored as ConcurrentHashMaps which do not guarantee ordering of elements
        // We want the statuses to be updated in the cloud in the order in which they were processed on the device.
        // This will be accurate representation of what happened on the device, especially when deployment service
        // processes multiple deployments in the order in which they come. Additionally, a customer workflow can
        // depend on this order. If Group2 gets successfully updated before Group1 then customer workflow may
        // error out.
        List<Topics> sortedByTimestamp = deployments.stream().sorted((o1, o2) -> {
            if (o1.getModtime() > o2.getModtime()) {
                return 1;
            }
            return -1;
        }).collect(Collectors.toList());
        List<Function<Map<String, Object>, Boolean>> consumers = getConsumersForDeploymentType(type);
        logger.atDebug().kv("deploymentType", type).kv("numberOfSubscribers", consumers.size()).log("Updating status of persisted deployments to subscribers");
        for (Topics topics : sortedByTimestamp) {
            boolean allConsumersUpdated = consumers.stream().allMatch(consumer -> consumer.apply(topics.toPOJO()));
            if (!allConsumersUpdated) {
                // If one deployment update fails, exit the loop to ensure the update order.
                logger.atDebug().log("Unable to update status of persisted deployments. Retry later");
                break;
            }
            processedDeployments.remove(topics);
        }
    }
}
Also used : Setter(lombok.Setter) DeploymentType(com.aws.greengrass.deployment.model.Deployment.DeploymentType) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Coerce(com.aws.greengrass.util.Coerce) Function(java.util.function.Function) Collectors(java.util.stream.Collectors) ArrayList(java.util.ArrayList) Objects(java.util.Objects) Topics(com.aws.greengrass.config.Topics) List(java.util.List) Map(java.util.Map) LogManager(com.aws.greengrass.logging.impl.LogManager) Collections(java.util.Collections) Logger(com.aws.greengrass.logging.api.Logger) Function(java.util.function.Function) Topics(com.aws.greengrass.config.Topics) ArrayList(java.util.ArrayList) DeploymentType(com.aws.greengrass.deployment.model.Deployment.DeploymentType)

Example 2 with DeploymentType

use of com.aws.greengrass.deployment.model.Deployment.DeploymentType in project aws-greengrass-nucleus by aws-greengrass.

the class FleetStatusService method deploymentStatusChanged.

private Boolean deploymentStatusChanged(Map<String, Object> deploymentDetails) {
    DeploymentType type = Coerce.toEnum(DeploymentType.class, deploymentDetails.get(DEPLOYMENT_TYPE_KEY_NAME));
    if (type == IOT_JOBS || type == SHADOW) {
        String status = deploymentDetails.get(DEPLOYMENT_STATUS_KEY_NAME).toString();
        if (JobStatus.IN_PROGRESS.toString().equals(status)) {
            isDeploymentInProgress.set(true);
            return true;
        }
        logger.atDebug().log("Updating Fleet Status service for deployment with ID: {}", deploymentDetails.get(DEPLOYMENT_ID_KEY_NAME));
        isDeploymentInProgress.set(false);
        DeploymentInformation deploymentInformation = getDeploymentInformation(deploymentDetails);
        updateEventTriggeredFleetStatusData(deploymentInformation);
    }
    // TODO: [P41214799] Handle local deployment update for FSS
    return true;
}
Also used : DeploymentType(com.aws.greengrass.deployment.model.Deployment.DeploymentType)

Example 3 with DeploymentType

use of com.aws.greengrass.deployment.model.Deployment.DeploymentType 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

DeploymentType (com.aws.greengrass.deployment.model.Deployment.DeploymentType)3 Topics (com.aws.greengrass.config.Topics)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 Configuration (com.amazon.aws.iot.greengrass.configuration.common.Configuration)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 PlatformResolver (com.aws.greengrass.config.PlatformResolver)1 Topic (com.aws.greengrass.config.Topic)1 Context (com.aws.greengrass.dependency.Context)1 ImplementsService (com.aws.greengrass.dependency.ImplementsService)1 State (com.aws.greengrass.dependency.State)1 DEVICE_DEPLOYMENT_GROUP_NAME_PREFIX (com.aws.greengrass.deployment.DefaultDeploymentTask.DEVICE_DEPLOYMENT_GROUP_NAME_PREFIX)1