Skip to content

Commit 2087adf

Browse files
committed
refactor: redefine configuration structure for all Database Lab products (#278)
To standardize configuration file names and store metadata: * redefine configuration structure for all Database Lab products * adjust Docker images of Database Lab Engine and CI Checker * remove old Docker images
1 parent 874e6d5 commit 2087adf

38 files changed

+279
-192
lines changed

Diff for: .gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -8,5 +8,7 @@
88
/deploy/
99

1010
/configs/config.yml
11+
/configs/server.yml
1112
/configs/run_ci.yaml
13+
/configs/ci_checker.yml
1214
/packer/example.com.key

Diff for: Dockerfile.ci-checker

-1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,5 @@ RUN apk update && apk add --no-cache bash
66
WORKDIR /home/dblab
77

88
COPY ./bin/run-ci ./bin/run-ci
9-
COPY ./configs ./configs
109

1110
CMD ./bin/run-ci

Diff for: Dockerfile.dblab-server

+2-31
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,4 @@
1-
# Run with server.yml mount:
2-
# sudo docker run \
3-
# --name dblab_server \
4-
# --label dblab_control \
5-
# --privileged \
6-
# --publish 2345:2345 \
7-
# --restart on-failure \
8-
# --volume /var/run/docker.sock:/var/run/docker.sock \
9-
# --volume /var/lib/dblab:/var/lib/dblab:rshared \
10-
# --volume ~/.dblab/server.yml:/home/dblab/configs/config.yml \
11-
# --env VERIFICATION_TOKEN=secret_token \
12-
# --detach \
13-
# postgresai/dblab-server:latest
14-
#
15-
# or run with envs options:
16-
# sudo docker run \
17-
# --name dblab_server \
18-
# --label dblab_control \
19-
# --privileged \
20-
# --publish 2345:2345 \
21-
# --restart on-failure \
22-
# --volume /var/run/docker.sock:/var/run/docker.sock \
23-
# --volume /var/lib/dblab:/var/lib/dblab:rshared \
24-
# --volume ~/.dblab/server.yml:/home/dblab/configs/config.yml \
25-
# --env VERIFICATION_TOKEN=token \
26-
# --env MOUNT_DIR=/var/lib/dblab/clones \
27-
# --env UNIX_SOCKET_DIR=/var/lib/dblab/sockets \
28-
# --env DOCKER_IMAGE=postgres:13-alpine \
29-
# --detach \
30-
# postgresai/dblab-server:latest
1+
# See Guides to learn how to start a container: https://postgres.ai/docs/how-to-guides/administration/engine-manage
312

323
FROM docker:19.03.14
334

@@ -46,7 +17,7 @@ WORKDIR /home/dblab
4617
COPY ./bin/dblab-server ./bin/dblab-server
4718
COPY ./api ./api
4819
COPY ./web ./web
49-
COPY ./configs ./configs
20+
COPY ./configs/standard ./standard
5021
COPY ./scripts ./scripts
5122

5223
CMD ./bin/dblab-server

Diff for: Dockerfile.runci

-9
This file was deleted.

Diff for: Dockerfile.sync-instance

-29
This file was deleted.

Diff for: cmd/cli/commands/config/file.go

+3-2
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ import (
1313
)
1414

1515
const (
16-
configPath = ".dblab"
16+
dblabDir = ".dblab"
17+
configPath = "cli"
1718
configFilename = "cli.yml"
1819
)
1920

@@ -24,7 +25,7 @@ func GetDirname() (string, error) {
2425
return "", err
2526
}
2627

27-
dirname := path.Join(currentUser.HomeDir, configPath)
28+
dirname := path.Join(currentUser.HomeDir, dblabDir, configPath)
2829

2930
return dirname, nil
3031
}

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

+2-16
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ func main() {
4949

5050
log.Msg("Database Lab Instance ID:", instanceID)
5151

52-
cfg, err := loadConfiguration(instanceID)
52+
cfg, err := config.LoadConfiguration(instanceID)
5353
if err != nil {
5454
log.Fatal(errors.WithMessage(err, "failed to parse config"))
5555
}
@@ -161,23 +161,9 @@ func main() {
161161
shutdownDatabaseLabEngine(shutdownCtx, dockerCLI, cfg.Global, pm.Active().Pool())
162162
}
163163

164-
func loadConfiguration(instanceID string) (*config.Config, error) {
165-
cfg, err := config.LoadConfig("config.yml")
166-
if err != nil {
167-
return nil, errors.Wrap(err, "failed to parse config")
168-
}
169-
170-
log.SetDebug(cfg.Global.Debug)
171-
log.Dbg("Config loaded", cfg)
172-
173-
cfg.Global.InstanceID = instanceID
174-
175-
return cfg, nil
176-
}
177-
178164
func reloadConfig(ctx context.Context, instanceID string, provisionSvc *provision.Provisioner, retrievalSvc *retrieval.Retrieval,
179165
pm *pool.Manager, cloningSvc cloning.Cloning, platformSvc *platform.Service, est *estimator.Estimator, server *srv.Server) error {
180-
cfg, err := loadConfiguration(instanceID)
166+
cfg, err := config.LoadConfiguration(instanceID)
181167
if err != nil {
182168
return err
183169
}

Diff for: cmd/runci/main.go

+4-25
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,13 @@ import (
1010
"github.com/docker/docker/api/types/network"
1111
"github.com/docker/docker/client"
1212
"github.com/pkg/errors"
13-
"gopkg.in/yaml.v2"
1413

1514
"gitlab.com/postgres-ai/database-lab/v2/pkg/client/dblabapi"
1615
"gitlab.com/postgres-ai/database-lab/v2/pkg/log"
1716
"gitlab.com/postgres-ai/database-lab/v2/pkg/retrieval/engine/postgres/tools"
1817
"gitlab.com/postgres-ai/database-lab/v2/pkg/runci"
1918
"gitlab.com/postgres-ai/database-lab/v2/pkg/runci/source"
2019
"gitlab.com/postgres-ai/database-lab/v2/pkg/services/platform"
21-
"gitlab.com/postgres-ai/database-lab/v2/pkg/util"
2220
"gitlab.com/postgres-ai/database-lab/v2/pkg/util/networks"
2321
)
2422

@@ -31,12 +29,15 @@ func main() {
3129
ctx, cancel := context.WithCancel(context.Background())
3230
defer cancel()
3331

34-
cfg, err := loadConfiguration()
32+
cfg, err := runci.LoadConfiguration()
3533
if err != nil {
3634
log.Errf("Failed to load config: %v", err)
3735
return
3836
}
3937

38+
log.SetDebug(cfg.App.Debug)
39+
log.Dbg("Config loaded: ", cfg)
40+
4041
networkID := discoverNetwork(ctx, cfg, dockerCLI)
4142
if networkID != "" {
4243
hostname := os.Getenv("HOSTNAME")
@@ -129,25 +130,3 @@ func discoverNetwork(ctx context.Context, cfg *runci.Config, dockerCLI *client.C
129130

130131
return networkID
131132
}
132-
133-
func loadConfiguration() (*runci.Config, error) {
134-
configPath, err := util.GetConfigPath("run_ci.yaml")
135-
if err != nil {
136-
return nil, errors.Wrap(err, "failed to get config path")
137-
}
138-
139-
b, err := os.ReadFile(configPath)
140-
if err != nil {
141-
return nil, errors.Errorf("error loading %s config file", configPath)
142-
}
143-
144-
cfg := &runci.Config{}
145-
if err := yaml.Unmarshal(b, cfg); err != nil {
146-
return nil, errors.WithMessagef(err, "error parsing %s config", configPath)
147-
}
148-
149-
log.SetDebug(cfg.App.Debug)
150-
log.Dbg("Config loaded: ", cfg)
151-
152-
return cfg, nil
153-
}
File renamed without changes.

Diff for: configs/config.example.logical_generic.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copy the following to: ~/.dblab/server.yml
1+
# Copy the following to: ~/.dblab/engine/configs/server.yml
22

33
# Database Lab API server. This API is used to work with clones
44
# (list them, create, delete, see how to connect to a clone).

Diff for: configs/config.example.logical_rds_iam.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copy the following to: ~/.dblab/server.yml
1+
# Copy the following to: ~/.dblab/engine/configs/server.yml
22

33
# Database Lab API server. This API is used to work with clones
44
# (list them, create, delete, see how to connect to a clone).

Diff for: configs/config.example.physical_generic.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copy the following to: ~/.dblab/server.yml
1+
# Copy the following to: ~/.dblab/engine/configs/server.yml
22

33
# Database Lab API server. This API is used to work with clones
44
# (list them, create, delete, see how to connect to a clone).

Diff for: configs/config.example.physical_walg.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copy the following to: ~/.dblab/server.yml
1+
# Copy the following to: ~/.dblab/engine/configs/server.yml
22

33
# Database Lab API server. This API is used to work with clones
44
# (list them, create, delete, see how to connect to a clone).
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

Diff for: packer/run_ci.yaml renamed to packer/ci_checker.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ app:
55
verificationToken: "secret_token"
66
dle:
77
url: "https://dblab_server"
8-
verificationToken: "dle_cerification_token"
8+
verificationToken: "dle_certification_token"
99
platform:
1010
url: "https://postgres.ai/api/general"
1111
accessToken: "platform_access_token"

Diff for: packer/template.json.pkr.hcl

+3-3
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,12 @@ build {
4747

4848
provisioner "file"{
4949
source = "joe.yml"
50-
destination = "/home/ubuntu/joe.yml"
50+
destination = "/home/ubuntu/.dblab/configs/joe.yml"
5151
}
5252

5353
provisioner "file"{
54-
source = "run_ci.yaml"
55-
destination = "/home/ubuntu/run_ci.yaml"
54+
source = "ci_checker.yml"
55+
destination = "/home/ubuntu/.dblab/configs/ci_checker.yml"
5656
}
5757

5858
provisioner "shell" {

Diff for: pkg/config/config.go

+25-5
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313

1414
"gitlab.com/postgres-ai/database-lab/v2/pkg/config/global"
1515
"gitlab.com/postgres-ai/database-lab/v2/pkg/estimator"
16+
"gitlab.com/postgres-ai/database-lab/v2/pkg/log"
1617
"gitlab.com/postgres-ai/database-lab/v2/pkg/observer"
1718
retConfig "gitlab.com/postgres-ai/database-lab/v2/pkg/retrieval/config"
1819
"gitlab.com/postgres-ai/database-lab/v2/pkg/services/cloning"
@@ -23,6 +24,10 @@ import (
2324
"gitlab.com/postgres-ai/database-lab/v2/pkg/util"
2425
)
2526

27+
const (
28+
configName = "server.yml"
29+
)
30+
2631
// Config contains a common database-lab configuration.
2732
type Config struct {
2833
Server srv.Config `yaml:"server"`
@@ -36,21 +41,36 @@ type Config struct {
3641
PoolManager pool.Config `yaml:"poolManager"`
3742
}
3843

39-
// LoadConfig instances a new Config by configuration filename.
40-
func LoadConfig(name string) (*Config, error) {
41-
configPath, err := util.GetConfigPath(name)
44+
// LoadConfiguration instances a new application configuration.
45+
func LoadConfiguration(instanceID string) (*Config, error) {
46+
cfg, err := readConfig()
47+
if err != nil {
48+
return nil, errors.Wrap(err, "failed to parse config")
49+
}
50+
51+
log.SetDebug(cfg.Global.Debug)
52+
log.Dbg("Config loaded", cfg)
53+
54+
cfg.Global.InstanceID = instanceID
55+
56+
return cfg, nil
57+
}
58+
59+
// readConfig reads application configuration.
60+
func readConfig() (*Config, error) {
61+
configPath, err := util.GetConfigPath(configName)
4262
if err != nil {
4363
return nil, errors.Wrap(err, "failed to get config path")
4464
}
4565

4666
b, err := os.ReadFile(configPath)
4767
if err != nil {
48-
return nil, errors.Errorf("error loading %s config file", name)
68+
return nil, errors.Errorf("error loading %s config file", configPath)
4969
}
5070

5171
cfg := &Config{}
5272
if err := yaml.Unmarshal(b, cfg); err != nil {
53-
return nil, errors.WithMessagef(err, "error parsing %s config", name)
73+
return nil, errors.WithMessagef(err, "error parsing %s config", configPath)
5474
}
5575

5676
return cfg, nil

Diff for: pkg/runci/migrator.go renamed to pkg/runci/config.go

+31-1
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,22 @@
22
2021 © Postgres.ai
33
*/
44

5-
// Package runci provides a tools to run and check migrations in CI.
5+
// Package runci provides tools to run and check migrations in CI.
66
package runci
77

88
import (
9+
"io/ioutil"
10+
11+
"github.com/pkg/errors"
12+
"gopkg.in/yaml.v2"
13+
914
"gitlab.com/postgres-ai/database-lab/v2/pkg/runci/source"
1015
"gitlab.com/postgres-ai/database-lab/v2/pkg/services/platform"
16+
"gitlab.com/postgres-ai/database-lab/v2/pkg/util"
17+
)
18+
19+
const (
20+
configFilename = "ci_checker.yml"
1121
)
1222

1323
// Config contains a runner configuration.
@@ -39,3 +49,23 @@ type DLE struct {
3949
type Runner struct {
4050
Image string `yaml:"image"`
4151
}
52+
53+
// LoadConfiguration loads configuration of DB Migration Checker.
54+
func LoadConfiguration() (*Config, error) {
55+
configPath, err := util.GetConfigPath(configFilename)
56+
if err != nil {
57+
return nil, errors.Wrap(err, "failed to get config path")
58+
}
59+
60+
b, err := ioutil.ReadFile(configPath)
61+
if err != nil {
62+
return nil, errors.Errorf("error loading %s config file", configPath)
63+
}
64+
65+
cfg := &Config{}
66+
if err := yaml.Unmarshal(b, cfg); err != nil {
67+
return nil, errors.WithMessagef(err, "error parsing %s config", configPath)
68+
}
69+
70+
return cfg, nil
71+
}

0 commit comments

Comments
 (0)