lmc/linodes/mutate/mutate.js

270 lines
8.4 KiB
JavaScript
Raw Permalink Normal View History

2020-01-22 21:01:47 -05:00
/*
* 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, migrateETA, parseParams, setupHeader } from "/global.js";
(function()
{
// Element names specific to this page
elements.cpu = "cpu";
elements.cpuCurrent = "cpu-current";
elements.cpuNew = "cpu-new";
elements.diskName = "disk-name";
elements.eta = "eta";
elements.gpu = "gpu";
elements.gpuCurrent = "gpu-current";
elements.gpuNew = "gpu-new";
elements.linodeLabel = "linode-label";
elements.linodeTag = "linode-tag";
elements.linodeTagLink = "linode-tag-link";
elements.mbit = "mbit";
elements.mbitCurrent = "mbit-current";
elements.mbitNew = "mbit-new";
elements.ram = "ram";
elements.ramCurrent = "ram-current";
elements.ramNew = "ram-new";
elements.resize = "resize";
elements.resizeBox = "resize-box";
elements.storage = "storage";
elements.storageCurrent = "storage-current";
elements.storageNew = "storage-new";
elements.upgradeButton = "upgrade-button";
elements.xfer = "xfer";
elements.xferCurrent = "xfer-current";
elements.xferNew = "xfer-new";
// Data recieved from API calls
var data = {};
data.disks = [];
data.linode = {};
data.plan = {};
data.successor = {};
// Static references to UI elements
var ui = {};
ui.cpu = {};
ui.cpuCurrent = {};
ui.cpuNew = {};
ui.diskName = {};
ui.eta = {};
ui.gpu = {};
ui.gpuCurrent = {};
ui.gpuNew = {};
ui.linodeLabel = {};
ui.linodeTag = {};
ui.linodeTagLink = {};
ui.mbit = {};
ui.mbitCurrent = {};
ui.mbitNew = {};
ui.ram = {};
ui.ramCurrent = {};
ui.ramNew = {};
ui.resize = {};
ui.resizeBox = {};
ui.storage = {};
ui.storageCurrent = {};
ui.storageNew = {};
ui.upgradeButton = {};
ui.xfer = {};
ui.xferCurrent = {};
ui.xferNew = {};
// 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";
}
// Get plan info
apiGet("/linode/types/" + data.linode.type, displayPlan, 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 pages
if (response.page != response.pages) {
apiGet("/linode/instances/" + data.params.lid + "/disks?page=" + (response.page + 1), displayDisks, null);
return;
}
// Compute total size and types of disks
var size = 0;
var numExt = 0;
var numSwap = 0;
for (var i = 0; i < data.disks.length; i++) {
size += data.disks[i].size;
if (data.disks[i].filesystem.match(/ext3|ext4/)) {
numExt++;
ui.diskName.innerHTML = data.disks[i].label;
}
else if (data.disks[i].filesystem == "swap")
numSwap++;
}
// Compute estimated resize time
ui.eta.innerHTML = migrateETA(size, true);
// Show auto-resize option if there is only a single ext disk
if ((data.disks.length == 1 && numExt == 1) || (data.disks.length == 2 && numExt == 1 && numSwap == 1)) {
ui.resizeBox.disabled = false;
ui.resizeBox.checked = true;
ui.resize.style.display = "initial";
}
};
// Callback for plan details API call
var displayPlan = function(response)
{
data.plan = response;
// Redirect to dashboard if there is no upgrade
if (!data.plan.successor) {
location.href = "/linodes/dashboard?lid=" + data.params.lid;
return;
}
// Get successor
apiGet("/linode/types/" + data.plan.successor, displaySuccessor, null);
};
// Callback for successor details API call
var displaySuccessor = function(response)
{
data.successor = response;
// Set UI stuff
if (data.plan.memory != data.successor.memory) {
ui.ramCurrent.innerHTML = data.plan.memory / 1024;
ui.ramNew.innerHTML = data.successor.memory / 1024;
ui.ram.style.display = "list-item";
}
if (data.plan.disk != data.successor.disk) {
ui.storageCurrent.innerHTML = data.plan.disk / 1024;
ui.storageNew.innerHTML = data.successor.disk / 1024;
ui.storage.style.display = "list-item";
}
if (data.plan.vcpus != data.successor.vcpus) {
ui.cpuCurrent.innerHTML = data.plan.vcpus;
ui.cpuNew.innerHTML = data.successor.vcpus;
ui.cpu.style.display = "list-item";
}
if (data.plan.gpus != data.successor.gpus) {
ui.gpuCurrent.innerHTML = data.plan.gpus;
ui.gpuNew.innerHTML = data.successor.gpus;
ui.gpu.style.display = "list-item";
}
if (data.plan.network_out != data.successor.network_out) {
ui.mbitCurrent.innerHTML = data.plan.network_out;
ui.mbitNew.innerHTML = data.successor.network_out;
ui.mbit.style.display = "list-item";
}
if (data.plan.transfer != data.successor.transfer) {
ui.xferCurrent.innerHTML = data.plan.transfer;
ui.xferNew.innerHTML = data.successor.transfer;
ui.xfer.style.display = "list-item";
}
ui.upgradeButton.disabled = false;
};
// Click handler for upgrade button
var handleUpgrade = function(event)
{
if (event.currentTarget.disabled)
return;
var req = {
"allow_auto_disk_resize": (!ui.resizeBox.disabled && ui.resizeBox.checked)
};
apiPost("/linode/instances/" + data.params.lid + "/mutate", req, 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.cpu = document.getElementById(elements.cpu);
ui.cpuCurrent = document.getElementById(elements.cpuCurrent);
ui.cpuNew = document.getElementById(elements.cpuNew);
ui.diskName = document.getElementById(elements.diskName);
ui.eta = document.getElementById(elements.eta);
ui.gpu = document.getElementById(elements.gpu);
ui.gpuCurrent = document.getElementById(elements.gpuCurrent);
ui.gpuNew = document.getElementById(elements.gpuNew);
ui.linodeLabel = document.getElementById(elements.linodeLabel);
ui.linodeTag = document.getElementById(elements.linodeTag);
ui.linodeTagLink = document.getElementById(elements.linodeTagLink);
ui.mbit = document.getElementById(elements.mbit);
ui.mbitCurrent = document.getElementById(elements.mbitCurrent);
ui.mbitNew = document.getElementById(elements.mbitNew);
ui.ram = document.getElementById(elements.ram);
ui.ramCurrent = document.getElementById(elements.ramCurrent);
ui.ramNew = document.getElementById(elements.ramNew);
ui.resize = document.getElementById(elements.resize);
ui.resizeBox = document.getElementById(elements.resizeBox);
ui.storage = document.getElementById(elements.storage);
ui.storageCurrent = document.getElementById(elements.storageCurrent);
ui.storageNew = document.getElementById(elements.storageNew);
ui.upgradeButton = document.getElementById(elements.upgradeButton);
ui.xfer = document.getElementById(elements.xfer);
ui.xferCurrent = document.getElementById(elements.xferCurrent);
ui.xferNew = document.getElementById(elements.xferNew);
// Attach event handlers
ui.upgradeButton.addEventListener("click", handleUpgrade);
// 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);
})();