mecano
Version:
Common functions for system deployment.
244 lines (231 loc) • 7.22 kB
JavaScript
// Generated by CoffeeScript 1.7.1
var child, conditions, copy, each, execute, ldap, misc, mkdir, move, remove, write;
each = require('each');
ldap = require('ldapjs');
misc = require('./misc');
conditions = require('./misc/conditions');
child = require('./misc/child');
execute = require('./execute');
copy = require('./copy');
write = require('./write');
move = require('./move');
remove = require('./remove');
mkdir = require('./mkdir');
module.exports = function(goptions, options, callback) {
var finish, result, _ref;
_ref = misc.args(arguments), goptions = _ref[0], options = _ref[1], callback = _ref[2];
result = child();
finish = function(err, created) {
if (callback) {
callback(err, created);
}
return result.end(err, created);
};
misc.options(options, function(err, options) {
var modified;
if (err) {
return finish(err);
}
modified = 0;
return each(options).parallel(goptions.parallel).on('item', function(options, next) {
var binddn, conf, do_clean, do_configure, do_dir, do_generate, do_register, do_registered, do_rename, do_write, ldif, passwd, schema, tempdir, uri;
if (!options.name) {
return next(new Error("Missing name"));
}
if (!options.schema) {
return next(new Error("Missing schema"));
}
options.schema = options.schema.trim();
tempdir = options.tempdir || ("/tmp/mecano_ldap_schema_" + (Date.now()));
schema = "" + tempdir + "/" + options.name + ".schema";
conf = "" + tempdir + "/schema.conf";
ldif = "" + tempdir + "/ldif";
binddn = options.binddn ? "-D " + options.binddn : '';
passwd = options.passwd ? "-w " + options.passwd : '';
if (options.uri === true) {
options.uri = 'ldapi:///';
}
uri = options.uri ? "-H " + options.uri : '';
do_registered = function() {
var cmd;
cmd = "ldapsearch " + binddn + " " + passwd + " " + uri + " -b \"cn=schema,cn=config\" | grep -E cn=\\{[0-9]+\\}" + options.name + ",cn=schema,cn=config";
if (typeof options.log === "function") {
options.log("Check if schema is registered: " + cmd);
}
return execute({
cmd: cmd,
code: 0,
code_skipped: 1,
ssh: options.ssh,
log: options.log,
stdout: options.stdout,
stderr: options.stderr
}, function(err, registered, stdout) {
if (err) {
return next(err);
}
if (registered) {
return next();
}
return do_dir();
});
};
do_dir = function() {
if (typeof options.log === "function") {
options.log('Create ldif directory');
}
return mkdir({
destination: ldif,
ssh: options.ssh
}, function(err, executed) {
if (err) {
return next(err);
}
return do_write();
});
};
do_write = function() {
if (typeof options.log === "function") {
options.log('Copy schema');
}
return copy({
source: options.schema,
destination: schema,
ssh: options.ssh
}, function(err, copied) {
if (err) {
return next(err);
}
if (typeof options.log === "function") {
options.log('Prepare configuration');
}
return write({
content: "include " + schema,
destination: conf,
ssh: options.ssh
}, function(err) {
if (err) {
return next(err);
}
return do_generate();
});
});
};
do_generate = function() {
if (typeof options.log === "function") {
options.log('Generate configuration');
}
return execute({
cmd: "slaptest -f " + conf + " -F " + ldif,
ssh: options.ssh,
log: options.log,
stdout: options.stdout,
stderr: options.stderr
}, function(err, executed) {
if (err) {
return next(err);
}
return do_rename();
});
};
do_rename = function() {
if (typeof options.log === "function") {
options.log('Rename configuration');
}
return move({
source: "" + ldif + "/cn=config/cn=schema/cn={0}" + options.name + ".ldif",
destination: "" + ldif + "/cn=config/cn=schema/cn=" + options.name + ".ldif",
force: true,
ssh: options.ssh
}, function(err, moved) {
if (err) {
return next(err);
}
if (!moved) {
return new Error('No generated schema');
}
return do_configure();
});
};
do_configure = function() {
if (typeof options.log === "function") {
options.log('Prepare ldif');
}
return write({
destination: "" + ldif + "/cn=config/cn=schema/cn=" + options.name + ".ldif",
write: [
{
match: /^dn: cn.*$/mg,
replace: "dn: cn=" + options.name + ",cn=schema,cn=config"
}, {
match: /^cn: {\d+}(.*)$/mg,
replace: 'cn: $1'
}, {
match: /^structuralObjectClass.*/mg,
replace: ''
}, {
match: /^entryUUID.*/mg,
replace: ''
}, {
match: /^creatorsName.*/mg,
replace: ''
}, {
match: /^createTimestamp.*/mg,
replace: ''
}, {
match: /^entryCSN.*/mg,
replace: ''
}, {
match: /^modifiersName.*/mg,
replace: ''
}, {
match: /^modifyTimestamp.*/mg,
replace: ''
}
],
ssh: options.ssh
}, function(err, written) {
if (err) {
return next(err);
}
return do_register();
});
};
do_register = function() {
var cmd;
cmd = "ldapadd " + uri + " " + binddn + " " + passwd + " -f " + ldif + "/cn=config/cn=schema/cn=" + options.name + ".ldif";
if (typeof options.log === "function") {
options.log("Add schema: " + cmd);
}
return execute({
cmd: cmd,
ssh: options.ssh,
log: options.log,
stdout: options.stdout,
stderr: options.stderr
}, function(err, executed) {
if (err) {
return next(err);
}
modified++;
return do_clean();
});
};
do_clean = function() {
if (typeof options.log === "function") {
options.log('Clean up');
}
return remove({
destination: tempdir,
ssh: options.ssh
}, function(err, removed) {
return next(err);
});
};
return conditions.all(options, next, do_registered);
}).on('both', function(err) {
return finish(err, modified);
});
});
return result;
};