Search in sources :

Example 1 with ComponentRecipe

use of com.aws.greengrass.componentmanager.models.ComponentRecipe in project aws-greengrass-nucleus by aws-greengrass.

the class DeviceConfiguration method initializeNucleusFromRecipe.

/**
 * Load Nucleus component information from build recipe.
 *
 * @param kernelAlts KernelAlternatives instance
 */
public void initializeNucleusFromRecipe(KernelAlternatives kernelAlts) {
    String nucleusComponentName = getNucleusComponentName();
    persistInitialLaunchParams(kernelAlts);
    Semver componentVersion = null;
    try {
        Path unpackDir = locateCurrentKernelUnpackDir();
        Path recipePath = unpackDir.resolve(NUCLEUS_BUILD_METADATA_DIRECTORY).resolve(NUCLEUS_RECIPE_FILENAME);
        if (!Files.exists(recipePath)) {
            throw new PackageLoadingException("Failed to find Nucleus recipe at " + recipePath);
        }
        // Update Nucleus in config store
        Optional<ComponentRecipe> resolvedRecipe = kernel.getContext().get(RecipeLoader.class).loadFromFile(new String(Files.readAllBytes(recipePath.toAbsolutePath()), StandardCharsets.UTF_8));
        if (!resolvedRecipe.isPresent()) {
            throw new PackageLoadingException("Failed to load Nucleus recipe");
        }
        ComponentRecipe componentRecipe = resolvedRecipe.get();
        componentVersion = componentRecipe.getVersion();
        initializeNucleusLifecycleConfig(nucleusComponentName, componentRecipe);
        initializeComponentStore(kernelAlts, nucleusComponentName, componentVersion, recipePath, unpackDir);
    } catch (IOException | URISyntaxException | PackageLoadingException e) {
        logger.atError().log("Unable to set up Nucleus from build recipe file", e);
    }
    initializeNucleusVersion(nucleusComponentName, componentVersion == null ? FALLBACK_VERSION : componentVersion.toString());
}
Also used : Path(java.nio.file.Path) CaseInsensitiveString(com.aws.greengrass.config.CaseInsensitiveString) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) IOException(java.io.IOException) URISyntaxException(java.net.URISyntaxException) Semver(com.vdurmont.semver4j.Semver) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) RecipeLoader(com.aws.greengrass.componentmanager.converter.RecipeLoader)

Example 2 with ComponentRecipe

use of com.aws.greengrass.componentmanager.models.ComponentRecipe in project aws-greengrass-nucleus by aws-greengrass.

the class DeviceConfigurationTest method GIVEN_recipe_WHEN_initializeNucleusLifecycleConfig_THEN_init_lifecycle_and_dependencies.

@Test
void GIVEN_recipe_WHEN_initializeNucleusLifecycleConfig_THEN_init_lifecycle_and_dependencies(@Mock KernelConfigResolver kernelConfigResolver, @Mock Context context) throws Exception {
    deviceConfiguration = new DeviceConfiguration(mockKernel);
    List mockDependencies = Arrays.asList("A", "B", "C");
    doReturn(mockDependencies).when(kernelConfigResolver).generateServiceDependencies(anyMap());
    Object interpolatedLifecycle = new HashMap<String, String>() {

        {
            put("lifecycle", "step");
        }
    };
    doReturn(interpolatedLifecycle).when(kernelConfigResolver).interpolate(anyMap(), any(), anySet(), anyMap());
    doReturn(kernelConfigResolver).when(context).get(KernelConfigResolver.class);
    doReturn(context).when(mockKernel).getContext();
    String nucleusComponentName = "test.component.name";
    ComponentRecipe componentRecipe = ComponentRecipe.builder().lifecycle((Map<String, Object>) interpolatedLifecycle).build();
    Topics lifecycleTopics = spy(Topics.of(context, SERVICE_LIFECYCLE_NAMESPACE_TOPIC, null));
    when(configuration.lookupTopics(eq(DEFAULT_VALUE_TIMESTAMP), eq(SERVICES_NAMESPACE_TOPIC), eq(nucleusComponentName), eq(SERVICE_LIFECYCLE_NAMESPACE_TOPIC))).thenReturn(lifecycleTopics);
    Topic dependencyTopic = Topic.of(context, SERVICE_DEPENDENCIES_NAMESPACE_TOPIC, null);
    when(configuration.lookup(eq(DEFAULT_VALUE_TIMESTAMP), eq(SERVICES_NAMESPACE_TOPIC), eq(nucleusComponentName), eq(SERVICE_DEPENDENCIES_NAMESPACE_TOPIC))).thenReturn(dependencyTopic);
    deviceConfiguration.initializeNucleusLifecycleConfig(nucleusComponentName, componentRecipe);
    assertEquals(mockDependencies, dependencyTopic.toPOJO());
    verify(lifecycleTopics).replaceAndWait((Map<String, Object>) interpolatedLifecycle);
}
Also used : Topics(com.aws.greengrass.config.Topics) HashMap(java.util.HashMap) List(java.util.List) ArrayList(java.util.ArrayList) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) Topic(com.aws.greengrass.config.Topic) Map(java.util.Map) HashMap(java.util.HashMap) ArgumentMatchers.anyMap(org.mockito.ArgumentMatchers.anyMap) Test(org.junit.jupiter.api.Test)

Example 3 with ComponentRecipe

use of com.aws.greengrass.componentmanager.models.ComponentRecipe in project aws-greengrass-nucleus by aws-greengrass.

the class ComponentManager method preparePackage.

private void preparePackage(ComponentIdentifier componentIdentifier) throws PackageLoadingException, PackageDownloadException, InvalidArtifactUriException, InterruptedException {
    logger.atInfo().setEventType("prepare-package-start").kv(PACKAGE_IDENTIFIER, componentIdentifier).log();
    try {
        ComponentRecipe pkg = componentStore.getPackageRecipe(componentIdentifier);
        prepareArtifacts(componentIdentifier, pkg.getArtifacts());
        logger.atDebug("prepare-package-finished").kv(PACKAGE_IDENTIFIER, componentIdentifier).log();
    } catch (SizeLimitException e) {
        logger.atError().log("Size limit reached", e);
        throw e;
    } catch (PackageLoadingException | PackageDownloadException e) {
        logger.atError().log("Failed to prepare package {}", componentIdentifier, e);
        throw e;
    }
}
Also used : SizeLimitException(com.aws.greengrass.componentmanager.exceptions.SizeLimitException) PackageDownloadException(com.aws.greengrass.componentmanager.exceptions.PackageDownloadException) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)

Example 4 with ComponentRecipe

use of com.aws.greengrass.componentmanager.models.ComponentRecipe in project aws-greengrass-nucleus by aws-greengrass.

the class RecipeLoader method loadFromFile.

/**
 * Converts from the recipe file with platform resolving.
 *
 * @param recipeFileContent recipe file content
 * @return Optional package recipe
 * @throws PackageLoadingException when failed to convert recipe file.
 */
public Optional<ComponentRecipe> loadFromFile(String recipeFileContent) throws PackageLoadingException {
    com.amazon.aws.iot.greengrass.component.common.ComponentRecipe componentRecipe = parseRecipe(recipeFileContent, RecipeFormat.YAML);
    if (componentRecipe.getManifests() == null || componentRecipe.getManifests().isEmpty()) {
        throw new PackageLoadingException(String.format("Recipe file %s-%s.yaml is missing manifests", componentRecipe.getComponentName(), componentRecipe.getComponentVersion().toString()));
    }
    Optional<PlatformSpecificManifest> optionalPlatformSpecificManifest = platformResolver.findBestMatch(componentRecipe.getManifests());
    if (!optionalPlatformSpecificManifest.isPresent()) {
        return Optional.empty();
    }
    PlatformSpecificManifest platformSpecificManifest = optionalPlatformSpecificManifest.get();
    Set<String> selectors = collectAllSelectors(componentRecipe.getManifests());
    Map<String, DependencyProperties> dependencyPropertiesMap = new HashMap<>();
    if (componentRecipe.getComponentDependencies() != null) {
        dependencyPropertiesMap.putAll(componentRecipe.getComponentDependencies());
    }
    ComponentRecipe packageRecipe = ComponentRecipe.builder().componentName(componentRecipe.getComponentName()).version(componentRecipe.getComponentVersion()).publisher(componentRecipe.getComponentPublisher()).recipeTemplateVersion(componentRecipe.getRecipeFormatVersion()).componentType(componentRecipe.getComponentType()).dependencies(dependencyPropertiesMap).lifecycle(convertLifecycleFromFile(componentRecipe.getLifecycle(), platformSpecificManifest, selectors)).artifacts(convertArtifactsFromFile(platformSpecificManifest.getArtifacts())).componentConfiguration(componentRecipe.getComponentConfiguration()).build();
    return Optional.of(packageRecipe);
}
Also used : PlatformSpecificManifest(com.amazon.aws.iot.greengrass.component.common.PlatformSpecificManifest) HashMap(java.util.HashMap) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) DependencyProperties(com.amazon.aws.iot.greengrass.component.common.DependencyProperties) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe)

Example 5 with ComponentRecipe

use of com.aws.greengrass.componentmanager.models.ComponentRecipe in project aws-greengrass-nucleus by aws-greengrass.

the class ComponentStoreTest method GIVEN_a_recipe_exists_WHEN_findPackageRecipe_THEN_return_it.

@Test
void GIVEN_a_recipe_exists_WHEN_findPackageRecipe_THEN_return_it() throws Exception {
    // GIVEN
    preloadRecipeFileFromTestResource(MONITORING_SERVICE_PKG_RECIPE_FILE_NAME);
    Path sourceRecipe = RECIPE_RESOURCE_PATH.resolve(MONITORING_SERVICE_PKG_RECIPE_FILE_NAME);
    // WHEN
    Optional<ComponentRecipe> optionalPackageRecipe = componentStore.findPackageRecipe(MONITORING_SERVICE_PKG_ID);
    // THEN
    assertTrue(optionalPackageRecipe.isPresent());
    ComponentRecipe expectedRecipe = recipeLoader.loadFromFile(new String(Files.readAllBytes(sourceRecipe))).get();
    assertThat(optionalPackageRecipe.get(), equalTo(expectedRecipe));
}
Also used : Path(java.nio.file.Path) ComponentRecipe(com.aws.greengrass.componentmanager.models.ComponentRecipe) Test(org.junit.jupiter.api.Test)

Aggregations

ComponentRecipe (com.aws.greengrass.componentmanager.models.ComponentRecipe)51 Test (org.junit.jupiter.api.Test)42 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)38 Semver (com.vdurmont.semver4j.Semver)38 HashMap (java.util.HashMap)26 DeploymentPackageConfiguration (com.aws.greengrass.deployment.model.DeploymentPackageConfiguration)21 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)20 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)20 Map (java.util.Map)18 Path (java.nio.file.Path)14 Topics (com.aws.greengrass.config.Topics)12 ConfigurationUpdateOperation (com.aws.greengrass.deployment.model.ConfigurationUpdateOperation)10 Topic (com.aws.greengrass.config.Topic)9 GreengrassService (com.aws.greengrass.lifecyclemanager.GreengrassService)9 IOException (java.io.IOException)6 DependencyProperties (com.amazon.aws.iot.greengrass.component.common.DependencyProperties)5 NoAvailableComponentVersionException (com.aws.greengrass.componentmanager.exceptions.NoAvailableComponentVersionException)5 PackagingException (com.aws.greengrass.componentmanager.exceptions.PackagingException)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)5