Skip to content

Commit

Permalink
feat: adding the enclave-db inside the enclave's volume (#2399)
Browse files Browse the repository at this point in the history
## Description
This is part of the "upgrade engine from UI" project.

With this refactor the APIC's container image will be ready to be
upgraded without losing the services list and file artifacts list.

## REMINDER: Tag Reviewers, so they get notified to review

## Is this change user facing?
NO

## References (if applicable)
<!-- Add relevant Github Issues, Discord threads, or other helpful
information. -->
<!-- You can auto-close issues by putting "Fixes #XXXX" here. -->
  • Loading branch information
leoporoli committed Apr 24, 2024
1 parent 807f208 commit 75805ef
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 19 deletions.
Expand Up @@ -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"
Expand All @@ -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"
Expand Down Expand Up @@ -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")
}
Expand Down
Expand Up @@ -2,6 +2,7 @@ package enclave_db

import (
"os"
"path"
"sync"
"time"

Expand All @@ -11,23 +12,31 @@ import (

const (
readWritePermissionToDatabase = 0666
enclaveDbFilePath = "enclave.db"
enclaveDbFileName = "enclave.db"
timeOut = 10 * time.Second
)

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,
Expand Down
Expand Up @@ -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")
}
Expand Down
17 changes: 9 additions & 8 deletions core/server/api_container/main.go
Expand Up @@ -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)

Expand Down
Expand Up @@ -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)
Expand Down Expand 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
}

0 comments on commit 75805ef

Please sign in to comment.