Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add files in enclave dump #2136

Merged
merged 9 commits into from
Feb 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -893,6 +893,10 @@ jobs:
- run: "${KURTOSIS_BINPATH} files storeservice test-enclave test1 /usr/local/apache2/conf/httpd.conf --name stored-file"
- run: "${KURTOSIS_BINPATH} files download test-enclave stored-file ."

# Check Dump Contains files
- run: "${KURTOSIS_BINPATH} enclave dump test-enclave test-dump"
- run: "ls test-dump/files/rendered-file"

# Module inside an enclave
- run: "${KURTOSIS_BINPATH} enclave ls"
- run: "${KURTOSIS_BINPATH} enclave inspect test-enclave"
Expand Down
45 changes: 43 additions & 2 deletions cli/cli/commands/enclave/dump/dump.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,14 @@ import (
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/args"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/flags"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/files"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/enclave"
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/metrics_client"
"github.com/kurtosis-tech/stacktrace"
"github.com/sirupsen/logrus"
"os"
"path"
)

const (
Expand All @@ -31,6 +34,9 @@ const (
defaultEnclaveDumpDir = "kurtosis-dump"
enclaveDumpSeparator = "--"
outputDirIsOptional = true

filesArtifactDestinationDirPermission = 0o777
filesArtifactFolderName = "files"
)

var EnclaveDumpCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisCommand{
Expand Down Expand Up @@ -85,16 +91,51 @@ func run(
}

enclaveUuid := enclaveInfo.GetEnclaveUuid()

if enclaveOutputDirpath == defaultEnclaveDumpDir {
enclaveName := enclaveInfo.GetName()
enclaveOutputDirpath = fmt.Sprintf("%s%s%s", enclaveName, enclaveDumpSeparator, enclaveUuid)
}

if err := kurtosisBackend.DumpEnclave(ctx, enclave.EnclaveUUID(enclaveUuid), enclaveOutputDirpath); err != nil {
if err = kurtosisBackend.DumpEnclave(ctx, enclave.EnclaveUUID(enclaveUuid), enclaveOutputDirpath); err != nil {
return stacktrace.Propagate(err, "An error occurred dumping enclave '%v' to '%v'", enclaveIdentifier, enclaveOutputDirpath)
}

if enclaveInfo.ApiContainerStatus != kurtosis_engine_rpc_api_bindings.EnclaveAPIContainerStatus_EnclaveAPIContainerStatus_RUNNING {
logrus.Debugf("Couldn't dump file information as the enclave '%v' is not running", enclaveIdentifier)
h4ck3rk3y marked this conversation as resolved.
Show resolved Hide resolved
logrus.Infof("Dumped enclave '%v' to directory '%v'", enclaveIdentifier, enclaveOutputDirpath)
return nil
}

enclaveCtx, err := kurtosisCtx.GetEnclaveContext(ctx, enclaveIdentifier)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while retrieving enclave context for enclave with identifier '%v'", enclaveIdentifier)
}

filesInEnclave, err := enclaveCtx.GetAllFilesArtifactNamesAndUuids(ctx)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while fetching files artifact in enclave '%v'", enclaveIdentifier)
}

if len(filesInEnclave) == 0 {
logrus.Infof("Dumped enclave '%v' to directory '%v'", enclaveIdentifier, enclaveOutputDirpath)
return nil
}

filesDownloadFolder := path.Join(enclaveOutputDirpath, filesArtifactFolderName)
if err = os.Mkdir(filesDownloadFolder, filesArtifactDestinationDirPermission); err != nil {
return stacktrace.Propagate(err, "An error occurred while creating a folder '%v' to download files to", filesArtifactFolderName)
}

for _, fileNameAndUuid := range filesInEnclave {
fileDownloadPath := path.Join(filesDownloadFolder, fileNameAndUuid.GetFileName())
if err = os.Mkdir(fileDownloadPath, filesArtifactDestinationDirPermission); err != nil {
return stacktrace.Propagate(err, "An error occurred while creating directory '%v' to write files artifact '%v'", fileDownloadPath, fileNameAndUuid.GetFileName())
}
if err = files.DownloadAndExtractFilesArtifact(ctx, enclaveCtx, fileNameAndUuid.GetFileName(), fileDownloadPath); err != nil {
return stacktrace.Propagate(err, "An error occurred while downloading and extracting file '%v'", fileNameAndUuid.GetFileName())
}
}

logrus.Infof("Dumped enclave '%v' to directory '%v'", enclaveIdentifier, enclaveOutputDirpath)
return nil
}
44 changes: 6 additions & 38 deletions cli/cli/commands/files/download/download.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,12 @@ import (
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/args"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_framework/lowlevel/flags"
"github.com/kurtosis-tech/kurtosis/cli/cli/command_str_consts"
"github.com/kurtosis-tech/kurtosis/cli/cli/helpers/files"
"github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface"
"github.com/kurtosis-tech/kurtosis/metrics-library/golang/lib/metrics_client"
"github.com/kurtosis-tech/stacktrace"
"github.com/mholt/archiver"
"github.com/sirupsen/logrus"
"os"
"path"
"path/filepath"
)

Expand All @@ -38,15 +37,10 @@ const (
noExtractFlagKey = "no-extract"
noExtractFlagDefaultValue = "false"

filesArtifactExtension = ".tgz"
filesArtifactPermission = 0o744
filesArtifactDestinationDirPermission = 0o777

kurtosisBackendCtxKey = "kurtosis-backend"
engineClientCtxKey = "engine-client"

defaultTmpDir = ""
tmpDirPattern = "tmp-dir-for-download-*"
)

var FilesUploadCmd = &engine_consuming_kurtosis_command.EngineConsumingKurtosisCommand{
Expand Down Expand Up @@ -139,45 +133,19 @@ func run(
return stacktrace.Propagate(err, "An error occurred getting the enclave context for enclave '%v'", enclaveIdentifier)
}

artifactBytes, err := enclaveCtx.DownloadFilesArtifact(ctx, artifactIdentifier)
if err != nil {
return stacktrace.Propagate(err, "An error occurred downloading files with identifier '%v' from enclave '%v'", artifactIdentifier, enclaveIdentifier)
}

fileNameToWriteTo := fmt.Sprintf("%v%v", artifactIdentifier, filesArtifactExtension)
destinationPathToDownloadFileTo := path.Join(absoluteDestinationPath, fileNameToWriteTo)

// if the user doesn't want to extract, we just download and return
if shouldNotExtract {
err = os.WriteFile(destinationPathToDownloadFileTo, artifactBytes, filesArtifactPermission)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while writing bytes to file '%v' with permission '%v'", destinationPathToDownloadFileTo, filesArtifactPermission)
if err = files.DownloadFilesArtifactToLocation(ctx, enclaveCtx, artifactIdentifier, absoluteDestinationPath); err != nil {
return stacktrace.Propagate(err, "An error occurred while downloading file '%v' to '%v' for enclave '%v'", artifactIdentifier, absoluteDestinationPath, enclaveIdentifier)
}
logrus.Infof("File package with identifier '%v' downloaded to '%v'", artifactIdentifier, destinationPathToDownloadFileTo)
logrus.Infof("File package with identifier '%v' downloaded to '%v'", artifactIdentifier, absoluteDestinationPath)
return nil
}

tmpDirPath, err := os.MkdirTemp(defaultTmpDir, tmpDirPattern)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while creating a temporary directory to download the files artifact with identifier '%v' to", artifactIdentifier)
}
shouldCleanupTmpDir := false
defer func() {
if shouldCleanupTmpDir {
os.RemoveAll(tmpDirPath)
}
}()
tmpFileToWriteTo := path.Join(tmpDirPath, fileNameToWriteTo)
err = os.WriteFile(tmpFileToWriteTo, artifactBytes, filesArtifactPermission)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while writing bytes to file '%v' with permission '%v'", tmpDirPath, filesArtifactPermission)
}
err = archiver.Unarchive(tmpFileToWriteTo, absoluteDestinationPath)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while extracting '%v' to '%v'", tmpFileToWriteTo, destinationPathToDownloadFileTo)
if err = files.DownloadAndExtractFilesArtifact(ctx, enclaveCtx, artifactIdentifier, absoluteDestinationPath); err != nil {
return stacktrace.Propagate(err, "An error occurred while downloading and extracting file '%v' to '%v' for enclave '%v'", artifactIdentifier, absoluteDestinationPath, enclaveIdentifier)
}
logrus.Infof("File package with identifier '%v' extracted to '%v'", artifactIdentifier, absoluteDestinationPath)

shouldCleanupTmpDir = true
return nil
}
67 changes: 67 additions & 0 deletions cli/cli/helpers/files/donwload.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package files

import (
"context"
"fmt"
"github.com/kurtosis-tech/kurtosis/api/golang/core/lib/enclaves"
"github.com/kurtosis-tech/stacktrace"
"github.com/mholt/archiver"
"os"
"path"
)

const (
filesArtifactExtension = ".tgz"
filesArtifactPermission = 0o744

defaultTmpDir = ""
tmpDirPattern = "tmp-dir-for-download-*"
)

func DownloadFilesArtifactToLocation(ctx context.Context, enclaveCtx *enclaves.EnclaveContext, artifactIdentifier string, absoluteDestinationPath string) error {

artifactBytes, err := enclaveCtx.DownloadFilesArtifact(ctx, artifactIdentifier)
if err != nil {
return stacktrace.Propagate(err, "An error occurred downloading files with identifier '%v' from enclave '%v'", artifactIdentifier, enclaveCtx.GetEnclaveName())
}

fileNameToWriteTo := fmt.Sprintf("%v%v", artifactIdentifier, filesArtifactExtension)
destinationPathToDownloadFileTo := path.Join(absoluteDestinationPath, fileNameToWriteTo)

err = os.WriteFile(destinationPathToDownloadFileTo, artifactBytes, filesArtifactPermission)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while writing bytes to file '%v' with permission '%v'", destinationPathToDownloadFileTo, filesArtifactPermission)
}
return nil
}

func DownloadAndExtractFilesArtifact(ctx context.Context, enclaveCtx *enclaves.EnclaveContext, artifactIdentifier string, absoluteDestinationPath string) error {
artifactBytes, err := enclaveCtx.DownloadFilesArtifact(ctx, artifactIdentifier)
if err != nil {
return stacktrace.Propagate(err, "An error occurred downloading files with identifier '%v' from enclave '%v'", artifactIdentifier, enclaveCtx.GetEnclaveName())
}
fileNameToWriteTo := fmt.Sprintf("%v%v", artifactIdentifier, filesArtifactExtension)

tmpDirPath, err := os.MkdirTemp(defaultTmpDir, tmpDirPattern)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while creating a temporary directory to download the files artifact with identifier '%v' to", artifactIdentifier)
}
shouldCleanupTmpDir := false
defer func() {
if shouldCleanupTmpDir {
os.RemoveAll(tmpDirPath)
}
}()
tmpFileToWriteTo := path.Join(tmpDirPath, fileNameToWriteTo)
err = os.WriteFile(tmpFileToWriteTo, artifactBytes, filesArtifactPermission)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while writing bytes to file '%v' with permission '%v'", tmpDirPath, filesArtifactPermission)
}
err = archiver.Unarchive(tmpFileToWriteTo, absoluteDestinationPath)
if err != nil {
return stacktrace.Propagate(err, "An error occurred while extracting '%v' to '%v'", tmpFileToWriteTo, absoluteDestinationPath)
}

shouldCleanupTmpDir = true
return nil
}
Loading