mecano
Version:
Common functions for system deployment.
132 lines (128 loc) • 3.22 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var file, fs;
module.exports = function(options, callback) {
var do_chkhash, do_dsthash, do_exists, do_move, do_remove_src, do_replace_dest, do_srchash;
options.log({
message: "Entering move",
level: 'DEBUG',
module: 'mecano/lib/move'
});
do_exists = function() {
options.log({
message: "Stat target",
level: 'DEBUG',
module: 'mecano/lib/move'
});
return fs.stat(options.ssh, options.target, function(err, stat) {
if ((err != null ? err.code : void 0) === 'ENOENT') {
return do_move();
}
if (err) {
return callback(err);
}
if (options.force) {
return do_replace_dest();
} else {
return do_srchash();
}
});
};
do_srchash = function() {
if (options.source_md5) {
return do_dsthash();
}
options.log({
message: "Get source md5",
level: 'DEBUG',
module: 'mecano/lib/move'
});
return file.hash(options.ssh, options.source, 'md5', function(err, hash) {
if (err) {
return callback(err);
}
options.log({
message: "Source md5 is \"hash\"",
level: 'INFO',
module: 'mecano/lib/move'
});
options.source_md5 = hash;
return do_dsthash();
});
};
do_dsthash = function() {
if (options.target_md5) {
return do_chkhash();
}
options.log({
message: "Get target md5",
level: 'DEBUG',
module: 'mecano/lib/move'
});
return file.hash(options.ssh, options.target, 'md5', function(err, hash) {
if (err) {
return callback(err);
}
options.log({
message: "Destination md5 is \"hash\"",
level: 'INFO',
module: 'mecano/lib/move'
});
options.target_md5 = hash;
return do_chkhash();
});
};
do_chkhash = function() {
if (options.source_md5 === options.target_md5) {
return do_remove_src();
} else {
return do_replace_dest();
}
};
do_replace_dest = (function(_this) {
return function() {
options.log({
message: "Remove " + options.target,
level: 'WARN',
module: 'mecano/lib/move'
});
return _this.remove({
target: options.target
}, function(err, removed) {
if (err) {
return callback(err);
}
return do_move();
});
};
})(this);
do_move = function() {
options.log({
message: "Rename " + options.source + " to " + options.target,
level: 'WARN',
module: 'mecano/lib/move'
});
return fs.rename(options.ssh, options.source, options.target, function(err) {
if (err) {
return callback(err);
}
return callback(null, true);
});
};
do_remove_src = (function(_this) {
return function() {
options.log({
message: "Remove " + options.source,
level: 'WARN',
module: 'mecano/lib/move'
});
return _this.remove({
target: options.source
}, function(err, removed) {
return callback(err);
});
};
})(this);
return do_exists();
};
fs = require('ssh2-fs');
file = require('../misc/file');