mecano
Version:
Common functions for system deployment.
161 lines (154 loc) • 4.24 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var file, fs, misc, path, ssh2fs;
module.exports = function(options) {
var algo, ref, ref1, source_stat, stage_target, status, target_stat;
options.log({
message: "Entering upload",
level: 'DEBUG',
module: 'mecano/lib/file/upload'
});
if (!options.source) {
throw Error("Required \"source\" option");
}
if (!options.target) {
throw Error("Required \"target\" option");
}
options.log({
message: "Source is \"" + options.source + "\"",
level: 'DEBUG',
module: 'mecano/lib/file/upload'
});
options.log({
message: "Destination is \"" + options.target + "\"",
level: 'DEBUG',
module: 'mecano/lib/file/upload'
});
status = false;
source_stat = null;
target_stat = null;
stage_target = options.target + "." + (Date.now()) + (Math.round(Math.random() * 1000));
if (options.md5 != null) {
if ((ref = typeof options.md5) !== 'string' && ref !== 'boolean') {
return callback(new Error("Invalid MD5 Hash:" + options.md5));
}
algo = 'md5';
} else if (options.sha1 != null) {
if ((ref1 = typeof options.sha1) !== 'string' && ref1 !== 'boolean') {
return callback(new Error("Invalid SHA-1 Hash:" + options.sha1));
}
algo = 'sha1';
} else {
algo = 'md5';
}
this.call(function(_, callback) {
return ssh2fs.stat(options.ssh, options.source, function(err, stat) {
if (err && err.code !== 'ENOENT') {
callback(err);
}
source_stat = stat;
return callback();
});
});
this.call(function(_, callback) {
return ssh2fs.stat(null, options.target, function(err, stat) {
if (err && err.code === 'ENOENT') {
return callback();
}
if (err) {
return callback(err);
}
if (stat.isFile()) {
target_stat = stat;
}
if (!stat.isDirectory()) {
return callback();
}
options.target = path.resolve(options.target, path.basename(options.source));
return ssh2fs.stat(null, options.target, function(err, stat) {
if (err && err.code === 'ENOENT') {
return callback();
}
if (err) {
return callback(err);
}
if (stat.isFile()) {
target_stat = stat;
}
if (stat.isFile()) {
return callback();
}
return callback(Error("Invalid target: " + options.target));
});
});
});
this.call({
handler: function(_, callback) {
if (!target_stat) {
return callback(null, true);
}
return file.compare_hash(options.ssh, options.source, null, options.target, algo, (function(_this) {
return function(err, match) {
return callback(err, !match);
};
})(this));
}
});
this.mkdir({
"if": function() {
return this.status(-1);
},
ssh: null,
target: path.dirname(stage_target)
});
this.call({
"if": function() {
return this.status(-2);
},
handler: function(_, callback) {
return ssh2fs.createReadStream(options.ssh, options.source, (function(_this) {
return function(err, rs) {
var ws;
if (err) {
return callback(err);
}
ws = fs.createWriteStream(stage_target);
return rs.pipe(ws).on('close', callback).on('error', callback);
};
})(this));
}
});
return this.call(function() {
this.move({
ssh: null,
"if": this.status(),
source: stage_target,
target: options.target
}, function(err, status) {
if (status) {
return options.log({
message: "Unstaged uploaded file",
level: 'INFO',
module: 'mecano/lib/file/upload'
});
}
});
this.chmod({
ssh: null,
target: options.target,
mode: options.mode,
"if": options.mode != null
});
return this.chown({
ssh: null,
target: options.target,
uid: options.uid,
gid: options.gid,
"if": (options.uid != null) || (options.gid != null)
});
});
};
fs = require('fs');
ssh2fs = require('ssh2-fs');
path = require('path');
misc = require('../misc');
file = require('../misc/file');