Search in sources :

Example 1 with Unarchive

use of com.amazon.aws.iot.greengrass.component.common.Unarchive 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);
                }
            }
        }
    }
}
Also used : Path(java.nio.file.Path) SizeLimitException(com.aws.greengrass.componentmanager.exceptions.SizeLimitException) ComponentArtifact(com.aws.greengrass.componentmanager.models.ComponentArtifact) PackageDownloadException(com.aws.greengrass.componentmanager.exceptions.PackageDownloadException) Unarchive(com.amazon.aws.iot.greengrass.component.common.Unarchive) IOException(java.io.IOException) ArtifactDownloader(com.aws.greengrass.componentmanager.builtins.ArtifactDownloader) File(java.io.File)

Aggregations

Unarchive (com.amazon.aws.iot.greengrass.component.common.Unarchive)1 ArtifactDownloader (com.aws.greengrass.componentmanager.builtins.ArtifactDownloader)1 PackageDownloadException (com.aws.greengrass.componentmanager.exceptions.PackageDownloadException)1 SizeLimitException (com.aws.greengrass.componentmanager.exceptions.SizeLimitException)1 ComponentArtifact (com.aws.greengrass.componentmanager.models.ComponentArtifact)1 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1