1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
18 "git.arvados.org/arvados.git/sdk/go/arvados"
22 type runPostgreSQL struct{}
24 func (runPostgreSQL) String() string {
28 func (runPostgreSQL) Run(ctx context.Context, fail func(error), boot *Booter) error {
29 err := boot.wait(ctx, createCertificates{})
34 buf := bytes.NewBuffer(nil)
35 err = boot.RunProgram(ctx, boot.tempdir, buf, nil, "pg_config", "--bindir")
39 bindir := strings.TrimSpace(buf.String())
41 datadir := filepath.Join(boot.tempdir, "pgdata")
42 err = os.Mkdir(datadir, 0755)
46 err = boot.RunProgram(ctx, boot.tempdir, nil, nil, filepath.Join(bindir, "initdb"), "-D", datadir)
51 err = boot.RunProgram(ctx, boot.tempdir, nil, nil, "cp", "server.crt", "server.key", datadir)
56 port := boot.cluster.PostgreSQL.Connection["port"]
58 boot.waitShutdown.Add(1)
60 defer boot.waitShutdown.Done()
61 fail(boot.RunProgram(ctx, boot.tempdir, nil, nil, filepath.Join(bindir, "postgres"),
63 "-D", datadir, // data dir
64 "-k", datadir, // socket dir
65 "-p", boot.cluster.PostgreSQL.Connection["port"],
73 if exec.CommandContext(ctx, "pg_isready", "--timeout=10", "--host="+boot.cluster.PostgreSQL.Connection["host"], "--port="+port).Run() == nil {
76 time.Sleep(time.Second / 2)
78 db, err := sql.Open("postgres", arvados.PostgreSQLConnection{
84 return fmt.Errorf("db open failed: %s", err)
87 conn, err := db.Conn(ctx)
89 return fmt.Errorf("db conn failed: %s", err)
92 _, err = conn.ExecContext(ctx, `CREATE USER `+pq.QuoteIdentifier(boot.cluster.PostgreSQL.Connection["user"])+` WITH SUPERUSER ENCRYPTED PASSWORD `+pq.QuoteLiteral(boot.cluster.PostgreSQL.Connection["password"]))
94 return fmt.Errorf("createuser failed: %s", err)
96 _, err = conn.ExecContext(ctx, `CREATE DATABASE `+pq.QuoteIdentifier(boot.cluster.PostgreSQL.Connection["dbname"]))
98 return fmt.Errorf("createdb failed: %s", err)