Replaced python script to fix filenames by internal background job.

This commit is contained in:
baldo 2016-06-21 15:21:08 +02:00
parent ce155951bf
commit d24e6e6307
6 changed files with 76 additions and 93 deletions

View file

@ -374,12 +374,6 @@ module.exports = function (grunt) {
'README.md' 'README.md'
] ]
}, },
{
expand: true,
cwd: 'bin',
dest: '<%= yeoman.dist %>/bin',
src: ['*']
},
{ {
expand: true, expand: true,
cwd: 'admin', cwd: 'admin',

View file

@ -1,74 +0,0 @@
#!/usr/bin/env python2
import os
import sys
if len(sys.argv) != 2:
print('usage: ' + sys.argv[0] + ' /path/to/peers')
sys.exit(1)
peersDir = sys.argv[1]
def normalizeMac(mac):
mac = mac.lower()
normalized = ''
n = 0
for c in mac:
if c != ':':
if n > 0 and n % 2 == 0:
normalized = normalized + ':'
normalized = normalized + c
n += 1
return normalized
def toFilename(peer):
filename = ''
for field in ['name', 'mac', 'vpn', 'token', 'monitoring-token']:
if peer.has_key(field):
filename = filename + peer[field]
filename = filename + '@'
return filename[:-1]
for filename in os.listdir(peersDir):
if len(filename) == 0 or filename[0] == '.':
continue
absFilename = peersDir + '/' + filename
if os.path.isfile(absFilename):
peerFile = open(absFilename, 'r')
try:
peerLines = peerFile.readlines()
peer = {}
mac = None
for line in peerLines:
parts = line.split()
if len(parts) > 0:
for i in range(0, 3 - len(parts)):
parts.append('')
if parts[1] == 'Knotenname:':
peer['name'] = parts[2].lower()
elif parts[1] == 'MAC:':
peer['mac'] = normalizeMac(parts[2])
elif parts[1] == 'Token:':
peer['token'] = parts[2].lower()
elif parts[1] == 'Monitoring-Token:':
peer['monitoring-token'] = parts[2].lower()
elif parts[0] == 'key':
peer['vpn'] = parts[1].split('"')[1].lower()
newFilename = toFilename(peer)
if filename != newFilename:
os.rename(absFilename, peersDir + '/' + newFilename)
except Exception as e:
print('Error in %s, ignoring peer: %s' % (absFilename, e));
finally:
peerFile.close()

View file

@ -20,8 +20,7 @@
}, },
"bin": { "bin": {
"ffffng": "server/main.js", "ffffng": "server/main.js"
"ffffng-fix-key-file-names": "bin/fix_key_file_names.py"
}, },
"dependencies": { "dependencies": {

View file

@ -0,0 +1,17 @@
'use strict';
angular.module('ffffng').factory('FixNodeFilenamesJob', function (NodeService, Logger) {
return {
description: 'Makes sure node files (holding fastd key, name, etc.) are correctly named.',
run: function (callback) {
NodeService.fixNodeFilenames(function (err) {
if (err) {
Logger.tag('nodes', 'fix-filenames').error('Error fixing filenames:', err);
}
callback();
});
}
};
});

View file

@ -69,6 +69,7 @@ angular.module('ffffng').factory('Scheduler', function ($injector, Logger, confi
try { try {
schedule('0 */1 * * * *', 'MailQueueJob'); schedule('0 */1 * * * *', 'MailQueueJob');
schedule('15 */1 * * * *', 'FixNodeFilenamesJob');
if (config.client.monitoring.enabled) { if (config.client.monitoring.enabled) {
schedule('30 */15 * * * *', 'NodeInformationRetrievalJob'); schedule('30 */15 * * * *', 'NodeInformationRetrievalJob');

View file

@ -44,6 +44,14 @@ angular.module('ffffng')
return glob.sync(config.server.peersPath + '/' + pattern.toLowerCase()); return glob.sync(config.server.peersPath + '/' + pattern.toLowerCase());
} }
function findFilesInPeersPath() {
var files = glob.sync(config.server.peersPath + '/*');
return _.filter(files, function (file) {
return file[0] !== '.' && fs.lstatSync(file).isFile();
});
}
function parseNodeFilename(filename) { function parseNodeFilename(filename) {
var parts = _.split(filename, '@', filenameParts.length); var parts = _.split(filename, '@', filenameParts.length);
var parsed = {}; var parsed = {};
@ -88,9 +96,8 @@ angular.module('ffffng')
return null; return null;
} }
function writeNodeFile(isUpdate, token, node, nodeSecrets, callback) { function toNodeFilename(token, node, nodeSecrets) {
var filename = return config.server.peersPath + '/' +
config.server.peersPath + '/' +
( (
node.hostname + '@' + node.hostname + '@' +
node.mac + '@' + node.mac + '@' +
@ -98,7 +105,10 @@ angular.module('ffffng')
token + '@' + token + '@' +
(nodeSecrets.monitoringToken || '') (nodeSecrets.monitoringToken || '')
).toLowerCase(); ).toLowerCase();
}
function writeNodeFile(isUpdate, token, node, nodeSecrets, callback) {
var filename = toNodeFilename(token, node, nodeSecrets);
var data = ''; var data = '';
_.each(linePrefixes, function (prefix, key) { _.each(linePrefixes, function (prefix, key) {
var value; var value;
@ -401,6 +411,42 @@ angular.module('ffffng')
getNodeDataByMonitoringToken: function (monitoringToken, callback) { getNodeDataByMonitoringToken: function (monitoringToken, callback) {
return getNodeDataByFilePattern({ monitoringToken: monitoringToken }, callback); return getNodeDataByFilePattern({ monitoringToken: monitoringToken }, callback);
},
fixNodeFilenames: function (callback) {
var files = findFilesInPeersPath();
async.mapLimit(
files,
MAX_PARALLEL_NODES_PARSING,
function (file, fileCallback) {
parseNodeFile(file, function (err, node, nodeSecrets) {
if (err) {
return fileCallback(err);
}
var expectedFilename = toNodeFilename(node.token, node, nodeSecrets);
if (file !== expectedFilename) {
try {
fs.renameSync(file, expectedFilename);
} catch (e) {
return fileCallback(new Error(
'Cannot rename file ' + file + ' to ' + expectedFilename + ' => ' + e
));
}
}
fileCallback(null);
});
},
function (err) {
if (err) {
return callback(err);
}
return callback(null);
}
);
} }
}; };
}); });