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 } 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';
18 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon';
20 const EXTRA_TOKEN = "exra";
22 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
24 marginTop: theme.spacing.unit,
25 marginBottom: theme.spacing.unit
28 borderRadius: theme.spacing.unit * 0.5,
30 borderColor: theme.palette.grey["400"],
33 textDecoration: 'none',
34 color: theme.palette.primary.main,
36 color: theme.palette.primary.dark,
37 transition: 'all 0.5s ease'
41 textDecoration: 'none',
42 color: theme.palette.grey["500"],
45 color: theme.palette.common.black,
46 transition: 'all 0.5s ease'
52 cardWithoutMachines: {
57 marginTop: theme.spacing.unit
61 const mapStateToProps = (state: RootState) => {
63 requestedDate: state.virtualMachines.date,
64 userUuid: state.auth.user!.uuid,
65 helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
66 hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "",
67 token: state.auth.extraApiToken || state.auth.apiToken || '',
68 tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''),
69 webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
70 ...state.virtualMachines
74 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate'> => ({
75 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
76 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
79 interface VirtualMachinesPanelDataProps {
80 requestedDate: string;
81 virtualMachines: ListResults<any>;
83 links: ListResults<any>;
87 tokenLocation: string,
91 interface VirtualMachinesPanelActionProps {
92 saveRequestedDate: () => void;
93 loadVirtualMachinesData: () => string;
96 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
98 export const VirtualMachineUserPanel = compose(
100 connect(mapStateToProps, mapDispatchToProps))(
101 class extends React.Component<VirtualMachineProps> {
102 componentDidMount() {
103 this.props.loadVirtualMachinesData();
107 const { virtualMachines, links } = this.props;
109 <Grid container spacing={16}>
110 {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
111 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
112 {<CardSSHSection {...this.props} />}
119 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
122 <CardContent className={props.classes.cardWithoutMachines}>
124 <Typography variant='body1'>
125 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.
128 <Grid item xs={6} className={props.classes.rightAlign}>
129 {virtualMachineSendRequest(props)}
135 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
140 <div className={props.classes.rightAlign}>
141 {virtualMachineSendRequest(props)}
143 <div className={props.classes.icon}>
144 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" rel="noopener noreferrer" className={props.classes.linkIcon}>
145 <Tooltip title="Access VM using webshell">
150 {virtualMachinesTable(props)}
157 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
159 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
160 SEND REQUEST FOR SHELL ACCESS
162 {props.requestedDate &&
164 A request for shell access was sent on {props.requestedDate}
168 const virtualMachinesTable = (props: VirtualMachineProps) =>
172 <TableCell>Host name</TableCell>
173 <TableCell>Login name</TableCell>
174 <TableCell>Command line</TableCell>
175 <TableCell>Web shell</TableCell>
179 {props.virtualMachines.items.map(it =>
180 props.links.items.map(lk => {
181 if (lk.tailUuid === props.userUuid) {
182 const username = lk.properties.username;
183 const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
185 if (props.tokenLocation === SESSION_STORAGE || props.tokenLocation === EXTRA_TOKEN) {
186 tokenParam = `&token=${encodeURIComponent(props.token)}`;
188 return <TableRow key={lk.uuid}>
189 <TableCell>{it.hostname}</TableCell>
190 <TableCell>{username}</TableCell>
195 <a href={`/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&login=${encodeURIComponent(username)}${tokenParam}`} target="_blank" rel="noopener noreferrer" className={props.classes.link}>
207 const CardSSHSection = (props: VirtualMachineProps) =>
212 <div dangerouslySetInnerHTML={{ __html: props.helpText }} style={{ margin: "1em" }} />