Skip to content

Commit 23b47a9

Browse files
authored
Merge pull request #3 from vigetlabs/db-better-env-loading
Better env prod loading Start a FAQ
2 parents 87387c6 + 129228c commit 23b47a9

File tree

5 files changed

+119
-27
lines changed

5 files changed

+119
-27
lines changed

README.md

Lines changed: 95 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,9 @@ Distillery's guide into scripts.
2222
of 20180524. These samples were pulled from
2323
[master](https://github.com/bitwalker/distillery/blob/fa6777fdc0c61aa8fcad54ffaabbb6829dd4fb38/docs/guides/running_migrations.md).
2424
- [lib/your_app/release_tasks.ex](./lib/your_app/release_tasks.ex).
25-
- [rel/commands/migrate.sh](./rel/commands/migrate.sh)
25+
- [rel/commands/migrate.sh](./rel/commands/migrate.sh) There is a
26+
difference here where the `env.prod` file is sourced to get the
27+
secrets in the environment before running the command.
2628
- [rel/config.exs](./rel/config.exs)
2729
- [priv/repo/seeds.exs](./priv/repo/seeds.exs) Sample seed file. All of
2830
it is commented out, but you can get an idea of what real seeds look
@@ -45,6 +47,7 @@ vary if you use a different OS or SaaS.
4547
# find .ssh -print | cpio -pdmv --owner=deploy ~deploy
4648
# sudo su - deploy
4749
$ mkdir -p your_app/postgres_backups
50+
$ mkdir -p your_app/config
4851
```
4952

5053
## HTTP server
@@ -62,8 +65,8 @@ $ sudo nginx -c /etc/nginx/nginx.conf -t
6265
$ sudo systemctl restart nginx
6366
```
6467

65-
The sample nginx config is not fit for real production usage. This will
66-
get you started, but you should definitely setup SSL and HTTPS.
68+
The sample nginx config is not fit for production usage. This will get
69+
you started, but you should definitely setup SSL and HTTPS.
6770

6871
Follow [Digital Ocean's guide] for enabling SSL, and make sure You
6972
update the server config in `config/prod.exs`
@@ -104,21 +107,24 @@ Please don't use `shh` as your password :\
104107
## Copy secrets over
105108

106109
```bash
107-
# ~/.bashrc
108-
export LANG=en_US.UTF-8
109-
export MIX_ENV=prod
110-
export REPLACE_OS_VARS=true
111-
# If you're using Phoenix, you'll need this:
112-
export HOST=localhost
113-
export SECRET_KEY_BASE=1234
114-
export PORT=4000
110+
# ~/your_app/config/env.prod
111+
LANG=en_US.UTF-8
112+
MIX_ENV=prod
113+
REPLACE_OS_VARS=true
114+
RELEASE_NAME=your_app_production
115+
116+
# If you're using Phoenix, you'll need these:
117+
HOST=localhost
118+
SECRET_KEY_BASE=1234
119+
PORT=4000
120+
115121
# if you're using Ecto, you'll need these:
116-
export DB_HOST=localhost
117-
export DB_NAME=your_app
118-
export DB_PASS=shh
119-
export DB_PORT=5432
120-
export DB_USER=deploy
121-
export POOL_SIZE=95
122+
DB_HOST=localhost
123+
DB_NAME=your_app
124+
DB_PASS=shh
125+
DB_PORT=5432
126+
DB_USER=deploy
127+
POOL_SIZE=95
122128
# 95 leaves you with 5 extra connections for other processes
123129
# Postgres defaults to having 100 connections available
124130
```
@@ -172,3 +178,75 @@ How to release:
172178
1. run [bin/release.sh](./bin/release.sh). When prompted, enter the
173179
release version that distillery built. It should be mentioned in the
174180
stdout above the prompt.
181+
182+
183+
184+
# FAQ
185+
186+
### I have two instances of the app running on the same server. Will this
187+
work?
188+
189+
The `RELEASE_NAME` environment variable will be the nodename of that
190+
release, and it's required for that name to be unique. When you have
191+
both instances on the server, edit the `env.prod` file for a given
192+
instance and make sure it's unique.
193+
194+
### I don't want to source the environment file before every command
195+
196+
is there a way to automatically do that? I keep forgetting to use
197+
`REPLACE_OS_VARS=true` and add my database credentials?
198+
199+
Yes! I feelz ya. One way is to add a Distillery plugin that creates a
200+
custom `boot_check` script. In this script, copy the [default boot_check
201+
template] into `rel/templates/boot_check.eex`, but add something like
202+
this before it loads the app:
203+
204+
```bash
205+
# /rel/templates/boot_check.eex
206+
207+
... snip ...
208+
. "${SCRIPT_DIR}/../config/env.prod"
209+
export REPLACE_OS_VARS=true
210+
211+
... snip ...
212+
```
213+
214+
Then create the Distillery plugin that uses this custom boot check template:
215+
216+
```elixir
217+
defmodule YourApp.Release.CustomBootCheck do
218+
use Mix.Releases.Plugin
219+
220+
def before_assembly(_release, _opts), do: nil
221+
222+
def after_assembly(_release, _opts), do: nil
223+
224+
def before_package(%Release{} = release, template: template) do
225+
info "Generating custom executable.."
226+
227+
executable =
228+
EEx.eval_file(template, [release_name: release.name,
229+
exec_options: release.profile.exec_opts])
230+
231+
bin_path = Path.join(release.profile.output_dir, "bin")
232+
233+
File.write!(Path.join(bin_path, Atom.to_string(release.name)), executable)
234+
235+
release
236+
end
237+
238+
def after_package(_release, _opts), do: nil
239+
240+
def after_cleanup(_args, _opts), do: nil
241+
end
242+
```
243+
244+
Finally, tell Distillery to use the plugin:
245+
246+
```elixir
247+
# rel/config.exs
248+
249+
plugin YourApp.Release.CustomBootCheck, template: "rel/templates/boot_check.eex"
250+
```
251+
252+
[boot_check]: https://github.com/bitwalker/distillery/blob/master/priv/templates/boot_check.eex

env.prod

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,19 @@
1-
DB_HOST=localhost
2-
DB_NAME=yourapp
3-
DB_PASS=postgres
4-
DB_USER=postgres
5-
HOST=localhost
61
LANG=en_US.UTF-8
72
MIX_ENV=prod
83
REPLACE_OS_VARS=true
9-
POOL_SIZE=90
10-
PORT=8080
11-
SECRET_KEY_BASE="1234"
12-
# Generate a real one with mix phx.gen.secret
4+
RELEASE_NAME=production
5+
6+
# If you're using Phoenix, you'll need these:
7+
HOST=localhost
8+
SECRET_KEY_BASE=1234
9+
PORT=4000
10+
11+
# if you're using Ecto, you'll need these:
12+
DB_HOST=localhost
13+
DB_NAME=your_app
14+
DB_PASS=shh
15+
DB_PORT=5432
16+
DB_USER=deploy
17+
POOL_SIZE=95
18+
# 95 leaves you with 5 extra connections for other processes
19+
# Postgres defaults to having 100 connections available

nginx-config

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ server {
2929
proxy_set_header X-Cluster-Client-Ip $remote_addr;
3030
# See https://www.nginx.com/blog/websocket-nginx
3131
proxy_set_header Upgrade $http_upgrade;
32-
proxy_set_header Connection "upgrade";
32+
proxy_set_header Connection $connection_upgrade;
3333
proxy_pass http://phoenix;
3434
proxy_redirect off;
3535
}

rel/commands/migrate.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
11
#!/bin/sh
22

3+
. /home/deploy/your_app/config/env.prod
4+
35
REPLACE_OS_VARS=true "$RELEASE_ROOT_DIR"/bin/your_app command Elixir.YourApp.ReleaseTasks seed

rel/config.exs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,9 @@ environment :prod do
1919
]
2020
end
2121

22+
release :your_app do
23+
set version: current_version(:your_app)
24+
set name: "your_app_${RELEASE_NAME}"
25+
end
26+
2227
# ..snip..

0 commit comments

Comments
 (0)