feat: background remote agent updates survive SAC navigation (0.20.19)
This commit is contained in:
@@ -4,6 +4,16 @@
|
||||
<main :class="showShell ? 'app-main' : ''">
|
||||
<RouterView />
|
||||
</main>
|
||||
<HostActionLogModal
|
||||
v-if="showShell"
|
||||
:open="hostRemoteActionLog.open"
|
||||
:title="hostRemoteActionLog.title"
|
||||
:loading="hostRemoteActionLog.loading"
|
||||
:ok="hostRemoteActionLog.ok"
|
||||
:message="hostRemoteActionLog.message"
|
||||
:output="hostRemoteActionLog.output"
|
||||
@close="closeHostRemoteActionLog"
|
||||
/>
|
||||
</div>
|
||||
</template>
|
||||
|
||||
@@ -12,6 +22,11 @@ import { computed, onMounted } from "vue";
|
||||
import { useRoute } from "vue-router";
|
||||
import { getToken } from "./api";
|
||||
import AppSidebar from "./components/AppSidebar.vue";
|
||||
import HostActionLogModal from "./components/HostActionLogModal.vue";
|
||||
import {
|
||||
closeHostRemoteActionLog,
|
||||
hostRemoteActionLog,
|
||||
} from "./composables/useHostRemoteAction";
|
||||
import { refreshSessionRole } from "./router";
|
||||
|
||||
const route = useRoute();
|
||||
|
||||
+36
-6
@@ -517,8 +517,33 @@ export function testHostSsh(hostId: number): Promise<HostSshActionResult> {
|
||||
});
|
||||
}
|
||||
|
||||
export function updateHostAgentViaSsh(hostId: number): Promise<HostSshActionResult> {
|
||||
return apiFetch<HostSshActionResult>(`/api/v1/hosts/${hostId}/actions/agent-update`, {
|
||||
export interface HostRemoteActionStartResult {
|
||||
status: string;
|
||||
host_id: number;
|
||||
title: string;
|
||||
message: string;
|
||||
}
|
||||
|
||||
export interface HostRemoteActionJobStatus {
|
||||
active: boolean;
|
||||
host_id: number;
|
||||
hostname?: string | null;
|
||||
status?: string | null;
|
||||
title?: string;
|
||||
message?: string;
|
||||
stdout?: string | null;
|
||||
stderr?: string | null;
|
||||
output?: string | null;
|
||||
ok?: boolean | null;
|
||||
exit_code?: number | null;
|
||||
product_version?: string | null;
|
||||
agent_update_state?: string | null;
|
||||
started_at?: string | null;
|
||||
finished_at?: string | null;
|
||||
}
|
||||
|
||||
export function updateHostAgentViaSsh(hostId: number): Promise<HostRemoteActionStartResult> {
|
||||
return apiFetch<HostRemoteActionStartResult>(`/api/v1/hosts/${hostId}/actions/agent-update`, {
|
||||
method: "POST",
|
||||
});
|
||||
}
|
||||
@@ -537,10 +562,15 @@ export function requestHostAgentUpdate(hostId: number): Promise<HostAgentUpdateR
|
||||
);
|
||||
}
|
||||
|
||||
export function runHostAgentUpdateFallback(hostId: number): Promise<HostSshActionResult> {
|
||||
return apiFetch<HostSshActionResult>(`/api/v1/hosts/${hostId}/actions/agent-update-fallback`, {
|
||||
method: "POST",
|
||||
});
|
||||
export function runHostAgentUpdateFallback(hostId: number): Promise<HostRemoteActionStartResult> {
|
||||
return apiFetch<HostRemoteActionStartResult>(
|
||||
`/api/v1/hosts/${hostId}/actions/agent-update-fallback`,
|
||||
{ method: "POST" },
|
||||
);
|
||||
}
|
||||
|
||||
export function fetchHostRemoteJob(hostId: number): Promise<HostRemoteActionJobStatus> {
|
||||
return apiFetch<HostRemoteActionJobStatus>(`/api/v1/hosts/${hostId}/actions/remote-job`);
|
||||
}
|
||||
|
||||
export interface HostAgentConfigResponse {
|
||||
|
||||
@@ -0,0 +1,120 @@
|
||||
import { reactive } from "vue";
|
||||
import {
|
||||
ApiError,
|
||||
fetchHost,
|
||||
fetchHostRemoteJob,
|
||||
runHostAgentUpdateFallback,
|
||||
updateHostAgentViaSsh,
|
||||
type HostDetail,
|
||||
type HostRemoteActionJobStatus,
|
||||
} from "../api";
|
||||
import { emitHostPatch } from "../utils/hostPatchBus";
|
||||
|
||||
export type HostRemoteActionKind = "fallback" | "ssh-update";
|
||||
|
||||
export const hostRemoteActionLog = reactive({
|
||||
open: false,
|
||||
title: "",
|
||||
loading: false,
|
||||
ok: null as boolean | null,
|
||||
message: "",
|
||||
output: "",
|
||||
hostId: 0,
|
||||
});
|
||||
|
||||
const POLL_MS = 1500;
|
||||
|
||||
function sleep(ms: number): Promise<void> {
|
||||
return new Promise((resolve) => setTimeout(resolve, ms));
|
||||
}
|
||||
|
||||
function applyJobToModal(job: HostRemoteActionJobStatus) {
|
||||
hostRemoteActionLog.title = job.title || hostRemoteActionLog.title;
|
||||
hostRemoteActionLog.message = job.message || hostRemoteActionLog.message;
|
||||
hostRemoteActionLog.output = job.output || job.stdout || "";
|
||||
}
|
||||
|
||||
function finishModal(job: HostRemoteActionJobStatus) {
|
||||
hostRemoteActionLog.loading = false;
|
||||
hostRemoteActionLog.ok = job.ok ?? job.status === "success";
|
||||
applyJobToModal(job);
|
||||
}
|
||||
|
||||
async function pollRemoteJob(hostId: number): Promise<HostRemoteActionJobStatus> {
|
||||
for (;;) {
|
||||
const job = await fetchHostRemoteJob(hostId);
|
||||
applyJobToModal(job);
|
||||
if (!job.active && job.status !== "running") {
|
||||
return job;
|
||||
}
|
||||
await sleep(POLL_MS);
|
||||
}
|
||||
}
|
||||
|
||||
async function patchHostFromJob(hostId: number, job: HostRemoteActionJobStatus) {
|
||||
try {
|
||||
const detail: HostDetail = await fetchHost(hostId);
|
||||
if (job.product_version) {
|
||||
detail.product_version = job.product_version;
|
||||
}
|
||||
if (job.agent_update_state) {
|
||||
detail.agent_update_state = job.agent_update_state;
|
||||
}
|
||||
if (job.ok === false && job.message) {
|
||||
detail.agent_update_last_error = job.message;
|
||||
}
|
||||
emitHostPatch(detail);
|
||||
} catch {
|
||||
/* list refresh on next open is enough */
|
||||
}
|
||||
}
|
||||
|
||||
async function startRemoteAction(hostId: number, kind: HostRemoteActionKind) {
|
||||
if (kind === "fallback") {
|
||||
await runHostAgentUpdateFallback(hostId);
|
||||
return;
|
||||
}
|
||||
await updateHostAgentViaSsh(hostId);
|
||||
}
|
||||
|
||||
export async function runHostRemoteAction(
|
||||
hostId: number,
|
||||
title: string,
|
||||
kind: HostRemoteActionKind,
|
||||
): Promise<HostRemoteActionJobStatus | null> {
|
||||
hostRemoteActionLog.open = true;
|
||||
hostRemoteActionLog.loading = true;
|
||||
hostRemoteActionLog.ok = null;
|
||||
hostRemoteActionLog.title = title;
|
||||
hostRemoteActionLog.message = "Запуск на сервере SAC…";
|
||||
hostRemoteActionLog.output = "";
|
||||
hostRemoteActionLog.hostId = hostId;
|
||||
|
||||
try {
|
||||
try {
|
||||
await startRemoteAction(hostId, kind);
|
||||
} catch (e) {
|
||||
if (e instanceof ApiError && e.status === 409) {
|
||||
/* already running — poll existing job */
|
||||
} else {
|
||||
throw e;
|
||||
}
|
||||
}
|
||||
hostRemoteActionLog.message =
|
||||
"Выполняется на удалённом хосте… Можно перейти в другой раздел SAC.";
|
||||
const job = await pollRemoteJob(hostId);
|
||||
finishModal(job);
|
||||
await patchHostFromJob(hostId, job);
|
||||
return job;
|
||||
} catch (e) {
|
||||
hostRemoteActionLog.loading = false;
|
||||
hostRemoteActionLog.ok = false;
|
||||
hostRemoteActionLog.message = e instanceof Error ? e.message : "Ошибка выполнения";
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export function closeHostRemoteActionLog() {
|
||||
if (hostRemoteActionLog.loading) return;
|
||||
hostRemoteActionLog.open = false;
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
/** Fallback до загрузки /health; при релизе держите в sync с backend/app/version.py */
|
||||
export const APP_NAME = "Security Alert Center";
|
||||
export const APP_VERSION = "0.20.18";
|
||||
export const APP_VERSION = "0.20.19";
|
||||
export const APP_VERSION_LABEL = `${APP_NAME} v.${APP_VERSION}`;
|
||||
|
||||
@@ -91,7 +91,7 @@
|
||||
{{ runningAgentFallback ? "Обновление…" : "Обновить через WinRM" }}
|
||||
</button>
|
||||
</div>
|
||||
<p v-if="agentControlMessage && !actionLog.open" :class="agentControlOk ? 'success' : 'error'">{{ agentControlMessage }}</p>
|
||||
<p v-if="agentControlMessage && !hostRemoteActionLog.open" :class="agentControlOk ? 'success' : 'error'">{{ agentControlMessage }}</p>
|
||||
</div>
|
||||
<div v-if="isLinuxHost" class="host-actions">
|
||||
<div class="host-actions-row">
|
||||
@@ -135,7 +135,7 @@
|
||||
{{ runningAgentFallback ? "Fallback…" : "Fallback SSH" }}
|
||||
</button>
|
||||
</div>
|
||||
<p v-if="agentControlMessage && !actionLog.open" :class="agentControlOk ? 'success' : 'error'">{{ agentControlMessage }}</p>
|
||||
<p v-if="agentControlMessage && !hostRemoteActionLog.open" :class="agentControlOk ? 'success' : 'error'">{{ agentControlMessage }}</p>
|
||||
<div v-if="showSshTestFeedback && (sshTestMessage || sshTestOutput)" class="host-action-feedback">
|
||||
<p :class="sshTestOk ? 'success' : 'error'">{{ sshTestMessage }}</p>
|
||||
<pre v-if="sshTestOutput" class="host-ssh-output">{{ sshTestOutput }}</pre>
|
||||
@@ -272,38 +272,28 @@
|
||||
</div>
|
||||
</template>
|
||||
</template>
|
||||
|
||||
<HostActionLogModal
|
||||
:open="actionLog.open"
|
||||
:title="actionLog.title"
|
||||
:loading="actionLog.loading"
|
||||
:ok="actionLog.ok"
|
||||
:message="actionLog.message"
|
||||
:output="actionLog.output"
|
||||
@close="closeActionLog"
|
||||
/>
|
||||
</template>
|
||||
|
||||
<script setup lang="ts">
|
||||
import { computed, onMounted, onUnmounted, reactive, ref, watch } from "vue";
|
||||
import { useRoute } from "vue-router";
|
||||
import { useSacLiveEventStream } from "../composables/useSacLiveEventStream";
|
||||
import {
|
||||
hostRemoteActionLog,
|
||||
runHostRemoteAction,
|
||||
} from "../composables/useHostRemoteAction";
|
||||
import {
|
||||
apiFetch,
|
||||
fetchHost,
|
||||
fetchRecentEvents,
|
||||
testHostWinRm,
|
||||
testHostSsh,
|
||||
updateHostAgentViaSsh,
|
||||
requestHostAgentUpdate,
|
||||
runHostAgentUpdateFallback,
|
||||
patchHostAgentConfig,
|
||||
type EventListResponse,
|
||||
type HostDetail,
|
||||
type HostSshActionResult,
|
||||
} from "../api";
|
||||
import { emitHostPatch } from "../utils/hostPatchBus";
|
||||
import HostActionLogModal from "../components/HostActionLogModal.vue";
|
||||
|
||||
const HOST_ACTION_FEEDBACK_MS = 30_000;
|
||||
|
||||
@@ -328,14 +318,6 @@ const showSshTestFeedback = ref(false);
|
||||
const sshTestMessage = ref("");
|
||||
const sshTestOk = ref(false);
|
||||
const sshTestOutput = ref("");
|
||||
const actionLog = reactive({
|
||||
open: false,
|
||||
title: "",
|
||||
loading: false,
|
||||
ok: null as boolean | null,
|
||||
message: "",
|
||||
output: "",
|
||||
});
|
||||
const requestingAgentUpdate = ref(false);
|
||||
const runningAgentFallback = ref(false);
|
||||
const agentControlMessage = ref("");
|
||||
@@ -609,59 +591,6 @@ function formatSshOutput(result: { stdout: string | null; stderr: string | null;
|
||||
return parts.join("\n\n");
|
||||
}
|
||||
|
||||
function openActionLog(title: string) {
|
||||
actionLog.open = true;
|
||||
actionLog.title = title;
|
||||
actionLog.loading = true;
|
||||
actionLog.ok = null;
|
||||
actionLog.message = "Подключение к хосту и выполнение команды…";
|
||||
actionLog.output = "";
|
||||
}
|
||||
|
||||
function finishActionLog(result: { ok: boolean; message: string; output?: string }) {
|
||||
actionLog.loading = false;
|
||||
actionLog.ok = result.ok;
|
||||
actionLog.message = result.message;
|
||||
actionLog.output = result.output ?? "";
|
||||
}
|
||||
|
||||
function closeActionLog() {
|
||||
if (actionLog.loading) return;
|
||||
actionLog.open = false;
|
||||
}
|
||||
|
||||
async function runRemoteHostAction(
|
||||
title: string,
|
||||
request: () => Promise<HostSshActionResult>,
|
||||
options: { refreshHost?: boolean; applyProductVersion?: boolean } = {},
|
||||
) {
|
||||
openActionLog(title);
|
||||
try {
|
||||
const result = await request();
|
||||
finishActionLog({
|
||||
ok: result.ok,
|
||||
message: result.message,
|
||||
output: formatSshOutput(result),
|
||||
});
|
||||
if (result.ok && result.product_version && host.value && options.applyProductVersion !== false) {
|
||||
host.value = { ...host.value, product_version: result.product_version };
|
||||
emitHostPatch(host.value);
|
||||
} else if (host.value && result.ssh_admin_ok != null) {
|
||||
host.value = { ...host.value, ssh_admin_ok: result.ssh_admin_ok };
|
||||
}
|
||||
if (options.refreshHost) {
|
||||
await loadHost({ preserveActionLog: true });
|
||||
}
|
||||
return result;
|
||||
} catch (e) {
|
||||
finishActionLog({
|
||||
ok: false,
|
||||
message: e instanceof Error ? e.message : "Ошибка выполнения",
|
||||
});
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
function applyHostDetail(detail: HostDetail) {
|
||||
host.value = detail;
|
||||
syncConfigFormFromHost(detail);
|
||||
@@ -691,14 +620,13 @@ async function runAgentFallback() {
|
||||
agentControlMessage.value = "";
|
||||
const title = isWindowsHost.value ? "Обновление через WinRM" : "Fallback SSH (ssh-monitor)";
|
||||
try {
|
||||
const result = await runRemoteHostAction(
|
||||
title,
|
||||
() => runHostAgentUpdateFallback(hostId.value),
|
||||
{ refreshHost: true },
|
||||
);
|
||||
if (result) {
|
||||
agentControlOk.value = result.ok;
|
||||
agentControlMessage.value = result.message;
|
||||
const job = await runHostRemoteAction(hostId.value, title, "fallback");
|
||||
if (job) {
|
||||
agentControlOk.value = job.ok ?? false;
|
||||
agentControlMessage.value = job.message || "";
|
||||
if (job.ok) {
|
||||
await loadHost();
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
runningAgentFallback.value = false;
|
||||
@@ -732,7 +660,7 @@ async function saveAgentConfig() {
|
||||
}
|
||||
}
|
||||
|
||||
async function loadHost(options: { preserveActionLog?: boolean } = {}) {
|
||||
async function loadHost() {
|
||||
loading.value = true;
|
||||
error.value = "";
|
||||
winRmTestMessage.value = "";
|
||||
@@ -743,9 +671,6 @@ async function loadHost(options: { preserveActionLog?: boolean } = {}) {
|
||||
showSshTestFeedback.value = false;
|
||||
sshTestMessage.value = "";
|
||||
sshTestOutput.value = "";
|
||||
if (!options.preserveActionLog) {
|
||||
closeActionLog();
|
||||
}
|
||||
try {
|
||||
const detail = await fetchHost(hostId.value);
|
||||
applyHostDetail(detail);
|
||||
@@ -762,18 +687,15 @@ async function loadHost(options: { preserveActionLog?: boolean } = {}) {
|
||||
async function runAgentUpdate() {
|
||||
updatingAgent.value = true;
|
||||
try {
|
||||
const result = await runRemoteHostAction(
|
||||
const job = await runHostRemoteAction(
|
||||
hostId.value,
|
||||
"Обновление ssh-monitor (SSH)",
|
||||
() => updateHostAgentViaSsh(hostId.value),
|
||||
{ refreshHost: false },
|
||||
"ssh-update",
|
||||
);
|
||||
if (result?.ok) {
|
||||
if (job?.ok) {
|
||||
const detail = await fetchHost(hostId.value);
|
||||
if (result.product_version) {
|
||||
detail.product_version = result.product_version;
|
||||
}
|
||||
if (result.ssh_admin_ok != null) {
|
||||
detail.ssh_admin_ok = result.ssh_admin_ok;
|
||||
if (job.product_version) {
|
||||
detail.product_version = job.product_version;
|
||||
}
|
||||
applyHostDetail(detail);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user