UNPKG

@remote.it/core

Version:

Core remote.it JavasScript/TypeScript library

111 lines (91 loc) 2.29 kB
import debug from 'debug' import { GenericSystemService } from './GenericSystemService' import { Environment } from './Environment' import { Systemd } from './Systemd' import { SystemdUnitFile } from './SystemdUnitFile' const d = debug('remoteit:MacSystemService') interface SystemdUnit { Unit: { Description: string Documentation: string After: string } Service: { Environment?: string Type?: string User?: string ExecStart: string Restart: string } Install: { WantedBy: string } } export class LinuxSystemService extends GenericSystemService { private unitFile: SystemdUnitFile<SystemdUnit> private systemd: Systemd constructor(public config: string = Environment.configPath) { super(config) this.unitFile = new SystemdUnitFile<SystemdUnit>( Environment.systemdUnitFilePath ) this.systemd = new Systemd() } async install() { d('install linux system service') // if (this.exists) this.uninstall() this.createUnitFile() this.systemd.enable() d('linux system service installed:', this.command) return this } async start() { d('start linux system service') if (!this.exists) this.install() this.systemd.start() return this } async stop() { d('stop system service') this.systemd.stop() return this } async uninstall() { d('uinstall system service') try { this.stop() this.systemd.disable() this.unitFile.remove() this.systemd.reboot() } catch (error) { console.error('uninstall error', error) } return this } private createUnitFile() { this.unitFile.write({ Unit: { After: 'network.target', Description: 'remote.it system service', Documentation: 'https://docs.remote.it', // Wants=network.target }, Service: { // Environment:'', ExecStart: this.command, // ExecStop=/sbin/dhcpcd -x // PIDFile=/run/dhcpcd.pid Restart: 'on-failure', Type: 'simple', // 'forking' User: Environment.username, }, Install: { // Alias=dhcpcd5.service WantedBy: 'multi-user.target', }, }) } get exists() { return this.unitFile.exists } }