| 1 |
/* |
| 2 |
* Copyright (C) 2026 Red Hat, Inc. |
| 3 |
* SPDX-License-Identifier: LGPL-2.1-or-later |
| 4 |
*/ |
| 5 |
import React from "react"; |
| 6 |
import cockpit from "cockpit"; |
| 7 |
|
| 8 |
import { Alert } from "@patternfly/react-core/dist/esm/components/Alert/index.js"; |
| 9 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 10 |
import { Flex } from "@patternfly/react-core/dist/esm/layouts/Flex/index.js"; |
| 11 |
import { Modal, ModalBody, ModalFooter, ModalHeader } from '@patternfly/react-core/dist/esm/components/Modal/index.js'; |
| 12 |
import { Spinner } from "@patternfly/react-core/dist/esm/components/Spinner/index.js"; |
| 13 |
import { Stack, StackItem } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 14 |
|
| 15 |
import { ModalError } from 'cockpit-components-inline-notification.jsx'; |
| 16 |
|
| 17 |
import { TwoColumnContent } from "./two-column"; |
| 18 |
|
| 19 |
const _ = cockpit.gettext; |
| 20 |
|
| 21 |
export interface RestartPackages { |
| 22 |
reboot: string[]; |
| 23 |
daemons: string[]; |
| 24 |
manual: string[]; |
| 25 |
} |
| 26 |
|
| 27 |
interface RestartServicesDialogProps { |
| 28 |
restartPackages: RestartPackages; |
| 29 |
close: () => void; |
| 30 |
state: string; |
| 31 |
checkNeedsRestart: () => void; |
| 32 |
onValueChanged: (delta: { restartPackages: RestartPackages }) => void; |
| 33 |
loadUpdates: () => void; |
| 34 |
checkRestartRunning?: boolean; |
| 35 |
} |
| 36 |
|
| 37 |
interface RestartServicesDialogState { |
| 38 |
dialogError: string | undefined; |
| 39 |
dialogErrorDetail: string | undefined; |
| 40 |
restartInProgress: boolean; |
| 41 |
} |
| 42 |
|
| 43 |
export class RestartServicesDialog extends React.Component<RestartServicesDialogProps, RestartServicesDialogState> { |
| 44 |
constructor(props: RestartServicesDialogProps) { |
| 45 |
super(props); |
| 46 |
this.state = { |
| 47 |
dialogError: undefined, |
| 48 |
dialogErrorDetail: undefined, |
| 49 |
restartInProgress: false, |
| 50 |
}; |
| 51 |
|
| 52 |
this.restart = this.restart.bind(this); |
| 53 |
} |
| 54 |
|
| 55 |
restart() { |
| 56 |
// make sure cockpit package is the last to restart |
| 57 |
const daemons = [...this.props.restartPackages.daemons].sort((a, b) => { |
| 58 |
if (a.includes("cockpit") && b.includes("cockpit")) |
| 59 |
return 0; |
| 60 |
if (a.includes("cockpit")) |
| 61 |
return 1; |
| 62 |
return a.localeCompare(b); |
| 63 |
}); |
| 64 |
const restarts = daemons.map(service => cockpit.spawn(["systemctl", "restart", service], { superuser: "require", err: "message" })); |
| 65 |
this.setState({ restartInProgress: true, dialogError: undefined, dialogErrorDetail: undefined }); |
| 66 |
Promise.all(restarts) |
| 67 |
.then(() => { |
| 68 |
this.props.onValueChanged({ restartPackages: { reboot: this.props.restartPackages.reboot, daemons: [], manual: this.props.restartPackages.manual } }); |
| 69 |
if (this.props.state === "updateSuccess") |
| 70 |
this.props.loadUpdates(); |
| 71 |
this.setState({ restartInProgress: false }); |
| 72 |
this.props.close(); |
| 73 |
}) |
| 74 |
.catch(ex => { |
| 75 |
this.setState({ dialogError: _("Failed to restart service"), dialogErrorDetail: ex.message }); |
| 76 |
// see what services remain |
| 77 |
this.props.checkNeedsRestart(); |
| 78 |
}); |
| 79 |
} |
| 80 |
|
| 81 |
render() { |
| 82 |
let body; |
| 83 |
if (this.props.checkRestartRunning) { |
| 84 |
body = ( |
| 85 |
<Flex spaceItems={{ default: 'spaceItemsSm' }} alignItems={{ default: 'alignItemsCenter' }}> |
| 86 |
<Spinner size="sm" /> |
| 87 |
<p>{_("Reloading the state of remaining services")}</p> |
| 88 |
</Flex> |
| 89 |
); |
| 90 |
} else if (this.props.restartPackages.daemons.length > 0) { |
| 91 |
body = (<> |
| 92 |
{cockpit.ngettext("The following service will be restarted:", "The following services will be restarted:", this.props.restartPackages.daemons.length)} |
| 93 |
<TwoColumnContent list={this.props.restartPackages.daemons} flexClassName="restart-services-modal-body" /> |
| 94 |
</>); |
| 95 |
} |
| 96 |
|
| 97 |
return ( |
| 98 |
<Modal id="restart-services-modal" isOpen |
| 99 |
position="top" |
| 100 |
variant="medium" |
| 101 |
onClose={this.props.close}> |
| 102 |
<ModalHeader title={_("Restart services")} /> |
| 103 |
<ModalBody> |
| 104 |
<Stack hasGutter> |
| 105 |
{this.state.dialogError && |
| 106 |
<ModalError dialogError={this.state.dialogError} {...(this.state.dialogErrorDetail && { dialogErrorDetail: this.state.dialogErrorDetail })} />} |
| 107 |
<StackItem>{body}</StackItem> |
| 108 |
</Stack> |
| 109 |
</ModalBody> |
| 110 |
<ModalFooter> |
| 111 |
{this.props.restartPackages.daemons.includes("cockpit") && |
| 112 |
<Alert variant="warning" |
| 113 |
title={_("Web Console will restart")} |
| 114 |
isInline> |
| 115 |
<p> |
| 116 |
{_("When the Web Console is restarted, you will no longer see progress information. However, the update process will continue in the background. Reconnect to continue watching the update process.")} |
| 117 |
</p> |
| 118 |
</Alert>} |
| 119 |
<Button variant='primary' |
| 120 |
isDisabled={ this.state.restartInProgress } |
| 121 |
onClick={ this.restart }> |
| 122 |
{_("Restart services")} |
| 123 |
</Button> |
| 124 |
<Button variant='link' className='btn-cancel' onClick={ this.props.close }> |
| 125 |
{_("Cancel")} |
| 126 |
</Button> |
| 127 |
</ModalFooter> |
| 128 |
</Modal> |
| 129 |
); |
| 130 |
} |
| 131 |
} |