Skip to content

Instantly share code, notes, and snippets.

@ytjohn
Forked from dalebremner/README.md
Last active July 22, 2016 19:55
Show Gist options
  • Save ytjohn/77e13ef159943347520bfc52c89f3cfb to your computer and use it in GitHub Desktop.
Save ytjohn/77e13ef159943347520bfc52c89f3cfb to your computer and use it in GitHub Desktop.
OBM Settings Migration Script

Migrating RackHD OBM Settings to 2.0 OBM Model

Setup

clone the GIST containing the OBM migration script
cd to the cloned directory
npm install

Running the migration script

mongodump
mv dump dump.save
node obmMigration.js

NOTE: All mongo databases are backed up into the ./dump directory on startup. Use mongorestore to restore the database from backup.

NOTE: This variation has the actual deletion of node.obmSettings commented out. That way you can copy the obm settings before the upgrade with no impact to the running system. After the upgrade, you should uncomment line 72 and run it to delete the node.obmSettings.

var di = require('di');
var core = require('on-core')(di);
var injector = new di.Injector(core.injectables);
var waterline = injector.get('Services.Waterline');
var waterlineProtocol = injector.get('Protocol.Waterline');
var _ = require('lodash');
var Promise = injector.get('Promise');
var exec = require('child_process').exec;
var encryption = injector.get('Services.Encryption');
// Override waterline message publish with no-op.
waterlineProtocol.publishRecord = function () {
return Promise.resolve();
};
exec('mongodump', function(error, stdout, stderr) { // Backup mongo
encryption.start()
.then (function(){
return waterline.start();
})
.then(function () {
return waterline.obms.setIndexes();
})
.then(function () {
// Get node documents.
// Use native mongo, since new node model doesn't include old obm settings.
return waterline.nodes.findMongo();
})
.then(function (nodeDocuments) {
// Save OBM settings using OBM model.
var obmSavesToBeDone = [];
_.forEach(nodeDocuments, function (thisNode) {
var nodeId = thisNode._id.toString();
console.log(nodeId);
var obmSettingsList = thisNode.obmSettings;
if (obmSettingsList) {
_.forEach(obmSettingsList, function (obmSettings) {
console.log('Saving: ' + nodeId + ' ' + JSON.stringify(obmSettings));
obmSave = waterline.obms.upsertByNode(nodeId, obmSettings)
.catch(function (err) {
console.log('Error saving OBM record: ' + err.message)
});
obmSavesToBeDone.push(obmSave);
});
}
});
return Promise.all(obmSavesToBeDone);
})
.then(function () {
// Delete OBM settings from all node documents.
console.log('Removing node OBM settings...');
var query = {
obmSettings: {
$exists: true
}
};
var update = {
$set: {
updatedAt: new Date()
},
$unset: {
obmSettings: ""
}
};
var options = {
multi: true
};
// uncomment the below line when you are ready to delete
// return waterline.nodes.runNativeMongo('update', [query, update, options]);
return true;
})
.then(function () {
waterline.stop();
});
});
{
"name": "obmMigration",
"version": "0.0.0",
"description": "OBM settings migration script",
"main": "obmMigration.js",
"author": "",
"dependencies": {
"acl": "~0.4.9",
"body-parser": "^1.10.1",
"cors": "^2.5.2",
"debug": "~2.2.0",
"di": "git+https://github.com/RackHD/di.js.git",
"ejs": "^2.0.8",
"es6-shim": "^0.22.2",
"express": "^4.10.7",
"glob": "^4.3.2",
"http-proxy-middleware": "~0.11.0",
"ip": "~1.1.2",
"jsonwebtoken": "^5.4.4",
"lodash": "^3.10.1",
"moment": "~2.11.1",
"mongodb": "^1.4.28",
"multer": "~1.1.0",
"node-ssdp": "~2.7.0",
"on-core": "git+https://github.com/RackHD/on-core.git",
"on-finished": "^2.2.0",
"on-tasks": "git+https://github.com/RackHD/on-tasks.git",
"os-tmpdir": "~1.0.1",
"passport": "^0.3.2",
"passport-anonymous": "~1.0.1",
"passport-http": "~0.3.0",
"passport-jwt": "~2.0.0",
"passport-local": "^1.0.0",
"pluralize": "~1.1.2",
"rimraf": "~2.5.0",
"swagger-express-mw": "^0.6.0",
"swagger-tools": "~0.9.11",
"tar": "~2.2.1",
"tv4": "~1.2.7",
"url-parse": "~1.1.1",
"validator": "^5.2.0",
"ws": "^0.8.0",
"fs-extra": "~0.30.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment