251 lines
7.2 KiB
JavaScript
251 lines
7.2 KiB
JavaScript
|
/*
|
||
|
* 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, migrateETA, parseParams, setupHeader } from "/global.js";
|
||
|
|
||
|
(function()
|
||
|
{
|
||
|
// Element names specific to this page
|
||
|
elements.autoResize = "auto-resize";
|
||
|
elements.autoResizeBox = "auto-resize-box";
|
||
|
elements.current = "current";
|
||
|
elements.dedicated = "dedicated";
|
||
|
elements.diskName = "disk-name";
|
||
|
elements.eta = "eta";
|
||
|
elements.gpu = "gpu";
|
||
|
elements.instanceType = "instance-type";
|
||
|
elements.highmem = "highmem";
|
||
|
elements.linodeLabel = "linode-label";
|
||
|
elements.linodeTag = "linode-tag";
|
||
|
elements.linodeTagLink = "linode-tag-link";
|
||
|
elements.nanode = "nanode";
|
||
|
elements.resizeButton = "resize-button";
|
||
|
elements.standard = "standard";
|
||
|
|
||
|
// Data received from API calls
|
||
|
var data = {};
|
||
|
data.disks = [];
|
||
|
data.linode = {};
|
||
|
data.types = [];
|
||
|
|
||
|
// Static references to UI elements
|
||
|
var ui = {};
|
||
|
ui.autoResize = {};
|
||
|
ui.autoResizeBox = {};
|
||
|
ui.dedicated = {};
|
||
|
ui.diskName = {};
|
||
|
ui.eta = {};
|
||
|
ui.gpu = {};
|
||
|
ui.highmem = {};
|
||
|
ui.linodeLabel = {};
|
||
|
ui.linodeTag = {};
|
||
|
ui.linodeTagLink = {};
|
||
|
ui.nanode = {};
|
||
|
ui.resizeButton = {};
|
||
|
ui.standard = {};
|
||
|
|
||
|
// Creates a Linode type radio button and label
|
||
|
var createTypeButton = function(type)
|
||
|
{
|
||
|
// Radio button
|
||
|
var radio = document.createElement("input");
|
||
|
radio.id = type.id;
|
||
|
radio.type = "radio";
|
||
|
radio["name"] = elements.instanceType;
|
||
|
radio.value = type.id;
|
||
|
|
||
|
// Label
|
||
|
var label = document.createElement("label");
|
||
|
label.htmlFor = type.id;
|
||
|
label.innerHTML = type.label;
|
||
|
|
||
|
// Text to display next to current plan
|
||
|
var current = document.createElement("span");
|
||
|
current.innerHTML = " (current plan)";
|
||
|
|
||
|
var br = document.createElement("br");
|
||
|
|
||
|
// Insert elements
|
||
|
ui[type["class"]].appendChild(radio);
|
||
|
ui[type["class"]].appendChild(label);
|
||
|
|
||
|
if (type.id == data.linode.type) {
|
||
|
radio.disabled = true;
|
||
|
label.className = elements.current;
|
||
|
ui[type["class"]].appendChild(current);
|
||
|
}
|
||
|
|
||
|
ui[type["class"]].appendChild(br);
|
||
|
};
|
||
|
|
||
|
// 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 linode types
|
||
|
apiGet("/linode/types", displayTypes, 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.autoResizeBox.disabled = false;
|
||
|
ui.autoResizeBox.checked = true;
|
||
|
ui.autoResize.style.display = "block";
|
||
|
}
|
||
|
};
|
||
|
|
||
|
// Callback for linode types API call
|
||
|
var displayTypes = function(response)
|
||
|
{
|
||
|
// Add types to array
|
||
|
data.types = data.types.concat(response.data);
|
||
|
|
||
|
// Request the next page if there are more pages
|
||
|
if (response.page != response.pages) {
|
||
|
apiGet("/linode/types?page=" + (response.page + 1), displayTypes, null);
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
// Insert types
|
||
|
for (var i = 0; i < data.types.length; i++)
|
||
|
createTypeButton(data.types[i]);
|
||
|
|
||
|
ui.resizeButton.disabled = false;
|
||
|
};
|
||
|
|
||
|
// Resize button handler
|
||
|
var handleResize = function(event)
|
||
|
{
|
||
|
if (event.currentTarget.disabled)
|
||
|
return;
|
||
|
|
||
|
// Find the selected type
|
||
|
var type = null;
|
||
|
var inputs = document.getElementsByTagName("input");
|
||
|
for (var i = 0; i < inputs.length; i++) {
|
||
|
if (inputs[i]["name"] == elements.instanceType && inputs[i].checked) {
|
||
|
type = inputs[i];
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
if (!type) {
|
||
|
alert("You must choose a new plan.");
|
||
|
return;
|
||
|
}
|
||
|
|
||
|
if (!confirm("Immediately shut down, migrate your Linode, and invoice this account for the difference?"))
|
||
|
return;
|
||
|
|
||
|
var request = {
|
||
|
"type": type.id,
|
||
|
"allow_auto_disk_resize": (!ui.autoResizeBox.disabled && ui.autoResizeBox.checked)
|
||
|
};
|
||
|
apiPost("/linode/instances/" + data.params.lid + "/resize", 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.autoResize = document.getElementById(elements.autoResize);
|
||
|
ui.autoResizeBox = document.getElementById(elements.autoResizeBox);
|
||
|
ui.dedicated = document.getElementById(elements.dedicated);
|
||
|
ui.diskName = document.getElementById(elements.diskName);
|
||
|
ui.eta = document.getElementById(elements.eta);
|
||
|
ui.gpu = document.getElementById(elements.gpu);
|
||
|
ui.highmem = document.getElementById(elements.highmem);
|
||
|
ui.linodeLabel = document.getElementById(elements.linodeLabel);
|
||
|
ui.linodeTag = document.getElementById(elements.linodeTag);
|
||
|
ui.linodeTagLink = document.getElementById(elements.linodeTagLink);
|
||
|
ui.nanode = document.getElementById(elements.nanode);
|
||
|
ui.resizeButton = document.getElementById(elements.resizeButton);
|
||
|
ui.standard = document.getElementById(elements.standard);
|
||
|
|
||
|
// Register resize button handler
|
||
|
ui.resizeButton.addEventListener("click", handleResize);
|
||
|
|
||
|
// 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);
|
||
|
})();
|