commands to start/restart/stop a systemd service

merge-requests/4/head
Matthew 2020-07-01 01:24:00 -04:00
parent 07e3e7ffdb
commit c0e1c54ce0
No known key found for this signature in database
GPG Key ID: 210AF32ADE3B5C4B
4 changed files with 93 additions and 0 deletions

View File

@ -2,6 +2,9 @@ import { Message } from 'eris';
import { Client, Command } from '../class'; import { Client, Command } from '../class';
import SystemD_Linger from './systemd_linger'; import SystemD_Linger from './systemd_linger';
import SystemD_Status from './systemd_status'; 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 { export default class SystemD extends Command {
constructor(client: Client) { constructor(client: Client) {

View File

@ -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 <service name>\`.`;
this.usage = `${this.client.config.prefix}systemd restart <service name>`;
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);
}
}
}

View File

@ -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 <service name>\`.`;
this.usage = `${this.client.config.prefix}systemd start <service name>`;
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);
}
}
}

View File

@ -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 <service name>`;
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);
}
}
}