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';
22 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon' | 'chipsRoot' | 'copyIcon' | 'tableWrapper' | 'webshellButton';
24 const EXTRA_TOKEN = "exraToken";
26 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
28 marginTop: theme.spacing.unit,
29 marginBottom: theme.spacing.unit
32 borderRadius: theme.spacing.unit * 0.5,
34 borderColor: theme.palette.grey["400"],
37 textDecoration: 'none',
38 color: theme.palette.primary.main,
40 color: theme.palette.primary.dark,
41 transition: 'all 0.5s ease'
45 textDecoration: 'none',
46 color: theme.palette.grey["500"],
49 color: theme.palette.common.black,
50 transition: 'all 0.5s ease'
56 cardWithoutMachines: {
61 marginTop: theme.spacing.unit
64 margin: `0px -${theme.spacing.unit / 2}px`,
67 marginLeft: theme.spacing.unit,
68 color: theme.palette.grey["500"],
79 textTransform: "initial",
83 const mapStateToProps = (state: RootState) => {
85 requestedDate: state.virtualMachines.date,
86 userUuid: state.auth.user!.uuid,
87 helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
88 hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "",
89 token: state.auth.extraApiToken || state.auth.apiToken || '',
90 tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''),
91 webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
92 idleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
93 ...state.virtualMachines
97 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate' | 'onCopy'> => ({
98 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
99 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
100 onCopy: (message: string) => {
101 dispatch(snackbarActions.OPEN_SNACKBAR({
104 kind: SnackbarKind.SUCCESS
109 interface VirtualMachinesPanelDataProps {
110 requestedDate: string;
111 virtualMachines: ListResults<any>;
113 links: ListResults<any>;
117 tokenLocation: string;
122 interface VirtualMachinesPanelActionProps {
123 saveRequestedDate: () => void;
124 loadVirtualMachinesData: () => string;
125 onCopy: (message: string) => void;
128 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
130 export const VirtualMachineUserPanel = compose(
132 connect(mapStateToProps, mapDispatchToProps))(
133 class extends React.Component<VirtualMachineProps> {
134 componentDidMount() {
135 this.props.loadVirtualMachinesData();
139 const { virtualMachines, links } = this.props;
141 <Grid container spacing={16} data-cy="vm-user-panel">
142 {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
143 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
144 {<CardSSHSection {...this.props} />}
151 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
154 <CardContent className={props.classes.cardWithoutMachines}>
156 <Typography variant='body1'>
157 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.
160 <Grid item xs={6} className={props.classes.rightAlign}>
161 {virtualMachineSendRequest(props)}
167 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
172 <div className={props.classes.rightAlign}>
173 {virtualMachineSendRequest(props)}
175 <div className={props.classes.icon}>
176 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" rel="noopener noreferrer" className={props.classes.linkIcon}>
177 <Tooltip title="Access VM using webshell">
182 <div className={props.classes.tableWrapper}>
183 {virtualMachinesTable(props)}
191 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
193 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
194 SEND REQUEST FOR SHELL ACCESS
196 {props.requestedDate &&
198 A request for shell access was sent on {props.requestedDate}
202 const virtualMachinesTable = (props: VirtualMachineProps) =>
203 <Table data-cy="vm-user-table">
206 <TableCell>Host name</TableCell>
207 <TableCell>Login name</TableCell>
208 <TableCell>Groups</TableCell>
209 <TableCell>Command line</TableCell>
210 <TableCell>Web shell</TableCell>
214 {props.virtualMachines.items.map(it =>
215 props.links.items.map(lk => {
216 if (lk.tailUuid === props.userUuid && lk.headUuid === it.uuid) {
217 const username = lk.properties.username;
218 const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
220 if (props.tokenLocation === SESSION_STORAGE || props.tokenLocation === EXTRA_TOKEN) {
221 tokenParam = `&token=${encodeURIComponent(props.token)}`;
223 const loginHref = `/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&timeout=${props.idleTimeout}&login=${encodeURIComponent(username)}${tokenParam}`;
224 return <TableRow key={lk.uuid}>
225 <TableCell>{it.hostname}</TableCell>
226 <TableCell>{username}</TableCell>
228 <Grid container spacing={8} className={props.classes.chipsRoot}>
230 (lk.properties.groups || []).map((group, i) => (
232 <Chip label={group} />
240 <Tooltip title="Copy to clipboard">
241 <span className={props.classes.copyIcon}>
242 <CopyToClipboard text={command || ""} onCopy={() => props.onCopy!("Copied")}>
250 className={props.classes.webshellButton}
255 rel="noopener noreferrer">
267 const CardSSHSection = (props: VirtualMachineProps) =>
272 <div dangerouslySetInnerHTML={{ __html: props.helpText }} style={{ margin: "1em" }} />