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 { 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, loadVirtualMachinesUserData } from '~/store/virtual-machines/virtual-machines-actions';
14 import { RootState } from '~/store/store';
15 import { ListResults } from '~/services/common-service/common-service';
16 import { HelpIcon } from '~/components/icon/icon';
17 import { Routes } from '~/routes/routes';
19 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon';
21 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
23 marginTop: theme.spacing.unit,
24 marginBottom: theme.spacing.unit
27 borderRadius: theme.spacing.unit * 0.5,
29 borderColor: theme.palette.grey["400"],
32 textDecoration: 'none',
33 color: theme.palette.primary.main,
35 color: theme.palette.primary.dark,
36 transition: 'all 0.5s ease'
40 textDecoration: 'none',
41 color: theme.palette.grey["500"],
44 color: theme.palette.common.black,
45 transition: 'all 0.5s ease'
51 cardWithoutMachines: {
56 marginTop: theme.spacing.unit
60 const mapStateToProps = (state: RootState) => {
62 requestedDate: state.virtualMachines.date,
63 userUuid: state.auth.user!.uuid,
64 ...state.virtualMachines
68 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate'> => ({
69 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
70 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
73 interface VirtualMachinesPanelDataProps {
74 requestedDate: string;
75 virtualMachines: ListResults<any>;
77 links: ListResults<any>;
80 interface VirtualMachinesPanelActionProps {
81 saveRequestedDate: () => void;
82 loadVirtualMachinesData: () => string;
85 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
87 export const VirtualMachineUserPanel = compose(
89 connect(mapStateToProps, mapDispatchToProps))(
90 class extends React.Component<VirtualMachineProps> {
92 this.props.loadVirtualMachinesData();
96 const { virtualMachines, links } = this.props;
98 <Grid container spacing={16}>
99 {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
100 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
101 {<CardSSHSection {...this.props} />}
108 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
111 <CardContent className={props.classes.cardWithoutMachines}>
113 <Typography variant='body1'>
114 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.
117 <Grid item xs={6} className={props.classes.rightAlign}>
118 {virtualMachineSendRequest(props)}
124 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
129 <div className={props.classes.rightAlign}>
130 {virtualMachineSendRequest(props)}
132 <div className={props.classes.icon}>
133 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" className={props.classes.linkIcon}>
134 <Tooltip title="Access VM using webshell">
139 {virtualMachinesTable(props)}
146 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
148 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
149 SEND REQUEST FOR SHELL ACCESS
151 {props.requestedDate &&
153 A request for shell access was sent on {props.requestedDate}
157 const virtualMachinesTable = (props: VirtualMachineProps) =>
161 <TableCell>Host name</TableCell>
162 <TableCell>Login name</TableCell>
163 <TableCell>Command line</TableCell>
164 <TableCell>Web shell</TableCell>
168 {props.virtualMachines.items.map((it, index) =>
169 <TableRow key={index}>
170 <TableCell>{it.hostname}</TableCell>
171 <TableCell>{getUsername(props.links, props.userUuid)}</TableCell>
172 <TableCell>ssh {getUsername(props.links, props.userUuid)}@{it.hostname}.arvados</TableCell>
174 <a href={`https://workbench.c97qk.arvadosapi.com${it.href}/webshell/${getUsername(props.links, props.userUuid)}`} target="_blank" className={props.classes.link}>
175 Log in as {getUsername(props.links, props.userUuid)}
183 const getUsername = (links: ListResults<any>, userUuid: string) => {
184 return links.items.map(it => it.tailUuid === userUuid ? it.properties.username : '');
187 const CardSSHSection = (props: VirtualMachineProps) =>
191 <Typography variant='body1'>
192 In order to access virtual machines using SSH, <Link to={Routes.SSH_KEYS_USER} 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):
195 className={props.classes.codeSnippet}
201 const textSSH = `Host *.arvados
203 ServerAliveInterval 60
204 ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;