UNPKG

mecano

Version:

Common functions for system deployment.

141 lines (132 loc) 4.21 kB
// Generated by CoffeeScript 1.7.1 var child, conditions, each, fs, misc, mkdir, path; fs = require('ssh2-fs'); path = require('path'); each = require('each'); misc = require('./misc'); conditions = require('./misc/conditions'); child = require('./misc/child'); 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 exec_create, exec_exists, linked, sym_create, sym_exists; if (err) { return finish(err); } linked = 0; sym_exists = function(options, callback) { return fs.exists(options.ssh, options.destination, function(err, exists) { if (!exists) { return callback(null, false); } return fs.readlink(options.ssh, options.destination, function(err, resolvedPath) { if (err) { return callback(err); } if (resolvedPath === options.source) { return callback(null, true); } return fs.unlink(options.ssh, options.destination, function(err) { if (err) { return callback(err); } return callback(null, false); }); }); }); }; sym_create = function(options, callback) { return fs.symlink(options.ssh, options.source, options.destination, function(err) { if (err) { return callback(err); } linked++; return callback(); }); }; exec_exists = function(options, callback) { return fs.exists(options.ssh, options.destination, function(err, exists) { if (!exists) { return callback(null, false); } return fs.readFile(options.ssh, options.destination, 'utf8', function(err, content) { var exec_cmd; if (err) { return callback(err); } exec_cmd = /exec (.*) \$@/.exec(content)[1]; return callback(null, exec_cmd && exec_cmd === options.source); }); }); }; exec_create = function(options, callback) { var content; content = "#!/bin/bash\nexec " + options.source + " $@"; return fs.writeFile(options.ssh, options.destination, content, function(err) { if (err) { return callback(err); } return fs.chmod(options.ssh, options.destination, options.mode, function(err) { if (err) { return callback(err); } linked++; return callback(); }); }); }; return each(options).parallel(goptions.parallel).on('item', function(options, next) { var do_dispatch, do_mkdir; if (!options.source) { return next(new Error("Missing source, got " + (JSON.stringify(options.source)))); } if (!options.destination) { return next(new Error("Missing destination, got " + (JSON.stringify(options.destination)))); } if (options.mode == null) { options.mode = 0x1ed; } do_mkdir = function() { return mkdir({ ssh: options.ssh, destination: path.dirname(options.destination) }, function(err, created) { if (err && err.code !== 'EEXIST') { return callback(err); } return do_dispatch(); }); }; do_dispatch = function() { if (options.exec) { return exec_exists(options, function(err, exists) { if (exists) { return next(); } return exec_create(options, next); }); } else { return sym_exists(options, function(err, exists) { if (exists) { return next(); } return sym_create(options, next); }); } }; return do_mkdir(); }).on('both', function(err) { return callback(err, linked); }); }); return result; };