Files
portainer-core/organizr-widgets/service-control.html
T

343 lines
11 KiB
HTML

<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Service Control</title>
<style>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
background: transparent;
color: #e0e0e0;
padding: 10px;
}
.container {
max-width: 1200px;
margin: 0 auto;
}
h2 {
color: #fff;
margin-bottom: 15px;
font-size: 20px;
font-weight: 500;
}
.service-grid {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(300px, 1fr));
gap: 15px;
}
.service-card {
background: rgba(40, 40, 40, 0.95);
border: 1px solid rgba(255, 255, 255, 0.1);
border-radius: 8px;
padding: 15px;
transition: all 0.3s ease;
}
.service-card:hover {
border-color: rgba(66, 153, 225, 0.5);
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}
.service-header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 12px;
}
.service-name {
font-size: 16px;
font-weight: 600;
color: #fff;
text-transform: capitalize;
}
.status-badge {
padding: 4px 12px;
border-radius: 12px;
font-size: 12px;
font-weight: 600;
text-transform: uppercase;
}
.status-running {
background: rgba(72, 187, 120, 0.2);
color: #48bb78;
border: 1px solid rgba(72, 187, 120, 0.4);
}
.status-stopped {
background: rgba(245, 101, 101, 0.2);
color: #f56565;
border: 1px solid rgba(245, 101, 101, 0.4);
}
.status-loading {
background: rgba(237, 137, 54, 0.2);
color: #ed8936;
border: 1px solid rgba(237, 137, 54, 0.4);
}
.service-info {
font-size: 13px;
color: #a0a0a0;
margin-bottom: 12px;
}
.service-actions {
display: flex;
gap: 8px;
}
.btn {
flex: 1;
padding: 8px 12px;
border: none;
border-radius: 6px;
font-size: 13px;
font-weight: 600;
cursor: pointer;
transition: all 0.2s ease;
text-transform: uppercase;
letter-spacing: 0.5px;
}
.btn:disabled {
opacity: 0.5;
cursor: not-allowed;
}
.btn-start {
background: linear-gradient(135deg, #48bb78 0%, #38a169 100%);
color: white;
}
.btn-start:hover:not(:disabled) {
background: linear-gradient(135deg, #38a169 0%, #2f855a 100%);
transform: translateY(-1px);
}
.btn-stop {
background: linear-gradient(135deg, #f56565 0%, #e53e3e 100%);
color: white;
}
.btn-stop:hover:not(:disabled) {
background: linear-gradient(135deg, #e53e3e 0%, #c53030 100%);
transform: translateY(-1px);
}
.btn-restart {
background: linear-gradient(135deg, #4299e1 0%, #3182ce 100%);
color: white;
}
.btn-restart:hover:not(:disabled) {
background: linear-gradient(135deg, #3182ce 0%, #2c5282 100%);
transform: translateY(-1px);
}
.loading {
text-align: center;
padding: 40px;
color: #a0a0a0;
}
.error {
background: rgba(245, 101, 101, 0.1);
border: 1px solid rgba(245, 101, 101, 0.4);
color: #f56565;
padding: 12px;
border-radius: 6px;
margin-bottom: 15px;
}
.always-on-badge {
display: inline-block;
padding: 2px 8px;
background: rgba(66, 153, 225, 0.2);
color: #4299e1;
border: 1px solid rgba(66, 153, 225, 0.4);
border-radius: 10px;
font-size: 11px;
margin-left: 8px;
}
@keyframes spin {
to { transform: rotate(360deg); }
}
.spinner {
display: inline-block;
width: 14px;
height: 14px;
border: 2px solid rgba(255, 255, 255, 0.3);
border-top-color: #fff;
border-radius: 50%;
animation: spin 0.6s linear infinite;
margin-right: 6px;
}
</style>
</head>
<body>
<div class="container">
<h2>🎛️ On-Demand Services</h2>
<div id="error-container"></div>
<div id="service-container" class="loading">Loading services...</div>
</div>
<script>
const API_BASE = 'http://localhost:8083';
let services = [];
let alwaysOnServices = [];
async function fetchServices() {
try {
const response = await fetch(`${API_BASE}/infrastructure/services`);
if (!response.ok) throw new Error('Failed to fetch services');
services = await response.json();
const groupsResponse = await fetch(`${API_BASE}/infrastructure/service-groups`);
if (groupsResponse.ok) {
const groupsData = await groupsResponse.json();
alwaysOnServices = groupsData.always_on || [];
}
renderServices();
document.getElementById('error-container').innerHTML = '';
} catch (error) {
console.error('Error fetching services:', error);
document.getElementById('error-container').innerHTML =
`<div class="error">❌ Failed to connect to API: ${error.message}</div>`;
}
}
function isAlwaysOn(serviceName) {
return alwaysOnServices.includes(serviceName.toLowerCase());
}
function getServiceStatus(service) {
if (service.containers_running > 0) {
return {
class: 'status-running',
text: `Running (${service.containers_running}/${service.containers_total})`
};
} else if (service.containers_total > 0) {
return {
class: 'status-stopped',
text: 'Stopped'
};
} else {
return {
class: 'status-stopped',
text: 'No containers'
};
}
}
function renderServices() {
const container = document.getElementById('service-container');
// Filter to only show stoppable services
const stoppableServices = services.filter(s => !isAlwaysOn(s.name));
if (stoppableServices.length === 0) {
container.innerHTML = '<div class="loading">No stoppable services found</div>';
return;
}
container.className = 'service-grid';
container.innerHTML = stoppableServices.map(service => {
const status = getServiceStatus(service);
const isRunning = service.containers_running > 0;
const alwaysOn = isAlwaysOn(service.name);
return `
<div class="service-card" data-service="${service.name}">
<div class="service-header">
<span class="service-name">
${service.name}
${alwaysOn ? '<span class="always-on-badge">ALWAYS ON</span>' : ''}
</span>
<span class="status-badge ${status.class}">${status.text}</span>
</div>
<div class="service-info">
Stack ID: ${service.stack_id || 'N/A'}
</div>
<div class="service-actions">
<button class="btn btn-start"
onclick="controlService('${service.name}', 'start')"
${isRunning || alwaysOn ? 'disabled' : ''}>
Start
</button>
<button class="btn btn-stop"
onclick="controlService('${service.name}', 'stop')"
${!isRunning || alwaysOn ? 'disabled' : ''}>
Stop
</button>
</div>
</div>
`;
}).join('');
}
async function controlService(serviceName, action) {
const card = document.querySelector(`[data-service="${serviceName}"]`);
const buttons = card.querySelectorAll('button');
// Disable all buttons and show loading
buttons.forEach(btn => {
btn.disabled = true;
if (btn.textContent.toLowerCase().includes(action)) {
btn.innerHTML = `<span class="spinner"></span>${action.toUpperCase()}...`;
}
});
try {
const response = await fetch(`${API_BASE}/infrastructure/services/${serviceName}/${action}`, {
method: 'POST'
});
const result = await response.json();
if (!response.ok || !result.success) {
throw new Error(result.message || result.detail || 'Operation failed');
}
console.log(`${action} ${serviceName}:`, result);
// Wait a bit for containers to start/stop
await new Promise(resolve => setTimeout(resolve, 2000));
// Refresh service list
await fetchServices();
} catch (error) {
console.error(`Error ${action}ing ${serviceName}:`, error);
alert(`Failed to ${action} ${serviceName}: ${error.message}`);
// Re-enable buttons on error
await fetchServices();
}
}
// Auto-refresh every 10 seconds
setInterval(fetchServices, 10000);
// Initial load
fetchServices();
</script>
</body>
</html>