1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import React from 'react';
6 import { connect } from 'react-redux';
7 import { Grid, Typography, Button, Card, CardContent, TableBody, TableCell, TableHead, TableRow, Table, Tooltip, Chip } from '@material-ui/core';
8 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
9 import { ArvadosTheme } from 'common/custom-theme';
10 import { compose, Dispatch } from 'redux';
11 import { saveRequestedDate, loadVirtualMachinesUserData } from 'store/virtual-machines/virtual-machines-actions';
12 import { RootState } from 'store/store';
13 import { ListResults } from 'services/common-service/common-service';
14 import { HelpIcon } from 'components/icon/icon';
15 import { SESSION_STORAGE } from "services/auth-service/auth-service";
16 // import * as CopyToClipboard from 'react-copy-to-clipboard';
17 import parse from "parse-duration";
18 import { CopyIcon } from 'components/icon/icon';
19 import CopyToClipboard from 'react-copy-to-clipboard';
20 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
21 import { sanitizeHTML } from 'common/html-sanitize';
23 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon' | 'chipsRoot' | 'copyIcon' | 'tableWrapper' | 'webshellButton';
25 const EXTRA_TOKEN = "exraToken";
27 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
29 marginTop: theme.spacing.unit,
30 marginBottom: theme.spacing.unit
33 borderRadius: theme.spacing.unit * 0.5,
35 borderColor: theme.palette.grey["400"],
38 textDecoration: 'none',
39 color: theme.palette.primary.main,
41 color: theme.palette.primary.dark,
42 transition: 'all 0.5s ease'
46 textDecoration: 'none',
47 color: theme.palette.grey["500"],
50 color: theme.palette.common.black,
51 transition: 'all 0.5s ease'
57 cardWithoutMachines: {
62 marginTop: theme.spacing.unit
65 margin: `0px -${theme.spacing.unit / 2}px`,
68 marginLeft: theme.spacing.unit,
69 color: theme.palette.grey["500"],
80 textTransform: "initial",
84 const mapStateToProps = (state: RootState) => {
86 requestedDate: state.virtualMachines.date,
87 userUuid: state.auth.user!.uuid,
88 helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
89 hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "",
90 token: state.auth.extraApiToken || state.auth.apiToken || '',
91 tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''),
92 webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
93 idleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
94 ...state.virtualMachines
98 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate' | 'onCopy'> => ({
99 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
100 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
101 onCopy: (message: string) => {
102 dispatch(snackbarActions.OPEN_SNACKBAR({
105 kind: SnackbarKind.SUCCESS
110 interface VirtualMachinesPanelDataProps {
111 requestedDate: string;
112 virtualMachines: ListResults<any>;
114 links: ListResults<any>;
118 tokenLocation: string;
123 interface VirtualMachinesPanelActionProps {
124 saveRequestedDate: () => void;
125 loadVirtualMachinesData: () => string;
126 onCopy: (message: string) => void;
129 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
131 export const VirtualMachineUserPanel = compose(
133 connect(mapStateToProps, mapDispatchToProps))(
134 class extends React.Component<VirtualMachineProps> {
135 componentDidMount() {
136 this.props.loadVirtualMachinesData();
140 const { virtualMachines, links } = this.props;
142 <Grid container spacing={16} data-cy="vm-user-panel">
143 {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
144 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
145 {<CardSSHSection {...this.props} />}
152 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
155 <CardContent className={props.classes.cardWithoutMachines}>
157 <Typography variant='body1'>
158 You do not have access to any virtual machines. Some Arvados features require using the command line. You may request access to a hosted virtual machine with the command line shell.
161 <Grid item xs={6} className={props.classes.rightAlign}>
162 {virtualMachineSendRequest(props)}
168 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
173 <div className={props.classes.rightAlign}>
174 {virtualMachineSendRequest(props)}
176 <div className={props.classes.icon}>
177 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" rel="noopener" className={props.classes.linkIcon}>
178 <Tooltip title="Access VM using webshell">
183 <div className={props.classes.tableWrapper}>
184 {virtualMachinesTable(props)}
192 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
194 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
195 SEND REQUEST FOR SHELL ACCESS
197 {props.requestedDate &&
199 A request for shell access was sent on {props.requestedDate}
203 const virtualMachinesTable = (props: VirtualMachineProps) =>
204 <Table data-cy="vm-user-table">
207 <TableCell>Host name</TableCell>
208 <TableCell>Login name</TableCell>
209 <TableCell>Groups</TableCell>
210 <TableCell>Command line</TableCell>
211 <TableCell>Web shell</TableCell>
215 {props.virtualMachines.items.map(it =>
216 props.links.items.map(lk => {
217 if (lk.tailUuid === props.userUuid && lk.headUuid === it.uuid) {
218 const username = lk.properties.username;
219 const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
221 if (props.tokenLocation === SESSION_STORAGE || props.tokenLocation === EXTRA_TOKEN) {
222 tokenParam = `&token=${encodeURIComponent(props.token)}`;
224 const loginHref = `/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&timeout=${props.idleTimeout}&login=${encodeURIComponent(username)}${tokenParam}`;
225 return <TableRow key={lk.uuid}>
226 <TableCell>{it.hostname}</TableCell>
227 <TableCell>{username}</TableCell>
229 <Grid container spacing={8} className={props.classes.chipsRoot}>
231 (lk.properties.groups || []).map((group, i) => (
233 <Chip label={group} />
241 <Tooltip title="Copy to clipboard">
242 <span className={props.classes.copyIcon}>
243 <CopyToClipboard text={command || ""} onCopy={() => props.onCopy!("Copied")}>
251 className={props.classes.webshellButton}
268 const CardSSHSection = (props: VirtualMachineProps) =>
273 <div dangerouslySetInnerHTML={{ __html: sanitizeHTML(props.helpText) }} style={{ margin: "1em" }} />