UNPKG

mecano

Version:

Common functions for system deployment.

213 lines (208 loc) 6.41 kB
// Generated by CoffeeScript 1.9.1 var fs, misc, path; module.exports = function(options, callback) { var do_chown_chmod, do_dest_checksum, do_end, do_md5, do_sha1, do_src_checksum, do_stat, do_upload, get_checksum, uploaded; if (!options.source) { return callback(Error("Required \"source\" option")); } if (!options.destination) { return callback(Error("Required \"destination\" option")); } if (typeof options.log === "function") { options.log("Mecano `upload`: source is \"" + options.source + "\""); } if (typeof options.log === "function") { options.log("Mecano `upload`: destination is \"" + options.destination + "\""); } if (!options.binary) { options = misc.merge(options, { local_source: true }); return this.write(options, function(err, written) { return callback(err, written); }); } uploaded = false; if (typeof options.log === "function") { options.log("Mecano `upload`: enter binary mode"); } get_checksum = (function(_this) { return function(ssh, path, algorithm, callback) { if (ssh) { return _this.execute({ cmd: "openssl " + algorithm + " " + path }, function(err, executed, stdout, stderr) { if (err) { return callback(err); } return callback(null, /[ ](.*)$/.exec(stdout.trim())[1]); }); } else { return misc.file.hash(null, path, algorithm, callback); } }; })(this); do_src_checksum = function() { var algorithm; if (!(options.md5 === true || options.sha1 === true)) { return do_stat(); } algorithm = options.md5 ? 'md5' : 'sha1'; if (typeof options.log === "function") { options.log("Mecano `upload`: get source " + algorithm + " checksum"); } return get_checksum(null, options.source, algorithm, function(err, checksum) { if (err) { return callback(err); } options[algorithm] = checksum; if (typeof options.log === "function") { options.log("Mecano `upload`: " + algorithm + " checksum is \"" + checksum + "\""); } return do_stat(); }); }; do_stat = function() { if (typeof options.log === "function") { options.log("Mecano `upload`: check if remote destination exists"); } return fs.stat(options.ssh, options.destination, function(err, stat) { if ((err != null ? err.code : void 0) === 'ENOENT') { return do_upload(); } if (err) { return callback(err); } if (stat.isDirectory()) { options.destination = options.destination + "/" + (path.basename(options.source)); } return do_dest_checksum(); }); }; do_dest_checksum = function() { if (!(options.md5 || options.sha1)) { return do_upload(); } if (typeof options.log === "function") { options.log("Mecano `upload`: validate destination checksum, otherwise re-upload"); } switch (false) { case options.md5 == null: return get_checksum(options.ssh, options.destination, 'md5', function(err, md5) { if (err) { return callback(err); } if (md5 === options.md5) { return callback(); } else { return do_upload(); } }); case options.sha1 == null: return get_checksum(options.ssh, options.destination, 'sha1', function(err, sha1) { if (err) { return callback(err); } if (sha1 === options.sha1) { return callback(); } else { return do_upload(); } }); } }; do_upload = (function(_this) { return function() { if (typeof options.log === "function") { options.log("Mecano `upload`: write source"); } return _this.mkdir({ destination: "" + (path.dirname(options.destination)) }, function(err) { if (err) { return next(err); } return fs.createWriteStream(options.ssh, options.destination, function(err, ws) { if (err) { return callback(err); } return fs.createReadStream(null, options.source, function(err, rs) { if (err) { return callback(err); } return rs.pipe(ws).on('close', function() { uploaded = true; return do_md5(); }).on('error', callback); }); }); }); }; })(this); do_md5 = function() { if (!options.md5) { return do_sha1(); } if (typeof options.log === "function") { options.log("Mecano `upload`: check destination md5"); } return get_checksum(options.ssh, options.destination, 'md5', function(err, md5) { if (md5 !== options.md5) { return callback(new Error("Invalid md5 checksum")); } return do_sha1(); }); }; do_sha1 = function() { if (!options.sha1) { return do_chown_chmod(); } if (typeof options.log === "function") { options.log("Mecano `upload`: check destination sha1"); } return get_checksum(options.ssh, options.destination, 'sha1', function(err, sha1) { if (sha1 !== options.sha1) { return callback(new Error("Invalid sha1 checksum")); } return do_chown_chmod(); }); }; do_chown_chmod = (function(_this) { return function() { if (typeof options.log === "function") { options.log("Mecano `upload`: change ownership"); } return _this.chown({ ssh: options.ssh, destination: options.destination, uid: options.uid, gid: options.gid, "if": (options.uid != null) || (options.gid != null) }).chmod({ ssh: options.ssh, destination: options.destination, mode: options.mode, "if": options.mode != null }).then(function(err, status) { var modified; if (err) { return callback(err); } if (status) { modified = true; } return do_end(); }); }; })(this); do_end = function() { if (typeof options.log === "function") { options.log("Mecano `upload`: upload succeed"); } return callback(null, true); }; return do_src_checksum(); }; fs = require('ssh2-fs'); path = require('path'); misc = require('./misc');