Skip to content

Instantly share code, notes, and snippets.

@MSch
Last active February 24, 2022 17:19
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save MSch/9cfc185e24e2f3509650 to your computer and use it in GitHub Desktop.
Save MSch/9cfc185e24e2f3509650 to your computer and use it in GitHub Desktop.
defmodule ReleaseManager.Plugin.ReleaseTasks do
@name "release_tasks"
@shortdoc "Generates an escript to invoke PS.ReleaseTasks"
use ReleaseManager.Plugin
alias ReleaseManager.Utils
def before_release(_), do: nil
def after_release(%Config{name: name}) do
File.write(Utils.rel_dest_path([name, "bin", "release_tasks.escript"]), """
#!/usr/bin/env escript
%%! -config running-config/sys.config
main(Args) -> 'Elixir.PS.ReleaseTasks':main(Args).
""")
end
def after_cleanup(_), do: nil
def after_package(_), do: nil
end
defmodule PS.ReleaseTasks do
@moduledoc ~S"""
Mix is not available in a built release. Instead we define the tasks here and use a small escript
that delegates to `PS.ReleaseTasks.main`. See `ReleaseManager.Plugin.ReleaseTasks`.
In the release you can invoke tasks like this:
bin/pssync escript bin/release_tasks.escript migrate
"""
def main(args) do
start_applications([:elixir])
run_task(args)
end
defp run_task(['migrate']), do: migrate()
defp run_task(['seed'|args]), do: seed(args)
def migrate do
start_repo()
migrations_path = Application.app_dir(:pssync, "priv/migrations")
Ecto.Migrator.run(PS.Repo, migrations_path, :up, all: true)
end
defp seed(args) do
start_repo()
# PS.Repo.insert(...)
end
defp start_applications(apps) do
Enum.each(apps, fn app ->
{:ok, _} = Application.ensure_all_started(app)
end)
end
defp start_repo do
start_applications([:logger, :sbroker, :postgrex, :ecto])
:ok = Application.load(:pssync)
{:ok, _} = PS.Repo.start_link()
end
defp fatal(message) do
IO.puts :stderr, message
System.halt(1)
end
end
@MSch
Copy link
Author

MSch commented Feb 12, 2016

This puts an escript into the release that just calls an Elixir module where the actual work happens. This Elixir module is part of the normal release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment