Commit 67c55c22 authored by Efthymia Bika's avatar Efthymia Bika
Browse files

Move functions to separate files

parent 34516f41
This diff is collapsed.
function load_backup(backup) {
$(".tabs").show();
$(".tabs-content").show();
$('#backup_details_tab').addClass('active');
$('#backup-name-error small').hide();
$('#directory-error small').hide();
$('#res-directory-error small').hide();
$('#cloud-error small').hide();
$('#exclude-error small').hide();
$('#include-error small').hide();
$('#res-file-error small').hide();
$('#timestamp-error small').hide();
$('#time-head-error small').hide();
$('#msg').html('');
$('#msg').removeClass('panel');
if(backup) {
$("#backup-name").val(backup.name);
$("#directory").html(backup.local_dir);
$("#cloud").val(backup.cloud);
$("#passphrase").val(backup.passphrase);
if(backup.passphrase != "") {
$("#save_passphrase").prop("checked", true);
}
if(backup.first_backup) {
disable_form(true);
disable_actions(false);
} else {
disable_actions(true);
disable_form(false);
}
$("#exclude").val(backup.exclude);
$("#include").val(backup.include);
} else {
$("#backup-name").val('');
$("#directory").html('');
$("#res-directory").html('');
$("#cloud").val('');
$("#passphrase").val('');
$("#exclude").val('');
$("#include").val('');
disable_form(false);
disable_actions(true);
// de-activate all tabs
activate_li("dummy");
}
$("#backup_details_link").trigger("click");
}
function save_backup_set() {
var backup_name = $("#backup-name").val().replace(/^\s+|\s+$/gm,'');
var directory = $("#directory").html();
var cloud = $("#cloud").val();
var save_pass = $("#save_passphrase").is(":checked");
var passphrase = (save_pass) ? $("#passphrase").val() : "";
var exclude = $("#exclude").val();
var include = $("#include").val();
if($("#" + backup_name).attr("id")) {
$.each(backups, function(i, backup_set) {
if(backup_set.name == backup_name) {
backup_set.name = backup_name;
backup_set.local_dir = directory;
backup_set.cloud = cloud;
backup_set.passphrase = passphrase;
backup_set.exclude = exclude;
backup_set.include = include;
}
});
} else {
var backup_set = {"name" : "", "local_dir" : "",
"cloud" : "", "passphrase" : "", "container" : "",
"first_backup" : "", "exclude" : "", "include" : "" };
backup_set.name = backup_name;
backup_set.local_dir = directory;
backup_set.cloud = cloud;
backup_set.passphrase = passphrase;
backup_set.container = "Backup_" + backup_name;
backup_set.exclude = exclude;
backup_set.include = include;
backups.push(backup_set);
render_backup_sets("");
}
var root_backup_sets = {"backups" : "" };
root_backup_sets.backups = backups;
write_conf_file(BACKUP_CONF_FILE, root_backup_sets);
activate_li("li_" + backup_name);
}
function delete_backup(backup) {
var i = backups.indexOf(backup);
backups.splice(i, 1);
render_backup_sets("");
var root_backup_sets = {"backups" : "" };
root_backup_sets.backups = backups;
write_conf_file(BACKUP_CONF_FILE, root_backup_sets);
$(".tabs").hide();
$(".tabs-content").hide();
}
function write_first_backup() {
var backup_name = $("#backup-name").val().replace(/^\s+|\s+$/gm,'');
var first_backup = null;
$.each(backups, function(i, backup_set) {
if(backup_set.name == backup_name) {
backup_set.first_backup = Date.now();
}
});
var root_backup_sets = {"backups" : "" };
root_backup_sets.backups = backups;
write_conf_file(BACKUP_CONF_FILE, root_backup_sets);
}
var dup_verbosity = " -v8 ";
function set_envs() {
var passphrase = $('#passphrase').val();
if(passphrase) {
process.env['PASSPHRASE'] = passphrase;
}
var cloud_name = $("#cloud").val().replace(/^\s+|\s+$/gm,'');
$.each(clouds, function(i, cloud) {
if(cloud.name == cloud_name) {
process.env['SWIFT_PREAUTHURL'] = cloud.pithos_public + '/' + cloud.uuid;
process.env['SWIFT_PREAUTHTOKEN'] = cloud.token;
}
});
}
function build_win_commands() {
var passphrase = $('#passphrase').val();
var cloud_name = $("#cloud").val().replace(/^\s+|\s+$/gm,'');
var preauth_url = null;
var preauth_token = null;
$.each(clouds, function(i, cloud) {
if(cloud.name == cloud_name) {
preauth_url = cloud.pithos_public + '/' + cloud.uuid;
preauth_token = cloud.token;
}
});
return "export PATH=/usr/bin/:$PATH;" +
"ulimit -n 1024;" +
"export PASSPHRASE=" + passphrase + ";" +
"export SWIFT_PREAUTHURL=" + preauth_url + ";" +
"export SWIFT_PREAUTHTOKEN=" + preauth_token + ";";
}
function run_duplicity(restore) {
var container = null;
var backup_name = $("#backup-name").val().replace(/^\s+|\s+$/gm,'');
$.each(backups, function(i, backup_set) {
if(backup_set.name == backup_name) {
container = backup_set.container;
}
});
var file_arg = "";
var file_to_restore = $("#res-file").val();
if(restore && file_to_restore) {
file_arg = " --file-to-restore '" + file_to_restore + "' ";
}
var time_arg = "";
var timestamp = $("#timestamp").val();
if(restore && timestamp) {
time_arg = " --time " + timestamp;
}
var exclude_arg = "";
var exclude = $("#exclude").val();
if(exclude && !restore) {
var args = exclude.split(",");
$.each(args, function(i, value) {
if(process.platform == 'win32') {
exec(CYGWIN_BASH + " -c \"/usr/bin/cygpath '" + value + "' \"",
function(error, stdout, stderr) {
if(error) $("#msg").html(error);
var win_value = String(stdout).replace(/(\r\n|\n|\r)/gm, "");
exclude_arg += " --exclude '" + win_value + "' ";
});
} else {
exclude_arg += " --exclude '" + value + "' ";
}
});
}
var include_arg = "";
var include = $("#include").val();
if(include && !restore) {
var args = include.split(",");
$.each(args, function(i, value) {
if(process.platform == 'win32') {
exec(CYGWIN_BASH + " -c \"/usr/bin/cygpath '" + value + "' \"",
function(error, stdout, stderr) {
if(error) $("#msg").html(error);
var win_value = String(stdout).replace(/(\r\n|\n|\r)/gm, "");
include_arg += " --include '" + win_value + "' ";
});
} else {
include_arg += " --include '" + value + "' ";
}
});
}
var directory = "";
if(restore) {
if(file_to_restore) {
directory = path.join($("#res-directory").html(), file_to_restore);
mkdirp(directory, function(err) {
if(err) console.error(err);
});
} else {
directory = $("#res-directory").html();
}
} else {
directory = $("#directory").html();
}
var log_file = path.join(BAAS_LOG_DIR, "dup_" + new Date().toISOString() + ".log");
var log_arg = " --log-file '" + log_file + "' ";
if(process.platform == 'win32') {
directory = directory.replace(/\\/g, "\\\\");
exec(CYGWIN_BASH + " -c \"/usr/bin/cygpath '" + directory + "' \"",
function(error, stdout, stderr) {
directory = String(stdout).replace(/(\r\n|\n|\r)/gm, "");
if(error) $("#msg").html(error);
var dirs = directory + " swift://" + container;
if(restore) {
dirs = " swift://" + container + " " + directory;
}
var cmd = build_win_commands();
var dup_cmd = "duplicity " + dup_verbosity + log_arg + include_arg + exclude_arg + file_arg + time_arg + dirs + ";";
exec(CYGWIN_BASH + " -c '" + cmd + dup_cmd + "'",
function(error, stdout, stderr){
if(error) {
$("#msg").html(error);
$("#msg").addClass("panel");
} else {
$("#msg").html("");
$("#msg").removeClass("panel");
}
$("#loader").hide();
});
});
} else {
set_envs();
var dirs = directory + " swift://" + container;
if(restore) {
dirs = " swift://" + container + " " + directory;
}
var dup_cmd = "duplicity " + dup_verbosity + log_arg + include_arg + exclude_arg + file_arg + time_arg + dirs + ";";
exec(dup_cmd , function(error, stdout, stderr) {
if(error) {
$("#msg").addClass("panel");
$("#msg").html(error);
} else {
$("#msg").html("");
$("#msg").removeClass("panel");
}
$("#loader").hide();
});
}
}
function load_status() {
$("#loader").show();
$("#status").html("");
var container = null;
var backup_name = $("#backup-name").val().replace(/^\s+|\s+$/gm,'');
$.each(backups, function(i, backup_set) {
if(backup_set.name == backup_name) {
container = backup_set.container;
}
});
function puts(error, stdout, stderr) {
if(error) {
$("#msg").html(error);
$("#msg").addClass("panel");
} else {
$("#msg").html("");
$("#msg").removeClass("panel");
$("#status").html(stdout.replace(/(?:\r\n|\r|\n)/g, '<br />'));
}
$("#loader").hide();
}
var dup_cmd = "duplicity collection-status swift://" + container;
if(process.platform == 'win32') {
var cmd = build_win_commands();
exec(CYGWIN_BASH + " -c '" + cmd + dup_cmd + "'", puts);
} else {
set_envs();
exec(dup_cmd, {maxBuffer: 1000*1024} , puts);
}
}
function load_contents() {
$("#loader").show();
$("#contents").html("");
var container = null;
var backup_name = $("#backup-name").val().replace(/^\s+|\s+$/gm,'');
$.each(backups, function(i, backup_set) {
if(backup_set.name == backup_name) {
container = backup_set.container;
}
});
function puts(error, stdout, stderr) {
if(error) {
$("#msg").html(error);
$("#msg").addClass("panel");
} else {
$("#msg").html("");
$("#msg").removeClass("panel");
$("#contents").html(stdout.replace(/(?:\r\n|\r|\n)/g, '<br />'));
}
$("#loader").hide();
}
var dup_cmd = "duplicity list-current-files swift://" + container;
if(process.platform == 'win32') {
var cmd = build_win_commands();
exec(CYGWIN_BASH + " -c '" + cmd + dup_cmd + "'", puts);
} else {
set_envs();
exec(dup_cmd , puts);
}
}
function show_contents_by_date(error, stdout, stderr) {
$("#timeview-contents-list").empty();
if(error) $("#msg").html(error);
var ul = $("<ul></ul>")
.attr("class", "no-bullet")
.attr("id", "timeview-contents-list");
var contents = JSON.parse(stdout);
$.each(contents, function(i, el) {
var f = (el.type == 'dir') ?
"open_folder('" + el.name + "')" : "";
var el_link = $("<a></a>")
.attr("href", "#")
.attr("onclick", f);
var li = $("<li>&nbsp;" + el.name + "</li>")
.attr("id", el.name);
var icon_class = (el.type == 'dir')
? "fa fa-folder yellow-folder left" :
"fa fa-file-text-o green-file left";
var icon = $("<i></i>")
.attr("class", icon_class);
li.append(icon);
el_link.append(li);
ul.append(el_link);
});
$("#loader").hide();
$("#time-contents").html(ul);
}
function open_folder(name) {
var time_path = $("#time-path").val();
$("#time-path").val(time_path + name + "/");
}
function get_contents_by_date(value) {
$("#loader").show();
var time_path = $("#time-path").val();
if(!time_path) {
$("#time-head-error small").text(errors.path_empty);
$("#time-head-error small").show();
return;
} else {
$("#time-head-error small").text('');
$("#time-head-error small").hide();
}
var container = null;
var backup_name = $("#backup-name").val().replace(/^\s+|\s+$/gm,'');
$.each(backups, function(i, backup_set) {
if(backup_set.name == backup_name) {
container = backup_set.container;
}
});
var time_cmd = "python src/timeview.py timeviews/ swift://" +
container + " get " + value + " " + time_path
if(process.platform == 'win32') {
var cmd = build_win_commands();
exec(CYGWIN_BASH + " -c '" + cmd + time_dup_cmd + "'",
show_contents_by_date);
} else {
exec(time_cmd, show_contents_by_date);
}
}
function load_timeview() {
$("#loader").show();
$("#time-dates").html("");
$("#time-contents").html("");
var container = null;
var backup_name = $("#backup-name").val().replace(/^\s+|\s+$/gm,'');
$.each(backups, function(i, backup_set) {
if(backup_set.name == backup_name) {
container = backup_set.container;
}
});
function puts(error, stdout, stderr) {
if(error) {
$("#msg").html(error);
$("#msg").addClass("panel");
} else {
$("#msg").html("");
$("#msg").removeClass("panel");
var reg = "Num volumes:";
var i = stdout.indexOf(reg);
if(i > 0) {
var sets = stdout.substring(i + reg.length);
var datetime_reg = /(\d{4})(-)(\d{2})(-)(\d{2})(.)(\d{2})(:)(\d{2})(:)(\d{2})/g;
var dates = sets.match(datetime_reg);
dates = dates.sort();
var dates_list = "";
$.each(dates, function(i, value) {
dates_list += "<a href='#' onclick='get_contents_by_date(\""
+ value.replace('.', 'T') + "\")'>" + value.replace('.', ' ') + "</a><br>";
});
$("#time-dates").html(dates_list);
}
}
$("#loader").hide();
}
var dup_cmd = "duplicity collection-status swift://" + container;
if(process.platform == 'win32') {
var cmd = build_win_commands();
exec(CYGWIN_BASH + " -c '" + cmd + dup_cmd + "'", puts);
} else {
set_envs();
exec(dup_cmd, {maxBuffer: 1000*1024} , puts);
}
}
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