domotz-remote-pawn
Version:
Domotz Agent
171 lines (157 loc) • 7.36 kB
JavaScript
/** 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 MODULE_NAME = 'COMMAND - (vlan config) - ';
const MISSING_PARAMS = 'Missing parameters';
const INVALID_PARAMS = 'Invalid parameters';
const UNSUPPORTED_FEATURE = 'Unsupported Feature';
const VERSION_FILE = '/opt/domotz_tui/bin/VERSION';
const VLAN_CONFIG_BIN = '/opt/domotz_tui/bin/domotz_vlan';
const PROCESS_SHUTDOWN_DELAY = 3000;
function checkParameters(payload, list, onError, logger) {
for (var i = 0; i < list; i++) {
if (!payload[list[i]]) {
logger.error('%s: Missing parameter type', MODULE_NAME);
onError(null, MISSING_PARAMS);
throw new Error('Missing parameters');
}
}
}
var ifConfig = function (message, resourceLocator) {
var myConsole = resourceLocator.log.decorateLogs();
var Ip = resourceLocator.ip.Address4;
var proc = resourceLocator.utils.proc;
var file = resourceLocator.utils.file;
var networkManager = resourceLocator.networkTools.networkManager;
var isUbuntuCore = process.env.DPLATFORM === 'ubuntu_core';
return {
execute: function (callback) {
var payload = message.payload;
checkParameters(payload, ['type'], callback, myConsole);
function shutdownProcess(delay) {
myConsole.warn('Scheduling process shutdown in %s ms', delay);
setTimeout(proc.gracefulShutdown, delay);
}
function executeCommandUbuntuCore(callable, tag, ipAddress, netmask, shutdown, delay) {
'use strict';
callable(tag, ipAddress, netmask)
.then(function (res) {
callback(res);
if (shutdown == true) {
delay = typeof delay === 'undefined' ? PROCESS_SHUTDOWN_DELAY : delay;
shutdownProcess(delay);
}
})
.catch(function (err) {
callback(null, err);
});
}
function executeCommand(command) {
if (!file.getFileStats(VLAN_CONFIG_BIN) || !file.getFileStats(VERSION_FILE)) {
myConsole.warn('cannot stat %s or %s', VLAN_CONFIG_BIN, VERSION_FILE);
callback(null, UNSUPPORTED_FEATURE);
return;
}
proc.exec(command)
.then(function () {
callback(null);
shutdownProcess(PROCESS_SHUTDOWN_DELAY);
})
.catch(function (error) {
callback(null, error);
});
}
myConsole.verbose('%s - Running on %s', MODULE_NAME, isUbuntuCore ? 'Ubuntu' : '...');
switch (payload.type) {
case 'add':
if (!payload.tag || !payload.ip_address || !payload.netmask) {
myConsole.error('Missing parameters');
callback(null, MISSING_PARAMS);
return;
}
var ipAddress = new Ip(payload.ip_address);
var netmask = new Ip(payload.netmask);
if (!ipAddress.isValid() || !netmask.isValid()) {
myConsole.error('Invalid ip address %s %s', payload.ip_address, payload.netmask);
callback(null, INVALID_PARAMS);
return;
}
if (isUbuntuCore) {
executeCommandUbuntuCore(networkManager.vlan.add, payload.tag, ipAddress.address, netmask.address, true);
} else {
executeCommand([VLAN_CONFIG_BIN, payload.tag, ipAddress.address, netmask.address].join(' '));
}
break;
case 'del':
if (!payload.tag) {
myConsole.error('Missing parameter tag');
callback(null, MISSING_PARAMS);
return;
}
if (isUbuntuCore) {
executeCommandUbuntuCore(networkManager.vlan.delete, payload.tag, undefined, undefined, true);
} else {
executeCommand([VLAN_CONFIG_BIN, 'delete', payload.tag].join(' '));
}
break;
case 'reset':
if (isUbuntuCore) {
executeCommandUbuntuCore(networkManager.vlan.reset, undefined, undefined, undefined, true, 5000);
} else {
executeCommand([VLAN_CONFIG_BIN, 'default', '-y'].join(' '));
}
break;
case 'list':
if (isUbuntuCore) {
executeCommandUbuntuCore(networkManager.vlan.list);
} else {
proc.exec(VLAN_CONFIG_BIN)
.then(function (output) {
var splitted = output.toString().split(/\n/);
if (splitted.length === 1) {
callback([]);
return;
}
var vlans = [];
for (var i = 1; i < splitted.length; i++) {
var vlan = splitted[i].split(/ +/);
vlans.push({
tag: vlan[0],
ip_address: vlan[1],
netmask: vlan[3],
});
}
callback(vlans);
})
.catch(function (error) {
myConsole.error('Unable to retrieve current vlan config %s', error);
callback(null, error);
});
}
break;
default:
myConsole.error('invalid command %s', payload.type);
callback(null, INVALID_PARAMS);
}
},
describe: function () {
return message.correlationId + ' - VLAN Config Message';
},
};
};
module.exports.command = ifConfig;