/*
Step: Storage
Lists the disks of the server (Linux) and turns one into a drive:
a partition nobody mounts is mounted under /media by ArozOS at every
start, one the host already mounts is used as a folder. A disk nothing
uses can be formatted first, after the admin types their password.
Any platform can also add a folder of the server as a drive, through
the Storage Pools endpoints.
The drives the guide adds are kept in Wizard.progress.drives, so the
accounts step can set who may use them.
*/
(function () {
"use strict";
var W = window.Wizard, T = W.T, esc = W.esc;
var USE_TEXT = {
system: ["Holds the operating system", "danger"],
mounted: ["Mounted by the server", "warn"],
drive: ["Already a drive", "ok"],
swap: ["Swap space", "warn"],
raid: ["Part of a RAID array", "warn"],
lvm: ["Part of an LVM volume", "warn"],
encrypted: ["Encrypted volume", "warn"],
holder: ["In use by another device", "warn"]
};
function useChip(reason, driveId) {
if (reason === "drive" && driveId) {
return '' + esc(T("storage/use/driveId", "Drive {id}:/", { id: driveId })) + "";
}
var info = USE_TEXT[reason];
if (!info) {
return "";
}
return '' + esc(T("storage/use/" + reason, info[0])) + "";
}
function diskNoteText(note) {
if (note === "sudo") {
return T("storage/note/sudo", "ArozOS is not running as root, so it can only use disks the server has already mounted. Start it with sudo to mount and format disks here.");
}
if (note === "hardware") {
return T("storage/note/hardware", "Hardware management is turned off (-enable_hwman=false), so disks cannot be mounted or formatted here.");
}
return T("storage/note/platform", "Disks can be listed and mounted here on Linux. On this server, add a folder as a drive instead.");
}
W.wizardDrives = function (kind) {
return W.progress.drives.filter(function (d) {
return kind === "network" ? d.kind === "network" : d.kind !== "network";
});
};
W.addWizardDrive = function (drive) {
W.progress.drives = W.progress.drives.filter(function (d) { return d.uuid !== drive.uuid; });
W.progress.drives.push(drive);
W.save();
};
//Removes a drive the guide added, keeping the files on it
W.removeWizardDrive = function (drive, done) {
ssConfirm({
title: T("drive/remove/title", "Remove {name}?", { name: drive.name }),
text: T("drive/remove/text", "ArozOS stops using this drive. The files on it are not deleted."),
confirmLabel: T("drive/remove/confirm", "Remove"),
cancelLabel: T("common/cancel", "Cancel"),
danger: true
}, function () {
W.api("POST", "system/storage/pool/removeHandler", { group: "system", uuid: drive.uuid }).then(function () {
W.progress.drives = W.progress.drives.filter(function (d) { return d.uuid !== drive.uuid; });
delete W.progress.access[drive.uuid];
W.save();
W.toast(T("drive/removed", "{name} was removed", { name: drive.name }));
done();
}, function (err) {
W.toast(err.message, true);
});
});
};
W.renderAddedDrives = function ($target, kind, onChange) {
var drives = W.wizardDrives(kind);
$target.empty();
if (drives.length === 0) {
return;
}
$target.append('
' + esc(T("drive/added", "Added in this guide")) + "
");
});
}
function renderDisks(el) {
var $box = $(el).find("#wzDisks").empty();
var canMount = W.state.canMountDisks;
if (disksCache.length === 0) {
$box.html('
' + esc(T("storage/disks/none", "No extra disks were found. Plug one in and press Refresh.")) + "
");
return;
}
disksCache.forEach(function (disk) {
var $card = $('');
var sub = [disk.path, W.sizeText(disk.size)];
if (disk.removable) {
sub.push(T("storage/removable", "Removable"));
}
$card.append('
");
var $rows = $('');
disk.partitions.forEach(function (part) {
var title = part.label || (disk.whole ? T("storage/wholeDisk", "Whole disk") : part.name);
var facts = [part.fstype ? part.fstype.toUpperCase() : T("storage/blank", "Not formatted"), W.sizeText(part.size)];
if (part.mountpoint) {
facts.push(part.mountpoint);
}
var actions = "";
if (part.canAdd && (part.addMode === "folder" || canMount)) {
actions += '";
}
if (part.canFormat && canMount) {
actions += '";
}
var hint = "";
if (!part.canAdd && part.canFormat && !part.inUse && !part.driveId) {
hint = part.fstype ? T("storage/hint/unsupported", "ArozOS cannot use this file system. Format the disk to use it.")
: T("storage/hint/blank", "Format this disk to use it.");
}
var $row = $('').attr("data-device", part.path).html(
'
' + esc(T("storage/hint/diskInUse", "Part of this disk is in use, so it cannot be formatted here.")) + "
");
}
$box.append($card);
});
}
function openAddPanel(el, $row, disk, part) {
$(el).find(".wz-inline-panel").remove();
var name = part.label || disk.model || part.name;
var id = W.suggestId(name) || part.name;
var $panel = $('').html(
'
' +
field("name", T("drive/field/name", "Name"), name) +
field("id", T("drive/field/id", "Drive ID"), id, T("drive/field/id/hint", "Files on it will be at {id}:/", { id: id })) +
layoutField("public") +
'
" +
'" +
(part.addMode === "folder" ? '' + esc(T("storage/folderMode", "Already mounted at {path}, so it is used as it is.", { path: part.mountpoint })) + ""
: '' + esc(T("storage/mountMode", "ArozOS mounts it at /media/{id} every time it starts.", { id: id })) + "") +
"
");
bindIdField($panel);
$panel.find("[data-action=cancel]").on("click", function () {
$panel.remove();
});
$panel.find("[data-action=save]").on("click", function () {
var button = this;
var data = {
device: part.path,
name: $panel.find("[data-field=name]").val().trim(),
id: $panel.find("[data-field=id]").val().trim(),
hierarchy: $panel.find("[data-field=hierarchy]").val()
};
if (!W.validId(data.id)) {
$panel.find("[data-field=id]").addClass("invalid").trigger("focus");
W.toast(T("drive/field/id/invalid", "A drive ID can only use letters, numbers, - and _"), true);
return;
}
W.working(button, true);
W.api("POST", "system/setup/disk/add", data).then(function (reply) {
W.addWizardDrive({ uuid: data.id, name: data.name || data.id, kind: "disk", path: reply.path || "" });
W.toast(T("drive/addedToast", "{name} is ready as {id}:/", { name: data.name || data.id, id: data.id }));
refreshAdded(el);
loadDisks(el);
}, function (err) {
W.working(button, false);
W.toast(err.message, true);
});
});
$row.append($panel);
$panel.find("input").first().trigger("focus");
}
function openFormat(el, disk, part) {
var defaultLabel = (part.label || disk.model || "Storage").replace(/[^A-Za-z0-9 _-]/g, "").trim().substring(0, 16);
var body =
'
' + W.icon("warning") + "
" +
esc(T("format/warning", "Everything on {device} ({size}) will be erased. This cannot be undone.", { device: part.path, size: W.sizeText(part.size) })) +
"
" +
'
" +
'
" +
'
" +
'
' +
'
" +
'
' +
'";
W.modal({
title: T("format/title", "Format {name}?", { name: part.label || disk.model || part.name }),
icon: "warning",
body: body,
actions: [
{ label: T("common/cancel", "Cancel") },
{
label: T("format/confirm", "Erase and format"),
kind: "danger",
onClick: function (dialog, button) {
var $m = $(dialog.el);
if (!$m.find("[data-field=agree]").prop("checked")) {
W.toast(T("format/needAgree", "Tick the box to confirm that the data may be erased"), true);
return;
}
var password = $m.find("[data-field=password]").val();
if (!password) {
$m.find("[data-field=password]").addClass("invalid").trigger("focus");
return;
}
W.working(button, true);
$m.find("input, select").prop("disabled", true);
W.api("POST", "system/setup/disk/format", {
device: part.path,
fstype: $m.find("[data-field=fstype]").val(),
label: $m.find("[data-field=label]").val(),
password: password
}).then(function () {
dialog.close();
W.toast(T("format/done", "{device} was formatted", { device: part.path }));
loadDisks(el, part.path);
}, function (err) {
W.working(button, false);
$m.find("input, select").prop("disabled", false);
W.toast(err.message, true);
});
}
}
]
});
}
/* Folder drive */
function field(name, label, value, hint, type) {
return '
" +
'' +
(hint !== undefined ? '
' + esc(hint) + "
" : "") + "
";
}
function layoutField(value) {
return '
" +
'
";
}
//Keeps the drive id following the name until the user edits it
function bindIdField($scope) {
var touched = false;
$scope.find("[data-field=id]").on("input", function () {
touched = true;
$(this).removeClass("invalid");
$scope.find("[data-hint=id]").text(T("drive/field/id/hint", "Files on it will be at {id}:/", { id: $(this).val() || "id" }));
});
$scope.find("[data-field=name]").on("input", function () {
if (!touched) {
var id = W.suggestId($(this).val());
$scope.find("[data-field=id]").val(id);
$scope.find("[data-hint=id]").text(T("drive/field/id/hint", "Files on it will be at {id}:/", { id: id || "id" }));
}
});
}
W.bindIdField = bindIdField;
W.driveField = field;
W.layoutField = layoutField;
function folderConfig($form) {
return {
name: $form.find("[data-field=name]").val().trim(),
uuid: $form.find("[data-field=id]").val().trim(),
path: $form.find("[data-field=path]").val().trim(),
access: "readwrite",
hierarchy: $form.find("[data-field=hierarchy]").val(),
automount: false
};
}
function renderFolderForm(el) {
var $form = $(el).find("#wzFolderForm");
var placeholder = W.state.platform === "windows" ? "D:\\Media" : "/mnt/storage";
$form.html('
' +
field("name", T("drive/field/name", "Name"), "") +
field("id", T("drive/field/id", "Drive ID"), "", T("drive/field/id/hint", "Files on it will be at {id}:/", { id: "id" })) +
'
" +
'
' +
layoutField("public") +
'
' +
'" +
'" +
'
');
bindIdField($form);
$form.find("[data-action=test]").on("click", function () {
var $result = $form.find("[data-result]").attr("class", "wz-result").text(T("common/checking", "Checking..."));
W.api("POST", "system/storage/pool/test", { config: JSON.stringify(folderConfig($form)), group: "system" }).then(function () {
$result.attr("class", "wz-result ok").text(T("folder/ok", "The folder exists"));
}, function (err) {
$result.attr("class", "wz-result bad").text(err.message);
});
});
$form.find("[data-action=add]").on("click", function () {
var button = this;
var config = folderConfig($form);
if (!config.name || !config.path) {
W.toast(T("folder/missing", "Enter a name and a folder"), true);
return;
}
if (!W.validId(config.uuid)) {
$form.find("[data-field=id]").addClass("invalid").trigger("focus");
W.toast(T("drive/field/id/invalid", "A drive ID can only use letters, numbers, - and _"), true);
return;
}
W.working(button, true);
W.api("POST", "system/storage/pool/edit", { opr: "new", group: "system", config: JSON.stringify(config) }).then(function (reply) {
W.working(button, false);
W.addWizardDrive({ uuid: config.uuid, name: config.name, kind: "folder", path: config.path });
if (reply && reply.mounted === false) {
W.toast(T("drive/offline", "Saved, but the drive could not be opened: {reason}", { reason: reply.reason || "" }), true);
} else {
W.toast(T("drive/addedToast", "{name} is ready as {id}:/", { name: config.name, id: config.uuid }));
}
renderFolderForm(el);
refreshAdded(el);
}, function (err) {
W.working(button, false);
W.toast(err.message, true);
});
});
}
function refreshAdded(el) {
W.renderAddedDrives($(el).find("#wzAddedDrives"), "local", function () {
refreshAdded(el);
if (W.state.canListDisks) {
loadDisks(el);
}
});
W.setNextLabel(W.wizardDrives("local").length ? T("nav/continue", "Continue") : T("storage/next/builtin", "Continue with built-in storage"));
}
W.register({
id: "storage",
title: function () {
return T("step/storage", "Storage");
},
nextLabel: function () {
return W.wizardDrives("local").length ? T("nav/continue", "Continue") : T("storage/next/builtin", "Continue with built-in storage");
},
render: function (el) {
var st = W.state;
var html = '
' + esc(T("storage/eyebrow", "Storage")) + "
" +
'
' + esc(T("storage/title", "Where should your files live?")) + "
" +
'
' + esc(T("storage/lead", "Your server can already store files: each person has a home folder (user:/) on its system disk. Add a disk to get more room, or keep it simple and continue.")) + "
";
if (st.canListDisks) {
if (!st.canMountDisks) {
html += '
' + W.icon("info") + "
" + esc(diskNoteText(st.diskNote)) + "
";
}
html += '
' + esc(T("storage/disks", "Disks in this server")) + "" +
'
" +
'';
} else {
html += '
' + W.icon("info") + "
" + esc(diskNoteText(st.diskNote)) + "
";
}
html += '';
html += '
' + esc(st.canListDisks ? T("folder/titleAlt", "Or use a folder on this server") : T("folder/title", "Use a folder on this server")) + "
" +
'
' +
esc(T("folder/lead", "Any folder the server can reach, such as a disk your system already mounts, can become a drive.")) +
'
';
el.innerHTML = html;
renderFolderForm(el);
refreshAdded(el);
if (st.canListDisks) {
$(el).find("#wzDiskRefresh").on("click", function () {
loadDisks(el);
});
loadDisks(el);
}
}
});
})();