Skip to content

Commit

Permalink
Add proxy deployment agent.
Browse files Browse the repository at this point in the history
Signed-off-by: edmondfrank <[email protected]>
  • Loading branch information
EdmondFrank committed Jan 13, 2025
1 parent a8e0ac3 commit 08ba14d
Show file tree
Hide file tree
Showing 8 changed files with 175 additions and 1 deletion.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@

# Ignore .fetch files in case you like to edit your project deps locally.
/.fetch
/zig_compiler

# If the VM crashes, it generates a dump, let's ignore it too.
erl_crash.dump
Expand Down
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ config :compass_admin, CompassAdmin.Agents.GatewayAgent,
input: ["\n"],
execute: "pyinfra prod-nodes.py prod-nginx-deploy.py"

config :compass_admin, CompassAdmin.Agents.ProxyAgent,
input: [""],
execute: "pyinfra prod-nodes.py prod-proxy-restart.py"

config :compass_admin, CompassAdminWeb.ConfigurationLive,
execute:
"cd {config_dir} && git config user.name {username} && git config user.email {useremail} && git add {config_path} && git commit -m '{commit_message}' && git push"
Expand Down
61 changes: 61 additions & 0 deletions lib/compass_admin/agents/proxy_agent.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
defmodule CompassAdmin.Agents.ProxyAgent do
use GenServer

alias CompassAdmin.User
alias CompassAdmin.Agents.DeployAgent

@agent_svn "proxy_v1"

def start_link(_) do
GenServer.start_link(__MODULE__, [], name: __MODULE__)
end

def init(_) do
{:ok, DeployAgent.init(@agent_svn)}
end

def execute(trigger_id) do
GenServer.cast(__MODULE__, {:deploy, trigger_id})
end

def get_state() do
GenServer.call(__MODULE__, :get_state)
end

def update_deploy_state(deploy_state, last_deploy_id, last_deploy_result) do
GenServer.cast(
__MODULE__,
{:update_deploy_state, deploy_state, last_deploy_id, last_deploy_result}
)
end

def append_log(log) do
GenServer.cast(__MODULE__, {:append, log})
end

def handle_call(:get_state, _from, state) do
{:reply, state, state}
end

def handle_cast({:update_deploy_state, deploy_state, last_deploy_id, last_deploy_result}, state) do
{:noreply,
DeployAgent.update_deploy_state(
state,
deploy_state,
last_deploy_id,
last_deploy_result
)}
end

def handle_cast({:append, log}, state) do
{:noreply, DeployAgent.append_log(state, log)}
end

def handle_cast({:deploy, trigger_id}, state) do
{:noreply, DeployAgent.deploy(__MODULE__, state, trigger_id, User.frontend_dev_role())}
end

def handle_info(_msg, state) do
{:noreply, state}
end
end
3 changes: 2 additions & 1 deletion lib/compass_admin/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ defmodule CompassAdmin.Application do
# Deployment agents
{CompassAdmin.Agents.GatewayAgent, []},
{CompassAdmin.Agents.BackendAgent, []},
{CompassAdmin.Agents.FrontendAgent, []}
{CompassAdmin.Agents.FrontendAgent, []},
{CompassAdmin.Agents.ProxyAgent, []},
] |> Enum.reject(&is_nil/1)

CompassAdmin.Plug.MetricsExporter.setup()
Expand Down
5 changes: 5 additions & 0 deletions lib/compass_admin_web/live/backoffice/layout.ex
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,11 @@ defmodule CompassAdminWeb.Live.Backoffice.Layout do
label: "Backend Applications",
link: "/admin/deployments/backend",
icon: ruby_icon()
},
%{
label: "Proxy Applications",
link: "/admin/deployments/proxy",
icon: proxy_icon()
}
]
},
Expand Down
88 changes: 88 additions & 0 deletions lib/compass_admin_web/live/proxy_deployment_live.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
defmodule CompassAdminWeb.ProxyDeploymentLive do
use CompassAdminWeb, :live_view

alias CompassAdmin.User
alias CompassAdmin.Agents.ProxyAgent

import CompassAdmin.Utils, only: [apm_call: 3]

@impl true
def mount(_params, %{"current_user" => current_user}, socket) do
if connected?(socket), do: Process.send_after(self(), :refresh, 5000)

state = apm_call(ProxyAgent, :get_state, [])
last_deploy_user = User.find(state.last_deploy_id, preloads: :login_binds)
last_trigger_user = User.find(state.last_trigger_id, preloads: :login_binds)
last_deployer = if last_deploy_user, do: List.first(last_deploy_user.login_binds)
last_trigger = if last_trigger_user, do: List.first(last_trigger_user.login_binds)
can_deploy = current_user.role_level >= User.frontend_dev_role()

{:ok,
assign(socket,
title: "Proxy recent deployment logs",
agent_state: state,
can_deploy: can_deploy,
current_user: current_user,
last_trigger: last_trigger,
last_deployer: last_deployer
)}
end

@impl true
def handle_params(_params, _uri, socket) do
case socket.assigns.live_action do
:index ->
{:noreply, assign(socket, action: :index)}
end
end

@impl true
def handle_info(:refresh, socket) do
Process.send_after(self(), :refresh, 5000)

state = apm_call(ProxyAgent, :get_state, [])
last_deploy_user = User.find(state.last_deploy_id, preloads: :login_binds)
last_trigger_user = User.find(state.last_trigger_id, preloads: :login_binds)
last_deployer = if last_deploy_user, do: List.first(last_deploy_user.login_binds)
last_trigger = if last_trigger_user, do: List.first(last_trigger_user.login_binds)

{:noreply,
assign(socket,
agent_state: state,
last_trigger: last_trigger,
last_deployer: last_deployer
)}
end

@impl true
def handle_event("deploy", _value, socket) do
apm_call(ProxyAgent, :execute, [socket.assigns.current_user.id])
Process.sleep(1000)
state = apm_call(ProxyAgent, :get_state, [])
last_deploy_user = User.find(state.last_deploy_id, preloads: :login_binds)
last_trigger_user = User.find(state.last_trigger_id, preloads: :login_binds)
last_deployer = if last_deploy_user, do: List.first(last_deploy_user.login_binds)
last_trigger = if last_trigger_user, do: List.first(last_trigger_user.login_binds)

{:noreply,
assign(socket,
agent_state: state,
last_trigger: last_trigger,
last_deployer: last_deployer
)}
end

@impl true
def render(assigns) do
~H"""
<.deployment_page
title={@title}
agent_state={@agent_state}
last_trigger={@last_trigger}
last_deployer={@last_deployer}
can_deploy={@can_deploy}
>
</.deployment_page>
"""
end
end
1 change: 1 addition & 0 deletions lib/compass_admin_web/router.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ defmodule CompassAdminWeb.Router do
live "/gitee/issues", GiteeIssuesLive, :index
live "/gitee/issues/bulk", GiteeIssuesLive, :bulk

live "/deployments/proxy", ProxyDeploymentLive, :index
live "/deployments/gateway", GatewayDeploymentLive, :index
live "/deployments/backend", BackendDeploymentLive, :index
live "/deployments/frontend", FrontendDeploymentLive, :index
Expand Down
13 changes: 13 additions & 0 deletions lib/compass_admin_web/views/icons_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,19 @@ defmodule CompassAdminWeb.View.IconsHelpers do
"""
end

def proxy_icon() do
"""
<svg viewBox="0 0 192 192" xmlns="http://www.w3.org/2000/svg" fill="#000000">
<g id="SVGRepo_bgCarrier" stroke-width="0"></g>
<g id="SVGRepo_tracerCarrier" stroke-linecap="round" stroke-linejoin="round"></g>
<g id="SVGRepo_iconCarrier">
<path d="M0 0h192v192H0z" style="fill:none"></path>
<path d="M78.65 58.81h58.33V34.14L170 67l-33.04 32.68V75.34H78.65V58.81zm33.93 65.43H54.42v24.86L22 116l32.49-32.33v23.97h58.09v16.6z" style="fill:none;stroke:#000000;stroke-linecap:round;stroke-linejoin:round;stroke-width:12px"></path>
</g>
</svg>
"""
end

def ruby_icon() do
"""
<svg xmlns="http://www.w3.org/2000/svg" aria-label="Ruby Gems" role="img" viewBox="0 0 512 512" fill="#000000">
Expand Down

0 comments on commit 08ba14d

Please sign in to comment.