Skip to content

Commit

Permalink
Initial proposal for hot upgrading erlang applications
Browse files Browse the repository at this point in the history
  • Loading branch information
thiagoesteves committed Oct 30, 2024
1 parent 007b43c commit d3e34e4
Show file tree
Hide file tree
Showing 11 changed files with 232 additions and 75 deletions.
8 changes: 7 additions & 1 deletion lib/deployex/deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -297,7 +297,13 @@ defmodule Deployex.Deployment do

from_version = Status.current_version(instance)

case Upgrade.execute(instance, from_version, release.version) do
case Upgrade.execute(
instance,
Storage.monitored_app_lang(),
Storage.monitored_app_name(),
from_version,
release.version
) do
:ok ->
Status.set_current_version_map(instance, release,
deployment: :hot_upgrade,
Expand Down
14 changes: 11 additions & 3 deletions lib/deployex/release/gcp_storage.ex
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,11 @@ defmodule Deployex.Release.GcpStorage do
def download_and_unpack(instance, version) do
{:ok, download_path} = Briefly.create()

monitored_app = Storage.monitored_app_name()
app_name = Storage.monitored_app_name()
app_lang = Storage.monitored_app_lang()

gcp_path =
"https://storage.googleapis.com/#{bucket()}/dist/#{monitored_app}/#{monitored_app}-#{version}.tar.gz"
"https://storage.googleapis.com/#{bucket()}/dist/#{app_name}/#{app_name}-#{version}.tar.gz"

:get
|> Finch.build(gcp_path, headers(), [])
Expand All @@ -60,7 +61,14 @@ defmodule Deployex.Release.GcpStorage do

{"", 0} = System.cmd("tar", ["-x", "-f", download_path, "-C", new_path])

Upgrade.check(instance, download_path, Status.current_version(instance), version)
Upgrade.check(
instance,
app_lang,
app_name,
download_path,
Status.current_version(instance),
version
)
after
Briefly.cleanup()
end
Expand Down
18 changes: 13 additions & 5 deletions lib/deployex/release/local.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ defmodule Deployex.Release.Local do
"""
@impl true
def get_current_version_map do
monitored_app = Storage.monitored_app_name()
app_name = Storage.monitored_app_name()

file_path = "#{bucket()}/versions/#{monitored_app}/#{env()}/current.json"
file_path = "#{bucket()}/versions/#{app_name}/#{env()}/current.json"

case File.read(file_path) do
{:ok, data} ->
Expand All @@ -39,16 +39,24 @@ defmodule Deployex.Release.Local do
"""
@impl true
def download_and_unpack(instance, version) do
monitored_app = Storage.monitored_app_name()
app_name = Storage.monitored_app_name()
app_lang = Storage.monitored_app_lang()

download_path = "#{bucket()}/dist/#{monitored_app}/#{monitored_app}-#{version}.tar.gz"
download_path = "#{bucket()}/dist/#{app_name}/#{app_name}-#{version}.tar.gz"

Status.clear_new(instance)
new_path = Storage.new_path(instance)

{"", 0} = System.cmd("tar", ["-x", "-f", download_path, "-C", new_path])

Upgrade.check(instance, download_path, Status.current_version(instance), version)
Upgrade.check(
instance,
app_lang,
app_name,
download_path,
Status.current_version(instance),
version
)
end

### ==========================================================================
Expand Down
14 changes: 11 additions & 3 deletions lib/deployex/release/s3.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,10 @@ defmodule Deployex.Release.S3 do
def download_and_unpack(instance, version) do
{:ok, download_path} = Briefly.create()

monitored_app = Storage.monitored_app_name()
app_name = Storage.monitored_app_name()
app_lang = Storage.monitored_app_lang()

s3_path = "dist/#{monitored_app}/#{monitored_app}-#{version}.tar.gz"
s3_path = "dist/#{app_name}/#{app_name}-#{version}.tar.gz"

{:ok, :done} =
bucket()
Expand All @@ -52,7 +53,14 @@ defmodule Deployex.Release.S3 do

{"", 0} = System.cmd("tar", ["-x", "-f", download_path, "-C", new_path])

Upgrade.check(instance, download_path, Status.current_version(instance), version)
Upgrade.check(
instance,
app_lang,
app_name,
download_path,
Status.current_version(instance),
version
)
after
Briefly.cleanup()
end
Expand Down
25 changes: 19 additions & 6 deletions lib/deployex/upgrade.ex
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,33 @@ defmodule Deployex.Upgrade do
This function check the release package type
"""
@impl true
@spec check(integer(), binary(), binary() | charlist() | nil, binary() | charlist()) ::
@spec check(
integer(),
String.t(),
String.t(),
binary(),
binary() | charlist() | nil,
binary() | charlist()
) ::
{:ok, :full_deployment | :hot_upgrade} | {:error, any()}
def check(instance, download_path, from_version, to_version) do
default().check(instance, download_path, from_version, to_version)
def check(instance, app_lang, app_name, download_path, from_version, to_version) do
default().check(instance, app_lang, app_name, download_path, from_version, to_version)
end

@doc """
This function triggers the hot code reloading process
"""
@impl true
@spec execute(integer(), binary() | charlist() | nil, binary() | charlist() | nil) ::
@spec execute(
integer(),
String.t(),
String.t(),
binary() | charlist() | nil,
binary() | charlist() | nil
) ::
:ok | {:error, any()}
def execute(instance, from_version, to_version) do
default().execute(instance, from_version, to_version)
def execute(instance, app_lang, app_name, from_version, to_version) do
default().execute(instance, app_lang, app_name, from_version, to_version)
end

### ==========================================================================
Expand Down
17 changes: 15 additions & 2 deletions lib/deployex/upgrade/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,21 @@ defmodule Deployex.Upgrade.Adapter do
"""

@callback connect(integer()) :: {:error, :not_connecting} | {:ok, atom()}
@callback check(integer(), binary(), binary() | charlist() | nil, binary() | charlist()) ::
@callback check(
integer(),
String.t(),
String.t(),
binary(),
binary() | charlist() | nil,
binary() | charlist()
) ::
{:ok, :full_deployment | :hot_upgrade} | {:error, any()}
@callback execute(integer(), binary() | charlist() | nil, binary() | charlist() | nil) ::
@callback execute(
integer(),
String.t(),
String.t(),
binary() | charlist() | nil,
binary() | charlist() | nil
) ::
:ok | {:error, any()}
end
Loading

0 comments on commit d3e34e4

Please sign in to comment.