mecano
Version:
Common functions for system deployment.
187 lines (174 loc) • 4.59 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var misc, ssh2fs, string;
module.exports = function(options, callback) {
var do_gid, do_uid;
do_uid = function() {
if (options.uid == null) {
return do_gid();
}
if (typeof options.uid === 'string' && /\d+/.test(options.uid)) {
options.uid = parseInt(options.uid, 10);
}
if (typeof options.uid === 'number') {
return do_gid();
}
return module.exports.passwd(options.ssh, options.store, options.uid, function(err, user) {
if (err) {
return do_gid(err);
}
if (user) {
options.uid = user.uid;
if (options.gid == null) {
options.gid = user.gid;
}
}
return do_gid();
});
};
do_gid = function() {
if (options.gid == null) {
return callback();
}
if (typeof options.gid === 'string' && /\d+/.test(options.gid)) {
options.gid = parseInt(options.gid, 10);
}
if (typeof options.gid === 'number') {
return callback();
}
return module.exports.group(options.ssh, options.store, options.gid, function(err, group) {
if (err) {
return callback(err);
}
if (group) {
options.gid = group.gid;
}
return callback();
});
};
return do_uid();
};
/*
passwd(ssh, [user], callback)
----------------------
Return information present in '/etc/passwd' and cache the
result in the provided ssh instance as "passwd".
Result look like:
{ root: {
uid: '0',
gid: '0',
comment: 'root',
home: '/root',
shell: '/bin/bash' }, ... }
*/
module.exports.passwd = function(ssh, store, username, callback) {
if (arguments.length === 4) {
if (!username) {
return callback(null, null);
}
if (store.cache_passwd && store.cache_passwd[username]) {
return callback(null, store.cache_passwd[username]);
}
store.cache_passwd = null;
return module.exports.passwd(ssh, store, function(err, users) {
var user;
if (err) {
return callback(err);
}
user = users[username];
return callback(null, user);
});
}
callback = username;
username = null;
if (store.cache_passwd) {
return callback(null, store.cache_passwd);
}
return ssh2fs.readFile(ssh, '/etc/passwd', 'ascii', function(err, lines) {
var i, info, len, line, passwd, ref;
if (err) {
return callback(err);
}
passwd = [];
ref = string.lines(lines);
for (i = 0, len = ref.length; i < len; i++) {
line = ref[i];
info = /(.*)\:\w\:(.*)\:(.*)\:(.*)\:(.*)\:(.*)/.exec(line);
if (!info) {
continue;
}
passwd[info[1]] = {
uid: parseInt(info[2]),
gid: parseInt(info[3]),
comment: info[4],
home: info[5],
shell: info[6]
};
}
store.cache_passwd = passwd;
return callback(null, passwd);
});
};
/*
group(ssh, [group], callback)
----------------------
Return information present in '/etc/group' and cache the
result in the provided ssh instance as "group".
Result look like:
{ root: {
password: 'x'
gid: 0,
user_list: [] },
bin: {
password: 'x',
gid: 1,
user_list: ['bin','daemon'] } }
*/
module.exports.group = function(ssh, store, group, callback) {
if (arguments.length === 4) {
if (!group) {
return callback(null, null);
}
if (store.cache_group && store.cache_group[group]) {
return callback(null, store.cache_group[group]);
}
store.cache_group = null;
return module.exports.group(ssh, store, function(err, groups) {
var gid;
if (err) {
return callback(err);
}
gid = groups[group];
return callback(null, gid);
});
}
callback = group;
group = null;
if (store.cache_group) {
return callback(null, store.cache_group);
}
return ssh2fs.readFile(ssh, '/etc/group', 'ascii', function(err, lines) {
var i, info, len, line, ref;
if (err) {
return callback(err);
}
group = [];
ref = string.lines(lines);
for (i = 0, len = ref.length; i < len; i++) {
line = ref[i];
info = /(.*)\:(.*)\:(.*)\:(.*)/.exec(line);
if (!info) {
continue;
}
group[info[1]] = {
password: info[2],
gid: parseInt(info[3]),
user_list: info[4] ? info[4].split(',') : []
};
}
store.cache_group = group;
return callback(null, group);
});
};
misc = require('./index');
ssh2fs = require('ssh2-fs');
string = require('./string');