TD-927: fix canal init

This commit is contained in:
ttt161 2024-08-17 12:52:37 +03:00
parent 44aa256172
commit d435cb934c
5 changed files with 49 additions and 9 deletions

View File

@ -4,9 +4,9 @@
default_db => #{ default_db => #{
host =>"127.0.0.1", host =>"127.0.0.1",
port => 5432, port => 5432,
database => "db_name", database => "progressor_db",
username => "postgres", username => "progressor",
password => "postgres" password => "progressor"
} }
}}, }},
{pools, #{ {pools, #{
@ -22,7 +22,7 @@
]}, ]},
{canal, [ {canal, [
{url, "http://vault:8200"}, {url, "http://vault"},
{engine, kvv2} {engine, kvv2}
]} ]}
]. ].

28
docker-compose.yml Normal file
View File

@ -0,0 +1,28 @@
services:
postgres:
image: postgres:15-bookworm
environment:
POSTGRES_DB: "progressor_db"
POSTGRES_USER: "progressor"
POSTGRES_PASSWORD: "progressor"
PGDATA: "/tmp/postgresql/data/pgdata"
volumes:
- progressor-data:/tmp/postgresql/data
ports:
- "5432:5432"
healthcheck:
test: ["CMD-SHELL", "pg_isready -U progressor -d progressor_db"]
interval: 10s
timeout: 5s
retries: 5
start_period: 10s
restart: unless-stopped
deploy:
resources:
limits:
cpus: "1"
memory: 4G
volumes:
progressor-data:

View File

@ -8,6 +8,6 @@
]}. ]}.
{shell, [ {shell, [
% {config, "config/sys.config"}, {config, "config/sys.config"},
{apps, [epg_connector]} {apps, [epg_connector]}
]}. ]}.

View File

@ -6,7 +6,6 @@
{applications, {applications,
[kernel, [kernel,
stdlib, stdlib,
canal,
epgsql, epgsql,
epgsql_pool epgsql_pool
]}, ]},

View File

@ -14,6 +14,7 @@
-export([start/2, stop/1]). -export([start/2, stop/1]).
start(_StartType, _StartArgs) -> start(_StartType, _StartArgs) ->
_ = maybe_start_canal(application:get_all_env(canal)),
Databases0 = application:get_env(epg_connector, databases, #{}), Databases0 = application:get_env(epg_connector, databases, #{}),
Databases = maybe_set_secrets(Databases0), Databases = maybe_set_secrets(Databases0),
Pools = application:get_env(epg_connector, pools, #{}), Pools = application:get_env(epg_connector, pools, #{}),
@ -25,6 +26,11 @@ stop(_State) ->
%% internal functions %% internal functions
maybe_start_canal([]) ->
ok;
maybe_start_canal(_Env) ->
_ = application:ensure_all_started(canal).
start_pools(Pools, Databases) -> start_pools(Pools, Databases) ->
maps:fold( maps:fold(
fun(PoolName, Opts, _Acc) -> fun(PoolName, Opts, _Acc) ->
@ -61,7 +67,7 @@ vault_client_auth(TokenPath) ->
case read_maybe_linked_file(TokenPath) of case read_maybe_linked_file(TokenPath) of
{ok, Token} -> {ok, Token} ->
Role = unicode:characters_to_binary(application:get_env(epg_connector, vault_role, ?VAULT_ROLE)), Role = unicode:characters_to_binary(application:get_env(epg_connector, vault_role, ?VAULT_ROLE)),
canal:auth({kubernetes, Role, Token}); try_auth(Role, Token);
Error -> Error ->
Error Error
end. end.
@ -79,6 +85,14 @@ read_maybe_linked_file(MaybeLinkName) ->
maybe_expand_relative(BaseFilename, Filename) -> maybe_expand_relative(BaseFilename, Filename) ->
filename:absname_join(filename:dirname(BaseFilename), Filename). filename:absname_join(filename:dirname(BaseFilename), Filename).
try_auth(Role, Token) ->
try
canal:auth({kubernetes, Role, Token})
catch
_:_ ->
{error, {canal, auth_error}}
end.
set_secrets({ok, #{<<"pg_creds">> := #{<<"pg_user">> := PgUser, <<"pg_password">> := PgPassword}}}, Databases) -> set_secrets({ok, #{<<"pg_creds">> := #{<<"pg_user">> := PgUser, <<"pg_password">> := PgPassword}}}, Databases) ->
logger:info("postgres credentials successfuly read from vault (as json)"), logger:info("postgres credentials successfuly read from vault (as json)"),
NewDbConfig = maps:fold(fun(DbName, ConnOpts, Acc) -> NewDbConfig = maps:fold(fun(DbName, ConnOpts, Acc) ->
@ -97,4 +111,3 @@ set_secrets({ok, #{<<"pg_creds">> := PgCreds}}, Databases) ->
set_secrets(Error, Databases) -> set_secrets(Error, Databases) ->
logger:error("can`t read postgres credentials from vault with error: ~p", [Error]), logger:error("can`t read postgres credentials from vault with error: ~p", [Error]),
Databases. Databases.