diff --git a/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/backend_creator/backend_creator.go b/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/backend_creator/backend_creator.go index 6fcd10665a..86d787d599 100644 --- a/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/backend_creator/backend_creator.go +++ b/container-engine-lib/lib/backend_impls/docker/docker_kurtosis_backend/backend_creator/backend_creator.go @@ -3,6 +3,7 @@ package backend_creator import ( "context" "fmt" + "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/database_accessors/enclave_db" "net" "os" "path" @@ -18,7 +19,6 @@ import ( "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface" "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/configs" "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/backend_interface/objects/enclave" - "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/database_accessors/enclave_db" "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/database_accessors/enclave_db/free_ip_addr_tracker" "github.com/kurtosis-tech/kurtosis/container-engine-lib/lib/database_accessors/enclave_db/service_registration" "github.com/kurtosis-tech/stacktrace" @@ -205,7 +205,9 @@ func getDockerKurtosisBackend( var serviceRegistrationRepository *service_registration.ServiceRegistrationRepository if optionalApiContainerModeArgs != nil { productionMode = optionalApiContainerModeArgs.IsProduction - enclaveDb, err := enclave_db.GetOrCreateEnclaveDatabase() + // using the noEnclaveDatabaseDirpath because at this point we know that the enclave database has been created, so we are getting it from this call + noEnclaveDatabaseDirpath := "" + enclaveDb, err := enclave_db.GetOrCreateEnclaveDatabase(noEnclaveDatabaseDirpath) if err != nil { return nil, stacktrace.Propagate(err, "An error occurred opening local database") } diff --git a/container-engine-lib/lib/database_accessors/enclave_db/enclave_db.go b/container-engine-lib/lib/database_accessors/enclave_db/enclave_db.go index f216db350f..f72ec3e214 100644 --- a/container-engine-lib/lib/database_accessors/enclave_db/enclave_db.go +++ b/container-engine-lib/lib/database_accessors/enclave_db/enclave_db.go @@ -2,6 +2,7 @@ package enclave_db import ( "os" + "path" "sync" "time" @@ -11,7 +12,7 @@ import ( const ( readWritePermissionToDatabase = 0666 - enclaveDbFilePath = "enclave.db" + enclaveDbFileName = "enclave.db" timeOut = 10 * time.Second ) @@ -19,15 +20,23 @@ var ( openDatabaseOnce sync.Once databaseInstance *bolt.DB databaseOpenError error + enclaveDbDirpath string ) type EnclaveDB struct { *bolt.DB } -func GetOrCreateEnclaveDatabase() (*EnclaveDB, error) { +func GetOrCreateEnclaveDatabase(enclaveDatabaseDirpath string) (*EnclaveDB, error) { + + //Checking first if there is already one enclaveDbDirpath and if it's different + if enclaveDbDirpath != "" && enclaveDbDirpath != enclaveDatabaseDirpath { + return nil, stacktrace.NewError("It's not possible to create a new enclave database in '%s' because there is already one in '%s'", enclaveDatabaseDirpath, enclaveDbDirpath) + } + openDatabaseOnce.Do(func() { - databaseInstance, databaseOpenError = bolt.Open(enclaveDbFilePath, readWritePermissionToDatabase, &bolt.Options{ + enclaveDatabaseFilepath := path.Join(enclaveDatabaseDirpath, enclaveDbFileName) + databaseInstance, databaseOpenError = bolt.Open(enclaveDatabaseFilepath, readWritePermissionToDatabase, &bolt.Options{ Timeout: timeOut, //to fail if any other process is locking the file NoGrowSync: false, NoFreelistSync: false, diff --git a/container-engine-lib/lib/database_accessors/enclave_db/file_artifacts_db/file_artifacts_db.go b/container-engine-lib/lib/database_accessors/enclave_db/file_artifacts_db/file_artifacts_db.go index c8bb1e5901..90f9afe0a7 100644 --- a/container-engine-lib/lib/database_accessors/enclave_db/file_artifacts_db/file_artifacts_db.go +++ b/container-engine-lib/lib/database_accessors/enclave_db/file_artifacts_db/file_artifacts_db.go @@ -109,7 +109,9 @@ func GetOrCreateNewFileArtifactsDb() (*FileArtifactPersisted, error) { map[string][]string{}, map[string][]byte{}, } - db, err := enclave_db.GetOrCreateEnclaveDatabase() + // using the noEnclaveDatabaseDirpath because at this point we know that the enclave database has been created, so we are getting it from this call + noEnclaveDatabaseDirpath := "" + db, err := enclave_db.GetOrCreateEnclaveDatabase(noEnclaveDatabaseDirpath) if err != nil { return nil, stacktrace.Propagate(err, "Failed to get enclave database") } diff --git a/core/server/api_container/main.go b/core/server/api_container/main.go index 6541459b31..2d22cd0624 100644 --- a/core/server/api_container/main.go +++ b/core/server/api_container/main.go @@ -112,25 +112,26 @@ func runMain() error { enclaveDataDir := enclave_data_directory.NewEnclaveDataDirectory(serverArgs.EnclaveDataVolumeDirpath) - filesArtifactStore, err := enclaveDataDir.GetFilesArtifactStore() - if err != nil { - return stacktrace.Propagate(err, "An error occurred getting the files artifact store") - } - clusterConfig := serverArgs.KurtosisBackendConfig if clusterConfig == nil { return stacktrace.NewError("Kurtosis backend type is '%v' but cluster configuration parameters are null.", args.KurtosisBackendType_Kubernetes.String()) } - enclaveDb, err := enclave_db.GetOrCreateEnclaveDatabase() + repositoriesDirPath, tempDirectoriesDirPath, githubAuthDirPath, enclaveDatabaseDirpath, err := enclaveDataDir.GetEnclaveDataDirectoryPaths() + if err != nil { + return stacktrace.Propagate(err, "An error occurred getting directory paths of the enclave data directory.") + } + + enclaveDb, err := enclave_db.GetOrCreateEnclaveDatabase(enclaveDatabaseDirpath) if err != nil { return stacktrace.Propagate(err, "An error occurred while getting the enclave db") } - repositoriesDirPath, tempDirectoriesDirPath, githubAuthDirPath, err := enclaveDataDir.GetEnclaveDataDirectoryPaths() + filesArtifactStore, err := enclaveDataDir.GetFilesArtifactStore() if err != nil { - return stacktrace.Propagate(err, "An error occurred getting directory paths of the enclave data directory.") + return stacktrace.Propagate(err, "An error occurred getting the files artifact store") } + githubAuthProvider := git_package_content_provider.NewGitHubPackageAuthProvider(githubAuthDirPath) gitPackageContentProvider := git_package_content_provider.NewGitPackageContentProvider(repositoriesDirPath, tempDirectoriesDirPath, githubAuthProvider, enclaveDb) diff --git a/core/server/commons/enclave_data_directory/enclave_data_directory.go b/core/server/commons/enclave_data_directory/enclave_data_directory.go index 3c1ba516ab..f01b73ba83 100644 --- a/core/server/commons/enclave_data_directory/enclave_data_directory.go +++ b/core/server/commons/enclave_data_directory/enclave_data_directory.go @@ -27,6 +27,9 @@ const ( // Name of directory INSIDE THE ENCLAVE DATA DIR at [absMountDirPath] that contains info for authenticating GitHub operations githubAuthStoreDirname = "github-auth" + + // Name of directory INSIDE THE ENCLAVE DATA DIR containing the enclave database (currently the bolt dB is implemented) + enclaveDatabase = "enclave-database" ) // A directory containing all the data associated with a certain enclave (i.e. a Docker subnetwork where services are spun up) @@ -67,21 +70,26 @@ func (dir EnclaveDataDirectory) GetFilesArtifactStore() (*FilesArtifactStore, er return currentFilesArtifactStore, dbError } -func (dir EnclaveDataDirectory) GetEnclaveDataDirectoryPaths() (string, string, string, error) { +func (dir EnclaveDataDirectory) GetEnclaveDataDirectoryPaths() (string, string, string, string, error) { repositoriesStoreDirpath := path.Join(dir.absMountDirpath, repositoriesStoreDirname) if err := ensureDirpathExists(repositoriesStoreDirpath); err != nil { - return "", "", "", stacktrace.Propagate(err, "An error occurred ensuring the repositories store dirpath '%v' exists.", repositoriesStoreDirpath) + return "", "", "", "", stacktrace.Propagate(err, "An error occurred ensuring the repositories store dirpath '%v' exists.", repositoriesStoreDirpath) } tempRepositoriesStoreDirpath := path.Join(dir.absMountDirpath, tmpRepositoriesStoreDirname) if err := ensureDirpathExists(tempRepositoriesStoreDirpath); err != nil { - return "", "", "", stacktrace.Propagate(err, "An error occurred ensuring the temporary repositories store dirpath '%v' exists.", tempRepositoriesStoreDirpath) + return "", "", "", "", stacktrace.Propagate(err, "An error occurred ensuring the temporary repositories store dirpath '%v' exists.", tempRepositoriesStoreDirpath) } githubAuthStoreDirpath := path.Join(dir.absMountDirpath, githubAuthStoreDirname) if err := ensureDirpathExists(githubAuthStoreDirpath); err != nil { - return "", "", "", stacktrace.Propagate(err, "An error occurred ensuring the GitHub auth store dirpath '%v' exists.", githubAuthStoreDirpath) + return "", "", "", "", stacktrace.Propagate(err, "An error occurred ensuring the GitHub auth store dirpath '%v' exists.", githubAuthStoreDirpath) + } + + enclaveDatabaseDirpath := path.Join(dir.absMountDirpath, enclaveDatabase) + if err := ensureDirpathExists(enclaveDatabaseDirpath); err != nil { + return "", "", "", "", stacktrace.Propagate(err, "An error occurred ensuring the enclave database store dirpath '%v' exists.", enclaveDatabaseDirpath) } - return repositoriesStoreDirpath, tempRepositoriesStoreDirpath, githubAuthStoreDirpath, nil + return repositoriesStoreDirpath, tempRepositoriesStoreDirpath, githubAuthStoreDirpath, enclaveDatabaseDirpath, nil }