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 datadir := filepath.Join(boot.tempdir, "pgdata")
41 err = os.Mkdir(datadir, 0755)
45 bindir := strings.TrimSpace(buf.String())
47 err = boot.RunProgram(ctx, boot.tempdir, nil, nil, filepath.Join(bindir, "initdb"), "-D", datadir)
52 err = boot.RunProgram(ctx, boot.tempdir, nil, nil, "cp", "server.crt", "server.key", datadir)
57 port := boot.cluster.PostgreSQL.Connection["port"]
60 fail(boot.RunProgram(ctx, boot.tempdir, nil, nil, filepath.Join(bindir, "postgres"),
62 "-D", datadir, // data dir
63 "-k", datadir, // socket dir
64 "-p", boot.cluster.PostgreSQL.Connection["port"],
72 if exec.CommandContext(ctx, "pg_isready", "--timeout=10", "--host="+boot.cluster.PostgreSQL.Connection["host"], "--port="+port).Run() == nil {
75 time.Sleep(time.Second / 2)
77 db, err := sql.Open("postgres", arvados.PostgreSQLConnection{
83 return fmt.Errorf("db open failed: %s", err)
86 conn, err := db.Conn(ctx)
88 return fmt.Errorf("db conn failed: %s", err)
91 _, err = conn.ExecContext(ctx, `CREATE USER `+pq.QuoteIdentifier(boot.cluster.PostgreSQL.Connection["user"])+` WITH SUPERUSER ENCRYPTED PASSWORD `+pq.QuoteLiteral(boot.cluster.PostgreSQL.Connection["password"]))
93 return fmt.Errorf("createuser failed: %s", err)
95 _, err = conn.ExecContext(ctx, `CREATE DATABASE `+pq.QuoteIdentifier(boot.cluster.PostgreSQL.Connection["dbname"]))
97 return fmt.Errorf("createdb failed: %s", err)