From 7dfdfe13fd22569d056440dc936d4065ba90327e Mon Sep 17 00:00:00 2001 From: Robert Lin Date: Wed, 6 Jun 2018 23:16:03 -0700 Subject: [PATCH] Refactor ConfigManager -> DataManager --- daemon/inertiad/daemon.go | 6 ++--- .../inertiad/project/{config.go => data.go} | 22 +++++++++---------- .../project/{config_test.go => data_test.go} | 4 ++-- daemon/inertiad/project/deployment.go | 10 ++++----- daemon/inertiad/up.go | 11 +++++----- 5 files changed, 27 insertions(+), 26 deletions(-) rename daemon/inertiad/project/{config.go => data.go} (85%) rename daemon/inertiad/project/{config_test.go => data_test.go} (87%) diff --git a/daemon/inertiad/daemon.go b/daemon/inertiad/daemon.go index c4a4cd6a..97164e7e 100644 --- a/daemon/inertiad/daemon.go +++ b/daemon/inertiad/daemon.go @@ -31,7 +31,7 @@ var ( sslDirectory = "/app/host/inertia/config/ssl/" userDatabasePath = "/app/host/inertia/data/users.db" - deploymentDatabasePath = "/app/host/inertia/data/deployments.db" + deploymentDatabasePath = "/app/host/inertia/data/project.db" ) const ( @@ -48,7 +48,7 @@ func run(host, port, version, keyPath, certDir, userDir string) { sslDirectory = certDir } if userDir != "" { - auth.UserDatabasePath = userDir + userDatabasePath = userDir } var ( @@ -85,7 +85,7 @@ func run(host, port, version, keyPath, certDir, userDir string) { webPrefix := "/web/" handler, err := auth.NewPermissionsHandler( - auth.UserDatabasePath, host, 120, + userDatabasePath, host, 120, ) if err != nil { println(err.Error()) diff --git a/daemon/inertiad/project/config.go b/daemon/inertiad/project/data.go similarity index 85% rename from daemon/inertiad/project/config.go rename to daemon/inertiad/project/data.go index 93925c9e..757d6729 100644 --- a/daemon/inertiad/project/config.go +++ b/daemon/inertiad/project/data.go @@ -16,8 +16,8 @@ var ( envVariableBucket = []byte("envVariables") ) -// ConfigManager stores persistent deployment configuration -type ConfigManager struct { +// DataManager stores persistent deployment configuration +type DataManager struct { // db is a boltdb database, which is an embedded // key/value database where each "bucket" is a collection db *bolt.DB @@ -31,7 +31,7 @@ type ConfigManager struct { decryptPrivateKey *[32]byte } -func newConfigManager(dbPath string) (*ConfigManager, error) { +func newDataManager(dbPath string) (*DataManager, error) { db, err := bolt.Open(dbPath, 0600, nil) if err != nil { return nil, err @@ -58,7 +58,7 @@ func newConfigManager(dbPath string) (*ConfigManager, error) { return nil, err } - return &ConfigManager{ + return &DataManager{ db, encryptPublicKey, encryptPrivateKey, decryptPublicKey, decryptPrivateKey, @@ -67,7 +67,7 @@ func newConfigManager(dbPath string) (*ConfigManager, error) { // AddEnvVariable adds a new environment variable that will be applied // to all project containers -func (c *ConfigManager) AddEnvVariable(name, value string, +func (c *DataManager) AddEnvVariable(name, value string, encrypt bool) error { if len(name) == 0 || len(value) == 0 { return errors.New("invalid env configuration") @@ -106,22 +106,22 @@ func (c *ConfigManager) AddEnvVariable(name, value string, } // RemoveEnvVariable removes a previously set env variable -func (c *ConfigManager) RemoveEnvVariable(name, value string) error { +func (c *DataManager) RemoveEnvVariable(name, value string) error { return nil } // AddPersistentFile adds a new persistent file to place in the project // directory at buildtime -func (c *ConfigManager) AddPersistentFile(path string) error { +func (c *DataManager) AddPersistentFile(path string) error { return nil } // RemovePersistentFile removes a persistent file -func (c *ConfigManager) RemovePersistentFile(path string) error { +func (c *DataManager) RemovePersistentFile(path string) error { return nil } -func (c *ConfigManager) getEnvVariables() (map[string]string, error) { +func (c *DataManager) getEnvVariables() (map[string]string, error) { env := map[string]string{} err := c.db.View(func(tx *bolt.Tx) error { @@ -163,11 +163,11 @@ func (c *ConfigManager) getEnvVariables() (map[string]string, error) { return env, nil } -func (c *ConfigManager) getPersistentFiles() ([]string, error) { +func (c *DataManager) getPersistentFiles() ([]string, error) { return nil, nil } -func (c *ConfigManager) destroy() error { +func (c *DataManager) destroy() error { return c.db.Update(func(tx *bolt.Tx) error { return nil }) diff --git a/daemon/inertiad/project/config_test.go b/daemon/inertiad/project/data_test.go similarity index 87% rename from daemon/inertiad/project/config_test.go rename to daemon/inertiad/project/data_test.go index d6effc98..8dcfd810 100644 --- a/daemon/inertiad/project/config_test.go +++ b/daemon/inertiad/project/data_test.go @@ -8,7 +8,7 @@ import ( "github.com/stretchr/testify/assert" ) -func TestConfigManager_EnvVariableOperations(t *testing.T) { +func TestDataManager_EnvVariableOperations(t *testing.T) { type args struct { name string value string @@ -30,7 +30,7 @@ func TestConfigManager_EnvVariableOperations(t *testing.T) { assert.Nil(t, err) defer os.RemoveAll(dir) - c, err := newConfigManager(path.Join(dir, "deployment.db")) + c, err := newDataManager(path.Join(dir, "deployment.db")) assert.Nil(t, err) err = c.AddEnvVariable(tt.args.name, tt.args.value, tt.args.encrypt) diff --git a/daemon/inertiad/project/deployment.go b/daemon/inertiad/project/deployment.go index 06cc74c3..50af3e75 100644 --- a/daemon/inertiad/project/deployment.go +++ b/daemon/inertiad/project/deployment.go @@ -47,7 +47,7 @@ type Deployment struct { auth ssh.AuthMethod mux sync.Mutex - ConfigManager *ConfigManager + DataManager *DataManager } // DeploymentConfig is used to configure Deployment @@ -79,7 +79,7 @@ func NewDeployment(cfg DeploymentConfig, out io.Writer) (*Deployment, error) { } // Set up deployment database - manager, err := newConfigManager(cfg.DatabasePath) + manager, err := newDataManager(cfg.DatabasePath) if err != nil { return nil, err } @@ -104,8 +104,8 @@ func NewDeployment(cfg DeploymentConfig, out io.Writer) (*Deployment, error) { auth: authMethod, repo: repo, - // Persistent configuration manager - ConfigManager: manager, + // Persistent data manager + DataManager: manager, }, nil } @@ -191,7 +191,7 @@ func (d *Deployment) Destroy(cli *docker.Client, out io.Writer) error { d.mux.Lock() defer d.mux.Unlock() - err := d.ConfigManager.destroy() + err := d.DataManager.destroy() if err != nil { fmt.Fprint(out, "unable to clear database records: "+err.Error()) } diff --git a/daemon/inertiad/up.go b/daemon/inertiad/up.go index 13903dd3..e201940d 100644 --- a/daemon/inertiad/up.go +++ b/daemon/inertiad/up.go @@ -45,11 +45,12 @@ func upHandler(w http.ResponseWriter, r *http.Request) { if deployment == nil { logger.Println("No deployment detected") d, err := project.NewDeployment(project.DeploymentConfig{ - ProjectName: upReq.Project, - BuildType: upReq.BuildType, - RemoteURL: gitOpts.RemoteURL, - Branch: gitOpts.Branch, - PemFilePath: auth.DaemonGithubKeyLocation, + ProjectName: upReq.Project, + BuildType: upReq.BuildType, + RemoteURL: gitOpts.RemoteURL, + Branch: gitOpts.Branch, + PemFilePath: auth.DaemonGithubKeyLocation, + DatabasePath: deploymentDatabasePath, }, logger) if err != nil { logger.WriteErr(err.Error(), http.StatusPreconditionFailed)