Skip to content

Commit

Permalink
Added Gitee Indexea management page.
Browse files Browse the repository at this point in the history
Signed-off-by: EdmondFrank <[email protected]>
  • Loading branch information
EdmondFrank committed Jul 9, 2024
1 parent 809a1b3 commit c789b2e
Show file tree
Hide file tree
Showing 13 changed files with 493 additions and 30 deletions.
11 changes: 11 additions & 0 deletions config/config.exs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,17 @@ config :compass_admin, :hostnames,
app2: "app-front-2",
apm: "compass-apm"

config ExIndexea.Config,
app: "<app-code>",
access_token: "<access_token>"

config :ex_indexea,
repo_index: 1027,
issue_index: 1028,
creator_id: 1072,
owner_id: 1066,
open_search_id: 1074

config :compass_admin, :configurations, %{
nginx_config: "/path/to/nginx/nginx.conf",
nginx_server_config: "/path/to/nginx/server.conf",
Expand Down
10 changes: 5 additions & 5 deletions lib/compass_admin/agents/backend_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule CompassAdmin.Agents.BackendAgent do
use GenServer

alias CompassAdmin.User
alias CompassAdmin.Agents.BaseAgent
alias CompassAdmin.Agents.DeployAgent

@agent_svn "backend_v1"

Expand All @@ -11,7 +11,7 @@ defmodule CompassAdmin.Agents.BackendAgent do
end

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

def execute(trigger_id) do
Expand Down Expand Up @@ -39,7 +39,7 @@ defmodule CompassAdmin.Agents.BackendAgent do

def handle_cast({:update_deploy_state, deploy_state, last_deploy_id, last_deploy_result}, state) do
{:noreply,
BaseAgent.update_deploy_state(
DeployAgent.update_deploy_state(
state,
deploy_state,
last_deploy_id,
Expand All @@ -48,11 +48,11 @@ defmodule CompassAdmin.Agents.BackendAgent do
end

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

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

def handle_info(_msg, state) do
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
defmodule CompassAdmin.Agents.BaseAgent do
defmodule CompassAdmin.Agents.DeployAgent do
@max_lines 5000

import Ecto.Query
Expand Down
30 changes: 30 additions & 0 deletions lib/compass_admin/agents/exec_agent.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
defmodule CompassAdmin.Agents.ExecAgent do
use GenServer

@exec_timeout 60_000

def start_link(args) do
GenServer.start_link(__MODULE__, args, name: __MODULE__)
end

def init(args) do
{:ok, args}
end

def shell_exec(cmd) do
GenServer.call(__MODULE__, {:shell_exec, cmd}, @exec_timeout)
end

def handle_call({:shell_exec, cmd}, _from, state) do
try do
result =
Exile.stream!(["bash", "-l", "-c", cmd])
|> Enum.into("")

{:reply, {:ok, result}, state}
rescue
e in Exile.Stream.AbnormalExit -> {:reply, {:error, e.exit_status, e.message}, state}
_ -> {:reply, {:error, -1, "Unknown error"}, state}
end
end
end
10 changes: 5 additions & 5 deletions lib/compass_admin/agents/frontend_agent.ex
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ defmodule CompassAdmin.Agents.FrontendAgent do
use GenServer

alias CompassAdmin.User
alias CompassAdmin.Agents.BaseAgent
alias CompassAdmin.Agents.DeployAgent

@agent_svn "frontend_v1"

Expand All @@ -11,7 +11,7 @@ defmodule CompassAdmin.Agents.FrontendAgent do
end

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

def execute(trigger_id) do
Expand Down Expand Up @@ -39,7 +39,7 @@ defmodule CompassAdmin.Agents.FrontendAgent do

def handle_cast({:update_deploy_state, deploy_state, last_deploy_id, last_deploy_result}, state) do
{:noreply,
BaseAgent.update_deploy_state(
DeployAgent.update_deploy_state(
state,
deploy_state,
last_deploy_id,
Expand All @@ -48,11 +48,11 @@ defmodule CompassAdmin.Agents.FrontendAgent do
end

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

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

def handle_info(_msg, state) do
Expand Down
2 changes: 2 additions & 0 deletions lib/compass_admin/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,8 @@ defmodule CompassAdmin.Application do
# {CompassAdmin.Worker, arg}
CompassAdmin.Scheduler,
{Highlander, CompassAdmin.GlobalScheduler},
# Exec agent
{CompassAdmin.Agents.ExecAgent, []},
# Deployment agents
{CompassAdmin.Agents.BackendAgent, []},
{CompassAdmin.Agents.FrontendAgent, []}
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 @@ -54,4 +54,6 @@ defmodule CompassAdmin.User do
def backend_dev_role, do: 7

def super_role, do: 10

def admin_role, do: 65535
end
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 @@ -98,6 +98,11 @@ defmodule CompassAdminWeb.Live.Backoffice.Layout do
label: "User",
link: "/admin/users",
icon: user_icon()
},
%{
label: "Gitee",
link: "/admin/gitee",
icon: gitee_icon()
}
]
end
Expand Down
50 changes: 31 additions & 19 deletions lib/compass_admin_web/live/configuration_live.ex
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
defmodule CompassAdminWeb.ConfigurationLive do
use CompassAdminWeb, :live_view

alias CompassAdmin.Agents.ExecAgent
import CompassAdmin.Utils, only: [apm_call: 3]

@max_lines 5000
Expand Down Expand Up @@ -48,16 +49,22 @@ defmodule CompassAdminWeb.ConfigurationLive do

diff_command = "cd #{config_dir} && git diff #{ori_content_hash_obj} #{new_content_hash_obj}"

result = apm_call(Exile, :stream!, [["bash", "-l", "-c", diff_command]]) |> Enum.into("")

{:noreply,
socket
|> assign(:staged, true)
|> assign(:staged_content, content)
|> assign(:content, result)
|> append_log("staged new changes")
|> push_event("update-editor", %{content: result})
|> push_event("set-read-only", %{value: true})}
case apm_call(ExecAgent, :shell_exec, [diff_command]) do
{:ok, result} ->
{:noreply,
socket
|> assign(:staged, true)
|> assign(:staged_content, content)
|> assign(:content, result)
|> append_log("staged new changes")
|> push_event("update-editor", %{content: result})
|> push_event("set-read-only", %{value: true})}

{:error, exit_status, message} ->
{:noreply,
socket
|> put_flash(:error, "Exit with #{exit_status}, reason: #{message}.")}
end
end

@impl true
Expand Down Expand Up @@ -113,16 +120,21 @@ defmodule CompassAdminWeb.ConfigurationLive do
|> String.replace("{username}", current_user.name)
|> String.replace("{useremail}", current_user.email)
|> String.replace("{commit_message}", commit_message)
|> IO.inspect(label: "commit")

result = apm_call(Exile, :stream!, [["bash", "-l", "-c", final_execute]]) |> Enum.into("")

{:noreply,
socket
|> append_log("committed new changes")
|> append_log(result)
|> put_flash(:info, "Updated Successfully.")
|> redirect(to: current_path)}
case apm_call(ExecAgent, :shell_exec, [final_execute]) do
{:ok, result} ->
{:noreply,
socket
|> append_log("committed new changes")
|> append_log(result)
|> put_flash(:info, "Updated Successfully.")
|> redirect(to: current_path)}

{:error, exit_status, message} ->
{:noreply,
socket
|> put_flash(:error, "Exit with #{exit_status}, reason: #{message}.")}
end
end

@impl true
Expand Down
Loading

0 comments on commit c789b2e

Please sign in to comment.