lmc/linodes/rescue/rescue.js

269 lines
8.6 KiB
JavaScript
Raw 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, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.disks = "disks";
elements.drive = "drive";
elements.filesystems = "filesystems";
elements.linodeLabel = "linode-label";
elements.linodeTag = "linode-tag";
elements.linodeTagLink = "linode-tag-link";
elements.rescueButton = "rescue-button";
elements.resetButton = "reset-button";
elements.rootPass = "root-pass";
elements.sda = "sda";
elements.sdb = "sdb";
elements.sdc = "sdc";
elements.sdd = "sdd";
elements.sde = "sde";
elements.sdf = "sdf";
elements.sdg = "sdg";
elements.volumes = "volumes";
// Data received from API calls
var data = {};
data.disks = [];
data.linode = {};
data.volumes = [];
// Static references to UI elements
var ui = {};
ui.disks = [];
ui.drive = [];
ui.filesystems = {};
ui.linodeLabel = {};
ui.linodeTag = {};
ui.linodeTagLink = {};
ui.rescueButton = {};
ui.resetButton = {};
ui.rootPass = {};
ui.sda = {};
ui.sdb = {};
ui.sdc = {};
ui.sdd = {};
ui.sde = {};
ui.sdf = {};
ui.sdg = {};
ui.volumes = [];
// 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";
} else {
ui.linodeTag.style.display = "none";
}
// Get list of volumes
apiGet("/volumes", displayVolumes, null);
};
// Callback for linode disks API call
var displayDisks = function(response)
{
// Add disks to array
data.disks = data.disks.concat(response.data);
// Request the next page if there are more
if (response.page != response.pages) {
apiGet("/linode/instances/" + data.params.lid + "/disks?page=" + (response.page + 1), displayDisks, null);
return;
}
// Add disks to drive lists, and disks with filesystems to filesystems list
for (var i = 0; i < data.disks.length; i++) {
var disk = document.createElement("option");
disk.value = "disk-" + data.disks[i].id;
disk.innerHTML = data.disks[i].label;
for (var j = 0; j < ui.disks.length; j++)
ui.disks[j].appendChild(disk.cloneNode(true));
if (data.disks[i].filesystem.match(/ext3|ext4/))
ui.filesystems.appendChild(disk.cloneNode(true));
}
// Preset value of drive lists
for (var i = 0; i < data.disks.length && i < ui.drive.length; i++)
ui.drive[i].value = "disk-" + data.disks[i].id;
if (ui.filesystems.childNodes.length > 0)
ui.resetButton.disabled = false;
};
// Callback for volumes API call
var displayVolumes = function(response)
{
// Add volumes to array
data.volumes = data.volumes.concat(response.data);
// Request the next page if there are more
if (response.page != response.pages) {
apiGet("/volumes?page=" + (response.page + 1), displayVolumes, null);
return;
}
// Add volumes from this Linode's region to drive lists
for (var i = 0; i < data.volumes.length; i++) {
if (data.volumes[i].region != data.linode.region)
continue;
var volume = document.createElement("option");
volume.value = "volume-" + data.volumes[i].id;
volume.innerHTML = data.volumes[i].label;
for (var j = 0; j < ui.volumes.length; j++)
ui.volumes[j].appendChild(volume.cloneNode(true));
}
ui.rescueButton.disabled = false;
};
// Rescue button handler
var handleRescue = function(event)
{
if (event.currentTarget.disabled)
return;
var request = {
"devices": {}
};
if (ui.sda.value.startsWith("disk-"))
request.devices.sda = { "disk_id": parseInt(ui.sda.value.split("-")[1]) };
else if (ui.sda.value.startsWith("volume-"))
request.devices.sda = { "volume_id": parseInt(ui.sda.value.split("-")[1]) };
if (ui.sdb.value.startsWith("disk-"))
request.devices.sdb = { "disk_id": parseInt(ui.sdb.value.split("-")[1]) };
else if (ui.sdb.value.startsWith("volume-"))
request.devices.sdb = { "volume_id": parseInt(ui.sdb.value.split("-")[1]) };
if (ui.sdc.value.startsWith("disk-"))
request.devices.sdc = { "disk_id": parseInt(ui.sdc.value.split("-")[1]) };
else if (ui.sdc.value.startsWith("volume-"))
request.devices.sdc = { "volume_id": parseInt(ui.sdc.value.split("-")[1]) };
if (ui.sdd.value.startsWith("disk-"))
request.devices.sdd = { "disk_id": parseInt(ui.sdd.value.split("-")[1]) };
else if (ui.sdd.value.startsWith("volume-"))
request.devices.sdd = { "volume_id": parseInt(ui.sdd.value.split("-")[1]) };
if (ui.sde.value.startsWith("disk-"))
request.devices.sde = { "disk_id": parseInt(ui.sde.value.split("-")[1]) };
else if (ui.sde.value.startsWith("volume-"))
request.devices.sde = { "volume_id": parseInt(ui.sde.value.split("-")[1]) };
if (ui.sdf.value.startsWith("disk-"))
request.devices.sdf = { "disk_id": parseInt(ui.sdf.value.split("-")[1]) };
else if (ui.sdf.value.startsWith("volume-"))
request.devices.sdf = { "volume_id": parseInt(ui.sdf.value.split("-")[1]) };
if (ui.sdg.value.startsWith("disk-"))
request.devices.sdg = { "disk_id": parseInt(ui.sdg.value.split("-")[1]) };
else if (ui.sdg.value.startsWith("volume-"))
request.devices.sdg = { "volume_id": parseInt(ui.sdg.value.split("-")[1]) };
apiPost("/linode/instances/" + data.params.lid + "/rescue", request, function(response)
{
location.href = "/linodes/dashboard?lid=" + data.params.lid;
});
};
// Reset button handler
var handleReset = function(event)
{
if (event.currentTarget.disabled)
return;
if (ui.rootPass.value.length == 0) {
alert("You must supply a root password.");
return;
}
var request = {
"password": ui.rootPass.value
};
apiPost("/linode/instances/" + data.params.lid + "/disks/" + ui.filesystems.value.split("-")[1] + "/password", request, function(response)
{
location.href = "/linodes/dashboard?lid=" + data.params.lid;
});
};
// 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.disks = document.getElementsByClassName(elements.disks);
ui.drive = document.getElementsByClassName(elements.drive);
ui.filesystems = document.getElementById(elements.filesystems);
ui.linodeLabel = document.getElementById(elements.linodeLabel);
ui.linodeTag = document.getElementById(elements.linodeTag);
ui.linodeTagLink = document.getElementById(elements.linodeTagLink);
ui.rescueButton = document.getElementById(elements.rescueButton);
ui.resetButton = document.getElementById(elements.resetButton);
ui.rootPass = document.getElementById(elements.rootPass);
ui.sda = document.getElementById(elements.sda);
ui.sdb = document.getElementById(elements.sdb);
ui.sdc = document.getElementById(elements.sdc);
ui.sdd = document.getElementById(elements.sdd);
ui.sde = document.getElementById(elements.sde);
ui.sdf = document.getElementById(elements.sdf);
ui.sdg = document.getElementById(elements.sdg);
ui.volumes = document.getElementsByClassName(elements.volumes);
// Register button handlers
ui.rescueButton.addEventListener("click", handleRescue);
ui.resetButton.addEventListener("click", handleReset);
// Get data from API
apiGet("/linode/instances/" + data.params.lid, displayDetails, null);
apiGet("/linode/instances/" + data.params.lid + "/disks", displayDisks, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();