Skip to content

Commit d47ee20

Browse files
committed
Merge branch '309-instance-id-location' into 'master'
feat: move the instance_id file to the metadata directory (#309) Closes #309 See merge request postgres-ai/database-lab!396
2 parents ec7631f + 6baa5cb commit d47ee20

File tree

3 files changed

+27
-10
lines changed

3 files changed

+27
-10
lines changed

Diff for: cmd/database-lab/main.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ func getEngineProperties(ctx context.Context, dockerCLI *client.Client, cfg *con
208208
return global.EngineProps{}, fmt.Errorf("failed to inspect DLE container: %w", err)
209209
}
210210

211-
instanceID, err := config.LoadInstanceID(cfg.PoolManager.MountDir)
211+
instanceID, err := config.LoadInstanceID()
212212
if err != nil {
213213
return global.EngineProps{}, fmt.Errorf("failed to load instance ID: %w", err)
214214
}

Diff for: pkg/config/config.go

+11-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ package config
88
import (
99
"fmt"
1010
"os"
11-
"path/filepath"
11+
"path"
1212

1313
"github.com/pkg/errors"
1414
"github.com/rs/xid"
@@ -61,16 +61,24 @@ func LoadConfiguration() (*Config, error) {
6161
}
6262

6363
// LoadInstanceID tries to make instance ID persistent across runs and load its value after restart
64-
func LoadInstanceID(mountDir string) (string, error) {
64+
func LoadInstanceID() (string, error) {
6565
instanceID := ""
66-
idFilepath := filepath.Join(mountDir, instanceIDFile)
66+
67+
idFilepath, err := util.GetMetaPath(instanceIDFile)
68+
if err != nil {
69+
return "", fmt.Errorf("failed to get path of the instanceID file: %w", err)
70+
}
6771

6872
data, err := os.ReadFile(idFilepath)
6973
if err != nil {
7074
if os.IsNotExist(err) {
7175
instanceID = xid.New().String()
7276
log.Dbg("no instance_id file was found, generate new instance ID", instanceID)
7377

78+
if err := os.MkdirAll(path.Dir(idFilepath), 0755); err != nil {
79+
return "", fmt.Errorf("failed to make directory meta: %w", err)
80+
}
81+
7482
return instanceID, os.WriteFile(idFilepath, []byte(instanceID), 0544)
7583
}
7684

Diff for: pkg/config/config_test.go

+15-6
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,15 @@ package config
33
import (
44
"bytes"
55
"os"
6+
"path"
67
"path/filepath"
78
"testing"
89

910
"github.com/rs/xid"
1011
"github.com/stretchr/testify/require"
1112
"github.com/stretchr/testify/suite"
13+
14+
"gitlab.com/postgres-ai/database-lab/v3/pkg/util"
1215
)
1316

1417
func TestLoadConfig(t *testing.T) {
@@ -59,22 +62,28 @@ func (s *ConfigSuite) TearDownTest() {
5962
}
6063

6164
func (s *ConfigSuite) TestGenerateNewID() {
62-
instanceID, err := LoadInstanceID(s.mountDir)
65+
instanceID, err := LoadInstanceID()
6366
s.Require().NoError(err)
6467
s.NotEmpty(instanceID)
6568

66-
data, err := os.ReadFile(filepath.Join(s.mountDir, "instance_id"))
67-
s.NoError(err)
69+
instanceIDPath, err := util.GetMetaPath("instance_id")
70+
s.Require().NoError(err)
71+
data, err := os.ReadFile(instanceIDPath)
72+
s.Require().NoError(err)
6873
s.Equal(instanceID, string(data))
6974
}
7075

7176
func (s *ConfigSuite) TestLoadInstanceID() {
7277
instanceID := xid.New().String()
7378

74-
err := os.WriteFile(filepath.Join(s.mountDir, "instance_id"), []byte(instanceID), 0600)
79+
instanceIDPath, err := util.GetMetaPath("instance_id")
80+
s.Require().NoError(err)
81+
err = os.MkdirAll(path.Dir(instanceIDPath), 0755)
82+
s.Require().NoError(err)
83+
err = os.WriteFile(instanceIDPath, []byte(instanceID), 0600)
7584
s.Require().NoError(err)
7685

77-
instanceID, err = LoadInstanceID(s.mountDir)
78-
s.NoError(err)
86+
instanceID, err = LoadInstanceID()
87+
s.Require().NoError(err)
7988
s.Equal(instanceID, instanceID)
8089
}

0 commit comments

Comments
 (0)