From fd71d10268cd92a8b4c1799d4479ffd4495a2a51 Mon Sep 17 00:00:00 2001 From: Tom Clegg Date: Wed, 20 May 2020 04:03:08 -0400 Subject: [PATCH] 16319: Fix restarting postgresql twice. Arvados-DCO-1.1-Signed-off-by: Tom Clegg --- lib/install/deps.go | 52 ++++++++++++++++++++++++--------------------- 1 file changed, 28 insertions(+), 24 deletions(-) diff --git a/lib/install/deps.go b/lib/install/deps.go index eee4190116..ba57c20c35 100644 --- a/lib/install/deps.go +++ b/lib/install/deps.go @@ -327,6 +327,7 @@ rm ${zip} // might never have been run. } + var needcoll []string // If the en_US.UTF-8 locale wasn't installed when // postgresql initdb ran, it needs to be added // explicitly before we can use it in our test suite. @@ -341,31 +342,34 @@ rm ${zip} if strings.Contains(string(out), "1") { logger.Infof("postgresql supports collation %s", collname) } else { - // In order for CREATE COLLATION to - // work, the locale must have existed - // when PostgreSQL started up. In most - // cases, either this is already true - // because we just started postgresql - // ourselves above, or systemd is here - // and we can force a restart. - if os.Getpid() != 1 { - if err = runBash(`sudo systemctl restart postgresql`, stdout, stderr); err != nil { - logger.Warn("`systemctl restart postgresql` failed; hoping postgresql does not need to be restarted") - } else if err = waitPostgreSQLReady(); err != nil { - return 1 - } - } - cmd = exec.Command("sudo", "-u", "postgres", "psql", "-c", "CREATE COLLATION \""+collname+"\" (LOCALE = \"en_US.UTF-8\")") - cmd.Stdout = stdout - cmd.Stderr = stderr - cmd.Dir = "/" - err = cmd.Run() - if err != nil { - err = fmt.Errorf("error adding postgresql collation %s: %s", collname, err) - return 1 - } + needcoll = append(needcoll, collname) + } + } + if len(needcoll) > 0 && os.Getpid() != 1 { + // In order for the CREATE COLLATION statement + // below to work, the locale must have existed + // when PostgreSQL started up. If we're + // running as init, we must have started + // PostgreSQL ourselves after installing the + // locales. Otherwise, it might need a + // restart, so we attempt to restart it with + // systemd. + if err = runBash(`sudo systemctl restart postgresql`, stdout, stderr); err != nil { + logger.Warn("`systemctl restart postgresql` failed; hoping postgresql does not need to be restarted") + } else if err = waitPostgreSQLReady(); err != nil { + return 1 + } + } + for _, collname := range needcoll { + cmd := exec.Command("sudo", "-u", "postgres", "psql", "-c", "CREATE COLLATION \""+collname+"\" (LOCALE = \"en_US.UTF-8\")") + cmd.Stdout = stdout + cmd.Stderr = stderr + cmd.Dir = "/" + err = cmd.Run() + if err != nil { + err = fmt.Errorf("error adding postgresql collation %s: %s", collname, err) + return 1 } - } withstuff := "WITH LOGIN SUPERUSER ENCRYPTED PASSWORD " + pq.QuoteLiteral(devtestDatabasePassword) -- 2.39.5