domotz-remote-pawn
Version:
Domotz Agent
223 lines (202 loc) • 8.59 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/>.
*
*
* Restarts networking service
* Created by Andrea Azzarà <a.azzara@domotz.com> on 12/01/17.
*/
var configurationManager;
var ubuntuCore;
var networkConfiguration;
module.exports.factory = function (resourceLocator) {
'use strict';
var file = resourceLocator.utils.file;
var proc = resourceLocator.utils.proc;
var q = resourceLocator.q;
var myConsole = resourceLocator.log.decorateLogs();
var ipWrapper = resourceLocator.utils.ipWrapper;
var deprecated = function () {
throw new Error('Method not supported');
};
var restart = deprecated;
var hasStaticIP = null;
var systemRestart = function () {
myConsole.info('System restart is not enabled on non-ubuntu-core platforms');
};
if (process.env.DPLATFORM === 'ubuntu_core') {
ubuntuCore = require('../net/ubuntu_core');
configurationManager = ubuntuCore.configurationManager(resourceLocator);
networkConfiguration = configurationManager.loadConfiguration();
hasStaticIP = function () {
return !getDHCPConfiguration();
};
systemRestart = function () {
proc.spawn('dbus-send', [
'--system',
'--print-reply',
'--dest=org.freedesktop.login1',
'/org/freedesktop/login1',
'org.freedesktop.login1.Manager.Reboot',
'boolean:true',
]);
};
} else {
// @deprecated
restart = function () {
var restartNetworkingCommand = 'service networking restart';
var dhcpcdServiceCommand = 'service dhcpcd restart';
var ipFlushCommand = 'ip -4 addr flush label "eth0"';
var restartCommand = proc.exec(ipFlushCommand);
restartCommand
.then(function () {
myConsole.info('Successfully flushed ip addresses interfaces');
})
.catch(function (ipFlushError) {
myConsole.error('Error flushing ip addresses' + ipFlushError);
})
.finally(function () {
proc.exec(restartNetworkingCommand)
.then(function () {
myConsole.info('Successfully restarted network service');
})
.catch(function (netRestartError) {
myConsole.error('Error restarting network service' + netRestartError);
})
.finally(function () {
proc.exec(dhcpcdServiceCommand)
.then(function () {
myConsole.info('Successfully restarted dhcpcd service');
})
.catch(function (dhcpError) {
myConsole.error('Error restarting dhcpcd service' + dhcpError);
});
});
});
return restartCommand;
};
hasStaticIP = function (ifName) {
var eth0Interfaces = '/etc/network/interfaces.d/' + ifName;
var interfacesFile = '/etc/network/interfaces';
var source;
if (file.getFileStats(eth0Interfaces)) {
source = eth0Interfaces;
} else if (file.getFileStats(interfacesFile)) {
source = interfacesFile;
} else {
return false;
}
var content = file.readFile(source, null, false);
if (!content) {
return false;
}
var splittedLines = content.split('\n');
for (var i = 0; i < splittedLines.length; i++) {
if (splittedLines[i] && splittedLines[i].search(ifName) > -1 && splittedLines[i].search('static') > -1) {
return true;
}
}
return false;
};
}
function _updateAndValidateConfiguration(newConfiguration) {
return configurationManager
.backupConfiguration()
.then(function () {
return configurationManager.writeConfiguration(newConfiguration);
})
.then(function () {
var newConfiguration = configurationManager.loadConfiguration();
return ubuntuCore
.applier(networkConfiguration, resourceLocator, ipWrapper)
.apply(newConfiguration)
.then(function () {
networkConfiguration = newConfiguration;
return true; // for testing
});
})
.then(configurationManager.deleteBackup)
.catch(function (error) {
myConsole.error('Error: %s. Message: %s', error.code, error.message);
myConsole.error(error.stack);
myConsole.error('Reverting configuration');
return configurationManager
.restoreConfiguration()
.then(function () {
return ubuntuCore
.applier(networkConfiguration, resourceLocator, ipWrapper)
.apply(configurationManager.loadConfiguration(), false);
})
.then(function () {
throw error;
});
});
}
// Need to expose private parts in order to inspect and test them - I _REALLY_ love Javascript!
var _ = { updateAndValidateConfiguration: _updateAndValidateConfiguration };
function setStaticConfiguration(ip, mask, gateway, primaryDNS, secondaryDNS) {
myConsole.debug('Setting static network configuration: %s - %s, dn1:%s, dns2:%s', ip, mask, gateway, primaryDNS, secondaryDNS);
return _.updateAndValidateConfiguration(networkConfiguration.setStatic(ip, mask, gateway, primaryDNS, secondaryDNS));
}
function setDHCPConfiguration() {
myConsole.debug('Setting DHCP network configuration');
return _.updateAndValidateConfiguration(networkConfiguration.setDHCP());
}
function getDHCPConfiguration() {
return networkConfiguration.getDHCP();
}
function addVLAN(tag, ip, netmask) {
myConsole.info('ADD VLan. Tag %s, IP %s, Netmask %s', tag, ip, netmask);
return _.updateAndValidateConfiguration(networkConfiguration.addVLAN(tag, ip, netmask));
}
function deleteVLAN(tag) {
myConsole.debug('DELETE VLan. Tag %s', tag);
return _.updateAndValidateConfiguration(networkConfiguration.deleteVLAN(tag));
}
function resetDefault() {
myConsole.debug('RESETTING Default network configuration');
return _.updateAndValidateConfiguration(networkConfiguration.resetNetwork());
}
return {
// Legacy
restart: restart,
hasStaticIp: hasStaticIP,
// End legacy
static: setStaticConfiguration,
dhcp: setDHCPConfiguration,
hasDHCP: function () {
return getDHCPConfiguration() === true;
},
vlan: {
add: addVLAN,
delete: function (tag) {
return deleteVLAN(tag);
},
show: function (iface) {
return ipWrapper.call('address', 'show', { dev: iface });
},
reset: resetDefault,
list: function () {
var defer = q.defer();
defer.resolve(networkConfiguration.listVLANs());
return defer.promise;
},
},
ping: ipWrapper.ping,
systemRestart: systemRestart,
// Shame on me
_: _,
};
};