~afontaine/home

1aa24828799b6be3e18279dd8efbab387a43cc78 — Andrew Fontaine 7 months ago 5cf0259
Add Oneshot Service for Creating and Migrating DB

This should create and update the database schema before starting the
web application via a SystemD oneshot service.
2 files changed, 70 insertions(+), 16 deletions(-)

A apps/blog/lib/release.ex
M flake.nix
A apps/blog/lib/release.ex => apps/blog/lib/release.ex +32 -0
@@ 0,0 1,32 @@
defmodule Blog.Release do
  @app :blog

  def migrate do
    load_app()

    for repo <- repos() do
      {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :up, all: true))
    end
  end

  def rollback(repo, version) do
    load_app()
    {:ok, _, _} = Ecto.Migrator.with_repo(repo, &Ecto.Migrator.run(&1, :down, to: version))
  end

  def create do
    load_app()

    for repo <- repos() do
      repo.__adapter__.storage_up(repo.config)
    end
  end

  defp repos do
    Application.fetch_env!(@app, :ecto_repos)
  end

  defp load_app do
    Application.load(@app)
  end
end

M flake.nix => flake.nix +38 -16
@@ 122,7 122,9 @@
        };
        defaultPackage = self.packages.${system}.home;
        nixosModule = { config, lib, pkgs, ... }:
          let cfg = config.services.home-web;
          let
            cfg = config.services.home-web;
            home = self.packages.${system}.home;
          in {
            options = {
              services.home-web = {


@@ 168,21 170,41 @@
                createhome = true;
              };

              systemd.services.home-web = {
                description = "Home, home on the web";
                wantedBy = [ "multi-user.target" ];
                after = [ "network.target" ];
                script = ''
                  ${self.packages.${system}.home}/bin/home start
                '';
                environment = {
                  RELEASE_TMP = cfg.dataDir;
                  TZ_DATA_DIR = cfg.dataDir;
                  POOL_SIZE = cfg.poolSize;
                  SECRET_KEY_BASE_FILE = cfg.secretKeyBaseFile;
                  PORT = cfg.port;
                  DATABASE_NAME = cfg.databaseName;
                  DATABASE_SOCKET_DIR = cfg.databaseSocketDir;
              systemd.services = {
                home-web-setup = {
                  description = "Create and Migrate Home Web Database";
                  wantedBy = [ "multi-user.target" ];
                  script = ''
                    ${home}/bin/home eval "Blog.Release.create"
                    ${home}/bin/home eval "Blog.Release.migrate"
                  '';
                  serviceConfig.Type = "oneshot";
                  environment = {
                    RELEASE_TMP = cfg.dataDir;
                    TZ_DATA_DIR = cfg.dataDir;
                    SECRET_KEY_BASE_FILE = cfg.secretKeyBaseFile;
                    DATABASE_NAME = cfg.databaseName;
                    DATABASE_SOCKET_DIR = cfg.databaseSocketDir;
                  };
                };

                home-web = {
                  description = "Home, home on the web";
                  wantedBy = [ "multi-user.target" ];
                  after = [ "network.target" "home-web-setup.oneshot" ];
                  wants = [ "home-web-setup.oneshot" ];
                  script = ''
                    ${home}/bin/home start
                  '';
                  environment = {
                    RELEASE_TMP = cfg.dataDir;
                    TZ_DATA_DIR = cfg.dataDir;
                    POOL_SIZE = cfg.poolSize;
                    SECRET_KEY_BASE_FILE = cfg.secretKeyBaseFile;
                    PORT = cfg.port;
                    DATABASE_NAME = cfg.databaseName;
                    DATABASE_SOCKET_DIR = cfg.databaseSocketDir;
                  };
                };
              };
            };