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' | '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"],
76 textTransform: "initial",
80 const mapStateToProps = (state: RootState) => {
82 requestedDate: state.virtualMachines.date,
83 userUuid: state.auth.user!.uuid,
84 helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
85 hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "",
86 token: state.auth.extraApiToken || state.auth.apiToken || '',
87 tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''),
88 webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
89 idleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
90 ...state.virtualMachines
94 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate' | 'onCopy'> => ({
95 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
96 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
97 onCopy: (message: string) => {
98 dispatch(snackbarActions.OPEN_SNACKBAR({
101 kind: SnackbarKind.SUCCESS
106 interface VirtualMachinesPanelDataProps {
107 requestedDate: string;
108 virtualMachines: ListResults<any>;
110 links: ListResults<any>;
114 tokenLocation: string;
119 interface VirtualMachinesPanelActionProps {
120 saveRequestedDate: () => void;
121 loadVirtualMachinesData: () => string;
122 onCopy: (message: string) => void;
125 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
127 export const VirtualMachineUserPanel = compose(
129 connect(mapStateToProps, mapDispatchToProps))(
130 class extends React.Component<VirtualMachineProps> {
131 componentDidMount() {
132 this.props.loadVirtualMachinesData();
136 const { virtualMachines, links } = this.props;
138 <Grid container spacing={16} data-cy="vm-user-panel">
139 {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
140 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
141 {<CardSSHSection {...this.props} />}
148 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
151 <CardContent className={props.classes.cardWithoutMachines}>
153 <Typography variant='body1'>
154 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.
157 <Grid item xs={6} className={props.classes.rightAlign}>
158 {virtualMachineSendRequest(props)}
164 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
169 <div className={props.classes.rightAlign}>
170 {virtualMachineSendRequest(props)}
172 <div className={props.classes.icon}>
173 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" rel="noopener noreferrer" className={props.classes.linkIcon}>
174 <Tooltip title="Access VM using webshell">
179 {virtualMachinesTable(props)}
186 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
188 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
189 SEND REQUEST FOR SHELL ACCESS
191 {props.requestedDate &&
193 A request for shell access was sent on {props.requestedDate}
197 const virtualMachinesTable = (props: VirtualMachineProps) =>
198 <Table data-cy="vm-user-table">
201 <TableCell>Host name</TableCell>
202 <TableCell>Login name</TableCell>
203 <TableCell>Groups</TableCell>
204 <TableCell>Command line</TableCell>
205 <TableCell>Web shell</TableCell>
209 {props.virtualMachines.items.map(it =>
210 props.links.items.map(lk => {
211 if (lk.tailUuid === props.userUuid && lk.headUuid === it.uuid) {
212 const username = lk.properties.username;
213 const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
215 if (props.tokenLocation === SESSION_STORAGE || props.tokenLocation === EXTRA_TOKEN) {
216 tokenParam = `&token=${encodeURIComponent(props.token)}`;
218 const loginHref = `/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&timeout=${props.idleTimeout}&login=${encodeURIComponent(username)}${tokenParam}`;
219 return <TableRow key={lk.uuid}>
220 <TableCell>{it.hostname}</TableCell>
221 <TableCell>{username}</TableCell>
223 <Grid container spacing={8} className={props.classes.chipsRoot}>
225 (lk.properties.groups || []).map((group, i) => (
227 <Chip label={group} />
235 <Tooltip title="Copy to clipboard">
236 <span className={props.classes.copyIcon}>
237 <CopyToClipboard text={command || ""} onCopy={() => props.onCopy!("Copied")}>
245 className={props.classes.webshellButton}
250 rel="noopener noreferrer">
262 const CardSSHSection = (props: VirtualMachineProps) =>
267 <div dangerouslySetInnerHTML={{ __html: props.helpText }} style={{ margin: "1em" }} />