Debian Codegraph @

pkg/storaged/nfs/nfs.jsx

raw
1 /*
2 * Copyright (C) 2023 Red Hat, Inc.
3 * SPDX-License-Identifier: LGPL-2.1-or-later
4 */
5
6 import cockpit from "cockpit";
7 import React from "react";
8 import client from "../client";
9
10 import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js";
11 import { CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js";
12 import { DescriptionList } from "@patternfly/react-core/dist/esm/components/DescriptionList/index.js";
13
14 import { NetworkIcon } from "../icons/gnome-icons.jsx";
15 import {
16 dialog_open, TextInput, ComboBox, CheckBoxes,
17 StopProcessesMessage, stop_processes_danger_message
18 } from "../dialog.jsx";
19
20 import { StorageUsageBar } from "../storage-controls.jsx";
21 import {
22 StorageCard, StorageDescription,
23 new_page, new_card, PAGE_CATEGORY_NETWORK,
24 navigate_to_new_card_location, navigate_away_from_card
25 } from "../pages.jsx";
26 import { parse_options, unparse_options, extract_option } from "../utils.js";
27
28 const _ = cockpit.gettext;
29
30 function nfs_busy_dialog(dialog_title, entry, error, action_title, action) {
31 function show(users) {
32 if (users.length === 0) {
33 dialog_open({
34 Title: dialog_title,
35 Body: error.toString()
36 });
37 } else {
38 dialog_open({
39 Title: dialog_title,
40 Teardown: <StopProcessesMessage users={users} />,
41 Action: {
42 DangerButton: true,
43 Danger: stop_processes_danger_message(users),
44 Title: action_title,
45 action: function () {
46 return action(users);
47 }
48 }
49 });
50 }
51 }
52
53 client.nfs.entry_users(entry)
54 .then(function (users) {
55 show(users);
56 })
57 .catch(function () {
58 show([]);
59 });
60 }
61
62 function get_exported_directories(server) {
63 return cockpit.spawn(["showmount", "-e", "--no-headers", server], { err: "message" })
64 .then(function (output) {
65 const dirs = [];
66 output.split("\n").forEach(function (line) {
67 const d = line.split(" ")[0];
68 if (d)
69 dirs.push(d);
70 });
71 return dirs;
72 });
73 }
74
75 export function nfs_fstab_dialog(entry, card) {
76 const mount_options = entry ? entry.fields[3] : "defaults";
77 const split_options = parse_options(mount_options == "defaults" ? "" : mount_options);
78 const opt_auto = !extract_option(split_options, "noauto");
79 const opt_ro = extract_option(split_options, "ro");
80 const extra_options = unparse_options(split_options);
81
82 function mounting_options(vals) {
83 let opts = [];
84 if (!vals.mount_options.auto)
85 opts.push("noauto");
86 if (vals.mount_options.ro)
87 opts.push("ro");
88 if (vals.mount_options.extra !== false)
89 opts = opts.concat(parse_options(vals.mount_options.extra));
90 return unparse_options(opts);
91 }
92
93 function show(busy) {
94 let alert = null;
95 if (busy)
96 alert = <>
97 <Alert isInline variant="danger" title={_("This NFS mount is in use and only its options can be changed.")} />
98 <br />
99 </>;
100
101 let server_to_check = null;
102 let server_check_timeout = null;
103
104 function check_server(dlg, server, delay) {
105 if (server_check_timeout)
106 window.clearTimeout(server_check_timeout);
107 server_to_check = server;
108 server_check_timeout = window.setTimeout(() => {
109 server_check_timeout = null;
110 dlg.set_options("remote", { choices: [] });
111 get_exported_directories(server).then(choices => {
112 if (server == server_to_check)
113 dlg.set_options("remote", { choices });
114 });
115 }, delay);
116 }
117
118 const dlg = dialog_open({
119 Title: entry ? _("NFS mount") : _("New NFS mount"),
120 Body: alert,
121 Fields: [
122 TextInput("server", _("Server address"),
123 {
124 value: entry ? entry.fields[0].split(":")[0] : "",
125 validate: function (val) {
126 if (val === "")
127 return _("Server cannot be empty.");
128 },
129 disabled: busy
130 }),
131 ComboBox("remote", _("Path on server"),
132 {
133 value: entry ? entry.fields[0].split(":")[1] : "",
134 validate: function (val) {
135 if (val === "")
136 return _("Path on server cannot be empty.");
137 if (val[0] !== "/")
138 return _("Path on server must start with \"/\".");
139 },
140 disabled: busy,
141 choices: [],
142 }),
143 TextInput("dir", _("Local mount point"),
144 {
145 value: entry ? entry.fields[1] : "",
146 validate: function (val) {
147 if (val === "")
148 return _("Mount point cannot be empty.");
149 if (val[0] !== "/")
150 return _("Mount point must start with \"/\".");
151 },
152 disabled: busy
153 }),
154 CheckBoxes("mount_options", _("Mount options"),
155 {
156 fields: [
157 { title: _("Mount at boot"), tag: "auto" },
158 { title: _("Mount read only"), tag: "ro" },
159 { title: _("Custom mount options"), tag: "extra", type: "checkboxWithInput" },
160 ],
161 value: {
162 auto: opt_auto,
163 ro: opt_ro,
164 extra: extra_options === "" ? false : extra_options
165 }
166 },
167 ),
168 ],
169 update: (dlg, vals, trigger) => {
170 if (trigger === "server")
171 check_server(dlg, vals.server, 500);
172 },
173 Action: {
174 Title: entry ? _("Save") : _("Add"),
175 action: async function (vals) {
176 const fields = [
177 vals.server + ":" + vals.remote,
178 vals.dir,
179 entry ? entry.fields[2] : "nfs",
180 mounting_options(vals) || "defaults"
181 ];
182 if (entry) {
183 await client.nfs.update_entry(entry, fields);
184 if (entry.fields[0] != fields[0] || entry.fields[1] != fields[1])
185 navigate_to_new_card_location(card, ["nfs", fields[0], fields[1]]);
186 } else
187 await client.nfs.add_entry(fields);
188 }
189 }
190 });
191
192 if (entry && !busy)
193 check_server(dlg, entry.fields[0].split(":")[0], 0);
194 }
195
196 if (entry) {
197 client.nfs.entry_users(entry)
198 .then(function (users) {
199 show(users.length > 0);
200 })
201 .catch(function () {
202 show(false);
203 });
204 } else
205 show(false);
206 }
207
208 function checked(error_title, promise) {
209 promise.catch(error => {
210 dialog_open({
211 Title: error_title,
212 Body: error.toString()
213 });
214 });
215 }
216
217 function mount(entry) {
218 checked("Could not mount the filesystem",
219 client.nfs.mount_entry(entry));
220 }
221
222 function unmount(entry, card) {
223 client.nfs.unmount_entry(entry)
224 .then(function () {
225 if (!entry.fstab)
226 navigate_away_from_card(card);
227 })
228 .catch(function (error) {
229 nfs_busy_dialog(_("Unable to unmount filesystem"),
230 entry, error,
231 _("Stop and unmount"),
232 function (users) {
233 return client.nfs.stop_and_unmount_entry(users, entry)
234 .then(function () {
235 if (!entry.fstab)
236 navigate_away_from_card(card);
237 });
238 });
239 });
240 }
241
242 function edit(entry, card) {
243 nfs_fstab_dialog(entry, card);
244 }
245
246 function remove(entry, card) {
247 client.nfs.remove_entry(entry)
248 .then(function () {
249 navigate_away_from_card(card);
250 })
251 .catch(function (error) {
252 nfs_busy_dialog(_("Unable to remove mount"),
253 entry, error,
254 _("Stop and remove"),
255 function (users) {
256 return client.nfs.stop_and_remove_entry(users, entry)
257 .then(function () {
258 navigate_away_from_card(card);
259 });
260 });
261 });
262 }
263
264 const NfsEntryUsageBar = ({ entry, not_mounted_text, short }) => {
265 if (entry.mounted)
266 return <StorageUsageBar stats={client.nfs.get_fsys_size(entry)} critical={0.95} block={entry.fields[1]}
267 short={short} />;
268 else
269 return not_mounted_text;
270 };
271
272 export function make_nfs_page(parent, entry) {
273 if (client.in_anaconda_mode())
274 return;
275
276 const remote = entry.fields[0];
277 const local = entry.fields[1];
278 let mount_point = local;
279 if (!entry.mounted)
280 mount_point += " " + _("(not mounted)");
281
282 const nfs_card = new_card({
283 title: _("NFS mount"),
284 location: mount_point,
285 next: null,
286 page_location: ["nfs", remote, local],
287 page_name: remote,
288 page_icon: NetworkIcon,
289 page_category: PAGE_CATEGORY_NETWORK,
290 page_size: <NfsEntryUsageBar key="size" entry={entry} not_mounted_text={null} short />,
291 component: NfsCard,
292 props: { entry },
293 actions: [
294 (entry.mounted
295 ? { title: _("Unmount"), action: () => unmount(entry, nfs_card) }
296 : { title: _("Mount"), action: () => mount(entry) }),
297 (entry.fstab
298 ? { title: _("Edit"), action: () => edit(entry, nfs_card) }
299 : null),
300 (entry.fstab
301 ? { title: _("Remove"), action: () => remove(entry, nfs_card), danger: true }
302 : null),
303 ]
304 });
305
306 new_page(parent, nfs_card);
307 }
308
309 const NfsCard = ({ card, entry }) => {
310 return (
311 <StorageCard card={card}>
312 <CardBody>
313 <DescriptionList className="pf-m-horizontal-on-sm">
314 <StorageDescription title={_("Server")} value={entry.fields[0]} />
315 <StorageDescription title={_("Mount point")} value={entry.fields[1]} />
316 <StorageDescription title={_("Size")}>
317 <NfsEntryUsageBar entry={entry} not_mounted_text="--" />
318 </StorageDescription>
319 </DescriptionList>
320 </CardBody>
321 </StorageCard>
322 );
323 };