/** * systemd service installation */ import { $ } from 'bun'; import { getConfigPaths } from '../config/manager.js'; import { logger } from '../utils/logger.js'; import { getClientServiceTemplate, getServerServiceTemplate } from './templates.js'; const SERVER_SERVICE_PATH = '/etc/systemd/system/usbip-supervisor-server.service'; const CLIENT_SERVICE_PATH = '/etc/systemd/system/usbip-supervisor-client.service'; /** * Install server systemd service */ export async function installServerSystemd(configPath?: string): Promise { try { // Check if running as root if (process.getuid?.() !== 0) { console.error('Error: Installing systemd service requires root privileges'); console.log('Please run with sudo:'); console.log(' sudo bunx @siwats/usbip-supervisor server --install-systemd'); process.exit(1); } const config = configPath || getConfigPaths().server; const serviceContent = getServerServiceTemplate(config); // Write service file await Bun.write(SERVER_SERVICE_PATH, serviceContent); logger.info(`Wrote service file to ${SERVER_SERVICE_PATH}`); // Reload systemd await $`systemctl daemon-reload`.quiet(); logger.info('Reloaded systemd daemon'); // Enable service await $`systemctl enable usbip-supervisor-server`.quiet(); logger.info('Enabled usbip-supervisor-server service'); console.log('\n✓ Server service installed successfully!'); console.log('\nTo start the service:'); console.log(' sudo systemctl start usbip-supervisor-server'); console.log('\nTo check status:'); console.log(' sudo systemctl status usbip-supervisor-server'); console.log('\nTo view logs:'); console.log(' sudo journalctl -u usbip-supervisor-server -f'); } catch (error) { logger.error(`Failed to install server service: ${error}`); throw error; } } /** * Install client systemd service */ export async function installClientSystemd(configPath?: string): Promise { try { // Check if running as root if (process.getuid?.() !== 0) { console.error('Error: Installing systemd service requires root privileges'); console.log('Please run with sudo:'); console.log(' sudo bunx @siwats/usbip-supervisor client --install-systemd'); process.exit(1); } const config = configPath || getConfigPaths().client; const serviceContent = getClientServiceTemplate(config); // Write service file await Bun.write(CLIENT_SERVICE_PATH, serviceContent); logger.info(`Wrote service file to ${CLIENT_SERVICE_PATH}`); // Reload systemd await $`systemctl daemon-reload`.quiet(); logger.info('Reloaded systemd daemon'); // Enable service await $`systemctl enable usbip-supervisor-client`.quiet(); logger.info('Enabled usbip-supervisor-client service'); console.log('\n✓ Client service installed successfully!'); console.log('\nTo start the service:'); console.log(' sudo systemctl start usbip-supervisor-client'); console.log('\nTo check status:'); console.log(' sudo systemctl status usbip-supervisor-client'); console.log('\nTo view logs:'); console.log(' sudo journalctl -u usbip-supervisor-client -f'); } catch (error) { logger.error(`Failed to install client service: ${error}`); throw error; } }