Search in sources :

Example 1 with PackageLoadingException

use of com.aws.greengrass.componentmanager.exceptions.PackageLoadingException 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 PackageLoadingException

use of com.aws.greengrass.componentmanager.exceptions.PackageLoadingException 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 3 with PackageLoadingException

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

the class ComponentStore method saveComponentRecipe.

/**
 * Save the given component recipe object into component store on the disk.
 *
 * <p>If the target recipe file exist, and its content is the same as the content to be written, it skip the
 * file write operation.
 * If content is different or the target recipe file does not exist, it will write to the file using YAML
 * serializer.
 * </p>
 *
 * @see com.amazon.aws.iot.greengrass.component.common.SerializerFactory#getRecipeSerializer
 * @param componentRecipe raw component recipe
 * @return persisted recipe content in component store on the disk.
 * @throws PackageLoadingException if fails to write the package recipe to disk.
 */
String saveComponentRecipe(@NonNull com.amazon.aws.iot.greengrass.component.common.ComponentRecipe componentRecipe) throws PackageLoadingException {
    ComponentIdentifier componentIdentifier = new ComponentIdentifier(componentRecipe.getComponentName(), componentRecipe.getComponentVersion());
    try {
        String recipeContent = com.amazon.aws.iot.greengrass.component.common.SerializerFactory.getRecipeSerializer().writeValueAsString(componentRecipe);
        Optional<String> componentRecipeContent = findComponentRecipeContent(componentIdentifier);
        if (componentRecipeContent.isPresent() && componentRecipeContent.get().equals(recipeContent)) {
            // same content and no need to write again
            return recipeContent;
        }
        FileUtils.writeStringToFile(resolveRecipePath(componentIdentifier).toFile(), recipeContent);
        return recipeContent;
    } catch (IOException e) {
        // TODO: [P41215929]: Better logging and exception messages in component store
        throw new PackageLoadingException("Failed to save package recipe", e);
    }
}
Also used : ComponentIdentifier(com.aws.greengrass.componentmanager.models.ComponentIdentifier) IOException(java.io.IOException) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)

Example 4 with PackageLoadingException

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

the class ComponentStore method findComponentRecipeContent.

Optional<String> findComponentRecipeContent(@NonNull ComponentIdentifier componentId) throws PackageLoadingException {
    Path recipePath = resolveRecipePath(componentId);
    logger.atDebug().setEventType("finding-package-recipe").addKeyValue("packageRecipePath", recipePath).log();
    if (!Files.exists(recipePath) || !Files.isRegularFile(recipePath)) {
        return Optional.empty();
    }
    try {
        return Optional.of(new String(Files.readAllBytes(recipePath), StandardCharsets.UTF_8));
    } catch (IOException e) {
        throw new PackageLoadingException(String.format("Failed to read package recipe from disk with path: `%s`", recipePath), e);
    }
}
Also used : Path(java.nio.file.Path) IOException(java.io.IOException) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)

Example 5 with PackageLoadingException

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

the class ArtifactDownloaderFactory method getArtifactDownloader.

/**
 * Return the artifact downloader instance.
 * @param identifier componentIdentifier
 * @param artifact componentArtifact
 * @param artifactDir directory to download artifact to
 * @return Artifact downloader
 * @throws PackageLoadingException throw if URI scheme not supported
 * @throws InvalidArtifactUriException throw if s3 url not valid
 */
public ArtifactDownloader getArtifactDownloader(ComponentIdentifier identifier, ComponentArtifact artifact, Path artifactDir) throws PackageLoadingException, InvalidArtifactUriException {
    URI artifactUri = artifact.getArtifactUri();
    String scheme = artifactUri.getScheme() == null ? null : artifactUri.getScheme().toUpperCase();
    if (GREENGRASS_SCHEME.equals(scheme)) {
        return new GreengrassRepositoryDownloader(clientFactory, identifier, artifact, artifactDir, componentStore);
    }
    if (S3_SCHEME.equals(scheme)) {
        return new S3Downloader(s3ClientFactory, identifier, artifact, artifactDir);
    }
    // an artifact downloader can register itself and be discoverable here.
    if (DOCKER_SCHEME.equals(scheme)) {
        return new DockerImageDownloader(identifier, artifact, artifactDir, context);
    }
    throw new PackageLoadingException(String.format("artifact URI scheme %s is not supported yet", scheme));
}
Also used : DockerImageDownloader(com.aws.greengrass.componentmanager.plugins.docker.DockerImageDownloader) PackageLoadingException(com.aws.greengrass.componentmanager.exceptions.PackageLoadingException) URI(java.net.URI)

Aggregations

PackageLoadingException (com.aws.greengrass.componentmanager.exceptions.PackageLoadingException)15 IOException (java.io.IOException)8 ComponentIdentifier (com.aws.greengrass.componentmanager.models.ComponentIdentifier)6 ComponentRecipe (com.aws.greengrass.componentmanager.models.ComponentRecipe)4 Path (java.nio.file.Path)4 PackageDownloadException (com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)2 DeploymentResult (com.aws.greengrass.deployment.model.DeploymentResult)2 Semver (com.vdurmont.semver4j.Semver)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 ComponentRecipe (com.amazon.aws.iot.greengrass.component.common.ComponentRecipe)1 DependencyProperties (com.amazon.aws.iot.greengrass.component.common.DependencyProperties)1 PlatformSpecificManifest (com.amazon.aws.iot.greengrass.component.common.PlatformSpecificManifest)1 RecipeLoader (com.aws.greengrass.componentmanager.converter.RecipeLoader)1 SizeLimitException (com.aws.greengrass.componentmanager.exceptions.SizeLimitException)1 DockerImageDownloader (com.aws.greengrass.componentmanager.plugins.docker.DockerImageDownloader)1 CaseInsensitiveString (com.aws.greengrass.config.CaseInsensitiveString)1 DeploymentTaskFailureException (com.aws.greengrass.deployment.exceptions.DeploymentTaskFailureException)1 DeploymentDocument (com.aws.greengrass.deployment.model.DeploymentDocument)1