mirror of
https://github.com/balena-os/balena-supervisor.git
synced 2025-04-24 04:55:42 +00:00
refactor: Convert systemd module to typescript
Change-type: patch Signed-off-by: Cameron Diver <cameron@balena.io>
This commit is contained in:
parent
e00954babd
commit
ec37db597d
@ -1,55 +0,0 @@
|
||||
dbus = require 'dbus-native'
|
||||
Promise = require 'bluebird'
|
||||
|
||||
bus = Promise.promisifyAll(dbus.systemBus())
|
||||
|
||||
systemdManagerMethodCall = (method, signature = '', body = []) ->
|
||||
bus.invokeAsync({
|
||||
path: '/org/freedesktop/systemd1'
|
||||
destination: 'org.freedesktop.systemd1'
|
||||
interface: 'org.freedesktop.systemd1.Manager'
|
||||
member: method
|
||||
signature
|
||||
body
|
||||
})
|
||||
|
||||
exports.restartService = (serviceName) ->
|
||||
systemdManagerMethodCall('RestartUnit', 'ss', [ "#{serviceName}.service", 'fail' ])
|
||||
|
||||
exports.startService = (serviceName) ->
|
||||
systemdManagerMethodCall('StartUnit', 'ss', [ "#{serviceName}.service", 'fail' ])
|
||||
|
||||
exports.stopService = (serviceName) ->
|
||||
systemdManagerMethodCall('StopUnit', 'ss', [ "#{serviceName}.service", 'fail' ])
|
||||
|
||||
exports.enableService = (serviceName) ->
|
||||
systemdManagerMethodCall('EnableUnitFiles', 'asbb', [ [ "#{serviceName}.service" ], false, false ])
|
||||
|
||||
exports.disableService = (serviceName) ->
|
||||
systemdManagerMethodCall('DisableUnitFiles', 'asb', [ [ "#{serviceName}.service" ], false ])
|
||||
|
||||
exports.reboot = Promise.method ->
|
||||
setTimeout( ->
|
||||
systemdManagerMethodCall('Reboot')
|
||||
, 1000)
|
||||
|
||||
exports.shutdown = Promise.method ->
|
||||
setTimeout( ->
|
||||
systemdManagerMethodCall('PowerOff')
|
||||
, 1000)
|
||||
|
||||
getUnitProperty = (unitName, property) ->
|
||||
systemdManagerMethodCall('GetUnit', 's', [ unitName ])
|
||||
.then (unitPath) ->
|
||||
bus.invokeAsync({
|
||||
path: unitPath
|
||||
destination: 'org.freedesktop.systemd1'
|
||||
interface: 'org.freedesktop.DBus.Properties'
|
||||
member: 'Get'
|
||||
signature: 'ss'
|
||||
body: [ 'org.freedesktop.systemd1.Unit', property ]
|
||||
})
|
||||
.get(1).get(0)
|
||||
|
||||
exports.serviceActiveState = (serviceName) ->
|
||||
getUnitProperty("#{serviceName}.service", 'ActiveState')
|
90
src/lib/systemd.ts
Normal file
90
src/lib/systemd.ts
Normal file
@ -0,0 +1,90 @@
|
||||
import * as Bluebird from 'bluebird';
|
||||
import * as dbus from 'dbus-native';
|
||||
|
||||
const bus = dbus.systemBus();
|
||||
const invokeAsync = Bluebird.promisify(bus.invoke).bind(bus);
|
||||
|
||||
const systemdManagerMethodCall = (
|
||||
method: string,
|
||||
signature?: string,
|
||||
body?: any[],
|
||||
) => {
|
||||
if (signature == null) {
|
||||
signature = '';
|
||||
}
|
||||
if (body == null) {
|
||||
body = [];
|
||||
}
|
||||
return invokeAsync({
|
||||
path: '/org/freedesktop/systemd1',
|
||||
destination: 'org.freedesktop.systemd1',
|
||||
interface: 'org.freedesktop.systemd1.Manager',
|
||||
member: method,
|
||||
signature,
|
||||
body,
|
||||
});
|
||||
};
|
||||
|
||||
export function restartService(serviceName: string) {
|
||||
return systemdManagerMethodCall('RestartUnit', 'ss', [
|
||||
`${serviceName}.service`,
|
||||
'fail',
|
||||
]);
|
||||
}
|
||||
|
||||
export function startService(serviceName: string) {
|
||||
return systemdManagerMethodCall('StartUnit', 'ss', [
|
||||
`${serviceName}.service`,
|
||||
'fail',
|
||||
]);
|
||||
}
|
||||
|
||||
export function stopService(serviceName: string) {
|
||||
return systemdManagerMethodCall('StopUnit', 'ss', [
|
||||
`${serviceName}.service`,
|
||||
'fail',
|
||||
]);
|
||||
}
|
||||
|
||||
export function enableService(serviceName: string) {
|
||||
return systemdManagerMethodCall('EnableUnitFiles', 'asbb', [
|
||||
[`${serviceName}.service`],
|
||||
false,
|
||||
false,
|
||||
]);
|
||||
}
|
||||
|
||||
export function disableService(serviceName: string) {
|
||||
return systemdManagerMethodCall('DisableUnitFiles', 'asb', [
|
||||
[`${serviceName}.service`],
|
||||
false,
|
||||
]);
|
||||
}
|
||||
|
||||
export const reboot = Bluebird.method(() =>
|
||||
setTimeout(() => systemdManagerMethodCall('Reboot'), 1000),
|
||||
);
|
||||
|
||||
export const shutdown = Bluebird.method(() =>
|
||||
setTimeout(() => systemdManagerMethodCall('PowerOff'), 1000),
|
||||
);
|
||||
|
||||
function getUnitProperty(unitName: string, property: string) {
|
||||
return systemdManagerMethodCall('GetUnit', 's', [unitName])
|
||||
.then((unitPath: string) =>
|
||||
invokeAsync({
|
||||
path: unitPath,
|
||||
destination: 'org.freedesktop.systemd1',
|
||||
interface: 'org.freedesktop.DBus.Properties',
|
||||
member: 'Get',
|
||||
signature: 'ss',
|
||||
body: ['org.freedesktop.systemd1.Unit', property],
|
||||
}),
|
||||
)
|
||||
.get(1)
|
||||
.get(0);
|
||||
}
|
||||
|
||||
export function serviceActiveState(serviceName: string) {
|
||||
return getUnitProperty(`${serviceName}.service`, 'ActiveState');
|
||||
}
|
134
typings/dbus-native.d.ts
vendored
Normal file
134
typings/dbus-native.d.ts
vendored
Normal file
@ -0,0 +1,134 @@
|
||||
// Adapted from: https://gist.github.com/TianyiLi/a231afa2f804d8fa0805baa4830f9242
|
||||
declare module 'dbus-native' {
|
||||
import * as net from 'net';
|
||||
import * as events from 'events';
|
||||
|
||||
interface msg {
|
||||
destination?: string;
|
||||
path?: string;
|
||||
interface?: any;
|
||||
member: string;
|
||||
signature?: any;
|
||||
body?: Array<any>;
|
||||
}
|
||||
|
||||
interface MessageBus {
|
||||
connection: any;
|
||||
serial: number;
|
||||
cookies: Object;
|
||||
methodCallHandlers: Object;
|
||||
signals: events.EventEmitter;
|
||||
exportedObjects: Object;
|
||||
|
||||
invoke(msg: msg, callback?: Callback<any>): void;
|
||||
invokeDbus(msg: msg, callback?: Callback<any>): void;
|
||||
mangle(path: any, iface: any, member: any): string;
|
||||
mangle(obj: { path: any; iface: any; member: any }): string;
|
||||
sendSignal(
|
||||
path: any,
|
||||
iface: any,
|
||||
name: any,
|
||||
signature: any,
|
||||
args: any,
|
||||
): void;
|
||||
sendError(msg: any, signature: any, body: any): void;
|
||||
setMethodCallHandler(
|
||||
objectPath: any,
|
||||
iface: any,
|
||||
member: any,
|
||||
handler: any,
|
||||
): void;
|
||||
exportInterface(
|
||||
obj: Object,
|
||||
path: string,
|
||||
ifaceDesc: {
|
||||
name: string;
|
||||
signals: Object;
|
||||
method: Object;
|
||||
properties: Object;
|
||||
},
|
||||
): void;
|
||||
getService(serviceName: string): DBusService;
|
||||
getObject(path: string, name: string, callback: Callback<any>): DBusService;
|
||||
getInterface(
|
||||
path: string,
|
||||
objname: string,
|
||||
name: string,
|
||||
callback: Callback<any>,
|
||||
): DBusService;
|
||||
|
||||
addMatch(match: string, callback?: Callback<any>): void;
|
||||
removeMatch(match: string, callback?: Callback<any>): void;
|
||||
getId(callback?: Callback<any>): void;
|
||||
requestName(name: string, flags: any, callback?: Callback<any>): void;
|
||||
releaseName(name: string, callback?: Callback<any>): void;
|
||||
listNames(callback?: Callback<any>): void;
|
||||
listActivatableNames(callback?: Callback<any>): void;
|
||||
updateActivationEnvironment(env: any, callback?: Callback<any>): void;
|
||||
startServiceByName(name: any, flags: any, callback?: Callback<any>): void;
|
||||
getConnectionUnixUser(name: any, callback?: Callback<any>): void;
|
||||
getConnectionUnixProcessId(name: any, callback?: Callback<any>): void;
|
||||
getNameOwner(name: any, callback?: Callback<any>): void;
|
||||
nameHasOwner(name: any, callback?: Callback<any>): void;
|
||||
}
|
||||
|
||||
/**
|
||||
* This Should Not Used
|
||||
*
|
||||
* TODO: Fix this
|
||||
*
|
||||
* @interface DBusService
|
||||
*/
|
||||
interface DBusService {
|
||||
name: string;
|
||||
bus: MessageBus;
|
||||
getObject(name: any, callback: Callback<any>): void;
|
||||
getInterface(
|
||||
objName: string,
|
||||
ifaceName: string,
|
||||
callback: Callback<any>,
|
||||
): void;
|
||||
}
|
||||
|
||||
interface Server {
|
||||
server: net.Server;
|
||||
listen: void;
|
||||
}
|
||||
|
||||
const messageType: {
|
||||
error: number;
|
||||
invalid: number;
|
||||
methodCall: number;
|
||||
methodReturn: number;
|
||||
signal: number;
|
||||
};
|
||||
|
||||
enum flags {
|
||||
noReplyExpected = 1,
|
||||
noAutoStart,
|
||||
}
|
||||
|
||||
interface StreamOptions {
|
||||
socket: string;
|
||||
host: any;
|
||||
port: any;
|
||||
busAddress: string;
|
||||
}
|
||||
|
||||
class CreateConnection extends events.EventEmitter {
|
||||
message(msg: { path: string }): void;
|
||||
end(): void;
|
||||
}
|
||||
|
||||
function createClient(options?: StreamOptions): MessageBus;
|
||||
function createConnection(opts?: StreamOptions): CreateConnection;
|
||||
/**
|
||||
* Default is /var/run/dbus/system_bus_socket
|
||||
*
|
||||
* @export
|
||||
* @returns {MessageBus}
|
||||
*/
|
||||
function systemBus(): MessageBus;
|
||||
function sessionBus(options?: StreamOptions): MessageBus;
|
||||
function createServer(): Server;
|
||||
}
|
4
typings/global.d.ts
vendored
4
typings/global.d.ts
vendored
@ -1,3 +1,7 @@
|
||||
interface Dictionary<T> {
|
||||
[key: string]: T;
|
||||
}
|
||||
|
||||
interface Callback<T> {
|
||||
(err?: Error, res?: T): void;
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user