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 } 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, virtualMachine) => {
84 dispatch<any>(openVirtualMachinesContextMenu(event, 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>, 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 {!isAdmin && 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) =>
151 {props.isAdmin ? <span>{adminVirtualMachinesTable(props)}</span>
153 <div className={props.classes.rightAlign}>
154 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
155 SEND REQUEST FOR SHELL ACCESS
157 {props.requestedDate &&
158 <Typography variant="body1">
159 A request for shell access was sent on {props.requestedDate}
162 <div className={props.classes.icon}>
163 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" className={props.classes.linkIcon}>
164 <Tooltip title="Access VM using webshell">
169 {userVirtualMachinesTable(props)}
176 const userVirtualMachinesTable = (props: VirtualMachineProps) =>
180 <TableCell>Host name</TableCell>
181 <TableCell>Login name</TableCell>
182 <TableCell>Command line</TableCell>
183 <TableCell>Web shell</TableCell>
187 {props.virtualMachines.items.map((it, index) =>
188 <TableRow key={index}>
189 <TableCell>{it.hostname}</TableCell>
190 <TableCell>{getUsername(props.links)}</TableCell>
191 <TableCell>ssh {getUsername(props.links)}@{it.hostname}.arvados</TableCell>
193 <a href={`https://workbench.c97qk.arvadosapi.com${it.href}/webshell/${getUsername(props.links)}`} target="_blank" className={props.classes.link}>
194 Log in as {getUsername(props.links)}
202 const adminVirtualMachinesTable = (props: VirtualMachineProps) =>
206 <TableCell>Uuid</TableCell>
207 <TableCell>Host name</TableCell>
208 <TableCell>Logins</TableCell>
213 {props.logins.items.length > 0 && props.virtualMachines.items.map((it, index) =>
214 <TableRow key={index}>
215 <TableCell>{it.uuid}</TableCell>
216 <TableCell>{it.hostname}</TableCell>
217 <TableCell>["{props.logins.items[0].username}"]</TableCell>
218 <TableCell className={props.classes.moreOptions}>
219 <Tooltip title="More options" disableFocusListener>
220 <IconButton onClick={event => props.onOptionsMenuOpen(event, it)} className={props.classes.moreOptionsButton}>
230 const getUsername = (links: ListResults<any>) => {
231 return links.items[0].properties.username;
234 const CardSSHSection = (props: VirtualMachineProps) =>
238 <Typography variant="body2">
239 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):
242 className={props.classes.codeSnippet}
248 const textSSH = `Host *.arvados
250 ServerAliveInterval 60
251 ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;