1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
5 import * as 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 * as CopyToClipboard from 'react-copy-to-clipboard';
17 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon';
19 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
21 marginTop: theme.spacing.unit,
22 marginBottom: theme.spacing.unit
25 borderRadius: theme.spacing.unit * 0.5,
27 borderColor: theme.palette.grey["400"],
30 textDecoration: 'none',
31 color: theme.palette.primary.main,
33 color: theme.palette.primary.dark,
34 transition: 'all 0.5s ease'
38 textDecoration: 'none',
39 color: theme.palette.grey["500"],
42 color: theme.palette.common.black,
43 transition: 'all 0.5s ease'
49 cardWithoutMachines: {
54 marginTop: theme.spacing.unit
58 const mapStateToProps = (state: RootState) => {
60 requestedDate: state.virtualMachines.date,
61 userUuid: state.auth.user!.uuid,
62 helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
63 hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "",
64 webShell: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
65 ...state.virtualMachines
69 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate'> => ({
70 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
71 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
74 interface VirtualMachinesPanelDataProps {
75 requestedDate: string;
76 virtualMachines: ListResults<any>;
78 links: ListResults<any>;
84 interface VirtualMachinesPanelActionProps {
85 saveRequestedDate: () => void;
86 loadVirtualMachinesData: () => string;
89 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
91 export const VirtualMachineUserPanel = compose(
93 connect(mapStateToProps, mapDispatchToProps))(
94 class extends React.Component<VirtualMachineProps> {
96 this.props.loadVirtualMachinesData();
100 const { virtualMachines, links } = this.props;
102 <Grid container spacing={16}>
103 {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
104 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
105 {<CardSSHSection {...this.props} />}
112 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
115 <CardContent className={props.classes.cardWithoutMachines}>
117 <Typography variant='body1'>
118 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.
121 <Grid item xs={6} className={props.classes.rightAlign}>
122 {virtualMachineSendRequest(props)}
128 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
133 <div className={props.classes.rightAlign}>
134 {virtualMachineSendRequest(props)}
136 <div className={props.classes.icon}>
137 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" className={props.classes.linkIcon}>
138 <Tooltip title="Access VM using webshell">
143 {virtualMachinesTable(props)}
150 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
152 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
153 SEND REQUEST FOR SHELL ACCESS
155 {props.requestedDate &&
157 A request for shell access was sent on {props.requestedDate}
161 const virtualMachinesTable = (props: VirtualMachineProps) =>
165 <TableCell>Host name</TableCell>
166 <TableCell>Login name</TableCell>
167 <TableCell>Command line</TableCell>
168 {props.webShell !== "" && <TableCell>Web shell</TableCell>}
172 {props.virtualMachines.items.map(it =>
173 props.links.items.map(lk => {
174 if (lk.tailUuid === props.userUuid) {
175 const username = lk.properties.username;
176 const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
177 return <TableRow key={lk.uuid}>
178 <TableCell>{it.hostname}</TableCell>
179 <TableCell>{username}</TableCell>
183 {props.webShell !== "" && <TableCell>
184 <a href={`${props.webShell}${it.href}/webshell/${username}`} target="_blank" className={props.classes.link}>
196 const CardSSHSection = (props: VirtualMachineProps) =>
201 <div dangerouslySetInnerHTML={{ __html: props.helpText }} style={{ margin: "1em" }} />