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 @@ ...@@ -86,6 +86,10 @@
default=None, default=None,
) )
parser.add_argument( parser.add_argument(
"--db_host",
help="database host address",
)
parser.add_argument(
"--max-cron-threads", "--max-cron-threads",
help="max cron threads [default: %(default)s]", help="max cron threads [default: %(default)s]",
default=None, default=None,
...@@ -449,18 +453,6 @@ ...@@ -449,18 +453,6 @@
_logger.debug("IP found %s", local_ip) _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 # auto detect local conf
local_conf_path = nmspc.config local_conf_path = nmspc.config
user = db_user user = db_user
...@@ -499,9 +491,17 @@ ...@@ -499,9 +491,17 @@
# default values if nothing else # default values if nothing else
if not user: if not user:
# Odoo uses default if nothing is set (including in PGUSER env var) # Odoo uses default if nothing is set (including in PGUSER env var)
_logger.info("No user defined, using odoo") if start_postgresql:
user = "odoo" _logger.info("No user defined, using odoo when starting postgres")
arg.append("--db_user %s" % user) 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 # data volume handling
base_data_volume_name = "{}_data".format(project_name) base_data_volume_name = "{}_data".format(project_name)
...@@ -590,11 +590,5 @@ ...@@ -590,11 +590,5 @@
options["extra_hosts"]["py3o"] = local_ip options["extra_hosts"]["py3o"] = local_ip
options["extra_hosts"]["py3o-fusion-server"] = local_ip options["extra_hosts"]["py3o-fusion-server"] = local_ip
if start_postgresql and not odoo_help: # Database connection
# 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"))
...@@ -600,4 +594,11 @@ ...@@ -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 # 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: handle the case where the database is still starting up
# TODO: (and remove the sleep) # TODO: (and remove the sleep)
...@@ -601,5 +602,4 @@ ...@@ -601,5 +602,4 @@
# Check that connection can be done, try to create user if asked to # 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: handle the case where the database is still starting up
# TODO: (and remove the sleep) # TODO: (and remove the sleep)
_logger.info("Testing database connection on base postgres")
if not odoo_help: if not odoo_help:
...@@ -605,4 +605,22 @@ ...@@ -605,4 +605,22 @@
if not odoo_help: 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: try:
if start_postgresql: if start_postgresql:
connect( connect(
...@@ -610,5 +628,5 @@ ...@@ -610,5 +628,5 @@
password=password, password=password,
database="postgres", database="postgres",
host=socket_path, host=socket_path,
port=5432, port=dbport,
) )
...@@ -614,7 +632,21 @@ ...@@ -614,7 +632,21 @@
) )
elif local_ip: _logger.info("Connection successful")
# TODO test this too 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( connect(
user=user, user=user,
password=password, password=password,
database="postgres", database="postgres",
...@@ -617,7 +649,7 @@ ...@@ -617,7 +649,7 @@
connect( connect(
user=user, user=user,
password=password, password=password,
database="postgres", database="postgres",
host=local_ip, host=db_host,
port=dbport, port=dbport,
) )
...@@ -622,7 +654,5 @@ ...@@ -622,7 +654,5 @@
port=dbport, port=dbport,
) )
else:
connect(user=user, password=password, database="postgres")
except OperationalError as exception: except OperationalError as exception:
if nmspc.create_user: if nmspc.create_user:
...@@ -633,7 +663,7 @@ ...@@ -633,7 +663,7 @@
connection = connect( connection = connect(
user="postgres", user="postgres",
database="postgres", database="postgres",
host=socket_path, host=db_host,
port=5432, port=5432,
password=POSTGRES_PASSWORD, password=POSTGRES_PASSWORD,
) )
...@@ -659,6 +689,9 @@ ...@@ -659,6 +689,9 @@
) )
return 16 return 16
arg.append("--db_host")
arg.append(db_host)
# --- restore --- # --- restore ---
# TODO find out why odoo_help would stop us from restoring # TODO find out why odoo_help would stop us from restoring
if start_postgresql and not odoo_help and restore_filename: 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