Search in sources :

Example 1 with PackagingException

use of com.aws.greengrass.componentmanager.exceptions.PackagingException in project aws-greengrass-nucleus by aws-greengrass.

the class DeploymentTaskTest method GIVEN_deploymentDocument_WHEN_resolveDependencies_errored_THEN_deploymentTask_aborted.

@Test
void GIVEN_deploymentDocument_WHEN_resolveDependencies_errored_THEN_deploymentTask_aborted(ExtensionContext context) throws Exception {
    ignoreExceptionUltimateCauseOfType(context, PackagingException.class);
    when(mockExecutorService.submit(any(Callable.class))).thenReturn(mockResolveDependencyFuture);
    when(mockResolveDependencyFuture.get()).thenThrow(new ExecutionException(new PackagingException("unknown package")));
    DeploymentResult result = deploymentTask.call();
    assertEquals(DeploymentResult.DeploymentStatus.FAILED_NO_STATE_CHANGE, result.getDeploymentStatus());
    assertTrue(result.getFailureCause() instanceof PackagingException);
    verify(mockComponentManager, times(0)).preparePackages(anyList());
    verify(mockKernelConfigResolver, times(0)).resolve(anyList(), eq(deploymentDocument), anyList());
    verify(mockDeploymentConfigMerger, times(0)).mergeInNewConfig(any(), any());
}
Also used : DeploymentResult(com.aws.greengrass.deployment.model.DeploymentResult) ExecutionException(java.util.concurrent.ExecutionException) Callable(java.util.concurrent.Callable) PackagingException(com.aws.greengrass.componentmanager.exceptions.PackagingException) Test(org.junit.jupiter.api.Test)

Example 2 with PackagingException

use of com.aws.greengrass.componentmanager.exceptions.PackagingException in project aws-greengrass-nucleus by aws-greengrass.

the class DependencyResolver method checkNonExplicitNucleusUpdate.

void checkNonExplicitNucleusUpdate(List<String> targetComponents, List<ComponentIdentifier> resolvedComponents) throws PackagingException {
    List<ComponentIdentifier> resolvedNucleusComponents = new ArrayList<>();
    for (ComponentIdentifier componentIdentifier : resolvedComponents) {
        if (ComponentType.NUCLEUS.equals(componentStore.getPackageRecipe(componentIdentifier).getComponentType())) {
            resolvedNucleusComponents.add(componentIdentifier);
        }
    }
    if (resolvedNucleusComponents.size() > 1) {
        throw new PackagingException(String.format("Deployment cannot have more than 1 component of type Nucleus " + "%s", Arrays.toString(resolvedNucleusComponents.toArray())));
    }
    if (resolvedNucleusComponents.isEmpty()) {
        return;
    }
    Optional<GreengrassService> activeNucleusOption = kernel.orderedDependencies().stream().filter(s -> ComponentType.NUCLEUS.name().equals(s.getServiceType())).findFirst();
    if (!activeNucleusOption.isPresent()) {
        return;
    }
    GreengrassService activeNucleus = activeNucleusOption.get();
    String activeNucleusVersionConfig = Coerce.toString(activeNucleus.getServiceConfig().find(VERSION_KEY));
    if (Utils.isEmpty(activeNucleusVersionConfig)) {
        throw new PackagingException(NO_ACTIVE_NUCLEUS_VERSION_ERROR_MSG);
    }
    Semver activeNucleusVersion = new Semver(activeNucleusVersionConfig);
    ComponentIdentifier activeNucleusId = new ComponentIdentifier(activeNucleus.getServiceName(), activeNucleusVersion);
    ComponentIdentifier resolvedNucleusId = resolvedNucleusComponents.get(0);
    if (!resolvedNucleusId.equals(activeNucleusId) && !targetComponents.contains(resolvedNucleusId.getName())) {
        Semver.VersionDiff diff = activeNucleusVersion.diff(resolvedNucleusId.getVersion());
        if (Semver.VersionDiff.MINOR.equals(diff) || Semver.VersionDiff.MAJOR.equals(diff)) {
            throw new PackagingException(String.format(NON_EXPLICIT_NUCLEUS_UPDATE_ERROR_MESSAGE_FMT, activeNucleusId.getName(), activeNucleusId.getVersion().toString(), resolvedNucleusId.getName(), resolvedNucleusId.getVersion().toString()));
        }
    }
}
Also used : DependencyOrder(com.aws.greengrass.util.DependencyOrder) Arrays(java.util.Arrays) NoAvailableComponentVersionException(com.aws.greengrass.componentmanager.exceptions.NoAvailableComponentVersionException) DeploymentPackageConfiguration(com.aws.greengrass.deployment.model.DeploymentPackageConfiguration) HashMap(java.util.HashMap) Coerce(com.aws.greengrass.util.Coerce) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) Inject(javax.inject.Inject) Requirement(com.vdurmont.semver4j.Requirement) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) Semver(com.vdurmont.semver4j.Semver) Map(java.util.Map) LogManager(com.aws.greengrass.logging.impl.LogManager) PackagingException(com.aws.greengrass.componentmanager.exceptions.PackagingException) DeploymentDocument(com.aws.greengrass.deployment.model.DeploymentDocument) LinkedList(java.util.LinkedList) LinkedHashSet(java.util.LinkedHashSet) ComponentVersionNegotiationException(com.aws.greengrass.componentmanager.exceptions.ComponentVersionNegotiationException) Set(java.util.Set) Collectors(java.util.stream.Collectors) Kernel(com.aws.greengrass.lifecyclemanager.Kernel) ComponentType(com.amazon.aws.iot.greengrass.component.common.ComponentType) ComponentMetadata(com.aws.greengrass.componentmanager.models.ComponentMetadata) List(java.util.List) Utils(com.aws.greengrass.util.Utils) Optional(java.util.Optional) Queue(java.util.Queue) ComponentRequirementIdentifier(com.aws.greengrass.componentmanager.models.ComponentRequirementIdentifier) Logger(com.aws.greengrass.logging.api.Logger) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) NoArgsConstructor(lombok.NoArgsConstructor) GreengrassService(com.aws.greengrass.lifecyclemanager.GreengrassService) ArrayList(java.util.ArrayList) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) Semver(com.vdurmont.semver4j.Semver) PackagingException(com.aws.greengrass.componentmanager.exceptions.PackagingException)

Example 3 with PackagingException

use of com.aws.greengrass.componentmanager.exceptions.PackagingException in project aws-greengrass-nucleus by aws-greengrass.

the class ComponentManager method negotiateVersionWithCloud.

@SuppressWarnings({ "PMD.AvoidCatchingGenericException", "PMD.AvoidRethrowingException", "PMD.NullAssignment", "PMD.AvoidInstanceofChecksInCatchClause", "PMD.PreserveStackTrace" })
private ComponentIdentifier negotiateVersionWithCloud(String componentName, Map<String, Requirement> versionRequirements, ComponentIdentifier localCandidate) throws PackagingException, InterruptedException {
    ResolvedComponentVersion resolvedComponentVersion;
    if (localCandidate == null) {
        try {
            resolvedComponentVersion = RetryUtils.runWithRetry(clientExceptionRetryConfig, () -> componentServiceHelper.resolveComponentVersion(componentName, null, versionRequirements), "resolve-component-version", logger);
        } catch (InterruptedException e) {
            throw e;
        } catch (Exception e) {
            // Don't bother logging the full stacktrace when it is NoAvailableComponentVersionException since we
            // know the reason for that error
            logger.atError().setCause(e instanceof NoAvailableComponentVersionException ? null : e).kv(COMPONENT_NAME, componentName).kv("versionRequirement", versionRequirements).log("Failed to negotiate version with cloud and no local version to fall back to");
            // know what the cause is.
            if (e instanceof NoAvailableComponentVersionException) {
                throw new NoAvailableComponentVersionException("No local or cloud component version satisfies the requirements.", componentName, versionRequirements);
            } else {
                throw new NoAvailableComponentVersionException("No local or cloud component version satisfies the requirements.", componentName, versionRequirements, e);
            }
        }
    } else {
        try {
            resolvedComponentVersion = componentServiceHelper.resolveComponentVersion(componentName, localCandidate.getVersion(), versionRequirements);
        } catch (Exception e) {
            // Don't bother logging the full stacktrace when it is NoAvailableComponentVersionException since we
            // know the reason for that error
            logger.atInfo().setCause(e instanceof NoAvailableComponentVersionException ? null : e).kv(COMPONENT_NAME, componentName).kv("versionRequirement", versionRequirements).kv("localVersion", localCandidate).log("Failed to negotiate version with cloud and fall back to use the local version");
            return localCandidate;
        }
    }
    ComponentIdentifier resolvedComponentId = new ComponentIdentifier(resolvedComponentVersion.componentName(), new Semver(resolvedComponentVersion.componentVersion()));
    String downloadedRecipeContent = StandardCharsets.UTF_8.decode(resolvedComponentVersion.recipe().asByteBuffer()).toString();
    com.amazon.aws.iot.greengrass.component.common.ComponentRecipe cloudResolvedRecipe = // cloud sends JSON
    RecipeLoader.parseRecipe(downloadedRecipeContent, RecipeLoader.RecipeFormat.JSON);
    // Persist the recipe
    String savedRecipeContent = componentStore.saveComponentRecipe(cloudResolvedRecipe);
    // Since plugin runs in the same JVM as Nucleus does, we need to calculate the digest for its recipe and
    // persist it, so that we can use it to detect and prevent a tampered plugin (recipe) gets loaded
    storeRecipeDigestInConfigStoreForPlugin(cloudResolvedRecipe, savedRecipeContent);
    // Save the arn to the recipe meta data file
    componentStore.saveRecipeMetadata(resolvedComponentId, new RecipeMetadata(resolvedComponentVersion.arn()));
    return resolvedComponentId;
}
Also used : NoAvailableComponentVersionException(com.aws.greengrass.componentmanager.exceptions.NoAvailableComponentVersionException) ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) Semver(com.vdurmont.semver4j.Semver) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) NoAvailableComponentVersionException(com.aws.greengrass.componentmanager.exceptions.NoAvailableComponentVersionException) InvalidArtifactUriException(com.aws.greengrass.componentmanager.exceptions.InvalidArtifactUriException) SizeLimitException(com.aws.greengrass.componentmanager.exceptions.SizeLimitException) SemverException(com.vdurmont.semver4j.SemverException) SdkClientException(software.amazon.awssdk.core.exception.SdkClientException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) PackageDownloadException(com.aws.greengrass.componentmanager.exceptions.PackageDownloadException) PackagingException(com.aws.greengrass.componentmanager.exceptions.PackagingException) ServiceLoadException(com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException) MissingRequiredComponentsException(com.aws.greengrass.componentmanager.exceptions.MissingRequiredComponentsException) IOException(java.io.IOException) ResolvedComponentVersion(software.amazon.awssdk.services.greengrassv2data.model.ResolvedComponentVersion) RecipeMetadata(com.aws.greengrass.componentmanager.models.RecipeMetadata)

Aggregations

PackagingException (com.aws.greengrass.componentmanager.exceptions.PackagingException)3 NoAvailableComponentVersionException (com.aws.greengrass.componentmanager.exceptions.NoAvailableComponentVersionException)2 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)2 Semver (com.vdurmont.semver4j.Semver)2 ComponentType (com.amazon.aws.iot.greengrass.component.common.ComponentType)1 ComponentVersionNegotiationException (com.aws.greengrass.componentmanager.exceptions.ComponentVersionNegotiationException)1 InvalidArtifactUriException (com.aws.greengrass.componentmanager.exceptions.InvalidArtifactUriException)1 MissingRequiredComponentsException (com.aws.greengrass.componentmanager.exceptions.MissingRequiredComponentsException)1 PackageDownloadException (com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)1 PackageLoadingException (com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)1 SizeLimitException (com.aws.greengrass.componentmanager.exceptions.SizeLimitException)1 ComponentMetadata (com.aws.greengrass.componentmanager.models.ComponentMetadata)1 ComponentRequirementIdentifier (com.aws.greengrass.componentmanager.models.ComponentRequirementIdentifier)1 RecipeMetadata (com.aws.greengrass.componentmanager.models.RecipeMetadata)1 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)1 DeploymentPackageConfiguration (com.aws.greengrass.deployment.model.DeploymentPackageConfiguration)1 DeploymentResult (com.aws.greengrass.deployment.model.DeploymentResult)1 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)1 Kernel (com.aws.greengrass.lifecyclemanager.Kernel)1 ServiceLoadException (com.aws.greengrass.lifecyclemanager.exceptions.ServiceLoadException)1