use of com.aws.greengrass.componentmanager.exceptions.SizeLimitException 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.SizeLimitException in project aws-greengrass-nucleus by aws-greengrass.
the class ComponentManager method prepareArtifacts.
void prepareArtifacts(ComponentIdentifier componentIdentifier, List<ComponentArtifact> artifacts) throws PackageLoadingException, PackageDownloadException, InvalidArtifactUriException, InterruptedException {
if (artifacts == null) {
logger.atWarn().kv(PACKAGE_IDENTIFIER, componentIdentifier).log("Artifact list was null, expected non-null and non-empty");
return;
}
Path packageArtifactDirectory = componentStore.resolveArtifactDirectoryPath(componentIdentifier);
logger.atDebug().setEventType("downloading-package-artifacts").addKeyValue(PACKAGE_IDENTIFIER, componentIdentifier).log();
for (ComponentArtifact artifact : artifacts) {
ArtifactDownloader downloader = artifactDownloaderFactory.getArtifactDownloader(componentIdentifier, artifact, packageArtifactDirectory);
if (downloader.downloadRequired()) {
Optional<String> errorMsg = downloader.checkDownloadable();
if (errorMsg.isPresent()) {
throw new PackageDownloadException(String.format("Download required for artifact %s but device configs are invalid: %s", artifact.getArtifactUri(), errorMsg.get()));
}
// Check disk size limits before download
// TODO: [P41215447]: Check artifact size for all artifacts to download early to fail early
long usableSpaceBytes = componentStore.getUsableSpace();
if (usableSpaceBytes < DEFAULT_MIN_DISK_AVAIL_BYTES) {
throw new SizeLimitException(String.format("Disk space critical: %d bytes usable, %d bytes minimum allowed", usableSpaceBytes, DEFAULT_MIN_DISK_AVAIL_BYTES));
}
if (downloader.checkComponentStoreSize()) {
long downloadSize = downloader.getDownloadSize();
long storeContentSize = componentStore.getContentSize();
if (storeContentSize + downloadSize > getConfiguredMaxSize()) {
throw new SizeLimitException(String.format("Component store size limit reached: %d bytes existing, %d bytes needed" + ", %d bytes maximum allowed total", storeContentSize, downloadSize, getConfiguredMaxSize()));
}
}
try {
downloader.download();
} catch (IOException e) {
throw new PackageDownloadException(String.format("Failed to download component %s artifact %s", componentIdentifier, artifact), e);
}
}
if (downloader.canSetFilePermissions()) {
File artifactFile = downloader.getArtifactFile();
if (artifactFile != null) {
try {
Permissions.setArtifactPermission(artifactFile.toPath(), artifact.getPermission().toFileSystemPermission());
} catch (IOException e) {
throw new PackageDownloadException(String.format("Failed to change permissions of component %s artifact %s", componentIdentifier, artifact), e);
}
}
}
if (downloader.canUnarchiveArtifact()) {
Unarchive unarchive = artifact.getUnarchive();
if (unarchive == null) {
unarchive = Unarchive.NONE;
}
File artifactFile = downloader.getArtifactFile();
if (artifactFile != null && !unarchive.equals(Unarchive.NONE)) {
try {
Path unarchivePath = nucleusPaths.unarchiveArtifactPath(componentIdentifier, getFileName(artifactFile));
unarchiver.unarchive(unarchive, artifactFile, unarchivePath);
if (downloader.canSetFilePermissions()) {
try {
Permissions.setArtifactPermission(unarchivePath, artifact.getPermission().toFileSystemPermission());
} catch (IOException e) {
throw new PackageDownloadException(String.format("Failed to change permissions of component %s artifact %s", componentIdentifier, artifact), e);
}
}
} catch (IOException e) {
throw new PackageDownloadException(String.format("Failed to unarchive component %s artifact %s", componentIdentifier, artifact), e);
}
}
}
}
}
Aggregations