diff --git a/config/sys.config b/config/sys.config index e12457d..dfbb67a 100644 --- a/config/sys.config +++ b/config/sys.config @@ -4,9 +4,9 @@ default_db => #{ host =>"127.0.0.1", port => 5432, - database => "db_name", - username => "postgres", - password => "postgres" + database => "progressor_db", + username => "progressor", + password => "progressor" } }}, {pools, #{ @@ -22,7 +22,7 @@ ]}, {canal, [ - {url, "http://vault:8200"}, + {url, "http://vault"}, {engine, kvv2} ]} -]. \ No newline at end of file +]. diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..080da5a --- /dev/null +++ b/docker-compose.yml @@ -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: diff --git a/rebar.config b/rebar.config index be80929..e535d78 100644 --- a/rebar.config +++ b/rebar.config @@ -8,6 +8,6 @@ ]}. {shell, [ - % {config, "config/sys.config"}, + {config, "config/sys.config"}, {apps, [epg_connector]} ]}. diff --git a/src/epg_connector.app.src b/src/epg_connector.app.src index f6fc3f2..84040ba 100644 --- a/src/epg_connector.app.src +++ b/src/epg_connector.app.src @@ -6,7 +6,6 @@ {applications, [kernel, stdlib, - canal, epgsql, epgsql_pool ]}, diff --git a/src/epg_connector_app.erl b/src/epg_connector_app.erl index 42edc98..3aebb09 100644 --- a/src/epg_connector_app.erl +++ b/src/epg_connector_app.erl @@ -14,6 +14,7 @@ -export([start/2, stop/1]). start(_StartType, _StartArgs) -> + _ = maybe_start_canal(application:get_all_env(canal)), Databases0 = application:get_env(epg_connector, databases, #{}), Databases = maybe_set_secrets(Databases0), Pools = application:get_env(epg_connector, pools, #{}), @@ -25,6 +26,11 @@ stop(_State) -> %% internal functions +maybe_start_canal([]) -> + ok; +maybe_start_canal(_Env) -> + _ = application:ensure_all_started(canal). + start_pools(Pools, Databases) -> maps:fold( fun(PoolName, Opts, _Acc) -> @@ -61,7 +67,7 @@ vault_client_auth(TokenPath) -> case read_maybe_linked_file(TokenPath) of {ok, Token} -> 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 end. @@ -79,6 +85,14 @@ read_maybe_linked_file(MaybeLinkName) -> maybe_expand_relative(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) -> logger:info("postgres credentials successfuly read from vault (as json)"), NewDbConfig = maps:fold(fun(DbName, ConnOpts, Acc) -> @@ -97,4 +111,3 @@ set_secrets({ok, #{<<"pg_creds">> := PgCreds}}, Databases) -> set_secrets(Error, Databases) -> logger:error("can`t read postgres credentials from vault with error: ~p", [Error]), Databases. -