UNPKG

mecano

Version:

Common functions for system deployment.

148 lines (145 loc) 4.72 kB
// Generated by CoffeeScript 1.11.1 var db; module.exports = function(options) { var cmd_database_create, cmd_database_exists, i, k, len, ref, ref1, ref2, results, user, v; if (options.db == null) { options.db = {}; } ref = options.db; for (k in ref) { v = ref[k]; if (options[k] == null) { options[k] = v; } } if (options.database == null) { options.database = options.argument; } if (!options.host) { throw Error('Missing option: "host"'); } if (!options.admin_username) { throw Error('Missing option: "admin_username"'); } if (!options.admin_password) { throw Error('Missing option: "admin_password"'); } if (!options.database) { throw Error('Missing option: "database"'); } if (!options.engine) { throw Error('Missing option: "engine"'); } if (options.user == null) { options.user = []; } if (typeof options.user === 'string') { options.user = [options.user]; } options.engine = options.engine.toLowerCase(); if ((ref1 = options.engine) !== 'mysql' && ref1 !== 'postgres') { throw Error("Unsupport engine: " + (JSON.stringify(options.engine))); } options.log({ message: "Database engine set to " + options.engine, level: 'INFO', module: 'mecano/db/database' }); if (options.port == null) { options.port = 5432; } options.log({ message: "Check if database " + options.database + " exists", level: 'DEBUG', module: 'mecano/db/database' }); switch (options.engine) { case 'mysql': cmd_database_create = db.cmd(options, { database: null }, "CREATE DATABASE " + options.database + ";"); cmd_database_exists = db.cmd(options, { database: options.database }, "USE " + options.database + ";"); break; case 'postgres': cmd_database_create = db.cmd(options, { database: null }, "CREATE DATABASE " + options.database + ";"); cmd_database_exists = db.cmd(options, { database: options.database }, "\\dt"); } this.execute({ cmd: cmd_database_create, unless_exec: cmd_database_exists }, function(err, status) { if (status) { return options.log({ message: "Database created: " + (JSON.stringify(options.database)), level: 'WARN', module: 'mecano/db/database' }); } }); ref2 = options.user; results = []; for (i = 0, len = ref2.length; i < len; i++) { user = ref2[i]; results.push((function(_this) { return function() { var cmd_grant_privileges, cmd_has_privileges; _this.call(function() { return options.log({ message: "Check if user " + user + " has PRIVILEGES on " + options.database + " ", level: 'DEBUG', module: 'mecano/db/database' }); }); _this.db.user.exists({ engine: options.engine, username: user, admin_username: options.admin_username, admin_password: options.admin_password, port: options.port, host: options.host }, function(err, exists) { if (!err && !exists) { throw Error("DB user does not exists: " + user); } }); switch (options.engine) { case 'mysql': cmd_grant_privileges = db.cmd(options, { database: "" + options.database }, "GRANT ALL PRIVILEGES ON " + options.database + ".* TO '" + user + "';"); cmd_has_privileges = db.cmd(options, { database: 'mysql' }, "SELECT user FROM db WHERE db='" + options.database + "';") + (" | grep '" + user + "'"); break; case 'postgres': cmd_grant_privileges = db.cmd(options, { database: options.database }, "GRANT ALL PRIVILEGES ON DATABASE " + options.database + " TO " + user); cmd_has_privileges = db.cmd(options, { database: options.database }, "\\l") + (" | egrep '^" + user + "='"); } return _this.execute({ cmd: "if " + cmd_has_privileges + "; then\n echo '[INFO] User already with privileges'\n exit 3\nfi\necho '[WARN] User privileges granted'\n" + cmd_grant_privileges, code_skipped: 3 }, function(err, status, stdout, stderr) { if (status) { return options.log({ message: "Privileges granted: to " + (JSON.stringify(user)) + " on " + (JSON.stringify(options.database)), level: 'WARN', module: 'mecano/db/database' }); } }); }; })(this)()); } return results; }; db = require('../../misc/db');