0
votes

I am running into a problem when trying to use Distillery to get system variables at run time. Currently, I have defined the connection to the database as so:

config :myapp, MyApp.Repo,
  adapter: Ecto.Adapters.Postgres,
  timeout: 2000000,
    username: "${DB_USER}",
    password: "${DB_PASSWORD}",
    database:  "${DB_DATABASE}",
    hostname: "${DB_HOST}",
   port: 5432,
  pool_size: 10

Then, I am running phx.digest and mix release to get my tar file. I then deploy this tar file using a systemd configuration that I run on my ubuntu server which has all the environment variables set. These are the important lines of the systemd service:

[Service]
Type=forking
WorkingDirectory=/opt/app
User=ubuntu
RemainAfterExit=yes
Restart=on-failure
RestartSec=5
Environment=MIX_ENV=stage "PORT=4000"
Environment=LANG=en_US.UTF-8
Environment=REPLACE_OS_VARS=true
EnvironmentFile=/etc/profile
ExecStart=/opt/app/bin/myapp migrate-start
ExecStop=/opt/app/bin/myapp stop
ExecReload=/opt/app/bin/myapp restart
SyslogIdentifier=myapp

When I try to run this service on the server instance. It is trying to connect to ${DB_HOST}:5432 as the url and is not evaluating that I wan't to replace that with a configured value. I have run successfully run this before using hardcoded values so I know the issue is with replacing the values. Does anyone have any suggestions to how I can fix this error? I have all the necessary values set on the server, including REPLACE_OS_VARS=true set again.

Any help or guidance is appreciated.

1
but how you know that the envirement variable can be used in ecto config?Jiang YD

1 Answers

0
votes

The REPLACE_OS_VARS setting is a helper for inserting "normal" environment variables, but at build time rather than run time.

The database configuration values here are a great use of the helper, since you likely need to connect to the DB at application startup. The limitation is they won't be able to access or grab any environment variables on the machine running the tar.

I'll usually have a .env file that contains all the environment variables I want at build time.

# .env contents

 export REPLACE_OS_VARS=true
 export DB_HOSTNAME="postgres"
...

Than the release-build process looks something like:

$ MIX_ENV=prod
$ source .env
$ mix deps.get
$ mix deps.compile
$ mix phx.digest
$ mix release

What this means is that you'll need to have the "environment" variables that are injected with REPLACE_OS_VARS=true that are all available on the machine that builds the release, rather than the machine that receives the built tar. Or use another configuration management solution.

Here's the resource that helped me when I was starting out: https://hexdocs.pm/distillery/getting-started.html#application-configuration