forked from cockpit-project/cockpit-machines
-
Notifications
You must be signed in to change notification settings - Fork 0
/
consoles.jsx
130 lines (113 loc) · 4.64 KB
/
consoles.jsx
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* This file is part of Cockpit.
*
* Copyright (C) 2016 Red Hat, Inc.
*
* Cockpit is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation; either version 2.1 of the License, or
* (at your option) any later version.
*
* Cockpit is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with Cockpit; If not, see <http://www.gnu.org/licenses/>.
*/
import React from 'react';
import PropTypes from 'prop-types';
import cockpit from 'cockpit';
import { AccessConsoles } from "@patternfly/react-console";
import SerialConsole from './serialConsole.jsx';
import Vnc from './vnc.jsx';
import DesktopConsole from './desktopConsole.jsx';
import {
domainCanConsole,
domainDesktopConsole,
domainSerialConsoleCommand
} from '../../../libvirtApi/domain.js';
import './consoles.css';
const _ = cockpit.gettext;
const VmNotRunning = () => {
return (
<div>
{_("Please start the virtual machine to access its console.")}
</div>
);
};
class Consoles extends React.Component {
constructor (props) {
super(props);
this.state = {
consoleDetail: undefined,
};
this.getDefaultConsole = this.getDefaultConsole.bind(this);
this.onDesktopConsoleDownload = this.onDesktopConsoleDownload.bind(this);
}
getDefaultConsole () {
const { vm } = this.props;
if (vm.displays) {
if (vm.displays.find(display => display.type == "vnc")) {
return 'VncConsole';
}
if (vm.displays.find(display => display.type == "spice")) {
return 'DesktopViewer';
}
}
const serialConsoleCommand = domainSerialConsoleCommand({ vm });
if (serialConsoleCommand) {
return 'SerialConsole';
}
// no console defined
return null;
}
onDesktopConsoleDownload (type) {
const { vm } = this.props;
// fire download of the .vv file
domainDesktopConsole({ name: vm.name, id: vm.id, connectionName: vm.connectionName, consoleDetail: vm.displays.find(display => display.type == type) });
}
render () {
const { vm, onAddErrorNotification } = this.props;
const spice = vm.displays && vm.displays.find(display => display.type == 'spice');
const serial = vm.displays && vm.displays.filter(display => display.type == 'pty');
const vnc = vm.displays && vm.displays.find(display => display.type == 'vnc');
if (!domainCanConsole || !domainCanConsole(vm.state)) {
return (<VmNotRunning />);
}
const onDesktopConsole = () => { // prefer spice over vnc
this.onDesktopConsoleDownload(spice ? 'spice' : 'vnc');
};
return (
<AccessConsoles preselectedType={this.getDefaultConsole()}
textSelectConsoleType={_("Select console type")}
textSerialConsole={_("Serial console")}
textVncConsole={_("VNC console")}
textDesktopViewerConsole={_("Desktop viewer")}>
{serial.map((pty, idx) => <SerialConsole type={serial.length == 1 ? "SerialConsole" : cockpit.format(_("Serial console ($0)"), pty.alias || idx)}
key={"pty-" + idx}
connectionName={vm.connectionName}
vmName={vm.name}
spawnArgs={domainSerialConsoleCommand({ vm, alias: pty.alias })} />)}
{vnc &&
<Vnc type="VncConsole"
vmName={vm.name}
vmId={vm.id}
connectionName={vm.connectionName}
consoleDetail={vnc}
onAddErrorNotification={onAddErrorNotification} />}
{(vnc || spice) &&
<DesktopConsole type="DesktopViewer"
onDesktopConsole={onDesktopConsole}
vnc={vnc}
spice={spice} />}
</AccessConsoles>
);
}
}
Consoles.propTypes = {
vm: PropTypes.object.isRequired,
onAddErrorNotification: PropTypes.func.isRequired,
};
export default Consoles;