Skip to content
Snippets Groups Projects
Commit 2c2abe2a21c0 authored by Vincent Hatakeyama's avatar Vincent Hatakeyama
Browse files

:pencil: Refactor docker_dev_start pg start/test code

parent 2378b2d027b3
No related branches found
No related tags found
2 merge requests!68🔖 14.0.0,!66Topic/default/refactor
......@@ -86,6 +86,10 @@
default=None,
)
parser.add_argument(
"--db_host",
help="database host address",
)
parser.add_argument(
"--max-cron-threads",
help="max cron threads [default: %(default)s]",
default=None,
......@@ -449,18 +453,6 @@
_logger.debug("IP found %s", local_ip)
arg.append("--db_host")
if start_postgresql:
arg.append("/var/run/postgresql")
else:
arg.append(local_ip)
dbport = None
if nmspc.dbport:
dbport = str(nmspc.dbport)
arg.append("--db_port")
arg.append(dbport)
# auto detect local conf
local_conf_path = nmspc.config
user = db_user
......@@ -499,9 +491,17 @@
# default values if nothing else
if not user:
# Odoo uses default if nothing is set (including in PGUSER env var)
_logger.info("No user defined, using odoo")
user = "odoo"
arg.append("--db_user %s" % user)
if start_postgresql:
_logger.info("No user defined, using odoo when starting postgres")
user = "odoo"
else:
# use environment variable
user = os.getenv("PGUSER")
# defaulting to odoo
if not user:
user = "odoo"
arg.append("--db_user")
arg.append(user)
# data volume handling
base_data_volume_name = "{}_data".format(project_name)
......@@ -590,11 +590,5 @@
options["extra_hosts"]["py3o"] = local_ip
options["extra_hosts"]["py3o-fusion-server"] = local_ip
if start_postgresql and not odoo_help:
# Use socket rather than export port (avoid having to find a free port
# number)
pg, stop_postgresql, socket_path = docker_run_postgresql(
project_name, postgresql_version, host_pg_port=None
)
mounts.append(Mount("/var/run/postgresql", socket_path, "bind"))
# Database connection
......@@ -600,4 +594,11 @@
dbport = None
if nmspc.dbport:
dbport = str(nmspc.dbport)
arg.append("--db_port")
arg.append(dbport)
db_host = nmspc.db_host
# Check that connection can be done, try to create user if asked to
# TODO: handle the case where the database is still starting up
# TODO: (and remove the sleep)
......@@ -601,5 +602,4 @@
# Check that connection can be done, try to create user if asked to
# TODO: handle the case where the database is still starting up
# TODO: (and remove the sleep)
_logger.info("Testing database connection on base postgres")
if not odoo_help:
......@@ -605,4 +605,22 @@
if not odoo_help:
if start_postgresql:
if db_host:
_logger.warning(
"Ignoring given db_host when starting postgres"
)
if dbport:
_logger.warning(
"Ignoring given db_port when starting postgres"
)
db_host = "/var/run/postgresql"
dbport = 5432
# Use socket rather than export port (avoid having to find a
# free port number)
pg, stop_postgresql, socket_path = docker_run_postgresql(
project_name, postgresql_version, host_pg_port=None
)
mounts.append(Mount(db_host, socket_path, "bind"))
_logger.info("Testing database connection on base postgres")
try:
if start_postgresql:
connect(
......@@ -610,5 +628,5 @@
password=password,
database="postgres",
host=socket_path,
port=5432,
port=dbport,
)
......@@ -614,7 +632,21 @@
)
elif local_ip:
# TODO test this too
_logger.info("Connection successful")
else:
# psql uses PGHOST if provided and a socket otherwise
if db_host:
pass
elif "PGHOST" in os.environ:
db_host = os.getenv("PGHOST")
_logger.debug("Using PGHOST=%s", db_host)
else:
# use socket, trying to guess it
# db_host = "/var/run/postgresql"
# _logger.debug("Defaulting to host %s", db_host)
db_host = local_ip
if db_host.startswith("/"):
# mount the path inside the docker
mounts.append(Mount(db_host, db_host, "bind"))
connect(
user=user,
password=password,
database="postgres",
......@@ -617,7 +649,7 @@
connect(
user=user,
password=password,
database="postgres",
host=local_ip,
host=db_host,
port=dbport,
)
......@@ -622,7 +654,5 @@
port=dbport,
)
else:
connect(user=user, password=password, database="postgres")
except OperationalError as exception:
if nmspc.create_user:
......@@ -633,7 +663,7 @@
connection = connect(
user="postgres",
database="postgres",
host=socket_path,
host=db_host,
port=5432,
password=POSTGRES_PASSWORD,
)
......@@ -659,6 +689,9 @@
)
return 16
arg.append("--db_host")
arg.append(db_host)
# --- restore ---
# TODO find out why odoo_help would stop us from restoring
if start_postgresql and not odoo_help and restore_filename:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment