154 lines
4.3 KiB
JavaScript
154 lines
4.3 KiB
JavaScript
// serviceManager.js - Gestion du service systemd Pilot V2
|
|
|
|
import GLib from 'gi://GLib';
|
|
import Gio from 'gi://Gio';
|
|
|
|
/**
|
|
* Gère les interactions avec le service systemd mqtt_pilot
|
|
*/
|
|
export class ServiceManager {
|
|
constructor() {
|
|
this.serviceName = 'mqtt_pilot.service';
|
|
}
|
|
|
|
/**
|
|
* Exécute une commande systemctl
|
|
*/
|
|
_executeSystemctl(action) {
|
|
try {
|
|
const command = `systemctl --user ${action} ${this.serviceName}`;
|
|
const [success, stdout, stderr, exitStatus] = GLib.spawn_command_line_sync(command);
|
|
|
|
if (exitStatus !== 0) {
|
|
const decoder = new TextDecoder('utf-8');
|
|
const errorMsg = decoder.decode(stderr);
|
|
console.error(`systemctl ${action} failed: ${errorMsg}`);
|
|
return false;
|
|
}
|
|
|
|
return true;
|
|
} catch (error) {
|
|
console.error(`Error executing systemctl: ${error.message}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Vérifie si le service est actif
|
|
*/
|
|
isServiceActive() {
|
|
try {
|
|
const command = `systemctl --user is-active ${this.serviceName}`;
|
|
const [success, stdout, stderr, exitStatus] = GLib.spawn_command_line_sync(command);
|
|
|
|
// Exit status 0 = active, 3 = inactive
|
|
return exitStatus === 0;
|
|
} catch (error) {
|
|
console.error(`Error checking service status: ${error.message}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Vérifie si le service est enabled
|
|
*/
|
|
isServiceEnabled() {
|
|
try {
|
|
const command = `systemctl --user is-enabled ${this.serviceName}`;
|
|
const [success, stdout, stderr, exitStatus] = GLib.spawn_command_line_sync(command);
|
|
|
|
return exitStatus === 0;
|
|
} catch (error) {
|
|
console.error(`Error checking service enabled status: ${error.message}`);
|
|
return false;
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Démarre le service
|
|
*/
|
|
startService() {
|
|
console.log('Starting Pilot service...');
|
|
return this._executeSystemctl('start');
|
|
}
|
|
|
|
/**
|
|
* Arrête le service
|
|
*/
|
|
stopService() {
|
|
console.log('Stopping Pilot service...');
|
|
return this._executeSystemctl('stop');
|
|
}
|
|
|
|
/**
|
|
* Redémarre le service
|
|
*/
|
|
restartService() {
|
|
console.log('Restarting Pilot service...');
|
|
return this._executeSystemctl('restart');
|
|
}
|
|
|
|
/**
|
|
* Active le service au démarrage
|
|
*/
|
|
enableService() {
|
|
console.log('Enabling Pilot service...');
|
|
return this._executeSystemctl('enable');
|
|
}
|
|
|
|
/**
|
|
* Désactive le service au démarrage
|
|
*/
|
|
disableService() {
|
|
console.log('Disabling Pilot service...');
|
|
return this._executeSystemctl('disable');
|
|
}
|
|
|
|
/**
|
|
* Recharge la configuration du service
|
|
*/
|
|
reloadService() {
|
|
console.log('Reloading Pilot service configuration...');
|
|
// Pour Pilot V2, on redémarre le service pour recharger la config
|
|
return this.restartService();
|
|
}
|
|
|
|
/**
|
|
* Obtient le status détaillé du service
|
|
*/
|
|
getServiceStatus() {
|
|
try {
|
|
const command = `systemctl --user status ${this.serviceName}`;
|
|
const [success, stdout, stderr, exitStatus] = GLib.spawn_command_line_sync(command);
|
|
|
|
const decoder = new TextDecoder('utf-8');
|
|
return {
|
|
active: exitStatus === 0,
|
|
output: decoder.decode(stdout)
|
|
};
|
|
} catch (error) {
|
|
console.error(`Error getting service status: ${error.message}`);
|
|
return {
|
|
active: false,
|
|
output: error.message
|
|
};
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Obtient les derniers logs du service
|
|
*/
|
|
getServiceLogs(lines = 20) {
|
|
try {
|
|
const command = `journalctl --user -u ${this.serviceName} -n ${lines} --no-pager`;
|
|
const [success, stdout, stderr, exitStatus] = GLib.spawn_command_line_sync(command);
|
|
|
|
const decoder = new TextDecoder('utf-8');
|
|
return decoder.decode(stdout);
|
|
} catch (error) {
|
|
console.error(`Error getting service logs: ${error.message}`);
|
|
return `Error: ${error.message}`;
|
|
}
|
|
}
|
|
}
|