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 } 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 } from '~/components/icon/icon';
17 import { VirtualMachinesLoginsResource, VirtualMachinesResource } from '~/models/virtual-machines';
18 import { Routes } from '~/routes/routes';
20 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon';
22 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
24 marginTop: theme.spacing.unit,
25 marginBottom: theme.spacing.unit
28 borderRadius: theme.spacing.unit * 0.5,
30 borderColor: theme.palette.grey["400"],
33 textDecoration: 'none',
34 color: theme.palette.primary.main,
36 color: theme.palette.primary.dark,
37 transition: 'all 0.5s ease'
41 textDecoration: 'none',
42 color: theme.palette.grey["500"],
45 color: theme.palette.common.black,
46 transition: 'all 0.5s ease'
52 cardWithoutMachines: {
57 marginTop: theme.spacing.unit
61 const mapStateToProps = ({ virtualMachines, auth }: RootState) => {
63 requestedDate: virtualMachines.date,
64 isAdmin: auth.user!.isAdmin,
69 const mapDispatchToProps = {
71 loadVirtualMachinesData
74 interface VirtualMachinesPanelDataProps {
75 requestedDate: string;
76 virtualMachines: ListResults<any>;
77 logins: VirtualMachinesLoginsResource[];
78 links: ListResults<any>;
82 interface VirtualMachinesPanelActionProps {
83 saveRequestedDate: () => void;
84 loadVirtualMachinesData: () => string;
87 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
89 export const VirtualMachinePanel = compose(
91 connect(mapStateToProps, mapDispatchToProps))(
92 class extends React.Component<VirtualMachineProps> {
94 this.props.loadVirtualMachinesData();
98 const { virtualMachines, links } = this.props;
100 <Grid container spacing={16}>
101 {virtualMachines.itemsAvailable === 0 && <CardContentWithNoVirtualMachines {...this.props} />}
102 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
103 {<CardSSHSection {...this.props} />}
110 const CardContentWithNoVirtualMachines = (props: VirtualMachineProps) =>
113 <CardContent className={props.classes.cardWithoutMachines}>
115 <Typography variant="body2">
116 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.
119 <Grid item xs={6} className={props.classes.rightAlign}>
120 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
121 SEND REQUEST FOR SHELL ACCESS
123 {props.requestedDate &&
124 <Typography variant="body1">
125 A request for shell access was sent on {props.requestedDate}
132 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
136 <div className={props.classes.rightAlign}>
137 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
138 SEND REQUEST FOR SHELL ACCESS
140 {props.requestedDate &&
141 <Typography variant="body1">
142 A request for shell access was sent on {props.requestedDate}
145 <div className={props.classes.icon}>
146 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" className={props.classes.linkIcon}>
147 <Tooltip title="Access VM using webshell">
152 {console.log(props.isAdmin)}
153 {props.isAdmin ? adminVirtualMachinesTable(props) : userVirtualMachinesTable(props)}
158 const userVirtualMachinesTable = (props: VirtualMachineProps) =>
162 <TableCell>Host name</TableCell>
163 <TableCell>Login name</TableCell>
164 <TableCell>Command line</TableCell>
165 <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, it)}</TableCell>
173 <TableCell>ssh {getUsername(props.links, it)}@shell.arvados</TableCell>
175 <a href={`https://workbench.c97qk.arvadosapi.com${it.href}/webshell/${getUsername(props.links, it)}`} target="_blank" className={props.classes.link}>
176 Log in as {getUsername(props.links, it)}
184 const adminVirtualMachinesTable = (props: VirtualMachineProps) =>
188 <TableCell>Uuid</TableCell>
189 <TableCell>Host name</TableCell>
190 <TableCell>Logins</TableCell>
195 {props.virtualMachines.items.map((it, index) =>
196 <TableRow key={index}>
197 <TableCell>{it.uuid}</TableCell>
198 <TableCell>shell</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 getUsername = (links: ListResults<any>, virtualMachine: VirtualMachinesResource) => {
211 const link = links.items.find((item: any) => item.headUuid === virtualMachine.uuid);
212 return link.properties.username || undefined;
215 const CardSSHSection = (props: VirtualMachineProps) =>
219 <Typography variant="body2">
220 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):
223 className={props.classes.codeSnippet}
229 const textSSH = `Host *.arvados
231 ServerAliveInterval 60
232 ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;