Skip to content

Commit

Permalink
Support gateway deployment online.
Browse files Browse the repository at this point in the history
Signed-off-by: Edmondfrank <[email protected]>
  • Loading branch information
EdmondFrank committed Dec 5, 2024
1 parent fffeda5 commit 81445b0
Show file tree
Hide file tree
Showing 8 changed files with 176 additions and 0 deletions.
4 changes: 4 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ config :compass_admin, CompassAdmin.Agents.BackendAgent,
input: [""],
execute: "pyinfra prod-nodes.py prod-backend-deploy.py"

config :compass_admin, CompassAdmin.Agents.GatewayAgent,
input: ["\n"],
execute: "pyinfra prod-nodes.py prod-nginx-deploy.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/gateway_agent.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
defmodule CompassAdmin.Agents.GatewayAgent do
use GenServer

alias CompassAdmin.User
alias CompassAdmin.Agents.DeployAgent

@agent_svn "gateway_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.gateway_dev_role())}
end

def handle_info(_msg, state) do
{:noreply, state}
end
end
1 change: 1 addition & 0 deletions lib/compass_admin/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ defmodule CompassAdmin.Application do
# Exec agent
{CompassAdmin.Agents.ExecAgent, []},
# Deployment agents
{CompassAdmin.Agents.GatewayAgent, []},
{CompassAdmin.Agents.BackendAgent, []},
{CompassAdmin.Agents.FrontendAgent, []}
] |> Enum.reject(&is_nil/1)
Expand Down
2 changes: 2 additions & 0 deletions lib/compass_admin/user.ex
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ defmodule CompassAdmin.User do

def backend_dev_role, do: 7

def gateway_dev_role, do: 7

def super_role, do: 10

def admin_role, do: 65535
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 @@ -77,6 +77,11 @@ defmodule CompassAdminWeb.Live.Backoffice.Layout do
icon: rocket_icon(),
expanded: true,
links: [
%{
label: "Gateway Applications",
link: "/admin/deployments/gateway",
icon: nginx_icon()
},
%{
label: "Frontend Applications",
link: "/admin/deployments/frontend",
Expand Down
88 changes: 88 additions & 0 deletions lib/compass_admin_web/live/gateway_deployment_live.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
defmodule CompassAdminWeb.GatewayDeploymentLive do
use CompassAdminWeb, :live_view

alias CompassAdmin.User
alias CompassAdmin.Agents.GatewayAgent

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(GatewayAgent, :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.gateway_dev_role()

{:ok,
assign(socket,
title: "Gateway 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(GatewayAgent, :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(GatewayAgent, :execute, [socket.assigns.current_user.id])
Process.sleep(1000)
state = apm_call(GatewayAgent, :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/gateway", GatewayDeploymentLive, :index
live "/deployments/backend", BackendDeploymentLive, :index
live "/deployments/frontend", FrontendDeploymentLive, :index

Expand Down
14 changes: 14 additions & 0 deletions lib/compass_admin_web/views/icons_helpers.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,20 @@ defmodule CompassAdminWeb.View.IconsHelpers do
"""
end

def nginx_icon() do
"""
<svg viewBox="0 0 32 32" 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">
<title>file_type_nginx</title>
<path d="M15.948,2h.065a10.418,10.418,0,0,1,.972.528Q22.414,5.65,27.843,8.774a.792.792,0,0,1,.414.788c-.008,4.389,0,8.777-.005,13.164a.813.813,0,0,1-.356.507q-5.773,3.324-11.547,6.644a.587.587,0,0,1-.657.037Q9.912,26.6,4.143,23.274a.7.7,0,0,1-.4-.666q0-6.582,0-13.163a.693.693,0,0,1,.387-.67Q9.552,5.657,14.974,2.535c.322-.184.638-.379.974-.535" style="fill:#019639"></path>
<path d="M8.767,10.538q0,5.429,0,10.859a1.509,1.509,0,0,0,.427,1.087,1.647,1.647,0,0,0,2.06.206,1.564,1.564,0,0,0,.685-1.293c0-2.62-.005-5.24,0-7.86q3.583,4.29,7.181,8.568a2.833,2.833,0,0,0,2.6.782,1.561,1.561,0,0,0,1.251-1.371q.008-5.541,0-11.081a1.582,1.582,0,0,0-3.152,0c0,2.662-.016,5.321,0,7.982-2.346-2.766-4.663-5.556-7-8.332A2.817,2.817,0,0,0,10.17,9.033,1.579,1.579,0,0,0,8.767,10.538Z" style="fill:#fff"></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 81445b0

Please sign in to comment.