lmc/linodes/rebuild/rebuild.js

174 lines
5.3 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.currentDistros = "current";
elements.customImages = "custom";
elements.deletedDisks = "deleted";
elements.deprecatedDistros = "deprecated";
elements.image = "image";
elements.linodeLabel = "linode-label";
elements.linodeTag = "linode-tag";
elements.linodeTagLink = "linode-tag-link";
elements.rebuildButton = "rebuild-button";
elements.rootPass = "root-pass";
// Data recieved from API calls
var data = {};
data.images = [];
data.linode = {};
// Static references to UI elements
var ui = {};
ui.currentDistros = {};
ui.customImages = {};
ui.deletedDisks = {};
ui.deprecatedDistros = {};
ui.image = {};
ui.linodeLabel = {};
ui.linodeTag = {};
ui.linodeTagLink = {};
ui.rebuildButton = {};
ui.rootPass = {};
// 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";
}
};
// Callback for images API call
var displayImages = function(response)
{
// Add images to array
data.images = data.images.concat(response.data);
// Request the next page if there are more pages
if (response.page != response.pages) {
apiGet("/images?page=" + (response.page + 1), displayImages, null);
return;
}
// Add images to selector
for (var i = 0; i < data.images.length; i++) {
// Don't display kubernetes distro variants
if (data.images[i].id.indexOf("-kube-") != -1)
continue;
var image = document.createElement("option");
image.value = data.images[i].id;
image.innerHTML = data.images[i].label;
if (data.images[i].deprecated)
ui.deprecatedDistros.appendChild(image);
else if (data.images[i].type == "automatic")
ui.deletedDisks.appendChild(image);
else if (!data.images[i].vendor)
ui.customImages.appendChild(image);
else
ui.currentDistros.appendChild(image);
if (data.images[i].id == settings.preferredDistro)
ui.image.value = data.images[i].id;
}
if (ui.customImages.childNodes.length == 0)
ui.customImages.style.display = "none";
if (ui.deletedDisks.childNodes.length == 0)
ui.deletedDisks.style.display = "none";
ui.rebuildButton.disabled = false;
};
// Rebuild button handler
var handleRebuild = function(event)
{
if (event.currentTarget.disabled)
return;
if (ui.rootPass.value.length == 0) {
alert("You must supply a root password.");
return;
}
if (!confirm("Delete all existing data on this Linode and redeploy?"))
return;
var request = {
"image": ui.image.value,
"root_pass": ui.rootPass.value,
"booted": false
};
apiPost("/linode/instances/" + data.params.lid + "/rebuild", 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.currentDistros = document.getElementById(elements.currentDistros);
ui.customImages = document.getElementById(elements.customImages);
ui.deletedDisks = document.getElementById(elements.deletedDisks);
ui.deprecatedDistros = document.getElementById(elements.deprecatedDistros);
ui.image = document.getElementById(elements.image);
ui.linodeLabel = document.getElementById(elements.linodeLabel);
ui.linodeTag = document.getElementById(elements.linodeTag);
ui.linodeTagLink = document.getElementById(elements.linodeTagLink);
ui.rebuildButton = document.getElementById(elements.rebuildButton);
ui.rootPass = document.getElementById(elements.rootPass);
// Register rebuild button handler
ui.rebuildButton.addEventListener("click", handleRebuild);
// Get data from API
apiGet("/linode/instances/" + data.params.lid, displayDetails, null);
apiGet("/images", displayImages, null);
};
// Attach onload handler
window.addEventListener("load", setup);
})();