| 1 |
/* |
| 2 |
* Copyright (C) 2021 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
|
| 6 |
import React from "react"; |
| 7 |
import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; |
| 8 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 9 |
import { Checkbox } from "@patternfly/react-core/dist/esm/components/Checkbox/index.js"; |
| 10 |
import { Form } from "@patternfly/react-core/dist/esm/components/Form/index.js"; |
| 11 |
import { Flex, FlexItem } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 12 |
import { |
| 13 |
Modal, ModalBody, ModalFooter, ModalHeader |
| 14 |
} from '@patternfly/react-core/dist/esm/components/Modal/index.js'; |
| 15 |
import { Radio } from "@patternfly/react-core/dist/esm/components/Radio/index.js"; |
| 16 |
import { Popover } from "@patternfly/react-core/dist/esm/components/Popover/index.js"; |
| 17 |
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js"; |
| 18 |
import { Split, SplitItem } from "@patternfly/react-core/dist/esm/layouts/Split/index.js"; |
| 19 |
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 20 |
import { Content, ContentVariants } from "@patternfly/react-core/dist/esm/components/Content/index.js"; |
| 21 |
import { InfoIcon, InfoCircleIcon } from "@patternfly/react-icons"; |
| 22 |
import { Icon } from "@patternfly/react-core/dist/esm/components/Icon/index.js"; |
| 23 |
|
| 24 |
import cockpit from "cockpit"; |
| 25 |
import { proxy as serviceProxy } from "service"; |
| 26 |
import { install_dialog } from "cockpit-components-install-dialog.jsx"; |
| 27 |
import { getPackageManager } from "packagemanager"; |
| 28 |
import type { PackageManager } from "_internal/packagemanager-abstract"; |
| 29 |
|
| 30 |
const _ = cockpit.gettext; |
| 31 |
|
| 32 |
type ServiceProxy = ReturnType<typeof serviceProxy> & cockpit.EventSource<{ changed(): void }>; |
| 33 |
|
| 34 |
interface KpatchSettingsProps { |
| 35 |
privileged?: boolean; |
| 36 |
} |
| 37 |
|
| 38 |
interface KpatchSettingsState { |
| 39 |
loaded: boolean; |
| 40 |
auto: boolean | null; |
| 41 |
enabled: boolean | null; |
| 42 |
missing: string[]; |
| 43 |
unavailable: string[]; |
| 44 |
error: string; |
| 45 |
updating: boolean; |
| 46 |
showModal: boolean; |
| 47 |
applyCheckbox: boolean; |
| 48 |
justCurrent: boolean | null; |
| 49 |
kernelName: string; |
| 50 |
patchName: string | null; |
| 51 |
patchInstalled: boolean | null; |
| 52 |
patchUnavailable: boolean | null; |
| 53 |
packageManager: PackageManager | null; |
| 54 |
} |
| 55 |
|
| 56 |
export class KpatchSettings extends React.Component<KpatchSettingsProps, KpatchSettingsState> { |
| 57 |
kpatchService: ServiceProxy; |
| 58 |
|
| 59 |
constructor(props: KpatchSettingsProps) { |
| 60 |
super(props); |
| 61 |
|
| 62 |
this.state = { |
| 63 |
loaded: false, |
| 64 |
auto: null, // `dnf kpatch` is set to `auto` |
| 65 |
enabled: null, // kpatch.service is enabled |
| 66 |
missing: [], // missing packages from `kpatch`, `kpatch-dnf` |
| 67 |
unavailable: [], // unavailable packages from `kpatch`, `kpatch-dnf` |
| 68 |
|
| 69 |
// Modal states |
| 70 |
error: "", |
| 71 |
updating: false, |
| 72 |
showModal: false, |
| 73 |
applyCheckbox: false, // state of the checkbox |
| 74 |
justCurrent: null, // radio state |
| 75 |
|
| 76 |
kernelName: "", // uname -r |
| 77 |
patchName: null, // kpatch-patch name |
| 78 |
patchInstalled: null, // kpatch-patch installed |
| 79 |
patchUnavailable: null, // kpatch-patch available |
| 80 |
|
| 81 |
packageManager: null, |
| 82 |
}; |
| 83 |
|
| 84 |
this.kpatchService = serviceProxy("kpatch") as ServiceProxy; |
| 85 |
|
| 86 |
this.checkSetup = this.checkSetup.bind(this); |
| 87 |
this.handleChange = this.handleChange.bind(this); |
| 88 |
this.onClose = this.onClose.bind(this); |
| 89 |
this.handleInstall = this.handleInstall.bind(this); |
| 90 |
} |
| 91 |
|
| 92 |
// Only current patches or also future ones |
| 93 |
current(enabled: boolean | null, installed: boolean | null, unavailable: boolean | null) { |
| 94 |
return enabled && (installed || unavailable); |
| 95 |
} |
| 96 |
|
| 97 |
async componentDidMount() { |
| 98 |
this.kpatchService.addEventListener('changed', () => { |
| 99 |
this.setState(state => { |
| 100 |
const current = this.current(this.kpatchService.enabled, state.patchInstalled, state.patchUnavailable); |
| 101 |
return ({ |
| 102 |
enabled: this.kpatchService.enabled, |
| 103 |
justCurrent: current && !state.auto, |
| 104 |
applyCheckbox: !!current, |
| 105 |
}); |
| 106 |
}); |
| 107 |
}); |
| 108 |
|
| 109 |
const packageManager = await getPackageManager(); |
| 110 |
this.setState({ packageManager }); |
| 111 |
packageManager.check_missing_packages(["kpatch", "kpatch-dnf"]) |
| 112 |
.then(d => |
| 113 |
this.checkSetup().then(() => |
| 114 |
this.setState({ |
| 115 |
loaded: true, |
| 116 |
unavailable: d.unavailable_names || [], |
| 117 |
missing: d.missing_names || [], |
| 118 |
}) |
| 119 |
) |
| 120 |
) |
| 121 |
.catch((e: cockpit.BasicError) => console.log("Could not determine kpatch availability:", JSON.stringify(e))); |
| 122 |
} |
| 123 |
|
| 124 |
checkSetup() { |
| 125 |
// TODO - replace both with `dnf kpatch status` once https://github.com/dynup/kpatch-dnf/pull/8 lands |
| 126 |
const kpatch_promise = cockpit.file("/etc/dnf/plugins/kpatch.conf").read() |
| 127 |
.then(data => { |
| 128 |
if (data) { |
| 129 |
const auto = /autoupdate\s*=\s*True/i.test(data); |
| 130 |
this.setState((state, _) => { |
| 131 |
const current = this.current(state.enabled, state.patchInstalled, state.patchUnavailable); |
| 132 |
return ({ |
| 133 |
auto: !!auto, |
| 134 |
justCurrent: current && !auto, |
| 135 |
applyCheckbox: !!current, |
| 136 |
}); |
| 137 |
}); |
| 138 |
} |
| 139 |
}) |
| 140 |
.catch(() => true); // Ignore errors, most likely just does not exist |
| 141 |
|
| 142 |
const uname_promise = cockpit.spawn(["uname", "-r"]) |
| 143 |
.then(data => { |
| 144 |
const fields = data.split("-"); |
| 145 |
// if there's no release field, we don't have an official kernel |
| 146 |
if (!fields[1]) |
| 147 |
return; |
| 148 |
const kpp_kernel_version = fields[0].replaceAll(".", "_"); |
| 149 |
let release = fields[1].split("."); |
| 150 |
release = release.slice(0, release.length - 2); // remove el8.x86_64 |
| 151 |
const kpp_kernel_release = release.join("_"); |
| 152 |
const patch_name = ["kpatch-patch", kpp_kernel_version, kpp_kernel_release].join("-"); |
| 153 |
cockpit.assert(this.state.packageManager, "packageManager not initialised"); |
| 154 |
return this.state.packageManager.check_missing_packages([patch_name]) |
| 155 |
.then(d => |
| 156 |
this.setState((state, _) => { |
| 157 |
const installed = (d.unavailable_names || []).length === 0 && (d.missing_names || []).length === 0; |
| 158 |
const unavailable = (d.unavailable_names || []).length > 0; |
| 159 |
const current = this.current(state.enabled, installed, unavailable); |
| 160 |
return ({ |
| 161 |
kernelName: data, |
| 162 |
patchName: patch_name, |
| 163 |
patchInstalled: installed, |
| 164 |
patchUnavailable: unavailable, |
| 165 |
justCurrent: current && !state.auto, |
| 166 |
applyCheckbox: !!current, |
| 167 |
}); |
| 168 |
}) |
| 169 |
); |
| 170 |
}) |
| 171 |
.catch(err => console.error("Could not determine kpatch packages:", JSON.stringify(err))); // not-covered: OS error |
| 172 |
|
| 173 |
return Promise.allSettled([kpatch_promise, uname_promise]); |
| 174 |
} |
| 175 |
|
| 176 |
handleInstall() { |
| 177 |
this.setState({ updating: true }); |
| 178 |
install_dialog(this.state.missing) |
| 179 |
.then(() => this.setState({ missing: [], updating: false })) |
| 180 |
.catch(() => this.setState({ updating: false })); |
| 181 |
} |
| 182 |
|
| 183 |
onClose() { |
| 184 |
this.setState((state, _) => { |
| 185 |
const current = this.current(state.enabled, state.patchInstalled, state.patchUnavailable); |
| 186 |
return ({ |
| 187 |
justCurrent: current && !state.auto, |
| 188 |
applyCheckbox: !!current, |
| 189 |
showModal: false, |
| 190 |
error: "", |
| 191 |
}); |
| 192 |
}); |
| 193 |
} |
| 194 |
|
| 195 |
handleChange() { |
| 196 |
this.setState({ updating: true }); |
| 197 |
|
| 198 |
if (this.state.applyCheckbox) { |
| 199 |
let install; |
| 200 |
if (this.state.justCurrent) { |
| 201 |
install = new Promise<void>((resolve, reject) => { |
| 202 |
cockpit.spawn(["dnf", "-y", "kpatch", "manual"], { superuser: "require", err: "message" }) |
| 203 |
.then(() => { |
| 204 |
if (!this.state.patchUnavailable && !this.state.patchInstalled) |
| 205 |
// TODO - replace with `dnf kpatch install` once https://github.com/dynup/kpatch-dnf/pull/8 lands |
| 206 |
cockpit.spawn(["dnf", "-y", "install", this.state.patchName!], { superuser: "require", err: "message" }).then(() => resolve()) |
| 207 |
.catch(reject); |
| 208 |
else |
| 209 |
resolve(); |
| 210 |
}) |
| 211 |
.catch(reject); |
| 212 |
}); |
| 213 |
} else { |
| 214 |
install = cockpit.spawn(["dnf", "-y", "kpatch", "auto"], { superuser: "require", err: "message" }); |
| 215 |
} |
| 216 |
install |
| 217 |
.then(() => |
| 218 |
this.kpatchService.enable().then(() => |
| 219 |
this.kpatchService.start().then(() => |
| 220 |
this.setState({ showModal: false, error: "" }) |
| 221 |
) |
| 222 |
) |
| 223 |
) |
| 224 |
.catch((e: cockpit.BasicError) => this.setState({ error: e.toString() })) |
| 225 |
.finally(() => this.checkSetup().then(() => this.setState({ updating: false }))); |
| 226 |
} else { |
| 227 |
cockpit.spawn(["dnf", "-y", "kpatch", "manual"], { superuser: "require", err: "message" }) |
| 228 |
.then(() => |
| 229 |
this.kpatchService.disable().then(() => |
| 230 |
this.kpatchService.stop().then(() => |
| 231 |
this.setState({ showModal: false, error: "" }) |
| 232 |
) |
| 233 |
) |
| 234 |
) |
| 235 |
.catch((e: cockpit.BasicError) => this.setState({ error: e.toString() })) |
| 236 |
.finally(() => this.checkSetup().then(() => this.setState({ updating: false }))); |
| 237 |
} |
| 238 |
} |
| 239 |
|
| 240 |
render() { |
| 241 |
let state; |
| 242 |
let actionText = _("Edit"); |
| 243 |
let action = () => this.setState({ showModal: true }); |
| 244 |
|
| 245 |
if (this.state.loaded === false || this.state.patchName === null) { |
| 246 |
// Not yet recognized |
| 247 |
state = <Spinner size="md" />; |
| 248 |
} else if (this.state.unavailable.length > 0) { |
| 249 |
state = <Popover headerContent={ _("Unavailable packages") } bodyContent={ this.state.unavailable.join(", ") }> |
| 250 |
<span> |
| 251 |
{ _("Not available") } |
| 252 |
|
| 253 |
<Icon status="info"> |
| 254 |
<InfoCircleIcon className="ct-info-circle" /> |
| 255 |
</Icon> |
| 256 |
</span> |
| 257 |
</Popover>; |
| 258 |
} else if (this.state.missing.length > 0) { |
| 259 |
state = _("Not installed"); |
| 260 |
actionText = _("Install"); |
| 261 |
action = this.handleInstall; |
| 262 |
} else if (!this.state.enabled) { |
| 263 |
state = _("Disabled"); |
| 264 |
actionText = _("Enable"); |
| 265 |
} else { |
| 266 |
state = _("Enabled"); |
| 267 |
} |
| 268 |
|
| 269 |
const kernel_name = this.state.kernelName ? " (" + this.state.kernelName + ")" : ""; |
| 270 |
const error = this.state.error ? <Alert variant='danger' isInline title={this.state.error} /> : null; |
| 271 |
|
| 272 |
const body = <Form><Checkbox id="apply-kpatch" |
| 273 |
isChecked={this.state.applyCheckbox} |
| 274 |
label={_("Apply kernel live patches")} |
| 275 |
onChange={(_event, checked) => this.setState({ applyCheckbox: checked })} |
| 276 |
body={<> |
| 277 |
<Radio id="current-future" |
| 278 |
name="policy" |
| 279 |
label={_("for current and future kernels")} |
| 280 |
onChange={() => this.setState({ justCurrent: false })} |
| 281 |
isDisabled={!this.state.applyCheckbox} |
| 282 |
isChecked={!this.state.justCurrent} /> |
| 283 |
<Radio id="current-only" |
| 284 |
name="policy" |
| 285 |
label={_("for current kernel only") + kernel_name} |
| 286 |
onChange={() => this.setState({ justCurrent: true })} |
| 287 |
isDisabled={!this.state.applyCheckbox} |
| 288 |
isChecked={!!this.state.justCurrent} /> |
| 289 |
</>} |
| 290 |
/></Form>; |
| 291 |
|
| 292 |
return (<> |
| 293 |
<div id="kpatch-settings"> |
| 294 |
<Flex alignItems={{ default: 'alignItemsCenter' }}> |
| 295 |
<Flex grow={{ default: 'grow' }} alignItems={{ default: 'alignItemsBaseline' }}> |
| 296 |
<FlexItem> |
| 297 |
<b>{_("Kernel live patching")}</b> |
| 298 |
</FlexItem> |
| 299 |
<FlexItem> |
| 300 |
{state} |
| 301 |
</FlexItem> |
| 302 |
</Flex> |
| 303 |
<Flex> |
| 304 |
<Button variant="secondary" |
| 305 |
size="sm" |
| 306 |
isDisabled={!this.props.privileged || this.state.updating || !this.state.loaded || this.state.unavailable.length > 0} |
| 307 |
onClick={action}> |
| 308 |
{actionText} |
| 309 |
</Button> |
| 310 |
</Flex> |
| 311 |
</Flex> |
| 312 |
</div> |
| 313 |
<Modal position="top" variant="small" id="kpatch-setup" isOpen={this.state.showModal} |
| 314 |
onClose={ this.onClose } |
| 315 |
> |
| 316 |
<ModalHeader title={_("Kernel live patch settings")} /> |
| 317 |
<ModalBody> |
| 318 |
{error} |
| 319 |
{body} |
| 320 |
</ModalBody> |
| 321 |
<ModalFooter> |
| 322 |
<Button variant="primary" |
| 323 |
isLoading={ this.state.updating } |
| 324 |
isDisabled={ this.state.updating } |
| 325 |
onClick={ this.handleChange }> |
| 326 |
{_("Save")} |
| 327 |
</Button> |
| 328 |
<Button variant="link" |
| 329 |
isDisabled={ this.state.updating } |
| 330 |
onClick={ this.onClose }> |
| 331 |
{_("Cancel")} |
| 332 |
</Button> |
| 333 |
</ModalFooter> |
| 334 |
</Modal> |
| 335 |
</>); |
| 336 |
} |
| 337 |
} |
| 338 |
|
| 339 |
interface KpatchStatusState { |
| 340 |
loaded: string[]; |
| 341 |
installed: string[]; |
| 342 |
changelog: string | null; |
| 343 |
} |
| 344 |
|
| 345 |
export class KpatchStatus extends React.Component<Record<string, never>, KpatchStatusState> { |
| 346 |
constructor(props: Record<string, never>) { |
| 347 |
super(props); |
| 348 |
|
| 349 |
this.state = { |
| 350 |
loaded: [], |
| 351 |
installed: [], |
| 352 |
changelog: null, // FIXME - load changelog |
| 353 |
}; |
| 354 |
} |
| 355 |
|
| 356 |
componentDidMount() { |
| 357 |
cockpit.spawn(["kpatch", "list"], { superuser: "try", err: "ignore", environ: ["LC_MESSAGES=C"] }) |
| 358 |
.then(m => { |
| 359 |
const parts = m.trim().split("\n\n"); |
| 360 |
if (parts.length !== 2 || |
| 361 |
!parts[0].startsWith("Loaded patch modules:") || |
| 362 |
!parts[1].startsWith("Installed patch modules:")) { |
| 363 |
console.warn("Unexpected output from `kpatch list`", m); |
| 364 |
return; |
| 365 |
} |
| 366 |
|
| 367 |
const loaded = parts[0].split("\n") |
| 368 |
.slice(1) |
| 369 |
.map(i => i.split(" ")[0]); |
| 370 |
const installed = parts[1].split("\n") |
| 371 |
.slice(1) |
| 372 |
.map(i => i.split(" ")[0]); |
| 373 |
this.setState({ loaded, installed }); |
| 374 |
}) |
| 375 |
.catch(() => true); // Ignore errors |
| 376 |
} |
| 377 |
|
| 378 |
render() { |
| 379 |
let text: React.ReactNode[] = []; |
| 380 |
text = this.state.loaded.map(i => |
| 381 |
<Content key={i} component={ContentVariants.p}> |
| 382 |
{ cockpit.format(_("Kernel live patch $0 is active"), i) } |
| 383 |
</Content> |
| 384 |
); |
| 385 |
|
| 386 |
if (text.length === 0) |
| 387 |
text = this.state.installed.map(i => |
| 388 |
<Content key={i} component={ContentVariants.p}> |
| 389 |
{ cockpit.format(_("Kernel live patch $0 is installed"), i) } |
| 390 |
</Content> |
| 391 |
); |
| 392 |
|
| 393 |
if (text.length > 0) |
| 394 |
return ( |
| 395 |
<Split hasGutter> |
| 396 |
<SplitItem> |
| 397 |
<InfoIcon /> |
| 398 |
</SplitItem> |
| 399 |
<SplitItem isFilled> |
| 400 |
<Stack> |
| 401 |
{text} |
| 402 |
</Stack> |
| 403 |
</SplitItem> |
| 404 |
</Split> |
| 405 |
); |
| 406 |
|
| 407 |
return null; |
| 408 |
} |
| 409 |
} |