| 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 { install_dialog } from "cockpit-components-install-dialog.jsx"; |
| 11 |
|
| 12 |
import { Card, CardBody } from "@patternfly/react-core/dist/esm/components/Card/index.js"; |
| 13 |
import { Stack, StackItem } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 14 |
import { DropdownGroup, DropdownList } from '@patternfly/react-core/dist/esm/components/Dropdown/index.js'; |
| 15 |
|
| 16 |
import { StoragePlots } from "../plot.jsx"; |
| 17 |
import { StorageMenuItem, StorageBarMenu } from "../storage-controls.jsx"; |
| 18 |
import { dialog_open } from "../dialog.jsx"; |
| 19 |
import { StorageLogsPanel } from "../logs-panel.jsx"; |
| 20 |
|
| 21 |
import { create_mdraid } from "../mdraid/create-dialog.jsx"; |
| 22 |
import { create_vgroup } from "../lvm2/create-dialog.jsx"; |
| 23 |
import { create_stratis_pool } from "../stratis/create-dialog.jsx"; |
| 24 |
import { iscsi_change_name, iscsi_discover } from "../iscsi/create-dialog.jsx"; |
| 25 |
import { get_other_devices } from "../utils.js"; |
| 26 |
|
| 27 |
import { new_page, new_card, StorageCard, ChildrenTable } from "../pages.jsx"; |
| 28 |
import { make_drive_page } from "../drive/drive.jsx"; |
| 29 |
import { make_lvm2_volume_group_page } from "../lvm2/volume-group.jsx"; |
| 30 |
import { make_mdraid_page } from "../mdraid/mdraid.jsx"; |
| 31 |
import { make_stratis_pool_page } from "../stratis/pool.jsx"; |
| 32 |
import { make_stratis_stopped_pool_page } from "../stratis/stopped-pool.jsx"; |
| 33 |
import { make_nfs_page, nfs_fstab_dialog } from "../nfs/nfs.jsx"; |
| 34 |
import { make_iscsi_session_page } from "../iscsi/session.jsx"; |
| 35 |
import { make_other_page } from "../block/other.jsx"; |
| 36 |
import { make_btrfs_volume_page } from "../btrfs/volume.jsx"; |
| 37 |
|
| 38 |
const _ = cockpit.gettext; |
| 39 |
|
| 40 |
export function make_overview_page() { |
| 41 |
const overview_card = new_card({ |
| 42 |
title: _("Storage"), |
| 43 |
page_location: [], |
| 44 |
page_name: _("Storage"), |
| 45 |
component: OverviewCard |
| 46 |
}); |
| 47 |
|
| 48 |
const overview_page = new_page(null, overview_card); |
| 49 |
|
| 50 |
Object.keys(client.iscsi_sessions).forEach(p => make_iscsi_session_page(overview_page, client.iscsi_sessions[p])); |
| 51 |
Object.keys(client.drives).forEach(p => { |
| 52 |
if (!client.drives_iscsi_session[p]) |
| 53 |
make_drive_page(overview_page, client.drives[p]); |
| 54 |
}); |
| 55 |
Object.keys(client.vgroups).forEach(p => make_lvm2_volume_group_page(overview_page, client.vgroups[p])); |
| 56 |
Object.keys(client.mdraids).forEach(p => make_mdraid_page(overview_page, client.mdraids[p])); |
| 57 |
Object.keys(client.stratis_pools).map(p => make_stratis_pool_page(overview_page, client.stratis_pools[p])); |
| 58 |
Object.keys(client.stratis_manager.StoppedPools).map(uuid => make_stratis_stopped_pool_page(overview_page, uuid)); |
| 59 |
client.nfs.entries.forEach(e => make_nfs_page(overview_page, e)); |
| 60 |
get_other_devices(client).map(p => make_other_page(overview_page, client.blocks[p])); |
| 61 |
Object.keys(client.uuids_btrfs_volume).forEach(uuid => make_btrfs_volume_page(overview_page, uuid)); |
| 62 |
} |
| 63 |
|
| 64 |
const OverviewCard = ({ card, plot_state }) => { |
| 65 |
function menu_item(feature, title, action) { |
| 66 |
const feature_enabled = !feature || feature.is_enabled(); |
| 67 |
const required_package = feature && feature.package; |
| 68 |
|
| 69 |
if (!feature_enabled && !(required_package && client.features.packagekit)) |
| 70 |
return null; |
| 71 |
|
| 72 |
function install_then_action() { |
| 73 |
if (!feature_enabled) { |
| 74 |
install_dialog(required_package, feature.dialog_options).then( |
| 75 |
() => { |
| 76 |
feature.enable() |
| 77 |
.then(action) |
| 78 |
.catch(error => { |
| 79 |
dialog_open({ |
| 80 |
Title: _("Error"), |
| 81 |
Body: error.toString() |
| 82 |
}); |
| 83 |
}); |
| 84 |
}, |
| 85 |
() => null /* ignore cancel */); |
| 86 |
} else { |
| 87 |
action(); |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
return <StorageMenuItem key={title} onClick={install_then_action}>{title}</StorageMenuItem>; |
| 92 |
} |
| 93 |
|
| 94 |
const lvm2_feature = { |
| 95 |
is_enabled: () => client.features.lvm2 |
| 96 |
}; |
| 97 |
|
| 98 |
const stratis_feature = { |
| 99 |
is_enabled: () => client.features.stratis, |
| 100 |
package: client.get_config("stratis_package", false), |
| 101 |
enable: () => { |
| 102 |
return cockpit.spawn(["systemctl", "start", "stratisd"], { superuser: "require" }) |
| 103 |
.then(() => client.stratis_start()); |
| 104 |
}, |
| 105 |
|
| 106 |
dialog_options: { |
| 107 |
title: _("Install Stratis support"), |
| 108 |
text: _("The $0 package must be installed to create Stratis pools.") |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
const nfs_feature = { |
| 113 |
is_enabled: () => client.features.nfs, |
| 114 |
package: client.get_config("nfs_client_package", false), |
| 115 |
enable: () => { |
| 116 |
client.features.nfs = true; |
| 117 |
client.nfs.start(); |
| 118 |
return Promise.resolve(); |
| 119 |
}, |
| 120 |
|
| 121 |
dialog_options: { |
| 122 |
title: _("Install NFS support") |
| 123 |
} |
| 124 |
}; |
| 125 |
|
| 126 |
const iscsi_feature = { |
| 127 |
is_enabled: () => client.features.iscsi, |
| 128 |
}; |
| 129 |
|
| 130 |
const local_menu_items = [ |
| 131 |
menu_item(null, _("Create MDRAID device"), () => create_mdraid()), |
| 132 |
menu_item(lvm2_feature, _("Create LVM2 volume group"), () => create_vgroup()), |
| 133 |
menu_item(stratis_feature, _("Create Stratis pool"), () => create_stratis_pool()), |
| 134 |
].filter(item => !!item); |
| 135 |
|
| 136 |
const net_menu_items = [ |
| 137 |
!client.in_anaconda_mode() && menu_item(nfs_feature, _("New NFS mount"), () => nfs_fstab_dialog(null, null)), |
| 138 |
menu_item(iscsi_feature, _("Change iSCSI initiator name"), () => iscsi_change_name()), |
| 139 |
menu_item(iscsi_feature, _("Add iSCSI portal"), () => iscsi_discover()), |
| 140 |
].filter(item => !!item); |
| 141 |
|
| 142 |
const groups = []; |
| 143 |
|
| 144 |
if (local_menu_items.length > 0) |
| 145 |
groups.push( |
| 146 |
<DropdownGroup key="local" label={_("Local storage")}> |
| 147 |
<DropdownList> |
| 148 |
{local_menu_items} |
| 149 |
</DropdownList> |
| 150 |
</DropdownGroup>); |
| 151 |
|
| 152 |
if (net_menu_items.length > 0) |
| 153 |
groups.push( |
| 154 |
<DropdownGroup key="net" label={_("Networked storage")}> |
| 155 |
<DropdownList> |
| 156 |
{net_menu_items} |
| 157 |
</DropdownList> |
| 158 |
</DropdownGroup>); |
| 159 |
|
| 160 |
const actions = <StorageBarMenu label={_("Create storage device")} menuItems={groups} />; |
| 161 |
|
| 162 |
return ( |
| 163 |
<Stack hasGutter> |
| 164 |
{ !client.in_anaconda_mode() && |
| 165 |
<StackItem> |
| 166 |
<Card isPlain> |
| 167 |
<CardBody> |
| 168 |
<StoragePlots plot_state={plot_state} /> |
| 169 |
</CardBody> |
| 170 |
</Card> |
| 171 |
</StackItem> |
| 172 |
} |
| 173 |
<StackItem> |
| 174 |
<StorageCard card={card} actions={actions}> |
| 175 |
<ChildrenTable |
| 176 |
emptyCaption={_("No storage found")} |
| 177 |
aria-label={_("Storage")} |
| 178 |
show_icons |
| 179 |
page={card.page} |
| 180 |
/> |
| 181 |
</StorageCard> |
| 182 |
</StackItem> |
| 183 |
{ !client.in_anaconda_mode() && |
| 184 |
<StackItem> |
| 185 |
<StorageLogsPanel /> |
| 186 |
</StackItem> |
| 187 |
} |
| 188 |
</Stack>); |
| 189 |
}; |