bitwalker / distillery

Simplify deployments in Elixir with OTP releases!
MIT License
2.97k stars 398 forks source link

Ecto / Postgrex connection params issue #664

Closed kminevskiy closed 5 years ago

kminevskiy commented 5 years ago

Steps to reproduce

I was following migrations guide from the official hexdocs page. The only difference is that I'm generating a development release.

MIX_ENV=dev mix compile
MIX_ENV=dev mix release

This is the first time I'm deploying Elixir app using Distillery, so I'm pretty sure I'm missing something obvious. I assumed my app's repo / db config would've been read during release packaging stage from the relevant environment config. Was that a wrong assumption?

Verbose Logs

``` ** (KeyError) key :database not found in: [types: Postgrex.DefaultTypes, hostname: "localhost", username: "user", port: 5432, repo: MyApp.Repo, telemetry_prefix: [:myapp, :repo], otp_app: :myapp, timeout: 15000, pool_size: 2, show_sensitive_data_on_connection_error: true, pool: DBConnection.ConnectionPool] (elixir) lib/keyword.ex:389: Keyword.fetch!/2 (postgrex) lib/postgrex/protocol.ex:90: Postgrex.Protocol.connect/1 (db_connection) lib/db_connection/connection.ex:66: DBConnection.Connection.connect/2 (connection) lib/connection.ex:622: Connection.enter_connect/5 (stdlib) proc_lib.erl:249: :proc_lib.init_p_do_apply/3 Last message: nil State: Postgrex.Protocol ** (EXIT from #PID<0.93.0>) shutdown ```

Description of issue

Custom command will start services and run migrations.

2.0.13

Ubuntu 18.04.02, Kernel 4.15.0-48, Elixir 1.8.2, Erlang/OTP 22

rel/config.exs contains default configuration.

Not sure if I'm missing additional configuration for Ecto or the guide misses these details.

Relevant portion of my config/dev.exs:

``` config :myapp, MyAppWeb.Endpoint, http: [port: {:system, "PORT"}], url: [host: "localhost", port: {:system, "PORT"}], cache_static_manifest: "priv/static/cache_manifest.json", server: true, root: ".", version: Application.spec(:myapp, :vsn), debug_errors: true, code_reloader: false, check_origin: false, watchers: [] # Configure your database config :myapp, MyApp.Repo, username: "user", password: "secret", database: "db_name_here", hostname: "localhost", pool_size: 10 ```
bitwalker commented 5 years ago

You say you are deploying a dev release, but using the default configuration - are you building the release locally and then deploying to another machine, or building the release on the same machine it is being run on?

I just ran mix phx.new --no-html --no-webpack --database=postgres, added distillery (2.0.14) to deps, changed config/dev.exs to use the correct username/password for my local pgsql instance, but left the default database name, then built a release with MIX_ENV=dev mix release and ran it with _build/dev/rel/app/bin/app console. It fails to connect to the database, but clearly uses the right configuration, and I can dump the config with Application.get_all_env/1 and it is correct.

If you are moving a dev release to another machine, that will not work. By default, dev releases are built with dev_mode: true, which is designed for running releases during development from within _build. Much of the release is symlinked rather than copied, and so the release will not function correctly or at all if moved.

You either need to set dev_mode: false, or use a custom env in rel/config.exs to manage the configuration so that you can build releases for non-prod environments.

If that is not the issue, then I'll need a more complete reproduction case to help troubleshoot, because a newly created app with the exact same parameters you described works correctly, which means there has to be another factor in the mix here.

I am going to close this for now, but feel free to reopen if you put together a repro case for me, and I'll do what I can to help find a solution!

kminevskiy commented 5 years ago

Paul,

Thanks a lot for your detailed response, really appreciate it!

I wanted to play with Distillery first, so I decided to spin up an Ubuntu VM, build my test releases there and then deploy to the actual server (which is also running Ubuntu).

I'll explain what I want(ed) to achieve: I'd like to have multi-environment (let's say development, staging and production) releases for my Elixir / Phoenix app. Obviously, they will be using different environment configs. I assumed (incorrectly, as it appears) that I can deploy development release just like production. But your explanation makes total sense.

I suppose I'll just add custom envs to my rel/config.exs. And as far as I understand, they will all have to have dev_mode set to false (if I want to deploy them to a different machine), right?