Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 088c103

Browse files
committed
Merge branch '152-clone-configuration' into 'master'
fix: clone configuration issues (#152) - create unix-socket directories and adjust start configuration - generate superuser password for PostgreSQL - remove unnecessary configuration variables(pgDataSubdir) See merge request postgres-ai/database-lab!149
2 parents 5bb1019 + e9c1208 commit 088c103

File tree

14 files changed

+87
-76
lines changed

14 files changed

+87
-76
lines changed

configs/config.example.logical_generic.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,6 @@ provision:
5555
# Do not edit this.
5656
mode: "local"
5757

58-
# Subdir where PGDATA located relative to the pool root dir.
59-
# It is a part of global.dataDir, *with* leading slashes.
60-
# If PGDATA is supposed to be in global.dataDir and this is the mount point,
61-
# then use "/" in pgDataSubdir.
62-
# Otherwise, use the final part (everything except the mount point), with leading slash.
63-
# Example:
64-
# dataDir (PostgreSQL data directory): /var/lib/dblab/data
65-
# pgDataSubdir: /pgdata
66-
# -- in this case, we assume that the mount point is: /var/lib/dblab
67-
# Note, that, despite the fact that it always begins with "/",
68-
# it is a relative path. Default: "/".
69-
pgDataSubdir: "/"
70-
7158
# Database username that will be used for Postgres management connections.
7259
# This user must exist.
7360
# The password is not needed – it will be set automatically.

configs/config.example.logical_rds.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,6 @@ provision:
5555
# Do not edit this.
5656
mode: "local"
5757

58-
# Subdir where PGDATA located relative to the pool root dir.
59-
# It is a part of global.dataDir, *with* leading slashes.
60-
# If PGDATA is supposed to be in global.dataDir and this is the mount point,
61-
# then use "/" in pgDataSubdir.
62-
# Otherwise, use the final part (everything except the mount point), with leading slash.
63-
# Example:
64-
# dataDir (PostgreSQL data directory): /var/lib/dblab/data
65-
# pgDataSubdir: /pgdata
66-
# -- in this case, we assume that the mount point is: /var/lib/dblab
67-
# Note, that, despite the fact that it always begins with "/",
68-
# it is a relative path. Default: "/".
69-
pgDataSubdir: "/"
70-
7158
# Database username that will be used for Postgres management connections.
7259
# This user must exist.
7360
# The password is not needed – it will be set automatically.

configs/config.example.physical_generic.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,6 @@ provision:
5555
# Do not edit this.
5656
mode: "local"
5757

58-
# Subdir where PGDATA located relative to the pool root dir.
59-
# It is a part of global.dataDir, *with* leading slashes.
60-
# If PGDATA is supposed to be in global.dataDir and this is the mount point,
61-
# then use "/" in pgDataSubdir.
62-
# Otherwise, use the final part (everything except the mount point), with leading slash.
63-
# Example:
64-
# dataDir (PostgreSQL data directory): /var/lib/dblab/data
65-
# pgDataSubdir: /pgdata
66-
# -- in this case, we assume that the mount point is: /var/lib/dblab
67-
# Note, that, despite the fact that it always begins with "/",
68-
# it is a relative path. Default: "/".
69-
pgDataSubdir: "/"
70-
7158
# Database username that will be used for Postgres management connections.
7259
# This user must exist.
7360
# The password is not needed – it will be set automatically.

configs/config.example.physical_walg.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,6 @@ provision:
5555
# Do not edit this.
5656
mode: "local"
5757

58-
# Subdir where PGDATA located relative to the pool root dir.
59-
# It is a part of global.dataDir, *with* leading slashes.
60-
# If PGDATA is supposed to be in global.dataDir and this is the mount point,
61-
# then use "/" in pgDataSubdir.
62-
# Otherwise, use the final part (everything except the mount point), with leading slash.
63-
# Example:
64-
# dataDir (PostgreSQL data directory): /var/lib/dblab/data
65-
# pgDataSubdir: /pgdata
66-
# -- in this case, we assume that the mount point is: /var/lib/dblab
67-
# Note, that, despite the fact that it always begins with "/",
68-
# it is a relative path. Default: "/".
69-
pgDataSubdir: "/"
70-
7158
# Database username that will be used for Postgres management connections.
7259
# This user must exist.
7360
# The password is not needed – it will be set automatically.

go.mod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ require (
2525
github.com/robfig/cron/v3 v3.0.1
2626
github.com/rs/xid v1.2.1
2727
github.com/sergi/go-diff v1.1.0
28+
github.com/sethvargo/go-password v0.2.0
2829
github.com/shirou/gopsutil v2.20.7+incompatible
2930
github.com/sirupsen/logrus v1.4.2
3031
github.com/stretchr/testify v1.5.1

go.sum

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ github.com/russross/blackfriday/v2 v2.0.1 h1:lPqVAte+HuHNfhJ/0LC98ESWRz8afy9tM/0
8484
github.com/russross/blackfriday/v2 v2.0.1/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
8585
github.com/sergi/go-diff v1.1.0 h1:we8PVUC3FE2uYfodKH/nBHMSetSfHDR6scGdBi+erh0=
8686
github.com/sergi/go-diff v1.1.0/go.mod h1:STckp+ISIX8hZLjrqAeVduY0gWCT9IjLuqbuNXdaHfM=
87+
github.com/sethvargo/go-password v0.2.0 h1:BTDl4CC/gjf/axHMaDQtw507ogrXLci6XRiLc7i/UHI=
88+
github.com/sethvargo/go-password v0.2.0/go.mod h1:Ym4Mr9JXLBycr02MFuVQ/0JHidNetSgbzutTr3zsYXE=
8789
github.com/shirou/gopsutil v2.20.7+incompatible h1:Ymv4OD12d6zm+2yONe39VSmp2XooJe8za7ngOLW/o/w=
8890
github.com/shirou/gopsutil v2.20.7+incompatible/go.mod h1:5b4v6he4MtMOwMlS0TUMTu2PcXUg8+E1lC7eC3UO/RA=
8991
github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5IYyJwS/kOiWx8mHo=

pkg/retrieval/engine/postgres/initialize/logical/dump.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020
"github.com/docker/docker/api/types/network"
2121
"github.com/docker/docker/client"
2222
"github.com/pkg/errors"
23+
"github.com/sethvargo/go-password/password"
2324

2425
dblabCfg "gitlab.com/postgres-ai/database-lab/pkg/config"
2526
"gitlab.com/postgres-ai/database-lab/pkg/log"
@@ -224,7 +225,12 @@ func (d *DumpJob) Run(ctx context.Context) (err error) {
224225
return errors.Wrap(err, "failed to build container host config")
225226
}
226227

227-
cont, err := d.dockerClient.ContainerCreate(ctx, d.buildContainerConfig(), hostConfig, &network.NetworkingConfig{},
228+
pwd, err := password.Generate(tools.PasswordLength, tools.PasswordMinDigits, tools.PasswordMinSymbols, false, true)
229+
if err != nil {
230+
return errors.Wrap(err, "failed to generate PostgreSQL password")
231+
}
232+
233+
cont, err := d.dockerClient.ContainerCreate(ctx, d.buildContainerConfig(pwd), hostConfig, &network.NetworkingConfig{},
228234
d.dumpContainerName(),
229235
)
230236
if err != nil {
@@ -328,9 +334,9 @@ func (d *DumpJob) performDumpCommand(ctx context.Context, cmdOutput io.Writer, c
328334
return nil
329335
}
330336

331-
func (d *DumpJob) getEnvironmentVariables() []string {
337+
func (d *DumpJob) getEnvironmentVariables(password string) []string {
332338
envs := []string{
333-
"POSTGRES_HOST_AUTH_METHOD=trust",
339+
"POSTGRES_PASSWORD=" + password,
334340
}
335341

336342
// Avoid initialization of PostgreSQL directory in case of preparing of a dump.
@@ -346,10 +352,10 @@ func (d *DumpJob) getEnvironmentVariables() []string {
346352
return envs
347353
}
348354

349-
func (d *DumpJob) buildContainerConfig() *container.Config {
355+
func (d *DumpJob) buildContainerConfig(password string) *container.Config {
350356
return &container.Config{
351357
Labels: map[string]string{"label": tools.DBLabControlLabel},
352-
Env: d.getEnvironmentVariables(),
358+
Env: d.getEnvironmentVariables(password),
353359
Image: d.DockerImage,
354360
Healthcheck: health.GetConfig(),
355361
}

pkg/retrieval/engine/postgres/initialize/logical/restore.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ import (
1717
"github.com/docker/docker/api/types/network"
1818
"github.com/docker/docker/client"
1919
"github.com/pkg/errors"
20+
"github.com/sethvargo/go-password/password"
2021

2122
dblabCfg "gitlab.com/postgres-ai/database-lab/pkg/config"
2223
"gitlab.com/postgres-ai/database-lab/pkg/log"
@@ -124,8 +125,13 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
124125
return errors.Wrap(err, "failed to build container host config")
125126
}
126127

128+
pwd, err := password.Generate(tools.PasswordLength, tools.PasswordMinDigits, tools.PasswordMinSymbols, false, true)
129+
if err != nil {
130+
return errors.Wrap(err, "failed to generate PostgreSQL password")
131+
}
132+
127133
cont, err := r.dockerClient.ContainerCreate(ctx,
128-
r.buildContainerConfig(),
134+
r.buildContainerConfig(pwd),
129135
hostConfig,
130136
&network.NetworkingConfig{},
131137
r.restoreContainerName(),
@@ -194,12 +200,12 @@ func (r *RestoreJob) Run(ctx context.Context) (err error) {
194200
return nil
195201
}
196202

197-
func (r *RestoreJob) buildContainerConfig() *container.Config {
203+
func (r *RestoreJob) buildContainerConfig(password string) *container.Config {
198204
return &container.Config{
199205
Labels: map[string]string{"label": tools.DBLabControlLabel},
200206
Env: []string{
201207
"PGDATA=" + r.globalCfg.DataDir,
202-
"POSTGRES_HOST_AUTH_METHOD=trust",
208+
"POSTGRES_PASSWORD=" + password,
203209
},
204210
Image: r.RestoreOptions.DockerImage,
205211
Healthcheck: health.GetConfig(),

pkg/retrieval/engine/postgres/initialize/physical/physical.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222
"github.com/docker/docker/api/types/network"
2323
"github.com/docker/docker/client"
2424
"github.com/pkg/errors"
25+
"github.com/sethvargo/go-password/password"
2526

2627
dblabCfg "gitlab.com/postgres-ai/database-lab/pkg/config"
2728
"gitlab.com/postgres-ai/database-lab/pkg/log"
@@ -245,8 +246,13 @@ func (r *RestoreJob) startContainer(ctx context.Context, containerName string) (
245246
return "", errors.Wrap(err, "failed to build container host config")
246247
}
247248

249+
pwd, err := password.Generate(tools.PasswordLength, tools.PasswordMinDigits, tools.PasswordMinSymbols, false, true)
250+
if err != nil {
251+
return "", errors.Wrap(err, "failed to generate PostgreSQL password")
252+
}
253+
248254
syncInstance, err := r.dockerClient.ContainerCreate(ctx,
249-
r.buildContainerConfig(),
255+
r.buildContainerConfig(pwd),
250256
hostConfig,
251257
&network.NetworkingConfig{},
252258
containerName,
@@ -345,9 +351,9 @@ func (r *RestoreJob) runSyncInstance(ctx context.Context) error {
345351
return nil
346352
}
347353

348-
func (r *RestoreJob) getEnvironmentVariables() []string {
354+
func (r *RestoreJob) getEnvironmentVariables(password string) []string {
349355
envVariables := append([]string{
350-
"POSTGRES_HOST_AUTH_METHOD=trust",
356+
"POSTGRES_PASSWORD=" + password,
351357
"PGDATA=" + r.globalCfg.DataDir,
352358
}, r.restorer.GetEnvVariables()...)
353359

@@ -358,10 +364,10 @@ func (r *RestoreJob) getEnvironmentVariables() []string {
358364
return envVariables
359365
}
360366

361-
func (r *RestoreJob) buildContainerConfig() *container.Config {
367+
func (r *RestoreJob) buildContainerConfig(password string) *container.Config {
362368
return &container.Config{
363369
Labels: map[string]string{"label": tools.DBLabControlLabel},
364-
Env: r.getEnvironmentVariables(),
370+
Env: r.getEnvironmentVariables(password),
365371
Image: r.DockerImage,
366372
}
367373
}

pkg/retrieval/engine/postgres/initialize/snapshot/physical.go

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525
"github.com/docker/docker/client"
2626
"github.com/pkg/errors"
2727
"github.com/robfig/cron/v3"
28+
"github.com/sethvargo/go-password/password"
2829

2930
dblabCfg "gitlab.com/postgres-ai/database-lab/pkg/config"
3031
"gitlab.com/postgres-ai/database-lab/pkg/log"
@@ -327,9 +328,14 @@ func (p *PhysicalInitial) promoteInstance(ctx context.Context, clonePath string)
327328
return errors.Wrap(err, "failed to scan image pulling response")
328329
}
329330

331+
pwd, err := password.Generate(tools.PasswordLength, tools.PasswordMinDigits, tools.PasswordMinSymbols, false, true)
332+
if err != nil {
333+
return errors.Wrap(err, "failed to generate PostgreSQL password")
334+
}
335+
330336
// Run promotion container.
331337
cont, err := p.dockerClient.ContainerCreate(ctx,
332-
p.buildContainerConfig(clonePath, promoteImage),
338+
p.buildContainerConfig(clonePath, promoteImage, pwd),
333339
hostConfig,
334340
&network.NetworkingConfig{},
335341
p.promoteContainerName(),
@@ -463,12 +469,12 @@ func (p *PhysicalInitial) adjustRecoveryConfiguration(pgVersion, clonePGDataDir
463469
return nil
464470
}
465471

466-
func (p *PhysicalInitial) buildContainerConfig(clonePath, promoteImage string) *container.Config {
472+
func (p *PhysicalInitial) buildContainerConfig(clonePath, promoteImage, password string) *container.Config {
467473
return &container.Config{
468474
Labels: map[string]string{"label": tools.DBLabControlLabel},
469475
Env: []string{
470476
"PGDATA=" + clonePath,
471-
"POSTGRES_HOST_AUTH_METHOD=trust",
477+
"POSTGRES_PASSWORD=" + password,
472478
},
473479
Image: promoteImage,
474480
Healthcheck: health.GetConfig(

pkg/retrieval/engine/postgres/initialize/tools/tools.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,13 @@ const (
4141

4242
// DBLabControlLabel defines a label to mark service containers.
4343
DBLabControlLabel = "dblab_control"
44+
45+
// PasswordLength defines length for autogenerated passwords.
46+
PasswordLength = 16
47+
// PasswordMinDigits defines minimum digits for autogenerated passwords.
48+
PasswordMinDigits = 4
49+
// PasswordMinSymbols defines minimum symbols for autogenerated passwords.
50+
PasswordMinSymbols = 0
4451
)
4552

4653
// IsEmptyDirectory checks whether a directory is empty.

pkg/services/provision/docker/docker.go

Lines changed: 35 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,10 +6,13 @@
66
package docker
77

88
import (
9+
"fmt"
10+
"os"
911
"strconv"
1012
"strings"
1113

1214
"github.com/pkg/errors"
15+
"github.com/shirou/gopsutil/host"
1316

1417
"gitlab.com/postgres-ai/database-lab/pkg/services/provision/resources"
1518
"gitlab.com/postgres-ai/database-lab/pkg/services/provision/runners"
@@ -21,19 +24,46 @@ const (
2124

2225
// RunContainer runs specified container.
2326
func RunContainer(r runners.Runner, c *resources.AppConfig) (string, error) {
27+
hostInfo, err := host.Info()
28+
if err != nil {
29+
return "", errors.Wrap(err, "failed to get host info")
30+
}
31+
32+
// Directly mount PGDATA if Database Lab is running without any virtualization.
33+
socketVolume := fmt.Sprintf("--volume %s:%s", c.Datadir, c.Datadir)
34+
35+
if hostInfo.VirtualizationRole == "guest" {
36+
// Use volumes from the Database Lab instance if it's running inside Docker container.
37+
socketVolume = "--volumes-from=" + hostInfo.Hostname
38+
}
39+
40+
if err := createSocketCloneDir(c.UnixSocketCloneDir); err != nil {
41+
return "", errors.Wrap(err, "failed to create socket clone directory")
42+
}
43+
2444
dockerRunCmd := "docker run " +
2545
"--name " + c.CloneName + " " +
2646
"--detach " +
27-
"--publish " + strconv.FormatUint(uint64(c.Port), 10) + ":5432 " +
28-
"--env PGDATA=/var/lib/postgresql/pgdata " +
29-
"--volume " + c.Datadir + ":/var/lib/postgresql/pgdata " +
30-
"--volume " + c.UnixSocketCloneDir + ":/var/run/postgresql " +
47+
"--publish " + strconv.Itoa(int(c.Port)) + ":5432 " +
48+
"--env PGDATA=" + c.Datadir + " " + socketVolume + " " +
3149
"--label " + labelClone + " " +
32-
c.DockerImage
50+
c.DockerImage + " -k " + c.UnixSocketCloneDir
3351

3452
return r.Run(dockerRunCmd, true)
3553
}
3654

55+
func createSocketCloneDir(socketCloneDir string) error {
56+
if err := os.MkdirAll(socketCloneDir, 0777); err != nil {
57+
return err
58+
}
59+
60+
if err := os.Chmod(socketCloneDir, 0777); err != nil {
61+
return err
62+
}
63+
64+
return nil
65+
}
66+
3767
// StopContainer stops specified container.
3868
func StopContainer(r runners.Runner, c *resources.AppConfig) (string, error) {
3969
dockerStopCmd := "docker container stop " + c.CloneName

pkg/services/provision/mode_local.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -474,7 +474,7 @@ func (j *provisionModeLocal) getAppConfig(name string, port uint) *resources.App
474474
CloneName: name,
475475
Version: j.config.PgVersion,
476476
DockerImage: j.config.ModeLocal.DockerImage,
477-
Datadir: path.Clean(j.config.ModeLocal.MountDir + name + j.config.PgDataSubdir),
477+
Datadir: path.Clean(j.config.ModeLocal.MountDir + name),
478478
Host: host,
479479
Port: port,
480480
UnixSocketCloneDir: unixSocketCloneDir,

pkg/services/provision/provision.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,7 @@ type Config struct {
3838
ModeLocal ModeLocalConfig `yaml:"local"`
3939

4040
// Postgres options.
41-
PgVersion string `yaml:"pgVersion"`
42-
PgDataSubdir string `yaml:"pgDataSubdir"`
41+
PgVersion string `yaml:"pgVersion"`
4342

4443
// Database user will be created with the specified credentials.
4544
PgMgmtUsername string `yaml:"pgMgmtUsername"`

0 commit comments

Comments
 (0)