Skip to content

Commit e7b6e73

Browse files
authored
[IMP] Use network alias as an alternative to db service unique name (#522)
* Revert "Using unique service db names (#515)" This reverts commit cb3e32a. * [IMP] Use network alias as an alternative to db service unique name
2 parents a3ae5ea + 8b2c66d commit e7b6e73

6 files changed

+24
-100
lines changed

common.yaml.jinja

-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{% import "_macros.jinja" as macros -%}
2-
{%- import "_traefik2_labels.yml.jinja" as traefik2_labels -%}
3-
42
version: "2.4"
53

64
services:
@@ -41,7 +39,6 @@ services:
4139
environment:
4240
POSTGRES_DB: *dbname
4341
POSTGRES_USER: *dbuser
44-
PGHOST: "${DB_HOST:-db}"
4542
CONF_EXTRA: |
4643
work_mem = 512MB
4744
volumes:

devel.yaml.jinja

+4-9
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
{%- import "_macros.jinja" as macros -%}
2-
{%- import "_traefik2_labels.yml.jinja" as traefik2_labels -%}
3-
{%- set _key = traefik2_labels.key(project_name, odoo_version, "devel") -%}
42
{% set whitelisted_hosts = (
53
"cdnjs.cloudflare.com",
64
"fonts.googleapis.com",
@@ -54,12 +52,11 @@ services:
5452
WDB_WEB_PORT: "{{ macros.version_major(odoo_version) }}984"
5553
# To avoid installing demo data export DOODBA_WITHOUT_DEMO=all
5654
WITHOUT_DEMO: "${DOODBA_WITHOUT_DEMO-false}"
57-
PGHOST: {{ _key }}-db
5855
volumes:
5956
- ./odoo/custom:/opt/odoo/custom:ro,z
6057
- ./odoo/auto:/opt/odoo/auto:rw,z
6158
depends_on:
62-
- {{ _key }}-db
59+
- db
6360
{% for host in whitelisted_hosts -%}
6461
- proxy_{{ host|replace(".", "_") }}
6562
{% endfor -%}
@@ -80,14 +77,13 @@ services:
8077
{%- endif %}
8178

8279
{% if postgres_version -%}
83-
{{ _key }}-db:
80+
db:
8481
extends:
8582
file: common.yaml
8683
service: db
8784
environment:
8885
POSTGRES_DB: *dbname
8986
POSTGRES_PASSWORD: odoopassword
90-
DB_HOST: {{ _key }}-db
9187
{%- endif %}
9288

9389
pgweb:
@@ -96,10 +92,9 @@ services:
9692
ports:
9793
- "127.0.0.1:{{ macros.version_major(odoo_version) }}081:8081"
9894
environment:
99-
DATABASE_URL: postgres://{{ postgres_username }}:odoopassword@{{ _key }}-db:5432/devel?sslmode=disable
100-
PGHOST: {{ _key }}-db
95+
DATABASE_URL: postgres://{{ postgres_username }}:odoopassword@db:5432/devel?sslmode=disable
10196
depends_on:
102-
- {{ _key }}-db
97+
- db
10398

10499
smtp:
105100
extends:

prod.yaml.jinja

+10-5
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ services:
3232
{%- endif %}
3333
PGHOST: {{ _key }}-db
3434
depends_on:
35-
- {{ _key }}-db
35+
- db
3636
{%- if smtp_relay_host %}
3737
- smtp
3838
{%- endif %}
@@ -84,7 +84,7 @@ services:
8484
{%- endif %}
8585

8686
{% if postgres_version -%}
87-
{{ _key }}-db:
87+
db:
8888
extends:
8989
file: common.yaml
9090
service: db
@@ -97,6 +97,8 @@ services:
9797
{%- if traefik_version == 3 %}
9898
networks:
9999
default:
100+
aliases:
101+
- "{{ _key }}-db"
100102
inverseproxy_shared:
101103
labels:
102104
traefik.enable: "true"
@@ -112,6 +114,11 @@ services:
112114
ports:
113115
- "{{ postgres_exposed_port }}:5432"
114116
{%- endif %}
117+
{%- else %}
118+
networks:
119+
default:
120+
aliases:
121+
- "{{ _key }}-db"
115122
{%- endif %}
116123
{%- endif %}
117124

@@ -139,10 +146,8 @@ services:
139146
- .docker/backup.env
140147
- .docker/db-access.env
141148
restart: unless-stopped
142-
environment:
143-
PGHOST: {{ _key }}-db
144149
depends_on:
145-
- {{ _key }}-db
150+
- db
146151
{%- if smtp_relay_host %}
147152
- smtp
148153
{%- endif %}

tasks_downstream.py

+4-40
Original file line numberDiff line numberDiff line change
@@ -26,42 +26,10 @@
2626

2727
PROJECT_ROOT = Path(__file__).parent.absolute()
2828
SRC_PATH = PROJECT_ROOT / "odoo" / "custom" / "src"
29-
30-
# _key = os.environ.get("_key", "").strip()
31-
# DB_SERVICE = os.environ.get("DB_HOST") or (_key + "-db" if _key else "db")
32-
33-
34-
def get_db_service_name():
35-
"""
36-
Return the database service name that ends with '-db'. If not found,
37-
fall back to any service containing 'postgres' or 'db'. As a last
38-
resort, return 'db'.
39-
"""
40-
for filename in ("devel.yaml", "devel.yml", "docker-compose.yml"):
41-
compose_file = PROJECT_ROOT / filename
42-
if compose_file.exists():
43-
with open(compose_file) as f:
44-
try:
45-
compose_data = yaml.safe_load(f) or {}
46-
services = compose_data.get("services", {})
47-
for svc in services:
48-
if svc.lower().endswith("-db"):
49-
return svc
50-
for svc in services:
51-
if "postgres" in svc.lower() or "db" in svc.lower():
52-
return svc
53-
except yaml.YAMLError:
54-
pass
55-
return "db"
56-
57-
58-
DB_SERVICE = get_db_service_name()
59-
6029
UID_ENV = {
6130
"GID": os.environ.get("DOODBA_GID", str(os.getgid())),
6231
"UID": os.environ.get("DOODBA_UID", str(os.getuid())),
6332
"DOODBA_UMASK": os.environ.get("DOODBA_UMASK", "27"),
64-
"PGHOST": DB_SERVICE,
6533
}
6634
UID_ENV.update(
6735
{
@@ -1014,9 +982,7 @@ def snapshot(
1014982
if not destination_db:
1015983
destination_db = f"{source_db}-{datetime.now().strftime('%Y_%m_%d-%H_%M')}"
1016984
with c.cd(str(PROJECT_ROOT)):
1017-
cur_state = c.run(
1018-
f"{DOCKER_COMPOSE_CMD} stop odoo {DB_SERVICE}", pty=True
1019-
).stdout
985+
cur_state = c.run(f"{DOCKER_COMPOSE_CMD} stop odoo db", pty=True).stdout
1020986
_logger.info("Snapshoting current %s DB to %s", (source_db, destination_db))
1021987
_run = f"{DOCKER_COMPOSE_CMD} run --rm -l traefik.enable=false odoo"
1022988
c.run(
@@ -1026,7 +992,7 @@ def snapshot(
1026992
)
1027993
if "Stopping" in cur_state:
1028994
# Restart services if they were previously active
1029-
c.run(f"{DOCKER_COMPOSE_CMD} start odoo {DB_SERVICE}", pty=True)
995+
c.run(f"{DOCKER_COMPOSE_CMD} start odoo db", pty=True)
1030996

1031997

1032998
@task(
@@ -1047,9 +1013,7 @@ def restore_snapshot(
10471013
Uses click-odoo-copydb behind the scenes to restore a DB snapshot.
10481014
"""
10491015
with c.cd(str(PROJECT_ROOT)):
1050-
cur_state = c.run(
1051-
f"{DOCKER_COMPOSE_CMD} stop odoo {DB_SERVICE}", pty=True
1052-
).stdout
1016+
cur_state = c.run(f"{DOCKER_COMPOSE_CMD} stop odoo db", pty=True).stdout
10531017
if not snapshot_name:
10541018
# List DBs
10551019
res = c.run(
@@ -1090,4 +1054,4 @@ def restore_snapshot(
10901054
pty=True,
10911055
)
10921056
if "Stopping" in cur_state:
1093-
c.run(f"{DOCKER_COMPOSE_CMD} start odoo {DB_SERVICE}", pty=True)
1057+
c.run(f"{DOCKER_COMPOSE_CMD} start odoo db", pty=True)

test.yaml.jinja

+2-5
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ services:
2424
SMTP_SERVER: smtplocal
2525
# Just in case you use queue_job
2626
ODOO_QUEUE_JOB_CHANNELS: "root:1"
27-
PGHOST: {{ _key }}-db
2827
restart: unless-stopped
2928
{%- if domains_test %}
3029
hostname: {{ macros.first_main_domain(domains_test)|tojson }}
3130
{%- endif %}
3231
depends_on:
33-
- {{ _key }}-db
32+
- db
3433
- smtp
3534
networks:
3635
default:
@@ -58,14 +57,12 @@ services:
5857
- --max-cron-threads=1
5958

6059
{% if postgres_version -%}
61-
{{ _key }}-db:
60+
db:
6261
extends:
6362
file: common.yaml
6463
service: db
6564
env_file:
6665
- .docker/db-creation.env
67-
environment:
68-
- DB_HOST={{ _key }}-db
6966
restart: unless-stopped
7067
{%- endif %}
7168

tests/test_postgres.py

+4-38
Original file line numberDiff line numberDiff line change
@@ -8,33 +8,6 @@
88

99
from .conftest import DBVER_PER_ODOO
1010

11-
try:
12-
from python_on_whales.components.compose.models import ComposeConfig
13-
except ImportError:
14-
ComposeConfig = None
15-
16-
17-
def _get_db_service_name(dc: DockerClient) -> str:
18-
config_data = dc.compose.config()
19-
20-
# (1) In newer versions of python-on-whales:
21-
# config_data is a ComposeConfig => config_data.services is a dict:
22-
# { "service_name": ServiceConfig(...) }
23-
# (2) In older versions: config_data is a "legacy" dict
24-
# => config_data["services"] => { "service_name": {...} }
25-
26-
if ComposeConfig and isinstance(config_data, ComposeConfig):
27-
services_dict = config_data.services
28-
else:
29-
services_dict = config_data["services"]
30-
for svc_name in services_dict:
31-
if svc_name.lower().endswith("-db"):
32-
return svc_name
33-
for svc_name in services_dict:
34-
if "postgres" in svc_name.lower() or "db" in svc_name.lower():
35-
return svc_name
36-
return "db"
37-
3811

3912
@pytest.mark.parametrize("dbver", ("oldest", "latest"))
4013
def test_postgresql_client_versions(
@@ -44,24 +17,18 @@ def test_postgresql_client_versions(
4417
dbver: str,
4518
):
4619
"""Test multiple postgresql-client versions in odoo, db and duplicity services"""
47-
unique_project_name = f"test_{uuid.uuid4().hex}"
48-
dc_prod = DockerClient(
49-
compose_files=["prod.yaml"],
50-
compose_project_name=unique_project_name,
51-
)
52-
5320
dbver_raw = DBVER_PER_ODOO[supported_odoo_version][dbver]
5421
dbver_mver = dbver_raw.split(".")[0]
55-
22+
dc_prod = DockerClient(compose_files=["prod.yaml"])
5623
with local.cwd(tmp_path):
5724
print(str(cloned_template))
58-
25+
assert True
5926
run_copy(
6027
str(cloned_template),
6128
dst_path=".",
6229
data={
6330
"odoo_version": supported_odoo_version,
64-
"project_name": unique_project_name,
31+
"project_name": uuid.uuid4().hex,
6532
"odoo_proxy": "",
6633
"postgres_version": dbver_raw,
6734
"backup_dst": "/tmp/dummy",
@@ -73,7 +40,6 @@ def test_postgresql_client_versions(
7340
)
7441
try:
7542
dc_prod.compose.build()
76-
db_svc = _get_db_service_name(dc_prod)
7743
odoo_pgdump_stdout = dc_prod.compose.run(
7844
"odoo",
7945
command=["pg_dump", "--version"],
@@ -84,7 +50,7 @@ def test_postgresql_client_versions(
8450
odoo_pgdump_stdout.splitlines()[-1].strip().split(" ")[2].split(".")[0]
8551
)
8652
db_pgdump_stdout = dc_prod.compose.run(
87-
db_svc,
53+
"db",
8854
command=["pg_dump", "--version"],
8955
remove=True,
9056
tty=False,

0 commit comments

Comments
 (0)