lmc/linodes/backups/backups.js

271 lines
8.3 KiB
JavaScript
Raw Permalink Normal View History

/*
* This file is part of Linode Manager Classic.
*
* Linode Manager Classic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Linode Manager Classic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Linode Manager Classic. If not, see <https://www.gnu.org/licenses/>.
*/
import { settings, elements, apiGet, apiPost, apiPut, parseParams, setupHeader, timeString } from "/global.js";
(function()
{
// Element names specific to this page
elements.backupDay = "backup-day";
elements.backupTable = "backup-table";
elements.backupWindow = "backup-window";
elements.inProgress = "in-progress";
elements.linodeLabel = "linode-label";
elements.linodeTag = "linode-tag";
elements.linodeTagLink = "linode-tag-link";
elements.lmcRow = "lmc-tr3";
elements.saveButton = "save-button";
elements.scheduleUpdated = "schedule-updated";
elements.snapshotButton = "snapshot-button";
elements.snapshotLabel = "snapshot-label";
elements.timezone = "timezone";
elements.window = "window";
// Data recieved from API calls
var data = {};
data.backups = {};
data.backupsSorted = [];
data.linode = {};
data.profile = {};
// Static references to UI elements
var ui = {};
ui.backupDay = {};
ui.backupTable = {};
ui.backupWindow = {};
ui.inProgress = {};
ui.linodeLabel = {};
ui.linodeTag = {};
ui.linodeTagLink = {};
ui.saveButton = {};
ui.scheduleUpdated = {};
ui.snapshotButton = {};
ui.snapshotLabel = {};
ui.timezone = {};
ui.windows = [];
// Generate a backup table row
var createBackupRow = function(backup)
{
var row = document.createElement("tr");
row.className = elements.lmcRow;
var type = document.createElement("td");
type.innerHTML = backup.type;
row.appendChild(type);
var started = document.createElement("td");
var startedDate = new Date(backup.created + "Z");
started.innerHTML = startedDate.toLocaleString();
row.appendChild(started);
var finished = document.createElement("td");
if (backup.finished) {
var finishedDate = new Date(backup.finished + "Z");
finished.innerHTML = finishedDate.toLocaleString();
}
row.appendChild(finished);
var duration = document.createElement("td");
if (backup.finished)
duration.innerHTML = timeString(finishedDate - startedDate, false);
row.appendChild(duration);
var status = document.createElement("td");
status.innerHTML = backup.status;
row.appendChild(status);
var restore = document.createElement("td");
if (backup.status == "successful") {
var restoreLink = document.createElement("a");
restoreLink.href = "/linodes/backup_details?lid=" + data.params.lid + "&bid=" + backup.id;
restoreLink.innerHTML = "Restore to...";
restore.appendChild(restoreLink);
}
row.appendChild(restore);
return row;
};
// Callback for backups API call
var displayBackups = function(response)
{
data.backups = response;
// Enable snapshot button if there's no snapshot in progress
if (!data.backups.snapshot.in_progress) {
ui.snapshotButton.disabled = false;
} else {
ui.snapshotButton.style.display = "none";
ui.snapshotLabel.style.display = "none";
ui.inProgress.style.display = "initial";
ui.backupTable.appendChild(createBackupRow(data.backups.snapshot.in_progress));
}
// Sort backups by date
data.backupsSorted = data.backups.automatic;
if (data.backups.snapshot.current)
data.backupsSorted.push(data.backups.snapshot.current);
data.backupsSorted.sort(function(a, b)
{
var startA = new Date(a.created + "Z");
var startB = new Date(b.created + "Z");
return startB - startA;
});
// Add them to the table
for (var i = 0; i < data.backupsSorted.length; i++)
ui.backupTable.appendChild(createBackupRow(data.backupsSorted[i]));
};
// Callback for linode details API call
var displayDetails = function(response)
{
data.linode = response;
// Set page title and header stuff
document.title += " // " + data.linode.label;
ui.linodeLabel.innerHTML = data.linode.label;
if (data.linode.tags.length == 1) {
ui.linodeTagLink.href = "/linodes?tag=" + data.linode.tags[0];
ui.linodeTagLink.innerHTML = "(" + data.linode.tags[0] + ")";
ui.linodeTag.style.display = "inline";
}
// Redirect to enable page if backups not enabled
if (!data.linode.backups.enabled)
location.href = "/linodes/backups_enable?lid=" + data.params.lid;
// Set backup settings
ui.backupWindow.value = data.linode.backups.schedule.window;
ui.backupDay.value = data.linode.backups.schedule.day;
ui.saveButton.disabled = false;
};
// Handler for save button
var handleSave = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"backups": {
"schedule": {
"day": ui.backupDay.value,
"window": ui.backupWindow.value
}
}
};
apiPut("/linode/instances/" + data.params.lid, req, function(response)
{
ui.scheduleUpdated.style.display = "block";
});
};
// Handler for snapshot button
var handleSnapshot = function(event)
{
if (event.currentTarget.disabled)
return;
if (!confirm("Taking a snapshot will back up your Linode in its current state, over-writing your previous snapshot. Are you sure?"))
return;
var req = {
"label": ui.snapshotLabel.value
};
apiPost("/linode/instances/" + data.params.lid + "/backups", req, function(response)
{
location.reload();
});
};
// Initial setup
var setup = function()
{
// Parse URL parameters
data.params = parseParams();
// We need a Linode ID, so die if we don't have it
if (!data.params.lid) {
alert("No Linode ID supplied!");
return;
}
setupHeader();
// Update links on page to include proper Linode ID
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++)
anchors[i].href = anchors[i].href.replace("lid=0", "lid=" + data.params.lid);
// Get element references
ui.backupDay = document.getElementById(elements.backupDay);
ui.backupTable = document.getElementById(elements.backupTable);
ui.backupWindow = document.getElementById(elements.backupWindow);
ui.inProgress = document.getElementById(elements.inProgress);
ui.linodeLabel = document.getElementById(elements.linodeLabel);
ui.linodeTag = document.getElementById(elements.linodeTag);
ui.linodeTagLink = document.getElementById(elements.linodeTagLink);
ui.saveButton = document.getElementById(elements.saveButton);
ui.scheduleUpdated = document.getElementById(elements.scheduleUpdated);
ui.snapshotButton = document.getElementById(elements.snapshotButton);
ui.snapshotLabel = document.getElementById(elements.snapshotLabel);
ui.timezone = document.getElementById(elements.timezone);
ui.windows = document.getElementsByClassName(elements.window);
// Display timezone info
var now = new Date();
var offset = now.getTimezoneOffset() / -60;
var offsetHours = Math.floor(offset);
var offsetMins = now.getTimezoneOffset() % 60;
if (offsetMins < 10)
offsetMins = "0" + offsetMins;
ui.timezone.innerHTML = "GMT";
if (offset >= 0)
ui.timezone.innerHTML += "+";
ui.timezone.innerHTML += offset;
for (var i = 0; i < ui.windows.length; i++) {
var windowNum = parseInt(ui.windows[i].value.substring(1));
var startHour = windowNum + offsetHours;
if (startHour < 0)
startHour += 24;
if (startHour < 10)
startHour = "0" + startHour;
var endHour = windowNum + offsetHours + 2;
if (endHour < 0)
endHour += 24;
if (endHour < 10)
endHour = "0" + endHour;
ui.windows[i].innerHTML = startHour + ":" + offsetMins + " - " + endHour + ":" + offsetMins;
}
// Attach button handlers
ui.saveButton.addEventListener("click", handleSave);
ui.snapshotButton.addEventListener("click", handleSnapshot);
// Get data from API
apiGet("/linode/instances/" + data.params.lid, displayDetails, null);
apiGet("/linode/instances/" + data.params.lid + "/backups", displayBackups, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();