X-Git-Url: https://git.arvados.org/arvados.git/blobdiff_plain/7a24a37aa9e5ed425550403b68c270316a24d772..c04ba156f832774ace933d4fc001cb18ad4164c3:/lib/boot/postgresql.go diff --git a/lib/boot/postgresql.go b/lib/boot/postgresql.go index 48e24ffaec..df98904151 100644 --- a/lib/boot/postgresql.go +++ b/lib/boot/postgresql.go @@ -19,49 +19,53 @@ import ( "github.com/lib/pq" ) +// Run a postgresql server in a private data directory. Set up a db +// user, database, and TCP listener that match the supervisor's +// configured database connection info. type runPostgreSQL struct{} func (runPostgreSQL) String() string { return "postgresql" } -func (runPostgreSQL) Run(ctx context.Context, fail func(error), boot *Booter) error { - err := boot.wait(ctx, createCertificates{}) +func (runPostgreSQL) Run(ctx context.Context, fail func(error), super *Supervisor) error { + err := super.wait(ctx, createCertificates{}) if err != nil { return err } buf := bytes.NewBuffer(nil) - err = boot.RunProgram(ctx, boot.tempdir, buf, nil, "pg_config", "--bindir") + err = super.RunProgram(ctx, super.tempdir, buf, nil, "pg_config", "--bindir") if err != nil { return err } - datadir := filepath.Join(boot.tempdir, "pgdata") + bindir := strings.TrimSpace(buf.String()) + datadir := filepath.Join(super.tempdir, "pgdata") err = os.Mkdir(datadir, 0755) if err != nil { return err } - bindir := strings.TrimSpace(buf.String()) - - err = boot.RunProgram(ctx, boot.tempdir, nil, nil, filepath.Join(bindir, "initdb"), "-D", datadir) + err = super.RunProgram(ctx, super.tempdir, nil, nil, filepath.Join(bindir, "initdb"), "-D", datadir) if err != nil { return err } - err = boot.RunProgram(ctx, boot.tempdir, nil, nil, "cp", "server.crt", "server.key", datadir) + err = super.RunProgram(ctx, super.tempdir, nil, nil, "cp", "server.crt", "server.key", datadir) if err != nil { return err } - port := boot.cluster.PostgreSQL.Connection["port"] + port := super.cluster.PostgreSQL.Connection["port"] + super.waitShutdown.Add(1) go func() { - fail(boot.RunProgram(ctx, boot.tempdir, nil, nil, filepath.Join(bindir, "postgres"), + defer super.waitShutdown.Done() + fail(super.RunProgram(ctx, super.tempdir, nil, nil, filepath.Join(bindir, "postgres"), "-l", // enable ssl "-D", datadir, // data dir "-k", datadir, // socket dir - "-p", boot.cluster.PostgreSQL.Connection["port"], + "-p", super.cluster.PostgreSQL.Connection["port"], )) }() @@ -69,7 +73,7 @@ func (runPostgreSQL) Run(ctx context.Context, fail func(error), boot *Booter) er if ctx.Err() != nil { return ctx.Err() } - if exec.CommandContext(ctx, "pg_isready", "--timeout=10", "--host="+boot.cluster.PostgreSQL.Connection["host"], "--port="+port).Run() == nil { + if exec.CommandContext(ctx, "pg_isready", "--timeout=10", "--host="+super.cluster.PostgreSQL.Connection["host"], "--port="+port).Run() == nil { break } time.Sleep(time.Second / 2) @@ -88,11 +92,11 @@ func (runPostgreSQL) Run(ctx context.Context, fail func(error), boot *Booter) er return fmt.Errorf("db conn failed: %s", err) } defer conn.Close() - _, err = conn.ExecContext(ctx, `CREATE USER `+pq.QuoteIdentifier(boot.cluster.PostgreSQL.Connection["user"])+` WITH SUPERUSER ENCRYPTED PASSWORD `+pq.QuoteLiteral(boot.cluster.PostgreSQL.Connection["password"])) + _, err = conn.ExecContext(ctx, `CREATE USER `+pq.QuoteIdentifier(super.cluster.PostgreSQL.Connection["user"])+` WITH SUPERUSER ENCRYPTED PASSWORD `+pq.QuoteLiteral(super.cluster.PostgreSQL.Connection["password"])) if err != nil { return fmt.Errorf("createuser failed: %s", err) } - _, err = conn.ExecContext(ctx, `CREATE DATABASE `+pq.QuoteIdentifier(boot.cluster.PostgreSQL.Connection["dbname"])) + _, err = conn.ExecContext(ctx, `CREATE DATABASE `+pq.QuoteIdentifier(super.cluster.PostgreSQL.Connection["dbname"])) if err != nil { return fmt.Errorf("createdb failed: %s", err) }