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 '@mui/material';
8 import { CustomStyleRulesCallback } from 'common/custom-theme';
9 import { WithStyles } from '@mui/styles';
10 import withStyles from '@mui/styles/withStyles';
11 import { ArvadosTheme } from 'common/custom-theme';
12 import { compose, Dispatch } from 'redux';
13 import { saveRequestedDate, loadVirtualMachinesUserData } from 'store/virtual-machines/virtual-machines-actions';
14 import { RootState } from 'store/store';
15 import { ListResults } from 'services/common-service/common-service';
16 import { HelpIcon } from 'components/icon/icon';
17 import { SESSION_STORAGE } from "services/auth-service/auth-service";
18 // import * as CopyToClipboard from 'react-copy-to-clipboard';
19 import parse from "parse-duration";
20 import { CopyIcon } from 'components/icon/icon';
21 import CopyToClipboard from 'react-copy-to-clipboard';
22 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
23 import { sanitizeHTML } from 'common/html-sanitize';
25 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon' | 'chipsRoot' | 'copyIcon' | 'tableWrapper' | 'webshellButton';
27 const EXTRA_TOKEN = "exraToken";
29 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
31 marginTop: theme.spacing(1),
32 marginBottom: theme.spacing(1)
35 borderRadius: theme.spacing(0.5),
37 borderColor: theme.palette.grey["400"],
40 textDecoration: 'none',
41 color: theme.palette.primary.main,
43 color: theme.palette.primary.dark,
44 transition: 'all 0.5s ease'
48 textDecoration: 'none',
49 color: theme.palette.grey["500"],
52 color: theme.palette.common.black,
53 transition: 'all 0.5s ease'
59 cardWithoutMachines: {
64 marginTop: theme.spacing(1)
67 margin: `0px -${theme.spacing(0.5)}`,
70 marginLeft: theme.spacing(1),
71 color: theme.palette.grey["500"],
82 textTransform: "initial",
86 const mapStateToProps = (state: RootState) => {
88 requestedDate: state.virtualMachines.date,
89 userUuid: state.auth.user!.uuid,
90 helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
91 hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "",
92 token: state.auth.extraApiToken || state.auth.apiToken || '',
93 tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''),
94 webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
95 idleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
96 ...state.virtualMachines
100 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate' | 'onCopy'> => ({
101 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
102 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
103 onCopy: (message: string) => {
104 dispatch(snackbarActions.OPEN_SNACKBAR({
107 kind: SnackbarKind.SUCCESS
112 interface VirtualMachinesPanelDataProps {
113 requestedDate: string;
114 virtualMachines: ListResults<any>;
116 links: ListResults<any>;
120 tokenLocation: string;
125 interface VirtualMachinesPanelActionProps {
126 saveRequestedDate: () => void;
127 loadVirtualMachinesData: () => string;
128 onCopy: (message: string) => void;
131 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
133 export const VirtualMachineUserPanel = compose(
135 connect(mapStateToProps, mapDispatchToProps))(
136 class extends React.Component<VirtualMachineProps> {
137 componentDidMount() {
138 this.props.loadVirtualMachinesData();
142 const { virtualMachines, links } = this.props;
144 <Grid container spacing={2} data-cy="vm-user-panel">
145 {virtualMachines.items.length === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
146 {virtualMachines.items.length > 0 && links.items.length > 0 && <CardContentWithVirtualMachines {...this.props} />}
147 {<CardSSHSection {...this.props} />}
154 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
157 <CardContent className={props.classes.cardWithoutMachines}>
159 <Typography variant='body1'>
160 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.
163 <Grid item xs={6} className={props.classes.rightAlign}>
164 {virtualMachineSendRequest(props)}
170 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
175 <div className={props.classes.rightAlign}>
176 {virtualMachineSendRequest(props)}
178 <div className={props.classes.icon}>
179 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" rel="noopener noreferrer" className={props.classes.linkIcon}>
180 <Tooltip title="Access VM using webshell">
185 <div className={props.classes.tableWrapper}>
186 {virtualMachinesTable(props)}
194 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
196 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
197 SEND REQUEST FOR SHELL ACCESS
199 {props.requestedDate &&
201 A request for shell access was sent on {props.requestedDate}
205 const virtualMachinesTable = (props: VirtualMachineProps) =>
206 <Table data-cy="vm-user-table">
209 <TableCell>Host name</TableCell>
210 <TableCell>Login name</TableCell>
211 <TableCell>Groups</TableCell>
212 <TableCell>Command line</TableCell>
213 <TableCell>Web shell</TableCell>
217 {props.virtualMachines.items.map(it =>
218 props.links.items.map(lk => {
219 if (lk.tailUuid === props.userUuid && lk.headUuid === it.uuid) {
220 const username = lk.properties.username;
221 const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
223 if (props.tokenLocation === SESSION_STORAGE || props.tokenLocation === EXTRA_TOKEN) {
224 tokenParam = `&token=${encodeURIComponent(props.token)}`;
226 const loginHref = `/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&timeout=${props.idleTimeout}&login=${encodeURIComponent(username)}${tokenParam}`;
227 return <TableRow key={lk.uuid}>
228 <TableCell>{it.hostname}</TableCell>
229 <TableCell>{username}</TableCell>
231 <Grid container spacing={1} className={props.classes.chipsRoot}>
233 (lk.properties.groups || []).map((group, i) => (
235 <Chip label={group} />
243 <Tooltip title="Copy link to clipboard">
244 <span className={props.classes.copyIcon}>
245 <CopyToClipboard text={command || ""} onCopy={() => props.onCopy!("Copied")}>
253 className={props.classes.webshellButton}
270 const CardSSHSection = (props: VirtualMachineProps) =>
275 <div dangerouslySetInnerHTML={{ __html: sanitizeHTML(props.helpText) }} style={{ margin: "1em" }} />