UNPKG

domotz-remote-pawn

Version:

Domotz Agent

156 lines (136 loc) 6.41 kB
/** This file is part of Domotz Agent. * Copyright (C) 2016 Domotz Ltd * * Domotz Agent is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Domotz Agent is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Domotz Agent. If not, see <http://www.gnu.org/licenses/>. **/ /** * * Created by Andrea Azzarà <a.azzara@domotz.com> on 10/01/17. */ const DEFAULT_IF_NAME = 'eth0'; var ifConfig = function (message, resourceLocator) { var myConsole = resourceLocator.log.decorateLogs(); var isUbuntuCore = process.env.DPLATFORM === 'ubuntu_core'; var missingParams = { error: 'Missing parameters' }; var invalidParams = { error: 'Invalid parameters' }; var unsupportedFeature = { error: 'Unsupported Feature' }; var ip = resourceLocator.ip.Address4; var proc = resourceLocator.utils.proc; var file = resourceLocator.utils.file; var networkManager = resourceLocator.networkTools.networkManager; var settingsManager = resourceLocator.settingsManager; var versionFile = '/opt/domotz_tui/bin/VERSION'; var networkConfigBin = '/opt/domotz_tui/bin/domotz_network'; function getBigNetworkScan() { return settingsManager.getSetting('configuration.settings.big_networks', { enabled: false }); } return { execute: function (callback) { var payload = message.payload; if (!payload.type) { myConsole.error('Missing parameter type'); callback(null, missingParams); return; } function finalize(systemReboot) { if (isUbuntuCore && systemReboot) { setTimeout(networkManager.systemRestart, 3000); } else { setTimeout(proc.gracefulShutdown, 3000); } } if (payload.type === 'static') { if (!payload.ip_address || !payload.netmask || !payload.gateway || !payload.primary_dns || !payload.secondary_dns) { myConsole.error('Missing parameter type'); callback(null, missingParams); return; } var ipAddress = new ip(payload.ip_address); var gateway = new ip(payload.gateway); var primaryDns = new ip(payload.primary_dns); var secondaryDns = new ip(payload.secondary_dns); var netmask = new ip(payload.netmask); if (!ipAddress.isValid() || !netmask.isValid() || !gateway.isValid() || !primaryDns.isValid() || !secondaryDns.isValid()) { myConsole.error('One of the provided ip addresses is invalid'); callback(null, invalidParams); return; } var execution; if (isUbuntuCore) { callback(payload); execution = networkManager.static(ipAddress.address, netmask.address, gateway.address, primaryDns.address, secondaryDns.address); } else if (!file.getFileStats(networkConfigBin) || !file.getFileStats(versionFile)) { myConsole.warn('cannot stat %s or %s', networkConfigBin, versionFile); callback(null, unsupportedFeature); return; } else { execution = proc.exec( [networkConfigBin, ipAddress.address, netmask.address, gateway.address, primaryDns.address, secondaryDns.address].join(' ') ); } execution .then(function (p1, p2) { myConsole.debug('Execution finished, calling callback: %s, %s', p1, p2); if (!isUbuntuCore) { callback(payload); } finalize(true); }) .catch(function (error) { myConsole.error('Error applying static network config: %s', error); callback(null, error); }); } else if (payload.type === 'dhcp') { var execDHCP; if (isUbuntuCore) { callback(payload); execDHCP = networkManager.dhcp(); } else if (!file.getFileStats(networkConfigBin) || !file.getFileStats(versionFile)) { myConsole.warn('cannot stat %s or %s', networkConfigBin, versionFile); callback(null, unsupportedFeature); return; } else { execDHCP = proc.exec([networkConfigBin, 'dhcp'].join(' ')); } execDHCP .then(function () { myConsole.debug('execDHCP finished - shutting down'); if (!isUbuntuCore) { callback(payload); } finalize(); }) .catch(function (error) { myConsole.error('Error applying DHCP network config: %s', error); callback(null, error); }); } else { var bigNetworks = getBigNetworkScan(); if (isUbuntuCore) { callback({ dhcp: networkManager.hasDHCP(), big_networks: bigNetworks }); return; } if (networkManager.hasStaticIp(DEFAULT_IF_NAME)) { callback({ dhcp: false, big_networks: bigNetworks }); } else { callback({ dhcp: true, big_networks: bigNetworks }); } } }, describe: function () { return message.correlationId + ' - Network Config Message'; }, }; }; module.exports.command = ifConfig;