Commit de3ad00d authored by Giorgos Korfiatis's avatar Giorgos Korfiatis

Unify command execution across platforms

Use the env option of spawn/execFile to pass env variables.
Run commands with /usr/bin/env on unix and env.exe on cygwin. This
simplifies how we pass arguments.
parent 9bff8f1c
......@@ -45,8 +45,10 @@ if(process.platform == 'darwin') {
var DEFAULT_CERT = path.join(exec_path, 'cacert.pem');
var CYGWIN_BIN = path.join(exec_path, "cygwin", "bin");
var CYGWIN_BASH = path.join(CYGWIN_BIN, "bash.exe");
var CYGWIN_CYGPATH = path.join(CYGWIN_BIN, "cygpath.exe");
var CYGWIN_ENV = path.join(CYGWIN_BIN, "env.exe");
var ENV_CMD = (process.platform == 'win32') ? CYGWIN_ENV : "/usr/bin/env";
function get_user_home() {
return process.env[(process.platform == 'win32') ? 'USERPROFILE' : 'HOME'];
......@@ -86,6 +88,10 @@ function get_unix_exec_path(target) {
return path.join(exec_path, target);
}
if(process.platform == "win32") {
BAAS_ARCHIVE_DIR = get_unix_path(BAAS_ARCHIVE_DIR);
}
var DUPLICITY_PATH = get_unix_exec_path("duplicity");
var TIMEVIEW_PATH = get_unix_exec_path("timeview.py");
......@@ -175,12 +181,6 @@ function load_data_from_file(filename, callback) {
});
}
function escape_quote_str(str) {
if(!str) return "";
var escaped_quoted = str.replace(/'/g, "'\\''");
return "'" + escaped_quoted + "'";
}
var clients = { };
var kamaki = require('./static/js/kamaki.js');
......
......@@ -54,22 +54,23 @@ function get_env_values() {
return [passphrase, cloud.pithos_public + '/' + cloud.uuid, cloud.token];
}
function set_envs() {
function make_env() {
var env = {};
for(var k in process.env) {
if(k.toUpperCase() != "PATH" || process.platform != "win32") {
env[k] = process.env[k];
}
}
var env_values = get_env_values();
process.env['PASSPHRASE'] = env_values[0];
process.env['SWIFT_PREAUTHURL'] = env_values[1];
process.env['SWIFT_PREAUTHTOKEN'] = env_values[2];
env['PASSPHRASE'] = env_values[0];
env['SWIFT_PREAUTHURL'] = env_values[1];
env['SWIFT_PREAUTHTOKEN'] = env_values[2];
if(process.platform == "win32") {
env["PATH"] = "/bin:/usr/bin";
}
return env;
}
function build_win_commands() {
var env_values = get_env_values();
return "export PATH=/usr/bin/:$PATH;" +
"ulimit -n 1024;" +
"export PASSPHRASE=" + env_values[0] + ";" +
"export SWIFT_PREAUTHURL=" + env_values[1] + ";" +
"export SWIFT_PREAUTHTOKEN=" + env_values[2] + ";";
}
function show_cloud_error() {
toggle_msgs(false, "msg", false);
show_alert_box("A problem occured.<br>" +
......@@ -156,13 +157,8 @@ function call_duplicity(mode, backup_set, force) {
toggle_msgs(false, "msg", false);
var win_cmd = "";
var args = [];
args.push(DUPLICITY_PATH);
if(process.platform == 'win32') {
win_cmd = build_win_commands();
args.push(win_cmd, DUPLICITY_PATH);
} else {
set_envs();
}
switch(mode) {
case "backup":
......@@ -247,12 +243,7 @@ function call_duplicity(mode, backup_set, force) {
// call duplicity
var wProcess = null;
if(process.platform == "win32") {
var args_str = array_to_str(args);
wProcess = spawn(CYGWIN_BASH, ["-c", args_str]);
} else {
wProcess = spawn(DUPLICITY_PATH, args);
}
wProcess = spawn(ENV_CMD, args, {env: make_env()});
running_processes.push(wProcess);
var output_str = "";
......
......@@ -159,22 +159,13 @@ function get_contents_by_date(value) {
var datapath = path.join(BAAS_CACHE_DIR, 'timeviews');
var cert = (clouds[$("#cloud").val()].cert) ?
clouds[$("#cloud").val()].cert : DEFAULT_CERT;
var cacert_file = " " + escape_quote_str(cert) + " ";
var archive_dir = " " + escape_quote_str(BAAS_ARCHIVE_DIR);
var backup_name = " " +
SHA256($("#backup_name").val() + "/" + $("#cloud").val());
var time_cmd = "python " + TIMEVIEW_PATH + " " + datapath + " swift://" +
container + cacert_file + archive_dir + backup_name + " get "
+ value + " " + escape_quote_str(time_path);
if(process.platform == 'win32') {
var cmd = build_win_commands();
var args = ["-c", cmd + time_cmd];
execFile(CYGWIN_BASH, args, show_contents_by_date);
} else {
exec(time_cmd, show_contents_by_date);
}
var archive_dir = BAAS_ARCHIVE_DIR;
var backup_name = SHA256($("#backup_name").val() + "/" + $("#cloud").val());
var args = ["python", TIMEVIEW_PATH, datapath,
"swift://" + container, cert, archive_dir,
backup_name, "get", value, time_path];
execFile(ENV_CMD, args, {env: make_env()}, show_contents_by_date);
}
function parse_collection_status(data) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment