Skip to content

Commit

Permalink
chore: optimize postgres binary lookup
Browse files Browse the repository at this point in the history
This will change the current hardwired PostgreSQL binary lookup mechanism to
utilizing the `pg_config --bindir` utility that should work better for most
distributions. The old method caused issues at least on Archlinux.

Changelog-None
  • Loading branch information
m-schmoock authored and rustyrussell committed Oct 27, 2020
1 parent 7a2e72f commit 6907e85
Showing 1 changed file with 15 additions and 22 deletions.
37 changes: 15 additions & 22 deletions contrib/pyln-testing/pyln/testing/db.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
from ephemeral_port_reserve import reserve # type: ignore
from glob import glob

import itertools
import logging
import os
import psycopg2 # type: ignore
import random
import re
import shutil
import signal
import sqlite3
Expand Down Expand Up @@ -117,28 +115,23 @@ def __init__(self, directory):
print("Starting PostgresDbProvider")

def locate_path(self):
prefix = '/usr/lib/postgresql/*'
matches = glob(prefix)

candidates = {}
for m in matches:
g = re.search(r'([0-9]+[\.0-9]*)', m)
if not g:
continue
candidates[float(g.group(1))] = m

if len(candidates) == 0:
raise ValueError("Could not find `postgres` and `initdb` binaries in {}. Is postgresql installed?".format(prefix))

# Now iterate in reverse order through matches
for k, v in sorted(candidates.items())[::-1]:
initdb = os.path.join(v, 'bin', 'initdb')
postgres = os.path.join(v, 'bin', 'postgres')
if os.path.isfile(initdb) and os.path.isfile(postgres):
logging.info("Found `postgres` and `initdb` in {}".format(os.path.join(v, 'bin')))
# Use `pg_config` to determine correct PostgreSQL installation
pg_config = shutil.which('pg_config')
if not pg_config:
raise ValueError("Could not find `pg_config` to determine PostgreSQL binaries. Is PostgreSQL installed?")

bindir = subprocess.check_output([pg_config, '--bindir']).decode().rstrip()
if not os.path.isdir(bindir):
raise ValueError("Error: `pg_config --bindir` didn't return a proper path: {}".format(bindir))

initdb = os.path.join(bindir, 'initdb')
postgres = os.path.join(bindir, 'postgres')
if os.path.isfile(initdb) and os.path.isfile(postgres):
if os.access(initdb, os.X_OK) and os.access(postgres, os.X_OK):
logging.info("Found `postgres` and `initdb` in {}".format(bindir))
return initdb, postgres

raise ValueError("Could not find `postgres` and `initdb` in any of the possible paths: {}".format(candidates.values()))
raise ValueError("Could not find `postgres` and `initdb` binaries in {}".format(bindir))

def start(self):
passfile = os.path.join(self.directory, "pgpass.txt")
Expand Down

0 comments on commit 6907e85

Please sign in to comment.