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

Improving test coverage #79

Merged
merged 2 commits into from
Oct 28, 2024
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: 1 addition & 1 deletion .github/workflows/pr-ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ jobs:
deployex-

- name: Run tests
run: mix coveralls.github
run: mix coveralls.html

analysis:
name: Static Analysis
Expand Down
6 changes: 4 additions & 2 deletions lib/deployex/deployment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,9 @@ defmodule Deployex.Deployment do
new_deploy_ref = Common.random_small_alphanum()

if current_app_version != nil do
{:ok, _} = Monitor.start_service(state.current, new_deploy_ref)
{:ok, _} =
Monitor.start_service(Storage.monitored_app_lang(), state.current, new_deploy_ref)

set_timeout_to_rollback(state, new_deploy_ref)
else
state
Expand Down Expand Up @@ -277,7 +279,7 @@ defmodule Deployex.Deployment do
deploy_ref: new_deploy_ref
)

{:ok, _} = Monitor.start_service(instance, new_deploy_ref)
{:ok, _} = Monitor.start_service(Storage.monitored_app_lang(), instance, new_deploy_ref)
end)

set_timeout_to_rollback(state, new_deploy_ref)
Expand Down
8 changes: 5 additions & 3 deletions lib/deployex/monitor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ defmodule Deployex.Monitor do
@type t :: %__MODULE__{
current_pid: pid() | nil,
instance: integer() | nil,
language: String.t(),
status: :idle | :running | :starting,
crash_restart_count: integer(),
force_restart_count: integer(),
Expand All @@ -19,6 +20,7 @@ defmodule Deployex.Monitor do

defstruct current_pid: nil,
instance: nil,
language: nil,
status: :idle,
crash_restart_count: 0,
force_restart_count: 0,
Expand All @@ -35,10 +37,10 @@ defmodule Deployex.Monitor do
Starts monitor service for an specific instance
"""
@impl true
@spec start_service(integer(), String.t(), list()) ::
@spec start_service(String.t(), integer(), String.t(), list()) ::
{:ok, pid} | {:error, pid(), :already_started}
def start_service(instance, deploy_ref, options \\ []) do
default().start_service(instance, deploy_ref, options)
def start_service(language, instance, deploy_ref, options \\ []) do
default().start_service(language, instance, deploy_ref, options)
end

@doc """
Expand Down
2 changes: 1 addition & 1 deletion lib/deployex/monitor/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ defmodule Deployex.Monitor.Adapter do
Behaviour that defines the monitor adapter callback
"""

@callback start_service(integer(), String.t(), list()) ::
@callback start_service(String.t(), integer(), String.t(), list()) ::
{:ok, pid} | {:error, pid(), :already_started}
@callback stop_service(integer()) :: :ok
@callback restart(integer()) :: :ok | {:error, :application_is_not_running}
Expand Down
24 changes: 13 additions & 11 deletions lib/deployex/monitor/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ defmodule Deployex.Monitor.Application do

instance = Keyword.fetch!(args, :instance)
deploy_ref = Keyword.fetch!(args, :deploy_ref)
language = Keyword.fetch!(args, :language)
options = Keyword.fetch!(args, :options)

timeout_app_ready =
Expand All @@ -50,7 +51,7 @@ defmodule Deployex.Monitor.Application do
|> String.to_atom()
|> :ets.new([:set, :protected, :named_table])

Logger.info("Initialising monitor server for instance: #{instance}")
Logger.info("Initialising monitor server for instance: #{instance} language: #{language}")

trigger_run_service(deploy_ref)

Expand All @@ -59,7 +60,8 @@ defmodule Deployex.Monitor.Application do
instance: instance,
timeout_app_ready: timeout_app_ready,
retry_delay_pre_commands: retry_delay_pre_commands,
deploy_ref: deploy_ref
deploy_ref: deploy_ref,
language: language
})}
end

Expand Down Expand Up @@ -213,7 +215,8 @@ defmodule Deployex.Monitor.Application do
end

@impl true
defdelegate start_service(instance, deploy_ref, options \\ []), to: Deployex.Monitor.Supervisor
defdelegate start_service(language, instance, deploy_ref, options \\ []),
to: Deployex.Monitor.Supervisor

@impl true
defdelegate stop_service(instance), to: Deployex.Monitor.Supervisor
Expand Down Expand Up @@ -246,12 +249,12 @@ defmodule Deployex.Monitor.Application do
instance: instance,
deploy_ref: deploy_ref,
timeout_app_ready: timeout_app_ready,
retry_delay_pre_commands: retry_delay_pre_commands
retry_delay_pre_commands: retry_delay_pre_commands,
language: language
} = state,
version_map
) do
monitore_app_lang = Storage.monitored_app_lang()
app_exec = Storage.bin_path(instance, monitore_app_lang, :current)
app_exec = Storage.bin_path(instance, language, :current)
version = version_map.version

with true <- File.exists?(app_exec),
Expand All @@ -261,7 +264,7 @@ defmodule Deployex.Monitor.Application do

{:ok, pid, os_pid} =
OpSys.run_link(
run_app_bin(monitore_app_lang, instance, app_exec, "start"),
run_app_bin(language, instance, app_exec, "start"),
[
{:stdout, Storage.stdout_path(instance) |> to_charlist, [:append, {:mode, 0o600}]},
{:stderr, Storage.stderr_path(instance) |> to_charlist, [:append, {:mode, 0o600}]}
Expand Down Expand Up @@ -376,12 +379,11 @@ defmodule Deployex.Monitor.Application do
defp execute_pre_commands(_state, pre_commands, _bin_service) when pre_commands == [], do: :ok

defp execute_pre_commands(
%{instance: instance, status: status} = state,
%{instance: instance, status: status, language: language} = state,
pre_commands,
bin_service
) do
monitore_app_lang = Storage.monitored_app_lang()
migration_exec = Storage.bin_path(instance, monitore_app_lang, bin_service)
migration_exec = Storage.bin_path(instance, language, bin_service)

update_non_blocking_state(%{state | status: :pre_commands})

Expand All @@ -391,7 +393,7 @@ defmodule Deployex.Monitor.Application do
Enum.reduce_while(pre_commands, :ok, fn pre_command, acc ->
Logger.info(" # Executing: #{pre_command}")

OpSys.run(run_app_bin(monitore_app_lang, instance, migration_exec, pre_command), [
OpSys.run(run_app_bin(language, instance, migration_exec, pre_command), [
:sync,
{:stdout, Storage.stdout_path(instance) |> to_charlist, [:append, {:mode, 0o600}]},
{:stderr, Storage.stderr_path(instance) |> to_charlist, [:append, {:mode, 0o600}]}
Expand Down
5 changes: 3 additions & 2 deletions lib/deployex/monitor/supervisor.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,16 @@ defmodule Deployex.Monitor.Supervisor do
### ==========================================================================
### Public APIs
### ==========================================================================
@spec start_service(integer(), String.t(), [Keyword.t()]) ::
@spec start_service(String.t(), integer(), String.t(), [Keyword.t()]) ::
{:ok, pid} | {:error, pid(), :already_started}
def start_service(instance, deploy_ref, options) do
def start_service(language, instance, deploy_ref, options) do
spec = %{
id: Deployex.Monitor.Application,
start:
{Deployex.Monitor.Application, :start_link,
[
[
language: language,
instance: instance,
deploy_ref: deploy_ref,
options: options
Expand Down
14 changes: 14 additions & 0 deletions lib/deployex/status.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,20 @@ defmodule Deployex.Status do
@spec monitoring() :: {:ok, list()} | {:error, :rescued}
def monitoring, do: default().monitoring()

@doc """
Retrieve the current monitored app name
"""
@impl true
@spec monitored_app_name() :: String.t()
def monitored_app_name, do: default().monitored_app_name()

@doc """
Retrieve the current monitored app language
"""
@impl true
@spec monitored_app_lang() :: String.t()
def monitored_app_lang, do: default().monitored_app_lang()

@doc """
Retrieve the current version set for the monitored application
"""
Expand Down
2 changes: 2 additions & 0 deletions lib/deployex/status/adapter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ defmodule Deployex.Status.Adapter do
alias Deployex.Status

@callback monitoring :: {:ok, list()} | {:error, :rescued}
@callback monitored_app_name() :: String.t()
@callback monitored_app_lang() :: String.t()
@callback current_version(integer()) :: String.t() | nil
@callback current_version_map(integer()) :: Status.Version.t()
@callback subscribe() :: :ok
Expand Down
6 changes: 6 additions & 0 deletions lib/deployex/status/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,12 @@ defmodule Deployex.Status.Application do
Common.call_gen_server(name, :monitoring)
end

@impl true
def monitored_app_name, do: Storage.monitored_app_name()

@impl true
def monitored_app_lang, do: Storage.monitored_app_lang()

@impl true
def subscribe, do: Phoenix.PubSub.subscribe(Deployex.PubSub, @apps_data_updated_topic)

Expand Down
7 changes: 6 additions & 1 deletion lib/deployex_web/live/applications/index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ defmodule DeployexWeb.ApplicationsLive do
module={Terminal}
id={@selected_instance}
title={@page_title}
action={@live_action}
monitored_app_name={@monitored_app_name}
monitored_app_lang={@monitored_app_lang}
terminal_process={@terminal_process}
terminal_message={@terminal_message}
cookie={Common.cookie()}
Expand Down Expand Up @@ -157,6 +158,8 @@ defmodule DeployexWeb.ApplicationsLive do
socket
|> assign(:node, Node.self())
|> assign(:monitoring_apps_data, monitoring)
|> assign(:monitored_app_name, Status.monitored_app_name())
|> assign(:monitored_app_lang, Status.monitored_app_lang())
|> assign(:selected_instance, nil)
|> assign(:terminal_message, nil)
|> assign(:terminal_process, nil)
Expand All @@ -175,6 +178,8 @@ defmodule DeployexWeb.ApplicationsLive do
socket
|> assign(:node, Node.self())
|> assign(:monitoring_apps_data, [])
|> assign(:monitored_app_name, nil)
|> assign(:monitored_app_lang, nil)
|> assign(:selected_instance, nil)
|> assign(:terminal_message, nil)
|> assign(:terminal_process, nil)
Expand Down
22 changes: 14 additions & 8 deletions lib/deployex_web/live/applications/terminal.ex
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,12 @@ defmodule DeployexWeb.ApplicationsLive.Terminal do
require Logger

@impl true
@spec render(any()) :: Phoenix.LiveView.Rendered.t()
def render(assigns) do
~H"""
<div>
<.header>
<%= "Terminal for #{@monitored_app} [#{@id}]" %>
<%= "Terminal for #{@monitored_app_name} [#{@id}]" %>
<:subtitle>Bin: <%= @bin_path %></:subtitle>
</.header>

Expand All @@ -37,11 +38,8 @@ defmodule DeployexWeb.ApplicationsLive.Terminal do

@impl true
def mount(socket) do
monitored_app = Deployex.Storage.monitored_app_name()

socket =
socket
|> assign(:monitored_app, monitored_app)
|> assign(:bin_path, "")

{:ok, socket}
Expand Down Expand Up @@ -86,18 +84,25 @@ defmodule DeployexWeb.ApplicationsLive.Terminal do
{:noreply, socket}
end

defp maybe_connect(%{assigns: %{id: instance, cookie: cookie}} = socket)
defp maybe_connect(
%{
assigns: %{
id: instance,
cookie: cookie,
monitored_app_name: app_name,
monitored_app_lang: app_lang
}
} =
socket
)
when cookie != :nocookie do
app_lang = Deployex.Storage.monitored_app_lang()

bin_path =
instance
|> String.to_integer()
|> Deployex.Storage.bin_path(app_lang, :current)

path = Common.remove_deployex_from_path()
suffix = if instance == "0", do: "", else: "-#{instance}"
app_name = Deployex.Storage.monitored_app_name()
{:ok, hostname} = :inet.gethostname()

ssl_options =
Expand Down Expand Up @@ -130,6 +135,7 @@ defmodule DeployexWeb.ApplicationsLive.Terminal do
#{bin_path} remote_console
"""

# Deafult to Elixir language
true ->
"""
unset $(env | grep '^RELEASE_' | awk -F'=' '{print $1}')
Expand Down
Loading