1
- import os
2
1
from time import sleep
3
2
from typing import Optional , Union
4
3
5
4
import docker
6
5
import psycopg
7
- from pydantic import Field
6
+ from pydantic import BaseModel , Field
8
7
9
8
from bridge .console import log_task
10
- from bridge .service .docker import BaseEnvironment , ContainerConfig , DockerService
9
+ from bridge .service .docker import ContainerConfig , DockerService
11
10
from bridge .utils .filesystem import resolve_dot_bridge
12
11
13
12
14
- class PostgresEnvironment (BaseEnvironment ):
15
- user : str = "postgres"
16
- password : str = "postgres"
17
- db : str = "postgres"
18
- host : str = "localhost"
19
- port : Union [int , str ] = "5432"
20
-
21
- @classmethod
22
- def from_env (cls ):
23
- return cls (
24
- user = os .environ .get ("POSTGRES_USER" , "postgres" ),
25
- password = os .environ .get ("POSTGRES_PASSWORD" , "postgres" ),
26
- db = os .environ .get ("POSTGRES_DB" , "postgres" ),
27
- host = os .environ .get ("POSTGRES_HOST" , "localhost" ),
28
- port = os .environ .get ("POSTGRES_PORT" , "5432" ),
29
- )
30
-
31
- def to_container_run_kwargs (self ) -> dict [str , Union [int , str ]]:
32
- return {
33
- "POSTGRES_USER" : self .user ,
34
- "POSTGRES_PASSWORD" : self .password ,
35
- "POSTGRES_DB" : self .db ,
36
- "POSTGRES_HOST" : self .host ,
37
- "POSTGRES_PORT" : self .port ,
38
- }
13
+ class PostgresEnvironment (BaseModel ):
14
+ POSTGRES_USER : str = "postgres"
15
+ POSTGRES_PASSWORD : str = "postgres"
16
+ POSTGRES_DB : str = "postgres"
17
+ POSTGRES_HOST : str = "localhost"
18
+ POSTGRES_PORT : str = "5432"
39
19
40
20
41
21
class PostgresConfig (ContainerConfig [PostgresEnvironment ]):
42
22
image : str = "postgres:12"
43
23
name : str = "bridge_postgres"
44
24
ports : dict [str , int ] = {"5432/tcp" : 5432 }
45
- volumes : dict [str , str ] = Field (
25
+ volumes : dict [str , Union [ list [ str ], dict [ str , str ]] ] = Field (
46
26
default_factory = lambda : {
47
27
f"{ resolve_dot_bridge ()} /pgdata" : {
48
28
"bind" : "/var/lib/postgresql/data" ,
49
29
"mode" : "rw" ,
50
30
}
51
31
}
52
32
)
53
- environment : PostgresEnvironment = PostgresEnvironment ( )
33
+ environment : PostgresEnvironment = Field ( default_factory = PostgresEnvironment )
54
34
55
35
56
36
class PostgresService (DockerService [PostgresConfig ]):
@@ -61,11 +41,11 @@ def __init__(
61
41
62
42
def ensure_ready (self ):
63
43
dsn = (
64
- f"dbname={ self .config .environment .db } "
65
- f"user={ self .config .environment .user } "
66
- f"password={ self .config .environment .password } "
67
- f"host={ self .config .environment .host } "
68
- f"port={ self .config .environment .port } "
44
+ f"dbname={ self .config .environment .POSTGRES_DB } "
45
+ f"user={ self .config .environment .POSTGRES_USER } "
46
+ f"password={ self .config .environment .POSTGRES_PASSWORD } "
47
+ f"host={ self .config .environment .POSTGRES_HOST } "
48
+ f"port={ self .config .environment .POSTGRES_PORT } "
69
49
)
70
50
with log_task (
71
51
start_message = f"Waiting for [white]{ self .config .name } [/white] to be ready" ,
0 commit comments