| 1 |
/* |
| 2 |
* Copyright (C) 2021 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import React, { useContext, useEffect, useState } from 'react'; |
| 7 |
import cockpit from 'cockpit'; |
| 8 |
import { getPackageManager } from "packagemanager"; |
| 9 |
|
| 10 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 11 |
import { Checkbox } from "@patternfly/react-core/dist/esm/components/Checkbox/index.js"; |
| 12 |
import { Form, FormGroup } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 13 |
import { |
| 14 |
Modal, ModalBody, ModalFooter, ModalHeader |
| 15 |
} from '@patternfly/react-core/dist/esm/components/Modal/index.js'; |
| 16 |
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 17 |
import { TextInput } from "@patternfly/react-core/dist/esm/components/TextInput/index.js"; |
| 18 |
|
| 19 |
import { BondDialog, getGhostSettings as getBondGhostSettings } from './bond.jsx'; |
| 20 |
import { BridgeDialog, getGhostSettings as getBridgeGhostSettings } from './bridge.jsx'; |
| 21 |
import { BridgePortDialog } from './bridgeport.jsx'; |
| 22 |
import { IpSettingsDialog } from './ip-settings.jsx'; |
| 23 |
import { TeamDialog, getGhostSettings as getTeamGhostSettings } from './team.jsx'; |
| 24 |
import { TeamPortDialog } from './teamport.jsx'; |
| 25 |
import { VlanDialog, getGhostSettings as getVlanGhostSettings } from './vlan.jsx'; |
| 26 |
import { WireGuardDialog, getWireGuardGhostSettings } from './wireguard.jsx'; |
| 27 |
import { MtuDialog } from './mtu.jsx'; |
| 28 |
import { MacDialog } from './mac.jsx'; |
| 29 |
import { ModalError } from 'cockpit-components-inline-notification.jsx'; |
| 30 |
import { ModelContext } from './model-context.jsx'; |
| 31 |
import { useDialogs } from "dialogs.jsx"; |
| 32 |
import { install_dialog } from "cockpit-components-install-dialog.jsx"; |
| 33 |
import { read_os_release } from "os-release.js"; |
| 34 |
import { TypeaheadSelect } from "cockpit-components-typeahead-select"; |
| 35 |
|
| 36 |
import { |
| 37 |
apply_group_member, |
| 38 |
syn_click, |
| 39 |
with_checkpoint, with_settings_checkpoint, |
| 40 |
connection_devices, |
| 41 |
settings_applier, |
| 42 |
show_unexpected_error, |
| 43 |
} from './interfaces.js'; |
| 44 |
import { isNonPersistentMultiCon } from './utils.js'; |
| 45 |
|
| 46 |
const _ = cockpit.gettext; |
| 47 |
// nm-dbus-interface.h |
| 48 |
const NM_CAPABILITY_TEAM = 1; |
| 49 |
|
| 50 |
export const MacMenu = ({ idPrefix, model, mac, setMAC }) => { |
| 51 |
const [optionsMap, setOptionsMap] = useState([]); |
| 52 |
|
| 53 |
useEffect(() => { |
| 54 |
// Find all macs and the interfaces that use them. |
| 55 |
const macs = {}; |
| 56 |
model.list_interfaces().forEach(iface => { |
| 57 |
if (iface.Device && iface.Device.HwAddress && iface.Device.HwAddress !== "00:00:00:00:00:00") { |
| 58 |
if (!macs[iface.Device.HwAddress]) |
| 59 |
macs[iface.Device.HwAddress] = []; |
| 60 |
macs[iface.Device.HwAddress].push(iface.Name); |
| 61 |
} |
| 62 |
}); |
| 63 |
|
| 64 |
const optionsMapInit = []; |
| 65 |
Object.keys(macs).sort().forEach(mac => { |
| 66 |
optionsMapInit.push({ |
| 67 |
content: cockpit.format("$0 ($1)", mac, macs[mac].join(", ")), |
| 68 |
value: mac, |
| 69 |
}); |
| 70 |
}); |
| 71 |
|
| 72 |
optionsMapInit.push( |
| 73 |
{ content: _("Permanent"), value: "permanent" }, |
| 74 |
{ content: _("Preserve"), value: "preserve" }, |
| 75 |
{ content: _("Random"), value: "random" }, |
| 76 |
{ content: _("Stable"), value: "stable" }, |
| 77 |
); |
| 78 |
setOptionsMap(optionsMapInit); |
| 79 |
}, [model]); |
| 80 |
|
| 81 |
const clearSelection = () => { |
| 82 |
setMAC(undefined); |
| 83 |
}; |
| 84 |
|
| 85 |
const onSelect = (_, selection) => { |
| 86 |
setMAC(selection); |
| 87 |
}; |
| 88 |
|
| 89 |
return ( |
| 90 |
<TypeaheadSelect toggleProps={{ id: idPrefix + "-mac-input" }} |
| 91 |
isScrollable |
| 92 |
placeholder="" |
| 93 |
isCreatable |
| 94 |
createOptionMessage={val => cockpit.format(_("Use $0"), val)} |
| 95 |
onClearSelection={clearSelection} |
| 96 |
onSelect={onSelect} |
| 97 |
selected={mac} |
| 98 |
selectOptions={optionsMap} /> |
| 99 |
); |
| 100 |
}; |
| 101 |
|
| 102 |
export const MemberInterfaceChoices = ({ idPrefix, memberChoices, setMemberChoices, model, group }) => { |
| 103 |
return ( |
| 104 |
<Stack id={idPrefix + "-interface-members-list"}> |
| 105 |
{Object.keys(memberChoices).map((iface, idx) => ( |
| 106 |
<Checkbox data-iface={iface} |
| 107 |
id={idPrefix + "-interface-members-" + iface} |
| 108 |
isChecked={memberChoices[iface]} |
| 109 |
key={iface} |
| 110 |
label={iface} |
| 111 |
onChange={(_event, checked) => setMemberChoices({ ...memberChoices, [iface]: checked })} |
| 112 |
/> |
| 113 |
))} |
| 114 |
</Stack> |
| 115 |
); |
| 116 |
}; |
| 117 |
|
| 118 |
export const Name = ({ idPrefix, iface, setIface }) => { |
| 119 |
return ( |
| 120 |
<FormGroup fieldId={idPrefix + "-interface-name-input"} label={_("Name")}> |
| 121 |
<TextInput id={idPrefix + "-interface-name-input"} value={iface} onChange={(_event, value) => setIface(value)} /> |
| 122 |
</FormGroup> |
| 123 |
); |
| 124 |
}; |
| 125 |
|
| 126 |
export const NetworkModal = ({ dialogError, help, idPrefix, title, onSubmit, children, isFormHorizontal, isCreateDialog, submitDisabled = false }) => { |
| 127 |
const Dialogs = useDialogs(); |
| 128 |
|
| 129 |
return ( |
| 130 |
<Modal id={idPrefix + "-dialog"} position="top" variant="medium" |
| 131 |
isOpen |
| 132 |
onClose={Dialogs.close} |
| 133 |
> |
| 134 |
<ModalHeader title={title} help={help} /> |
| 135 |
<ModalBody> |
| 136 |
<Form id={idPrefix + "-body"} onSubmit={onSubmit} isHorizontal={isFormHorizontal !== false}> |
| 137 |
{dialogError && <ModalError id={idPrefix + "-error"} dialogError={_("Failed to save settings")} dialogErrorDetail={dialogError} />} |
| 138 |
{children} |
| 139 |
</Form> |
| 140 |
</ModalBody> |
| 141 |
<ModalFooter> |
| 142 |
<Button variant='primary' id={idPrefix + "-save"} onClick={onSubmit} isDisabled={submitDisabled}> |
| 143 |
{isCreateDialog ? _("Add") : _("Save")} |
| 144 |
</Button> |
| 145 |
<Button variant='link' id={idPrefix + "-cancel"} onClick={Dialogs.close}> |
| 146 |
{_("Cancel")} |
| 147 |
</Button> |
| 148 |
</ModalFooter> |
| 149 |
</Modal> |
| 150 |
); |
| 151 |
}; |
| 152 |
|
| 153 |
export const NetworkAction = ({ buttonText, iface, connectionSettings, type }) => { |
| 154 |
const Dialogs = useDialogs(); |
| 155 |
const model = useContext(ModelContext); |
| 156 |
|
| 157 |
if (type == "team" && !model.get_manager().Capabilities.includes(NM_CAPABILITY_TEAM)) |
| 158 |
return null; |
| 159 |
|
| 160 |
const con = iface && iface.MainConnection; |
| 161 |
const dev = iface && iface.Device; |
| 162 |
|
| 163 |
const getName = () => { |
| 164 |
let name; |
| 165 |
// Find the first free interface name |
| 166 |
for (let i = 0; i < 100; i++) { |
| 167 |
name = type + i; |
| 168 |
if (!model.find_interface(name)) |
| 169 |
break; |
| 170 |
} |
| 171 |
return name; |
| 172 |
}; |
| 173 |
|
| 174 |
const newIfaceName = !iface ? getName() : undefined; |
| 175 |
|
| 176 |
let settings = connectionSettings; |
| 177 |
if (!settings) { |
| 178 |
if (type == 'bond') settings = getBondGhostSettings({ newIfaceName }); |
| 179 |
if (type == 'vlan') settings = getVlanGhostSettings(); |
| 180 |
if (type == 'team') settings = getTeamGhostSettings({ newIfaceName }); |
| 181 |
if (type == 'bridge') settings = getBridgeGhostSettings({ newIfaceName }); |
| 182 |
if (type == 'wg') settings = getWireGuardGhostSettings({ newIfaceName }); |
| 183 |
} |
| 184 |
|
| 185 |
const properties = { connection: con, dev, settings }; |
| 186 |
|
| 187 |
async function resolveDeps(type) { |
| 188 |
if (type === 'wg') { |
| 189 |
try { |
| 190 |
await cockpit.script("command -v wg"); |
| 191 |
} catch { |
| 192 |
const os_release = await read_os_release(); |
| 193 |
|
| 194 |
try { |
| 195 |
await getPackageManager(); |
| 196 |
// RHEL 8 does not have wireguard-tools |
| 197 |
if (os_release.PLATFORM_ID !== "platform:el8") |
| 198 |
await install_dialog("wireguard-tools"); |
| 199 |
} catch (exc) { |
| 200 |
console.log("no package manager support"); |
| 201 |
} |
| 202 |
} |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
function show() { |
| 207 |
let dlg = null; |
| 208 |
if (type == 'bond') |
| 209 |
dlg = <BondDialog {...properties} />; |
| 210 |
else if (type == 'vlan') |
| 211 |
dlg = <VlanDialog {...properties} />; |
| 212 |
else if (type == 'team') |
| 213 |
dlg = <TeamDialog {...properties} />; |
| 214 |
else if (type == 'bridge') |
| 215 |
dlg = <BridgeDialog {...properties} />; |
| 216 |
else if (type == 'wg') |
| 217 |
dlg = <WireGuardDialog {...properties} />; |
| 218 |
else if (type == 'mtu') |
| 219 |
dlg = <MtuDialog {...properties} />; |
| 220 |
else if (type == 'mac') |
| 221 |
dlg = <MacDialog {...properties} />; |
| 222 |
else if (type == 'teamport') |
| 223 |
dlg = <TeamPortDialog {...properties} />; |
| 224 |
else if (type == 'bridgeport') |
| 225 |
dlg = <BridgePortDialog {...properties} />; |
| 226 |
else if (type == 'ipv4') |
| 227 |
dlg = <IpSettingsDialog topic="ipv4" {...properties} />; |
| 228 |
else if (type == 'ipv6') |
| 229 |
dlg = <IpSettingsDialog topic="ipv6" {...properties} />; |
| 230 |
|
| 231 |
if (dlg) |
| 232 |
resolveDeps(type) |
| 233 |
.then(() => Dialogs.show(dlg)) |
| 234 |
.catch(err => console.error("NetworkAction Dialog failed:", err)); // not-covered: OS error |
| 235 |
} |
| 236 |
|
| 237 |
return ( |
| 238 |
<Button id={"networking-" + (!iface ? "add-" : "edit-") + type} |
| 239 |
isInline={!!iface} |
| 240 |
onClick={syn_click(model, show)} |
| 241 |
variant={!iface ? "secondary" : "link"}> |
| 242 |
{buttonText || _("edit")} |
| 243 |
</Button> |
| 244 |
); |
| 245 |
}; |
| 246 |
|
| 247 |
function reactivateConnection({ con, dev }) { |
| 248 |
if (con.Settings.connection.interface_name && |
| 249 |
con.Settings.connection.interface_name != dev.Interface) { |
| 250 |
return dev.disconnect() |
| 251 |
.then(() => con.activate(null, null)) |
| 252 |
.catch(show_unexpected_error); |
| 253 |
} else { |
| 254 |
return con.activate(dev, null) |
| 255 |
.catch(show_unexpected_error); |
| 256 |
} |
| 257 |
} |
| 258 |
|
| 259 |
export const dialogSave = ({ model, dev, connection, members, membersInit, settings, setDialogError, onClose }) => { |
| 260 |
const apply_settings = settings_applier(model, dev, connection); |
| 261 |
const iface = settings.connection.interface_name ?? dev?.Interface; |
| 262 |
const type = settings.connection.type; |
| 263 |
const membersChanged = members ? Object.keys(membersInit).some(iface => membersInit[iface] != members[iface]) : false; |
| 264 |
|
| 265 |
model.set_operation_in_progress(true); |
| 266 |
|
| 267 |
const modify = () => { |
| 268 |
return ((members !== undefined) |
| 269 |
? apply_group_member(members, |
| 270 |
model, |
| 271 |
apply_settings, |
| 272 |
connection, |
| 273 |
settings, |
| 274 |
type) |
| 275 |
: apply_settings(settings)) |
| 276 |
.then(() => { |
| 277 |
onClose(); |
| 278 |
if (connection && iface) |
| 279 |
cockpit.location.go([iface]); |
| 280 |
else if (!connection && iface) |
| 281 |
return model.synchronize().then(() => cockpit.location.go([iface])); |
| 282 |
if (connection && dev?.ActiveConnection?.Connection === connection && !isNonPersistentMultiCon(connection)) { |
| 283 |
return reactivateConnection({ con: connection, dev }); |
| 284 |
} |
| 285 |
}) |
| 286 |
.catch(ex => setDialogError(typeof ex === 'string' ? ex : ex.message)) |
| 287 |
.then(() => model.set_operation_in_progress(false)); |
| 288 |
}; |
| 289 |
if (connection) { |
| 290 |
with_settings_checkpoint(model, modify, |
| 291 |
{ |
| 292 |
...(type != 'vlan' && { |
| 293 |
devices: (membersChanged ? [] : connection_devices(connection)) |
| 294 |
}), |
| 295 |
hack_does_add_or_remove: type == 'vlan' || membersChanged || isNonPersistentMultiCon(connection), |
| 296 |
rollback_on_failure: type !== 'vlan' && membersChanged |
| 297 |
}); |
| 298 |
} else { |
| 299 |
try { |
| 300 |
with_checkpoint( |
| 301 |
model, |
| 302 |
modify, |
| 303 |
{ |
| 304 |
fail_text: cockpit.format(_("Creating this $0 will break the connection to the server, and will make the administration UI unavailable."), type == 'vlan' ? 'VLAN' : type), |
| 305 |
anyway_text: _("Create it"), |
| 306 |
hack_does_add_or_remove: true, |
| 307 |
rollback_on_failure: type != 'vlan', |
| 308 |
}); |
| 309 |
} catch (e) { |
| 310 |
setDialogError(typeof e === 'string' ? e : e.message); |
| 311 |
} |
| 312 |
} |
| 313 |
}; |