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());
}
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;
}
}
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);
}
}
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);
}
}
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));
}
Aggregations