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 = ({ virtualMachines }: RootState) => {
62 requestedDate: virtualMachines.date,
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>;
75 links: ListResults<any>;
78 interface VirtualMachinesPanelActionProps {
79 saveRequestedDate: () => void;
80 loadVirtualMachinesData: () => string;
83 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
85 export const VirtualMachineUserPanel = compose(
87 connect(mapStateToProps, mapDispatchToProps))(
88 class extends React.Component<VirtualMachineProps> {
90 this.props.loadVirtualMachinesData();
94 const { virtualMachines, links } = this.props;
96 <Grid container spacing={16}>
97 {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
98 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
99 {<CardSSHSection {...this.props} />}
106 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
109 <CardContent className={props.classes.cardWithoutMachines}>
111 <Typography variant="body2">
112 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.
115 <Grid item xs={6} className={props.classes.rightAlign}>
116 {virtualMachineSendRequest(props)}
122 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
127 <div className={props.classes.rightAlign}>
128 {virtualMachineSendRequest(props)}
130 <div className={props.classes.icon}>
131 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" className={props.classes.linkIcon}>
132 <Tooltip title="Access VM using webshell">
137 {virtualMachinesTable(props)}
144 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
146 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
147 SEND REQUEST FOR SHELL ACCESS
149 {props.requestedDate &&
150 <Typography variant="body1">
151 A request for shell access was sent on {props.requestedDate}
155 const virtualMachinesTable = (props: VirtualMachineProps) =>
159 <TableCell>Host name</TableCell>
160 <TableCell>Login name</TableCell>
161 <TableCell>Command line</TableCell>
162 <TableCell>Web shell</TableCell>
166 {props.virtualMachines.items.map((it, index) =>
167 <TableRow key={index}>
168 <TableCell>{it.hostname}</TableCell>
169 <TableCell>{getUsername(props.links)}</TableCell>
170 <TableCell>ssh {getUsername(props.links)}@{it.hostname}.arvados</TableCell>
172 <a href={`https://workbench.c97qk.arvadosapi.com${it.href}/webshell/${getUsername(props.links)}`} target="_blank" className={props.classes.link}>
173 Log in as {getUsername(props.links)}
181 const getUsername = (links: ListResults<any>) => {
182 return links.items[0].properties.username;
185 const CardSSHSection = (props: VirtualMachineProps) =>
189 <Typography variant="body2">
190 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):
193 className={props.classes.codeSnippet}
199 const textSSH = `Host *.arvados
201 ServerAliveInterval 60
202 ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;