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';
16 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon';
18 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
20 marginTop: theme.spacing.unit,
21 marginBottom: theme.spacing.unit
24 borderRadius: theme.spacing.unit * 0.5,
26 borderColor: theme.palette.grey["400"],
29 textDecoration: 'none',
30 color: theme.palette.primary.main,
32 color: theme.palette.primary.dark,
33 transition: 'all 0.5s ease'
37 textDecoration: 'none',
38 color: theme.palette.grey["500"],
41 color: theme.palette.common.black,
42 transition: 'all 0.5s ease'
48 cardWithoutMachines: {
53 marginTop: theme.spacing.unit
57 const mapStateToProps = (state: RootState) => {
59 requestedDate: state.virtualMachines.date,
60 userUuid: state.auth.user!.uuid,
61 helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
62 webShell: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
63 ...state.virtualMachines
67 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate'> => ({
68 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
69 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
72 interface VirtualMachinesPanelDataProps {
73 requestedDate: string;
74 virtualMachines: ListResults<any>;
76 links: ListResults<any>;
81 interface VirtualMachinesPanelActionProps {
82 saveRequestedDate: () => void;
83 loadVirtualMachinesData: () => string;
86 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
88 export const VirtualMachineUserPanel = compose(
90 connect(mapStateToProps, mapDispatchToProps))(
91 class extends React.Component<VirtualMachineProps> {
93 this.props.loadVirtualMachinesData();
97 const { virtualMachines, links } = this.props;
99 <Grid container spacing={16}>
100 {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
101 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
102 {<CardSSHSection {...this.props} />}
109 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
112 <CardContent className={props.classes.cardWithoutMachines}>
114 <Typography variant='body1'>
115 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.
118 <Grid item xs={6} className={props.classes.rightAlign}>
119 {virtualMachineSendRequest(props)}
125 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
130 <div className={props.classes.rightAlign}>
131 {virtualMachineSendRequest(props)}
133 <div className={props.classes.icon}>
134 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" className={props.classes.linkIcon}>
135 <Tooltip title="Access VM using webshell">
140 {virtualMachinesTable(props)}
147 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
149 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
150 SEND REQUEST FOR SHELL ACCESS
152 {props.requestedDate &&
154 A request for shell access was sent on {props.requestedDate}
158 const virtualMachinesTable = (props: VirtualMachineProps) =>
162 <TableCell>Host name</TableCell>
163 <TableCell>Login name</TableCell>
164 <TableCell>Command line</TableCell>
165 {props.webShell !== "" && <TableCell>Web shell</TableCell>}
169 {props.virtualMachines.items.map((it, index) =>
170 <TableRow key={index}>
171 <TableCell>{it.hostname}</TableCell>
172 <TableCell>{getUsername(props.links, props.userUuid)}</TableCell>
173 <TableCell>ssh {getUsername(props.links, props.userUuid)}@{it.hostname}</TableCell>
174 {props.webShell !== "" && <TableCell>
175 <a href={`${props.webShell}${it.href}/webshell/${getUsername(props.links, props.userUuid)}`} target="_blank" className={props.classes.link}>
176 Log in as {getUsername(props.links, props.userUuid)}
184 const getUsername = (links: ListResults<any>, userUuid: string) => {
185 return links.items.map(it => it.tailUuid === userUuid ? it.properties.username : '');
188 const CardSSHSection = (props: VirtualMachineProps) =>
193 <div dangerouslySetInnerHTML={{ __html: props.helpText }} style={{ margin: "1em" }} />