Merge pull request #866 from balena-io/861-logind-shutdown

fix: Use logind manager to request reboots and shutdowns
This commit is contained in:
CameronDiver 2019-01-09 17:11:11 +00:00 committed by GitHub
commit 6b5617be15
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -4,10 +4,21 @@ import * as dbus from 'dbus-native';
const bus = dbus.systemBus(); const bus = dbus.systemBus();
const invokeAsync = Bluebird.promisify(bus.invoke).bind(bus); const invokeAsync = Bluebird.promisify(bus.invoke).bind(bus);
const defaultPathInfo = {
path: '/org/freedesktop/systemd1',
destination: 'org.freedesktop.systemd1',
interface: 'org.freedesktop.systemd1.Manager',
};
const systemdManagerMethodCall = ( const systemdManagerMethodCall = (
method: string, method: string,
signature?: string, signature?: string,
body?: any[], body?: any[],
pathInfo: {
path: string;
destination: string;
interface: string;
} = defaultPathInfo,
) => { ) => {
if (signature == null) { if (signature == null) {
signature = ''; signature = '';
@ -16,9 +27,7 @@ const systemdManagerMethodCall = (
body = []; body = [];
} }
return invokeAsync({ return invokeAsync({
path: '/org/freedesktop/systemd1', ...pathInfo,
destination: 'org.freedesktop.systemd1',
interface: 'org.freedesktop.systemd1.Manager',
member: method, member: method,
signature, signature,
body, body,
@ -62,11 +71,27 @@ export function disableService(serviceName: string) {
} }
export const reboot = Bluebird.method(() => export const reboot = Bluebird.method(() =>
setTimeout(() => systemdManagerMethodCall('Reboot'), 1000), setTimeout(
() =>
systemdManagerMethodCall('Reboot', 'b', [false], {
path: '/org/freedesktop/login1',
destination: 'org.freedesktop.login1',
interface: 'org.freedesktop.login1.Manager',
}),
1000,
),
); );
export const shutdown = Bluebird.method(() => export const shutdown = Bluebird.method(() =>
setTimeout(() => systemdManagerMethodCall('PowerOff'), 1000), setTimeout(
() =>
systemdManagerMethodCall('PowerOff', 'b', [false], {
path: '/org/freedesktop/login1',
destination: 'org.freedesktop.login1',
interface: 'org.freedesktop.login1.Manager',
}),
1000,
),
); );
function getUnitProperty(unitName: string, property: string) { function getUnitProperty(unitName: string, property: string) {