balena-supervisor/src/lib/avahi.ts
Cameron Diver 03ca0ee9ad
avahi: Control with HOST_DISCOVERABILITY
The host config variable HOST_DISCOVERABILITY can be set to
true or false, controlling the state of the avahi service. This
determines if the device advertises it's presence over mDNS.

Change-type: patch
Signed-off-by: Cameron Diver <cameron@balena.io>
Signed-off-by: Rich Bayliss <rich@balena.io>
2020-07-06 13:02:29 +01:00

33 lines
856 B
TypeScript

import * as config from '../config';
import * as dbus from './dbus';
import log from './supervisor-console';
export const initialized = (async () => {
await config.initialized;
config.on('change', (conf) => {
if (conf.hostDiscoverability != null) {
switchDiscoverability(conf.hostDiscoverability);
}
});
await switchDiscoverability(await config.get('hostDiscoverability'));
})();
async function switchDiscoverability(discoverable: boolean) {
try {
if (discoverable) {
log.info('Setting host to discoverable');
await dbus.startService('avahi-daemon');
await dbus.startSocket('avahi-daemon');
} else {
log.info('Setting host to undiscoverable');
await dbus.stopService('avahi-daemon');
await dbus.stopSocket('avahi-daemon');
}
} catch (e) {
log.error('There was an error switching host discoverability:', e);
}
}