Windshaft-cartodb/tools/reset_styles

106 lines
2.9 KiB
Plaintext
Raw Normal View History

#!/usr/bin/env node
/*
This scripts drops all extended map_style keys in redis and regenerates
the XML caches in all the base ones to target the configured mapnik_version.
Optionally (with --convert) it also re-writes the CartoCSS if needed
to target the configured mapnik_version.
It is recommended to make a backup of the redis database before using
this script.
*/
var path = require('path');
// Reset all styles in the store
var grainstore = require('../node_modules/grainstore/lib/grainstore');
var mapnik = require('mapnik');
var redis = require('redis');
function usage(me, exitcode) {
console.log("Usage: " + me + " [--convert] <environment>");
process.exit(exitcode);
}
var doConvert = false;
var node_path = process.argv.shift();
var script_path = process.argv.shift();
var me = path.basename(script_path);
var ENV;
var arg;
while ( arg = process.argv.shift() ) {
if ( arg == '--convert' ) {
doConvert = true;
}
else if ( ! ENV ) {
ENV = arg;
}
else {
usage(me, 1);
}
}
if ( ! ENV ) usage(me, 1);
global.environment = require('../config/environments/' + ENV);
var serverOptions = require('../lib/cartodb/server_options'); // _after_ setting global.environment
var MAPNIK_VERSION = global.environment.mapnik_version || mapnik.versions.mapnik;
console.log( (doConvert ? "Converting" : "Resetting" ) + ' all styles to target ' + MAPNIK_VERSION);
var dbnum = 0;
var mml_store = new grainstore.MMLStore(serverOptions.redis, serverOptions.grainstore);
var failures = [];
var client = redis.createClient(serverOptions.redis.port, serverOptions.redis.host);
client.on('connect', function() {
client.select(dbnum);
client.keys('map_style|*', function(err, matches) {
2012-09-21 18:58:34 +08:00
processNext = function() {
if ( ! matches.length ) process.exit(failures.length);
2012-09-21 18:58:34 +08:00
var k = matches.shift();
if ( /map_style\|.*\|.*\|/.test(k) ) {
2012-11-26 19:51:55 +08:00
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/58
//console.warn("Key " + k + " is EXTENDED, dropping");
client.del(k, function(err) {
if ( err ) console.warn("Error dropping key " + k);
processNext();
});
2012-11-26 19:51:55 +08:00
return;
}
var params = RegExp(/map_style\|(.*)\|(.*)/).exec(k);
var db = params[1];
var tab = params[2];
var out = 'map_style|' + db + '|' + tab + ': ';
var mml_builder = mml_store.mml_builder({dbname:db, table:tab},
function(err, payload) {
if ( err ) { console.warn(out + err.message); failures.push(k); processNext(); }
else {
mml_builder.resetStyle(function(err, data) {
if ( err ) { console.warn(out + err.message); failures.push(k); }
else console.log(out + 'OK' + ( doConvert ? ' (converted)' : '' ));
processNext();
}, doConvert);
}
});
};
2012-09-21 18:58:34 +08:00
processNext();
});
});