diff --git a/ansible/playbook.yml b/ansible/playbook.yml index dc86f05b2..d44e716bd 100644 --- a/ansible/playbook.yml +++ b/ansible/playbook.yml @@ -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 }}" diff --git a/apps/fg_http/lib/fg_http/application.ex b/apps/fg_http/lib/fg_http/application.ex index a957f0768..71fe7d33c 100644 --- a/apps/fg_http/lib/fg_http/application.ex +++ b/apps/fg_http/lib/fg_http/application.ex @@ -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 diff --git a/apps/fg_http/lib/fg_http/release.ex b/apps/fg_http/lib/fg_http/release.ex index 7219fbd42..c744d2f92 100644 --- a/apps/fg_http/lib/fg_http/release.ex +++ b/apps/fg_http/lib/fg_http/release.ex @@ -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 diff --git a/pkg/debian/lib/systemd/system/fireguard.service b/pkg/debian/lib/systemd/system/fireguard.service index f69ec29b6..7bd801c30 100644 --- a/pkg/debian/lib/systemd/system/fireguard.service +++ b/pkg/debian/lib/systemd/system/fireguard.service @@ -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