File vms-v2.js of Package PersistenceOS
/**
* PersistenceOS VM Management Module v2
* Virtual Machine management system for PersistenceOS
* Extracted for Phase 4 modular architecture
*/
/**
* VM Configuration
*/
const VM_CONFIG = {
REFRESH_INTERVAL: 15000, // 15 seconds
API_ENDPOINTS: {
LIST: '/api/virtualization/vms',
STATISTICS: '/api/vms/statistics',
CREATE: '/api/vms/create',
START: '/api/vms/{name}/start',
STOP: '/api/vms/{name}/stop',
DELETE: '/api/vms/{name}/delete',
DETAILS: '/api/vms/{name}'
},
DEBUG: true
};
/**
* VM Management System
* Handles all VM operations and state management
*/
class PersistenceVMs {
constructor() {
this.vms = [];
this.isRefreshing = false;
this.refreshInterval = null;
this.init();
}
init() {
if (VM_CONFIG.DEBUG) {
console.log('đĨī¸ PersistenceOS VM Management Module v2 initialized');
}
// Start auto-refresh
this.startAutoRefresh();
}
/**
* Get all VMs
*/
getVMs() {
return [...this.vms];
}
/**
* Get VM by ID
*/
getVM(id) {
return this.vms.find(vm => vm.id === id);
}
/**
* Get VM statistics
*/
getVMStats() {
const running = this.vms.filter(vm => vm.status === 'running').length;
const stopped = this.vms.filter(vm => vm.status === 'stopped').length;
const total = this.vms.length;
return { running, stopped, total };
}
/**
* Start a VM
*/
async startVM(id) {
if (VM_CONFIG.DEBUG) {
console.log(`đ Starting VM: ${id}`);
}
const vm = this.getVM(id);
if (!vm) {
throw new Error(`VM ${id} not found`);
}
if (vm.status === 'running') {
throw new Error(`VM ${vm.name} is already running`);
}
// Simulate API call
await this.simulateAPICall();
vm.status = 'running';
vm.ip = `192.168.122.${Math.floor(Math.random() * 100) + 10}`;
vm.uptime = '0 minutes';
this.notifyVMUpdate();
if (VM_CONFIG.DEBUG) {
console.log(`â
VM ${vm.name} started successfully`);
}
}
/**
* Stop a VM
*/
async stopVM(id) {
if (VM_CONFIG.DEBUG) {
console.log(`đ Stopping VM: ${id}`);
}
const vm = this.getVM(id);
if (!vm) {
throw new Error(`VM ${id} not found`);
}
if (vm.status === 'stopped') {
throw new Error(`VM ${vm.name} is already stopped`);
}
// Simulate API call
await this.simulateAPICall();
vm.status = 'stopped';
vm.ip = 'N/A';
vm.uptime = 'N/A';
this.notifyVMUpdate();
if (VM_CONFIG.DEBUG) {
console.log(`â
VM ${vm.name} stopped successfully`);
}
}
/**
* Delete a VM
*/
async deleteVM(id) {
if (VM_CONFIG.DEBUG) {
console.log(`đī¸ Deleting VM: ${id}`);
}
const vmIndex = this.vms.findIndex(vm => vm.id === id);
if (vmIndex === -1) {
throw new Error(`VM ${id} not found`);
}
const vm = this.vms[vmIndex];
// Simulate API call
await this.simulateAPICall();
this.vms.splice(vmIndex, 1);
this.notifyVMUpdate();
if (VM_CONFIG.DEBUG) {
console.log(`â
VM ${vm.name} deleted successfully`);
}
}
/**
* Refresh VM list
*/
async refreshVMs() {
if (this.isRefreshing) return;
this.isRefreshing = true;
if (VM_CONFIG.DEBUG) {
console.log('đ Refreshing VM list...');
}
try {
// Simulate API call with random updates
await this.simulateVMRefresh();
if (VM_CONFIG.DEBUG) {
console.log('â
VM list refreshed successfully');
}
this.notifyVMUpdate();
} catch (error) {
if (VM_CONFIG.DEBUG) {
console.error('â VM refresh failed:', error);
}
throw error;
} finally {
this.isRefreshing = false;
}
}
/**
* Simulate API call delay
*/
async simulateAPICall() {
return new Promise((resolve) => {
setTimeout(resolve, 1000 + Math.random() * 1000);
});
}
/**
* Simulate VM refresh with random updates
*/
async simulateVMRefresh() {
return new Promise((resolve) => {
setTimeout(() => {
// Update running VMs with new uptime
this.vms.forEach(vm => {
if (vm.status === 'running') {
const hours = Math.floor(Math.random() * 24);
const minutes = Math.floor(Math.random() * 60);
vm.uptime = `${hours} hours, ${minutes} minutes`;
}
});
resolve();
}, 1000);
});
}
/**
* Start auto-refresh timer
*/
startAutoRefresh() {
if (this.refreshInterval) {
clearInterval(this.refreshInterval);
}
this.refreshInterval = setInterval(() => {
this.refreshVMs().catch(error => {
if (VM_CONFIG.DEBUG) {
console.warn('â ī¸ VM auto-refresh failed:', error);
}
});
}, VM_CONFIG.REFRESH_INTERVAL);
if (VM_CONFIG.DEBUG) {
console.log('â° VM auto-refresh started (15s interval)');
}
}
/**
* Stop auto-refresh timer
*/
stopAutoRefresh() {
if (this.refreshInterval) {
clearInterval(this.refreshInterval);
this.refreshInterval = null;
if (VM_CONFIG.DEBUG) {
console.log('âšī¸ VM auto-refresh stopped');
}
}
}
/**
* Notify VM update listeners
*/
notifyVMUpdate() {
// Dispatch custom event for Vue reactivity
window.dispatchEvent(new CustomEvent('vmDataUpdate', {
detail: {
vms: this.getVMs(),
stats: this.getVMStats()
}
}));
}
/**
* Cleanup resources
*/
destroy() {
this.stopAutoRefresh();
if (VM_CONFIG.DEBUG) {
console.log('đī¸ VM Management module destroyed');
}
}
}
// Create global VM management instance
window.PersistenceVMs = new PersistenceVMs();
// Backward compatibility
window.VMs = window.PersistenceVMs;
// Export for module systems
if (typeof module !== 'undefined' && module.exports) {
module.exports = { PersistenceVMs, VM_CONFIG };
}
console.log('â
PersistenceOS VM Management Module v2 loaded successfully');