Files
OpenVPN-Monitoring-Simple/APP_UI/src/views/ServerManagement.vue
T
ayurishchevandClaude Sonnet 5.5 56dae90ff5 Guard server setup behind an initialized PKI
- Profiler: server_files_missing/require_* helpers; 409 for
  /server/configure, start/restart and POST /profiles when the PKI is
  not initialized or server files (honouring fqdn_server) are missing;
  stop stays allowed; DELETE /system/pki returns 409 while OpenVPN runs;
  GET /system/init/status adds pki_ready, server_ready, missing.
- UI: shared usePkiStatus; VPN menu item locked until the PKI is ready,
  route guard for /config/vpn, Start/Restart and New Client disabled
  with hints, lock notice on the VPN page.
- Test: one pytest (test_pki_guard.py).
- Docs: plan and summary in DOCS/Changes, API reference, README, Index.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-04 21:52:40 +03:00

167 lines
6.0 KiB
Vue

<template>
<div class="config-wrapper">
<div class="config-card">
<div class="config-header">
<h1>Server Process Management</h1>
</div>
<!-- Stats Grid -->
<!-- User requested specific order: CPU (full), Status, PID, Memory, Uptime -->
<div class="stats-grid mb-4">
<!-- CPU Usage (Full Width) -->
<div class="stat-item full-width cpu-progress">
<label class="stat-label d-flex justify-content-between">
<span>CPU Usage</span>
<span>{{ stats.cpu_percent }}%</span>
</label>
<div class="progress">
<div class="progress-bar progress-bar-brand" role="progressbar"
:style="{ width: Math.min(stats.cpu_percent, 100) + '%' }"
:aria-valuenow="stats.cpu_percent" aria-valuemin="0" aria-valuemax="100"></div>
</div>
</div>
<!-- Status -->
<div class="stat-item">
<label class="stat-label">Status</label>
<div class="d-flex align-items-center">
<span :class="['status-badge', 'status-' + (stats.status === 'running' ? 'active' : 'revoked')]">
{{ stats.status.toUpperCase() }}
</span>
</div>
</div>
<!-- PID -->
<div class="stat-item">
<label class="stat-label">Process ID</label>
<div class="stat-value">{{ stats.pid || 'N/A' }}</div>
</div>
<!-- Memory -->
<div class="stat-item">
<label class="stat-label">Memory (RSS)</label>
<div class="stat-value">{{ stats.memory_mb }} MB</div>
</div>
<!-- Uptime -->
<div class="stat-item">
<label class="stat-label">Uptime</label>
<div class="stat-value monospace">{{ stats.uptime || 'N/A' }}</div>
</div>
</div>
<!-- Action Buttons -->
<div class="btn-action-group justify-content-center">
<!-- Start -->
<button type="button" class="btn-action btn-action-primary"
@click="handleControl('start')"
:disabled="isLoading || stats.status === 'running' || !pkiStatus.server_ready"
:title="startHint">
<i v-if="isLoading && action==='start'" class="fas fa-spinner fa-spin"></i>
<i v-else class="fas fa-play"></i>
Start Service
</button>
<!-- Restart -->
<button type="button" class="btn-action btn-action-save"
@click="handleControl('restart')"
:disabled="isLoading || !pkiStatus.server_ready"
:title="startHint">
<i v-if="isLoading && action==='restart'" class="fas fa-spinner fa-spin"></i>
<i v-else class="fas fa-sync-alt"></i>
Restart Service
</button>
<!-- Stop -->
<button type="button" class="btn-action btn-action-danger"
@click="handleControl('stop')"
:disabled="isLoading || stats.status !== 'running'">
<i v-if="isLoading && action==='stop'" class="fas fa-spinner fa-spin"></i>
<i v-else class="fas fa-stop"></i>
Stop Service
</button>
</div>
</div>
</div>
</template>
<script setup>
import { ref, reactive, computed, onMounted, onUnmounted } from 'vue';
import { useApi } from '../composables/useApi';
import { useToast } from '../composables/useToast';
import { usePkiStatus } from '../composables/usePkiStatus';
const { profilesApiClient } = useApi();
const toast = useToast();
const { status: pkiStatus, refresh: refreshPkiStatus } = usePkiStatus();
// Start/Restart need the PKI server files (Stop stays available)
const startHint = computed(() => {
if (pkiStatus.server_ready) return '';
return pkiStatus.pki_ready && pkiStatus.missing.length
? `Missing server files: ${pkiStatus.missing.join(', ')}`
: 'Initialize PKI first';
});
// State
const stats = reactive({
status: 'unknown',
pid: null,
cpu_percent: 0.0,
memory_mb: 0.0,
uptime: null
});
const isLoading = ref(false);
const action = ref(''); // 'start', 'stop', 'restart'
let pollInterval = null;
// Methods
const showMessage = (text, type = 'success') => toast.show(text, type);
const fetchStats = async () => {
try {
const res = await profilesApiClient.get('/server/process/stats');
Object.assign(stats, res.data);
} catch (err) {
console.error("Failed to fetch stats:", err);
// Don't show error toast on every poll failure to avoid spam
}
};
const handleControl = async (cmd) => {
if (!confirm(`Are you sure you want to ${cmd.toUpperCase()} the OpenVPN service?`)) return;
isLoading.value = true;
action.value = cmd;
try {
const res = await profilesApiClient.post(`/server/process/${cmd}`);
showMessage(res.data.message || `Service ${cmd} successful`, 'success');
// Refresh stats immediately after action
setTimeout(fetchStats, 1000);
} catch (err) {
const errMsg = err.response?.data?.detail || err.message;
showMessage(`Failed to ${cmd}: ${errMsg}`, 'error');
} finally {
isLoading.value = false;
action.value = '';
}
};
onMounted(() => {
refreshPkiStatus();
fetchStats();
// Poll every 5 seconds
pollInterval = setInterval(fetchStats, 5000);
});
onUnmounted(() => {
if (pollInterval) clearInterval(pollInterval);
});
</script>