Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add mix tasks for generating release artifacts #119

Merged
merged 7 commits into from
Jul 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,5 @@ npm-debug.log
/assets/node_modules/

burrito_out/*

supavisor-*.tar.gz
25 changes: 24 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ dev.node2:
SECRET_KEY_BASE="dev" \
CLUSTER_POSTGRES="true" \
ERL_AFLAGS="-kernel shell_history enabled" \
iex --name [email protected] --cookie cookie -S mix phx.server
iex --name [email protected] --cookie cookie -S mix phx.server

dev_bin:
MIX_ENV=dev mix release supavisor_bin && ls -l burrito_out
Expand Down Expand Up @@ -55,3 +55,26 @@ pgbench_short:

pgbench_long:
PGPASSWORD=postgres pgbench -M extended --transactions 100 --jobs 10 --client 60 -h localhost -p 7654 -U transaction.localhost postgres

clean:
rm -rf _build && rm -rf deps

dev_release:
mix deps.get && mix compile && mix release supavisor

dev_up:
rm -rf _build/dev/lib/supavisor && \
MIX_ENV=dev mix compile && \
mix release supavisor

dev_start_rel:
MIX_ENV=dev \
VAULT_ENC_KEY="aHD8DZRdk2emnkdktFZRh3E9RNg4aOY7" \
API_JWT_SECRET=dev \
METRICS_JWT_SECRET=dev \
REGION=eu \
FLY_ALLOC_ID=111e4567-e89b-12d3-a456-426614174000 \
SECRET_KEY_BASE="dev" \
CLUSTER_POSTGRES="true" \
ERL_AFLAGS="-kernel shell_history enabled" \
./_build/dev/rel/supavisor/bin/supavisor start_iex
2 changes: 1 addition & 1 deletion VERSION
Original file line number Diff line number Diff line change
@@ -1 +1 @@
0.2.9
0.2.10
51 changes: 51 additions & 0 deletions lib/tasks/gen.appup.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
defmodule Mix.Tasks.Supavisor.Gen.Appup do
@moduledoc """
Generates an appup file for a given release.

## Examples

# Generate an appup from 0.0.1 to 0.0.2 versions

mix supavisor.gen.appup --from=0.0.1 --to=0.0.2
"""

use Mix.Task
alias Distillery.Releases.Appup

@impl true
def run(args) do
{parsed, _, _} = OptionParser.parse(args, strict: [from: :string, to: :string])

{from_vsn, to_vsn} =
if !parsed[:from] || !parsed[:to] do
Mix.Task.run("help", ["supavisor.gen.appup"])
System.halt(1)
else
{parsed[:from], parsed[:to]}
end

IO.puts("Generating appup from #{from_vsn} to #{to_vsn}...\n")

rel_dir = Path.join([File.cwd!(), "_build", "#{Mix.env()}", "rel", "supavisor"])
lib_path = Path.join(rel_dir, "lib")
path_from = Path.join(lib_path, "supavisor-#{from_vsn}")
path_to = Path.join(lib_path, "supavisor-#{to_vsn}")
appup_path = Path.join([path_to, "ebin", "supavisor.appup"])

case Appup.make(:supavisor, from_vsn, to_vsn, path_from, path_to) do
{:ok, appup} ->
IO.puts("Writing appup to #{appup_path}")

case File.write(appup_path, :io_lib.format("~p.", [appup]), [:utf8]) do
:ok ->
IO.puts("Appup:\n#{File.read!(appup_path)}")

{:error, reason} ->
Mix.raise("Failed to write appup file: #{reason}")
end

{:error, reason} ->
Mix.raise("Failed to generate appup file: #{inspect(reason)}")
end
end
end
58 changes: 58 additions & 0 deletions lib/tasks/gen.relup.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
defmodule Mix.Tasks.Supavisor.Gen.Relup do
@moduledoc """
Generates an appup file for a given release.

## Examples

# Generate an appup from 0.0.1 to 0.0.2 versions

mix supavisor.gen.appup --from=0.0.1 --to=0.0.2
"""

use Mix.Task

@impl true
def run(args) do
{parsed, _, _} = OptionParser.parse(args, strict: [from: :string, to: :string])

{from_vsn, to_vsn} =
if !parsed[:from] || !parsed[:to] do
Mix.Task.run("help", ["supavisor.gen.relup"])
System.halt(1)
else
{parsed[:from], parsed[:to]}
end

IO.puts("Generating relup from #{from_vsn} to #{to_vsn}...\n")

rel_dir = Path.join([File.cwd!(), "_build", "#{Mix.env()}", "rel", "supavisor"])
prev_rel_dir = Path.join([rel_dir, "releases", from_vsn])
curr_rel_dir = Path.join([rel_dir, "releases", to_vsn])
lib_path = Path.join(rel_dir, "lib")
relup_path = Path.join(curr_rel_dir, "relup")

opts = [
{:path, [Path.join(lib_path, "*/ebin") |> to_charlist()]},
{:outdir, to_charlist(curr_rel_dir)}
]

rel1 = Path.join(prev_rel_dir, "supavisor") |> to_charlist()
rel2 = Path.join(curr_rel_dir, "supavisor") |> to_charlist()

case :systools.make_relup(rel2, [rel1], [rel1], opts) do
:ok ->
IO.puts("Writing relup to #{relup_path}\n")

case File.read(relup_path) do
{:ok, content} ->
IO.puts("Relup:\n#{content}")

{:error, reason} ->
Mix.raise("Failed to read relup file: #{reason}")
end

other ->
Mix.raise("Failed to generate relup file: #{other}")
end
end
end
23 changes: 21 additions & 2 deletions mix.exs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ defmodule Supavisor.MixProject do
start_permanent: Mix.env() == :prod,
aliases: aliases(),
deps: deps(),
releases: releases()
releases: releases(),
dialyzer: [plt_add_apps: [:mix]]
]
end

Expand Down Expand Up @@ -57,6 +58,7 @@ defmodule Supavisor.MixProject do
{:burrito, github: "burrito-elixir/burrito"},
{:libcluster, "~> 3.3.1"},
{:logflare_logger_backend, github: "Logflare/logflare_logger_backend", tag: "v0.11.1-rc.1"},
{:distillery, "~> 2.1"},

# pooller
{:poolboy, "~> 1.5.2"},
Expand All @@ -69,6 +71,7 @@ defmodule Supavisor.MixProject do
def releases do
[
supavisor: [
steps: [:assemble, &upgrade/1, :tar],
include_erts: System.get_env("INCLUDE_ERTS", "true") == "true"
],
supavisor_bin: [
Expand Down Expand Up @@ -107,5 +110,21 @@ defmodule Supavisor.MixProject do
]
end

defp version, do: File.read!("./VERSION")
defp upgrade(release) do
from = System.get_env("UPGRADE_FROM")

if from do
vsn = release.version
path = Path.join([release.path, "releases", "supavisor-#{vsn}.rel"])
rel_content = File.read!(Path.join(release.version_path, "supavisor.rel"))

Mix.Task.run("supavisor.gen.appup", ["--from=" <> from, "--to=" <> vsn])
:ok = File.write!(path, rel_content)
Mix.Task.run("supavisor.gen.relup", ["--from=" <> from, "--to=" <> vsn])
end

release
end

defp version, do: File.read!("./VERSION") |> String.trim()
end
2 changes: 2 additions & 0 deletions mix.lock
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
%{
"artificery": {:hex, :artificery, "0.4.3", "0bc4260f988dcb9dda4b23f9fc3c6c8b99a6220a331534fdf5bf2fd0d4333b02", [:mix], [], "hexpm", "12e95333a30e20884e937abdbefa3e7f5e05609c2ba8cf37b33f000b9ffc0504"},
"backoff": {:hex, :backoff, "1.1.6", "83b72ed2108ba1ee8f7d1c22e0b4a00cfe3593a67dbc792799e8cce9f42f796b", [:rebar3], [], "hexpm", "cf0cfff8995fb20562f822e5cc47d8ccf664c5ecdc26a684cbe85c225f9d7c39"},
"benchee": {:hex, :benchee, "1.1.0", "f3a43817209a92a1fade36ef36b86e1052627fd8934a8b937ac9ab3a76c43062", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}], "hexpm", "7da57d545003165a012b587077f6ba90b89210fd88074ce3c60ce239eb5e6d93"},
"bertex": {:hex, :bertex, "1.3.0", "0ad0df9159b5110d9d2b6654f72fbf42a54884ef43b6b651e6224c0af30ba3cb", [:mix], [], "hexpm", "0a5d5e478bb5764b7b7bae37cae1ca491200e58b089df121a2fe1c223d8ee57a"},
Expand All @@ -16,6 +17,7 @@
"decimal": {:hex, :decimal, "2.1.1", "5611dca5d4b2c3dd497dec8f68751f1f1a54755e8ed2a966c2633cf885973ad6", [:mix], [], "hexpm", "53cfe5f497ed0e7771ae1a475575603d77425099ba5faef9394932b35020ffcc"},
"deep_merge": {:hex, :deep_merge, "1.0.0", "b4aa1a0d1acac393bdf38b2291af38cb1d4a52806cf7a4906f718e1feb5ee961", [:mix], [], "hexpm", "ce708e5f094b9cd4e8f2be4f00d2f4250c4095be93f8cd6d018c753894885430"},
"dialyxir": {:hex, :dialyxir, "1.1.0", "c5aab0d6e71e5522e77beff7ba9e08f8e02bad90dfbeffae60eaf0cb47e29488", [:mix], [{:erlex, ">= 0.2.6", [hex: :erlex, repo: "hexpm", optional: false]}], "hexpm", "07ea8e49c45f15264ebe6d5b93799d4dd56a44036cf42d0ad9c960bc266c0b9a"},
"distillery": {:hex, :distillery, "2.1.1", "f9332afc2eec8a1a2b86f22429e068ef35f84a93ea1718265e740d90dd367814", [:mix], [{:artificery, "~> 0.2", [hex: :artificery, repo: "hexpm", optional: false]}], "hexpm", "bbc7008b0161a6f130d8d903b5b3232351fccc9c31a991f8fcbf2a12ace22995"},
"ecto": {:hex, :ecto, "3.8.4", "e06b8b87e62b27fea17fd2ff6041572ddd10339fd16cdf58446e402c6c90a74b", [:mix], [{:decimal, "~> 1.6 or ~> 2.0", [hex: :decimal, repo: "hexpm", optional: false]}, {:jason, "~> 1.0", [hex: :jason, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "f9244288b8d42db40515463a008cf3f4e0e564bb9c249fe87bf28a6d79fe82d4"},
"ecto_sql": {:hex, :ecto_sql, "3.8.3", "a7d22c624202546a39d615ed7a6b784580391e65723f2d24f65941b4dd73d471", [:mix], [{:db_connection, "~> 2.4.1 or ~> 2.5", [hex: :db_connection, repo: "hexpm", optional: false]}, {:ecto, "~> 3.8.4", [hex: :ecto, repo: "hexpm", optional: false]}, {:myxql, "~> 0.6.0", [hex: :myxql, repo: "hexpm", optional: true]}, {:postgrex, "~> 0.15.0 or ~> 0.16.0 or ~> 1.0", [hex: :postgrex, repo: "hexpm", optional: true]}, {:tds, "~> 2.1.1 or ~> 2.2", [hex: :tds, repo: "hexpm", optional: true]}, {:telemetry, "~> 0.4.0 or ~> 1.0", [hex: :telemetry, repo: "hexpm", optional: false]}], "hexpm", "348cb17fb9e6daf6f251a87049eafcb57805e2892e5e6a0f5dea0985d367329b"},
"erlex": {:hex, :erlex, "0.2.6", "c7987d15e899c7a2f34f5420d2a2ea0d659682c06ac607572df55a43753aa12e", [:mix], [], "hexpm", "2ed2e25711feb44d52b17d2780eabf998452f6efda104877a3881c2f8c0c0c75"},
Expand Down
Loading