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, IconButton } from '@material-ui/core';
8 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
9 import { ArvadosTheme } from '~/common/custom-theme';
10 import { DefaultCodeSnippet } from '~/components/default-code-snippet/default-code-snippet';
11 import { Link } from 'react-router-dom';
12 import { compose, Dispatch } from 'redux';
13 import { saveRequestedDate, loadVirtualMachinesData } from '~/store/virtual-machines/virtual-machines-actions';
14 import { RootState } from '~/store/store';
15 import { ListResults } from '~/services/common-service/common-resource-service';
16 import { HelpIcon, MoreOptionsIcon, AddIcon } from '~/components/icon/icon';
17 import { VirtualMachineLogins, VirtualMachinesResource } from '~/models/virtual-machines';
18 import { Routes } from '~/routes/routes';
19 import { openVirtualMachinesContextMenu } from '~/store/context-menu/context-menu-actions';
21 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon' | 'moreOptionsButton' | 'moreOptions';
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
25 marginTop: theme.spacing.unit,
26 marginBottom: theme.spacing.unit
29 borderRadius: theme.spacing.unit * 0.5,
31 borderColor: theme.palette.grey["400"],
34 textDecoration: 'none',
35 color: theme.palette.primary.main,
37 color: theme.palette.primary.dark,
38 transition: 'all 0.5s ease'
42 textDecoration: 'none',
43 color: theme.palette.grey["500"],
46 color: theme.palette.common.black,
47 transition: 'all 0.5s ease'
53 cardWithoutMachines: {
58 marginTop: theme.spacing.unit
71 const mapStateToProps = ({ virtualMachines, auth }: RootState) => {
73 requestedDate: virtualMachines.date,
74 isAdmin: auth.user!.isAdmin,
75 logins: virtualMachines.logins,
80 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate' | 'onOptionsMenuOpen'> => ({
81 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
82 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesData()),
83 onOptionsMenuOpen: (event, index, virtualMachine) => {
84 dispatch<any>(openVirtualMachinesContextMenu(event, index, virtualMachine));
88 interface VirtualMachinesPanelDataProps {
89 requestedDate: string;
90 virtualMachines: ListResults<any>;
91 logins: VirtualMachineLogins;
92 links: ListResults<any>;
96 interface VirtualMachinesPanelActionProps {
97 saveRequestedDate: () => void;
98 loadVirtualMachinesData: () => string;
99 onOptionsMenuOpen: (event: React.MouseEvent<HTMLElement>, index: number, virtualMachine: VirtualMachinesResource) => void;
102 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
104 export const VirtualMachinePanel = compose(
106 connect(mapStateToProps, mapDispatchToProps))(
107 class extends React.Component<VirtualMachineProps> {
108 componentDidMount() {
109 this.props.loadVirtualMachinesData();
113 const { virtualMachines, links, isAdmin } = this.props;
115 <Grid container spacing={16}>
116 {virtualMachines.itemsAvailable === 0 && <CardContentWithNoVirtualMachines {...this.props} />}
117 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
118 {!isAdmin && <CardSSHSection {...this.props} />}
125 const CardContentWithNoVirtualMachines = (props: VirtualMachineProps) =>
128 <CardContent className={props.classes.cardWithoutMachines}>
130 <Typography variant="body2">
131 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.
134 <Grid item xs={6} className={props.classes.rightAlign}>
135 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
136 SEND REQUEST FOR SHELL ACCESS
138 {props.requestedDate &&
139 <Typography variant="body1">
140 A request for shell access was sent on {props.requestedDate}
147 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
153 <div className={props.classes.rightAlign}>
154 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
155 <AddIcon /> NEW VIRTUAL MACHINE
158 {adminVirtualMachinesTable(props)}
161 <div className={props.classes.rightAlign}>
162 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
163 SEND REQUEST FOR SHELL ACCESS
165 {props.requestedDate &&
166 <Typography variant="body1">
167 A request for shell access was sent on {props.requestedDate}
170 <div className={props.classes.icon}>
171 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" className={props.classes.linkIcon}>
172 <Tooltip title="Access VM using webshell">
177 {userVirtualMachinesTable(props)}
184 const userVirtualMachinesTable = (props: VirtualMachineProps) =>
188 <TableCell>Host name</TableCell>
189 <TableCell>Login name</TableCell>
190 <TableCell>Command line</TableCell>
191 <TableCell>Web shell</TableCell>
195 {props.virtualMachines.items.map((it, index) =>
196 <TableRow key={index}>
197 <TableCell>{it.hostname}</TableCell>
198 <TableCell>{getUsername(props.links, it)}</TableCell>
199 <TableCell>ssh {getUsername(props.links, it)}@shell.arvados</TableCell>
201 <a href={`https://workbench.c97qk.arvadosapi.com${it.href}/webshell/${getUsername(props.links, it)}`} target="_blank" className={props.classes.link}>
202 Log in as {getUsername(props.links, it)}
210 const adminVirtualMachinesTable = (props: VirtualMachineProps) =>
214 <TableCell>Uuid</TableCell>
215 <TableCell>Host name</TableCell>
216 <TableCell>Logins</TableCell>
221 {props.logins.items.length > 0 && props.virtualMachines.items.map((it, index) =>
222 <TableRow key={index}>
223 <TableCell>{it.uuid}</TableCell>
224 <TableCell>{props.logins.items[0].hostname}</TableCell>
225 <TableCell>["{props.logins.items[0].username}"]</TableCell>
226 <TableCell className={props.classes.moreOptions}>
227 <Tooltip title="More options" disableFocusListener>
228 <IconButton onClick={event => props.onOptionsMenuOpen(event, index, it)} className={props.classes.moreOptionsButton}>
238 const getUsername = (links: ListResults<any>, virtualMachine: VirtualMachinesResource) => {
239 const link = links.items.find((item: any) => item.headUuid === virtualMachine.uuid);
240 return link.properties.username || undefined;
243 const CardSSHSection = (props: VirtualMachineProps) =>
247 <Typography variant="body2">
248 In order to access virtual machines using SSH, <Link to={Routes.SSH_KEYS} className={props.classes.link}>add an SSH key to your account</Link> and add a section like this to your SSH configuration file ( ~/.ssh/config):
251 className={props.classes.codeSnippet}
257 const textSSH = `Host *.arvados
259 ServerAliveInterval 60
260 ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;