mecano
Version:
Common functions for system deployment.
113 lines (109 loc) • 3.34 kB
JavaScript
// Generated by CoffeeScript 1.11.1
var fs, path;
module.exports = function(options, callback) {
var creates, ext, extract, format, ref, stat, success, tar_opts, target;
options.log({
message: "Entering extract",
level: 'DEBUG',
module: 'mecano/lib/extract'
});
if (!options.source) {
return callback(new Error("Missing source: " + options.source));
}
target = (ref = options.target) != null ? ref : path.dirname(options.source);
tar_opts = [];
if (options.preserve_owner === true) {
tar_opts.push('--same-owner');
} else if (options.preserve_owner === false) {
tar_opts.push('--no-same-owner');
}
if (options.preserve_permissions === true) {
tar_opts.push('-p');
} else if (options.preserve_permissions === false) {
tar_opts.push('--no-same-permissions');
}
if (typeof options.strip === 'number') {
tar_opts.push("--strip-components " + options.strip);
}
if (options.format != null) {
format = options.format;
} else {
if (/\.(tar\.gz|tgz)$/.test(options.source)) {
format = 'tgz';
} else if (/\.tar$/.test(options.source)) {
format = 'tar';
} else if (/\.zip$/.test(options.source)) {
format = 'zip';
} else if (/\.tar\.bz2$/.test(options.source)) {
format = 'bz2';
} else if (/\.tar\.xz$/.test(options.source)) {
format = 'xz';
} else {
ext = path.extname(options.source);
return callback(Error("Unsupported extension, got " + (JSON.stringify(ext))));
}
}
stat = function() {
return fs.stat(options.ssh, options.source, function(err, stat) {
if (err) {
return callback(Error("File does not exist: " + options.source));
}
if (!stat.isFile()) {
return callback(Error("Not a File: " + options.source));
}
return extract();
});
};
extract = (function(_this) {
return function() {
var cmd;
cmd = null;
options.log({
message: "Format is " + format,
level: 'DEBUG',
module: 'mecano/lib/extract'
});
switch (format) {
case 'tgz':
cmd = "tar xzf " + options.source + " -C " + target + " " + (tar_opts.join(' '));
break;
case 'tar':
cmd = "tar xf " + options.source + " -C " + target + " " + (tar_opts.join(' '));
break;
case 'bz2':
cmd = "tar xjf " + options.source + " -C " + target + " " + (tar_opts.join(' '));
break;
case 'xz':
cmd = "tar xJf " + options.source + " -C " + target + " " + (tar_opts.join(' '));
break;
case 'zip':
cmd = "unzip -u " + options.source + " -d " + target;
}
return _this.execute({
cmd: cmd
}, function(err, created) {
if (err) {
return callback(err);
}
return creates();
});
};
})(this);
creates = function() {
if (options.creates == null) {
return success();
}
return fs.exists(options.ssh, options.creates, function(err, exists) {
if (!exists) {
return callback(new Error("Failed to create '" + (path.basename(options.creates)) + "'"));
}
return success();
});
};
success = function() {
return callback(null, true);
};
return stat();
};
fs = require('ssh2-fs');
path = require('path');