Use systemd like a boss

This commit is contained in:
Jamil Bou Kheir
2021-01-03 12:00:52 -08:00
parent 32cf8b4c0f
commit f678af145e
4 changed files with 30 additions and 37 deletions

View File

@@ -82,26 +82,18 @@
shell: |
cd /home/vagrant/fireguard
dpkg -i fireguard_amd64.deb
- name: Migrate DB
become: yes
become_user: fireguard
shell: |
env $(cat /opt/fireguard/config.env | grep "^[^#]" | xargs) \
/opt/fireguard/bin/fireguard eval "FgHttp.Release.migrate"
- name: Create user
become: yes
become_user: fireguard
shell: |
env $(cat /opt/fireguard/config.env | grep "^[^#]" | xargs) \
/opt/fireguard/bin/fireguard eval "FgHttp.Release.create_admin_user"
register: create_user
- name: Display user credentials
debug: msg="{{ create_user.stdout_lines }}"
- name: Enable systemd unit
- name: Enable systemd units
become: yes
shell: |
systemctl enable fireguard
- name: Start systemd unit
- name: Start systemd units
become: yes
shell: |
systemctl start fireguard
- name: Capture user credentials
become: yes
shell: |
journalctl -u fireguard
register: create_user
- name: Display user credentials
debug: msg="{{ create_user.stdout_lines }}"

View File

@@ -6,16 +6,18 @@ defmodule FgHttp.Application do
use Application
def start(_type, _args) do
children = case Application.get_env(:fg_http, :minimal) do
true ->
[FgHttp.Repo]
_ ->
[
FgHttp.Repo,
{Phoenix.PubSub, name: :fg_http_pub_sub},
FgHttpWeb.Endpoint
]
end
children =
case Application.get_env(:fg_http, :minimal) do
true ->
[FgHttp.Repo]
_ ->
[
FgHttp.Repo,
{Phoenix.PubSub, name: :fg_http_pub_sub},
FgHttpWeb.Endpoint
]
end
# See https://hexdocs.pm/elixir/Supervisor.html
# for other strategies and supported options

View File

@@ -24,12 +24,12 @@ defmodule FgHttp.Release do
{:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
end
# App should be loaded at this point; call with `rpc` not `eval`
def create_admin_user do
start_app()
unless Repo.exists?(User) do
email = "admin@fireguard.local"
password = secret(12)
Users.create_user(
email: email,
password: password,
@@ -40,12 +40,6 @@ defmodule FgHttp.Release do
end
end
defp start_app do
load_app()
Application.put_env(@app, :minimal, true)
Application.ensure_all_started(@app)
end
defp secret(length) do
:crypto.strong_rand_bytes(length) |> Base.encode64() |> binary_part(0, length)
end
@@ -59,10 +53,11 @@ defmodule FgHttp.Release do
end
defp log_email_password(email, password) do
IO.puts("================================================================================")
IO.puts("=================================================================================")
IO.puts("FireGuard user created! Save this information because it will NOT be shown again.")
IO.puts("Use this to log into the Web UI at #{FgHttpWeb.Endpoint.url()}.")
IO.puts("Email: #{email}")
IO.puts("Password: #{password}")
IO.puts("================================================================================")
IO.puts("=================================================================================")
end
end

View File

@@ -1,5 +1,6 @@
[Unit]
Description=FireGuard
Requires=postgresql.service
After=postgresql.service
[Service]
@@ -7,7 +8,10 @@ Restart=on-failure
RestartSec=1
User=fireguard
EnvironmentFile=/opt/fireguard/config.env
ExecStartPre=/opt/fireguard/bin/fireguard eval "FgHttp.Release.migrate"
ExecStart=/opt/fireguard/bin/fireguard start
ExecStartPost=sleep 5
ExecStartPost=/opt/fireguard/bin/fireguard rpc "FgHttp.Release.create_admin_user"
[Install]
WantedBy=multi-user.target