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 { CustomStyleRulesCallback } from 'common/custom-theme';
9 import { WithStyles, withStyles } from '@material-ui/core/styles';
10 import { ArvadosTheme } from 'common/custom-theme';
11 import { compose, Dispatch } from 'redux';
12 import { saveRequestedDate, loadVirtualMachinesUserData } from 'store/virtual-machines/virtual-machines-actions';
13 import { RootState } from 'store/store';
14 import { ListResults } from 'services/common-service/common-service';
15 import { HelpIcon } from 'components/icon/icon';
16 import { SESSION_STORAGE } from "services/auth-service/auth-service";
17 // import * as CopyToClipboard from 'react-copy-to-clipboard';
18 import parse from "parse-duration";
19 import { CopyIcon } from 'components/icon/icon';
20 import CopyToClipboard from 'react-copy-to-clipboard';
21 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
22 import { sanitizeHTML } from 'common/html-sanitize';
24 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon' | 'chipsRoot' | 'copyIcon' | 'tableWrapper' | 'webshellButton';
26 const EXTRA_TOKEN = "exraToken";
28 const styles: CustomStyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
30 marginTop: theme.spacing.unit,
31 marginBottom: theme.spacing.unit
34 borderRadius: theme.spacing.unit * 0.5,
36 borderColor: theme.palette.grey["400"],
39 textDecoration: 'none',
40 color: theme.palette.primary.main,
42 color: theme.palette.primary.dark,
43 transition: 'all 0.5s ease'
47 textDecoration: 'none',
48 color: theme.palette.grey["500"],
51 color: theme.palette.common.black,
52 transition: 'all 0.5s ease'
58 cardWithoutMachines: {
63 marginTop: theme.spacing.unit
66 margin: `0px -${theme.spacing.unit / 2}px`,
69 marginLeft: theme.spacing.unit,
70 color: theme.palette.grey["500"],
81 textTransform: "initial",
85 const mapStateToProps = (state: RootState) => {
87 requestedDate: state.virtualMachines.date,
88 userUuid: state.auth.user!.uuid,
89 helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
90 hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "",
91 token: state.auth.extraApiToken || state.auth.apiToken || '',
92 tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''),
93 webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
94 idleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
95 ...state.virtualMachines
99 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate' | 'onCopy'> => ({
100 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
101 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
102 onCopy: (message: string) => {
103 dispatch(snackbarActions.OPEN_SNACKBAR({
106 kind: SnackbarKind.SUCCESS
111 interface VirtualMachinesPanelDataProps {
112 requestedDate: string;
113 virtualMachines: ListResults<any>;
115 links: ListResults<any>;
119 tokenLocation: string;
124 interface VirtualMachinesPanelActionProps {
125 saveRequestedDate: () => void;
126 loadVirtualMachinesData: () => string;
127 onCopy: (message: string) => void;
130 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
132 export const VirtualMachineUserPanel = compose(
134 connect(mapStateToProps, mapDispatchToProps))(
135 class extends React.Component<VirtualMachineProps> {
136 componentDidMount() {
137 this.props.loadVirtualMachinesData();
141 const { virtualMachines, links } = this.props;
143 <Grid container spacing={16} data-cy="vm-user-panel">
144 {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
145 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
146 {<CardSSHSection {...this.props} />}
153 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
156 <CardContent className={props.classes.cardWithoutMachines}>
158 <Typography variant='body1'>
159 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.
162 <Grid item xs={6} className={props.classes.rightAlign}>
163 {virtualMachineSendRequest(props)}
169 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
174 <div className={props.classes.rightAlign}>
175 {virtualMachineSendRequest(props)}
177 <div className={props.classes.icon}>
178 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" rel="noopener noreferrer" className={props.classes.linkIcon}>
179 <Tooltip title="Access VM using webshell">
184 <div className={props.classes.tableWrapper}>
185 {virtualMachinesTable(props)}
193 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
195 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
196 SEND REQUEST FOR SHELL ACCESS
198 {props.requestedDate &&
200 A request for shell access was sent on {props.requestedDate}
204 const virtualMachinesTable = (props: VirtualMachineProps) =>
205 <Table data-cy="vm-user-table">
208 <TableCell>Host name</TableCell>
209 <TableCell>Login name</TableCell>
210 <TableCell>Groups</TableCell>
211 <TableCell>Command line</TableCell>
212 <TableCell>Web shell</TableCell>
216 {props.virtualMachines.items.map(it =>
217 props.links.items.map(lk => {
218 if (lk.tailUuid === props.userUuid && lk.headUuid === it.uuid) {
219 const username = lk.properties.username;
220 const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
222 if (props.tokenLocation === SESSION_STORAGE || props.tokenLocation === EXTRA_TOKEN) {
223 tokenParam = `&token=${encodeURIComponent(props.token)}`;
225 const loginHref = `/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&timeout=${props.idleTimeout}&login=${encodeURIComponent(username)}${tokenParam}`;
226 return <TableRow key={lk.uuid}>
227 <TableCell>{it.hostname}</TableCell>
228 <TableCell>{username}</TableCell>
230 <Grid container spacing={8} className={props.classes.chipsRoot}>
232 (lk.properties.groups || []).map((group, i) => (
234 <Chip label={group} />
242 <Tooltip title="Copy link to clipboard">
243 <span className={props.classes.copyIcon}>
244 <CopyToClipboard text={command || ""} onCopy={() => props.onCopy!("Copied")}>
252 className={props.classes.webshellButton}
269 const CardSSHSection = (props: VirtualMachineProps) =>
274 <div dangerouslySetInnerHTML={{ __html: sanitizeHTML(props.helpText) }} style={{ margin: "1em" }} />