| 1 |
/* |
| 2 |
* Copyright (C) 2021 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 |
|
| 9 |
import { Button } from "@patternfly/react-core/dist/esm/components/Button/index.js"; |
| 10 |
import { ClipboardCopy } from "@patternfly/react-core/dist/esm/components/ClipboardCopy/index.js"; |
| 11 |
import { Page, PageSection } from "@patternfly/react-core/dist/esm/components/Page/index.js"; |
| 12 |
import { Stack } from "@patternfly/react-core/dist/esm/layouts/Stack/index.js"; |
| 13 |
import { ExclamationCircleIcon } from "@patternfly/react-icons"; |
| 14 |
|
| 15 |
import { EmptyStatePanel } from "cockpit-components-empty-state"; |
| 16 |
|
| 17 |
import { codes } from "./hosts_dialog.jsx"; |
| 18 |
import { Machine } from "./machines/machines"; |
| 19 |
|
| 20 |
const _ = cockpit.gettext; |
| 21 |
|
| 22 |
export const EarlyFailure = () => { |
| 23 |
let ca_cert_url = null; |
| 24 |
if (window.navigator.userAgent.indexOf("Safari") >= 0) |
| 25 |
ca_cert_url = window.sessionStorage.getItem("CACertUrl"); |
| 26 |
|
| 27 |
return ( |
| 28 |
<div id="early-failure" className="early-failure"> |
| 29 |
<Page className="pf-m-no-sidebar"> |
| 30 |
<PageSection hasBodyWrapper={false}> |
| 31 |
<EmptyStatePanel icon={ExclamationCircleIcon} |
| 32 |
title={ _("Connection failed") } |
| 33 |
paragraph={ |
| 34 |
<Stack hasGutter> |
| 35 |
<div>{_("There was an unexpected error while connecting to the machine.")}</div> |
| 36 |
<div>{_("Messages related to the failure might be found in the journal:")}</div> |
| 37 |
<ClipboardCopy isReadOnly hoverTip={_("Copy")} clickTip={_("Copied")}>journalctl -u cockpit</ClipboardCopy> |
| 38 |
{ca_cert_url && <div id="safari-cert-help"> |
| 39 |
<div>{_("Safari users need to import and trust the certificate of the self-signing CA:")}</div> |
| 40 |
<Button variant="link" component="a" id="safari-cert" href={ca_cert_url} download>ca.cer</Button> |
| 41 |
</div>} |
| 42 |
</Stack> |
| 43 |
} /> |
| 44 |
</PageSection> |
| 45 |
</Page> |
| 46 |
</div> |
| 47 |
); |
| 48 |
}; |
| 49 |
|
| 50 |
const EarlyFailureReady = ({ |
| 51 |
loading = false, |
| 52 |
title, |
| 53 |
paragraph, |
| 54 |
reconnect = false, |
| 55 |
troubleshoot = false, |
| 56 |
onTroubleshoot = () => {}, |
| 57 |
onReconnect = () => {}, |
| 58 |
} : { |
| 59 |
loading?: boolean, |
| 60 |
title: string, |
| 61 |
paragraph: string, |
| 62 |
reconnect?: boolean, |
| 63 |
troubleshoot?: boolean, |
| 64 |
onTroubleshoot?: () => void, |
| 65 |
onReconnect?: () => void, |
| 66 |
}) => { |
| 67 |
return ( |
| 68 |
<div id="early-failure-ready" className="curtains-ct"> |
| 69 |
<Page className="pf-m-no-sidebar"> |
| 70 |
<PageSection hasBodyWrapper={false}> |
| 71 |
<EmptyStatePanel {... !loading ? { icon: ExclamationCircleIcon } : {} } |
| 72 |
loading={loading} |
| 73 |
title={title} |
| 74 |
action={<> |
| 75 |
{reconnect && |
| 76 |
<Button id="machine-reconnect" onClick={onReconnect}> |
| 77 |
{_("Reconnect")} |
| 78 |
</Button>} |
| 79 |
{troubleshoot && |
| 80 |
<Button id="machine-troubleshoot" onClick={onTroubleshoot}> |
| 81 |
{_("Log in")} |
| 82 |
</Button>} |
| 83 |
</>} |
| 84 |
paragraph={paragraph} /> |
| 85 |
</PageSection> |
| 86 |
</Page> |
| 87 |
</div> |
| 88 |
); |
| 89 |
}; |
| 90 |
|
| 91 |
export const Disconnected = ({ |
| 92 |
problem |
| 93 |
} : { |
| 94 |
problem: string |
| 95 |
}) => { |
| 96 |
return ( |
| 97 |
<EarlyFailureReady title={_("Disconnected")} |
| 98 |
reconnect |
| 99 |
onReconnect={() => { |
| 100 |
cockpit.sessionStorage.clear(); |
| 101 |
window.location.reload(); |
| 102 |
}} |
| 103 |
paragraph={cockpit.message(problem)} /> |
| 104 |
); |
| 105 |
}; |
| 106 |
|
| 107 |
export const MachineTroubleshoot = ({ |
| 108 |
machine, |
| 109 |
onClick |
| 110 |
} : { |
| 111 |
machine: Machine, |
| 112 |
onClick: () => void |
| 113 |
}) => { |
| 114 |
const connecting = (machine.state == "connecting"); |
| 115 |
let title; |
| 116 |
let message; |
| 117 |
if (machine.restarting) { |
| 118 |
title = _("The machine is rebooting"); |
| 119 |
message = ""; |
| 120 |
} else if (connecting) { |
| 121 |
title = _("Connecting to the machine"); |
| 122 |
message = ""; |
| 123 |
} else { |
| 124 |
title = _("Not connected to host"); |
| 125 |
if (machine.problem == "not-found") { |
| 126 |
message = _("Cannot connect to an unknown host"); |
| 127 |
} else { |
| 128 |
const error = machine.problem || machine.state; |
| 129 |
if (error) |
| 130 |
message = cockpit.message(error); |
| 131 |
else |
| 132 |
message = ""; |
| 133 |
} |
| 134 |
} |
| 135 |
|
| 136 |
let troubleshooting = false; |
| 137 |
|
| 138 |
if (!machine.restarting && (machine.problem === "no-host" || (machine.problem && machine.problem in codes))) { |
| 139 |
troubleshooting = true; |
| 140 |
} |
| 141 |
|
| 142 |
const restarting = !!machine.restarting; |
| 143 |
const reconnect = !connecting && machine.problem != "not-found" && !troubleshooting; |
| 144 |
|
| 145 |
return ( |
| 146 |
<EarlyFailureReady loading={connecting || restarting} |
| 147 |
title={title} |
| 148 |
reconnect={reconnect} |
| 149 |
troubleshoot={troubleshooting} |
| 150 |
onTroubleshoot={onClick} |
| 151 |
onReconnect={onClick} |
| 152 |
paragraph={message} /> |
| 153 |
); |
| 154 |
}; |