From c0e1c54ce0798cd73912b9803f48b4dfe005a67e Mon Sep 17 00:00:00 2001 From: Matthew R Date: Wed, 1 Jul 2020 01:24:00 -0400 Subject: [PATCH] commands to start/restart/stop a systemd service --- src/commands/systemd.ts | 3 +++ src/commands/systemd_restart.ts | 30 ++++++++++++++++++++++++++++++ src/commands/systemd_start.ts | 30 ++++++++++++++++++++++++++++++ src/commands/systemd_stop.ts | 30 ++++++++++++++++++++++++++++++ 4 files changed, 93 insertions(+) create mode 100644 src/commands/systemd_restart.ts create mode 100644 src/commands/systemd_start.ts create mode 100644 src/commands/systemd_stop.ts diff --git a/src/commands/systemd.ts b/src/commands/systemd.ts index 6adf97c..65015f7 100644 --- a/src/commands/systemd.ts +++ b/src/commands/systemd.ts @@ -2,6 +2,9 @@ import { Message } from 'eris'; import { Client, Command } from '../class'; import SystemD_Linger from './systemd_linger'; import SystemD_Status from './systemd_status'; +import SystemD_Start from './systemd_start'; +import SystemD_Restart from './systemd_restart'; +import SystemD_Stop from './systemd_stop'; export default class SystemD extends Command { constructor(client: Client) { diff --git a/src/commands/systemd_restart.ts b/src/commands/systemd_restart.ts new file mode 100644 index 0000000..14428b9 --- /dev/null +++ b/src/commands/systemd_restart.ts @@ -0,0 +1,30 @@ +import { Message } from 'eris'; +import { Client, Command } from '../class'; + +export default class SystemD_Restart extends Command { + constructor(client: Client) { + super(client); + this.name = 'restart'; + this.description = `Restarts a SystemD user service, if the service has never been started or isn't running use \`${this.client.config.prefix}systemd start \`.`; + this.usage = `${this.client.config.prefix}systemd restart `; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[0]) return this.client.commands.get('help').run(message, ['systemd', this.name]); + const account = await this.client.db.Account.findOne({ userID: message.author.id }); + if (!account) return this.error(message.channel, 'You do not have a Cloud Services account.'); + try { + await this.client.util.exec(`runuser ${account.username} -c 'DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/\${UID}/bus" systemctl --user restart ${args[0]}'`); + message.delete(); + return this.success(message.channel, 'Service restarted.'); + } catch (err) { + if (err.includes('not found')) return this.error(message.channel, 'Service not found.'); + return this.error(message.channel, err.toString()); + } + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +} diff --git a/src/commands/systemd_start.ts b/src/commands/systemd_start.ts new file mode 100644 index 0000000..2b57db9 --- /dev/null +++ b/src/commands/systemd_start.ts @@ -0,0 +1,30 @@ +import { Message } from 'eris'; +import { Client, Command } from '../class'; + +export default class SystemD_Start extends Command { + constructor(client: Client) { + super(client); + this.name = 'start'; + this.description = `Starts a SystemD user service, if the service is already running use \`${this.client.config.prefix}systemd restart \`.`; + this.usage = `${this.client.config.prefix}systemd start `; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[0]) return this.client.commands.get('help').run(message, ['systemd', this.name]); + const account = await this.client.db.Account.findOne({ userID: message.author.id }); + if (!account) return this.error(message.channel, 'You do not have a Cloud Services account.'); + try { + await this.client.util.exec(`runuser ${account.username} -c 'DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/\${UID}/bus" systemctl --user start ${args[0]}'`); + message.delete(); + return this.success(message.channel, 'Service started.'); + } catch (err) { + if (err.includes('not found')) return this.error(message.channel, 'Service not found.'); + return this.error(message.channel, err.toString()); + } + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +} diff --git a/src/commands/systemd_stop.ts b/src/commands/systemd_stop.ts new file mode 100644 index 0000000..b7e4ef6 --- /dev/null +++ b/src/commands/systemd_stop.ts @@ -0,0 +1,30 @@ +import { Message } from 'eris'; +import { Client, Command } from '../class'; + +export default class SystemD_Stop extends Command { + constructor(client: Client) { + super(client); + this.name = 'stop'; + this.description = 'Stops a SystemD user service.'; + this.usage = `${this.client.config.prefix}systemd stop `; + this.enabled = true; + } + + public async run(message: Message, args: string[]) { + try { + if (!args[0]) return this.client.commands.get('help').run(message, ['systemd', this.name]); + const account = await this.client.db.Account.findOne({ userID: message.author.id }); + if (!account) return this.error(message.channel, 'You do not have a Cloud Services account.'); + try { + await this.client.util.exec(`runuser ${account.username} -c 'DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/\${UID}/bus" systemctl --user stop ${args[0]}'`); + message.delete(); + return this.success(message.channel, 'Service stopped.'); + } catch (err) { + if (err.includes('not found')) return this.error(message.channel, 'Service not found.'); + return this.error(message.channel, err.toString()); + } + } catch (err) { + return this.client.util.handleError(err, message, this); + } + } +}