mecano
Version:
Common functions for system deployment.
67 lines (64 loc) • 2.83 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var db;
module.exports = function(options) {
var cmd_password_change, cmd_password_is_invalid, cmd_user_create, cmd_user_exists, k, ref, ref1, 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.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.username) {
throw Error('Missing option: "username"');
}
if (!options.password) {
throw Error('Missing option: "password"');
}
if (!options.engine) {
throw Error('Missing option: "engine"');
}
options.engine = options.engine.toLowerCase();
if ((ref1 = options.engine) !== 'mysql' && ref1 !== 'postgres') {
throw Error("Unsupport engine: " + (JSON.stringify(options.engine)));
}
if (options.port == null) {
options.port = 5432;
}
switch (options.engine) {
case 'mysql':
cmd_user_exists = db.cmd(options, "SELECT User FROM mysql.user WHERE User='" + options.username + "'") + (" | grep " + options.username);
cmd_user_create = db.cmd(options, "CREATE USER " + options.username + " IDENTIFIED BY '" + options.password + "';");
cmd_password_is_invalid = db.cmd(options, {
admin_username: null,
admin_password: null
}, '\\dt') + " 2>&1 >/dev/null | grep -e '^ERROR 1045.*'";
cmd_password_change = db.cmd(options, "SET PASSWORD FOR " + options.username + " = PASSWORD ('" + options.password + "');");
break;
case 'postgres':
cmd_user_exists = db.cmd(options, "SELECT 1 FROM pg_roles WHERE rolname='" + options.username + "'") + " | grep 1";
cmd_user_create = db.cmd(options, "CREATE USER " + options.username + " WITH PASSWORD '" + options.password + "';");
cmd_password_is_invalid = db.cmd(options, {
admin_username: null,
admin_password: null
}, '\\dt') + " 2>&1 >/dev/null | grep -e '^psql:\\sFATAL.*password\\sauthentication\\sfailed\\sfor\\suser.*'";
cmd_password_change = db.cmd(options, "ALTER USER " + options.username + " WITH PASSWORD '" + options.password + "';");
}
return this.execute({
cmd: "signal=3\nif " + cmd_user_exists + "; then\n echo '[INFO] User already exists'\nelse\n " + cmd_user_create + "\n echo '[WARN] User created'\n signal=0\nfi\nif [ $signal -eq 3 ]; then\n if ! " + cmd_password_is_invalid + "; then\n echo '[INFO] Password not modified'\n else\n " + cmd_password_change + "\n echo '[WARN] Password modified'\n signal=0\n fi\nfi\nexit $signal",
code_skipped: 3
});
};
db = require('../../misc/db');