refs #14498-usernames-in-machines
[arvados-workbench2.git] / src / views / virtual-machine-panel / virtual-machine-user-panel.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
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';
18
19 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon';
20
21 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
22     button: {
23         marginTop: theme.spacing.unit,
24         marginBottom: theme.spacing.unit
25     },
26     codeSnippet: {
27         borderRadius: theme.spacing.unit * 0.5,
28         border: '1px solid',
29         borderColor: theme.palette.grey["400"],
30     },
31     link: {
32         textDecoration: 'none',
33         color: theme.palette.primary.main,
34         "&:hover": {
35             color: theme.palette.primary.dark,
36             transition: 'all 0.5s ease'
37         }
38     },
39     linkIcon: {
40         textDecoration: 'none',
41         color: theme.palette.grey["500"],
42         textAlign: 'right',
43         "&:hover": {
44             color: theme.palette.common.black,
45             transition: 'all 0.5s ease'
46         }
47     },
48     rightAlign: {
49         textAlign: "right"
50     },
51     cardWithoutMachines: {
52         display: 'flex'
53     },
54     icon: {
55         textAlign: "right",
56         marginTop: theme.spacing.unit
57     }
58 });
59
60 const mapStateToProps = (state: RootState) => {
61     return {
62         requestedDate: state.virtualMachines.date,
63         userUuid: state.auth.user!.uuid,
64         ...state.virtualMachines
65     };
66 };
67
68 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate'> => ({
69     saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
70     loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
71 });
72
73 interface VirtualMachinesPanelDataProps {
74     requestedDate: string;
75     virtualMachines: ListResults<any>;
76     userUuid: string;
77     links: ListResults<any>;
78 }
79
80 interface VirtualMachinesPanelActionProps {
81     saveRequestedDate: () => void;
82     loadVirtualMachinesData: () => string;
83 }
84
85 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
86
87 export const VirtualMachineUserPanel = compose(
88     withStyles(styles),
89     connect(mapStateToProps, mapDispatchToProps))(
90         class extends React.Component<VirtualMachineProps> {
91             componentDidMount() {
92                 this.props.loadVirtualMachinesData();
93             }
94
95             render() {
96                 const { virtualMachines, links } = this.props;
97                 return (
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} />}
102                     </Grid>
103                 );
104             }
105         }
106     );
107
108 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
109     <Grid item xs={12}>
110         <Card>
111             <CardContent className={props.classes.cardWithoutMachines}>
112                 <Grid item xs={6}>
113                     <Typography variant="body2">
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.
115                     </Typography>
116                 </Grid>
117                 <Grid item xs={6} className={props.classes.rightAlign}>
118                     {virtualMachineSendRequest(props)}
119                 </Grid>
120             </CardContent>
121         </Card>
122     </Grid>;
123
124 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
125     <Grid item xs={12}>
126         <Card>
127             <CardContent>
128                 <span>
129                     <div className={props.classes.rightAlign}>
130                         {virtualMachineSendRequest(props)}
131                     </div>
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">
135                                 <HelpIcon />
136                             </Tooltip>
137                         </a>
138                     </div>
139                     {virtualMachinesTable(props)}
140                 </span>
141
142             </CardContent>
143         </Card>
144     </Grid>;
145
146 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
147     <span>
148         <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
149             SEND REQUEST FOR SHELL ACCESS
150         </Button>
151         {props.requestedDate &&
152             <Typography variant="body1">
153                 A request for shell access was sent on {props.requestedDate}
154             </Typography>}
155     </span>;
156
157 const virtualMachinesTable = (props: VirtualMachineProps) =>
158     <Table>
159         <TableHead>
160             <TableRow>
161                 <TableCell>Host name</TableCell>
162                 <TableCell>Login name</TableCell>
163                 <TableCell>Command line</TableCell>
164                 <TableCell>Web shell</TableCell>
165             </TableRow>
166         </TableHead>
167         <TableBody>
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>
173                     <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)}
176                         </a>
177                     </TableCell>
178                 </TableRow>
179             )}
180         </TableBody>
181     </Table>;
182
183 const getUsername = (links: ListResults<any>, userUuid: string) => {
184     return links.items.map(it => it.tailUuid === userUuid ? it.properties.username : '');
185 };
186
187 const CardSSHSection = (props: VirtualMachineProps) =>
188     <Grid item xs={12}>
189         <Card>
190             <CardContent>
191                 <Typography variant="body2">
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):
193                 </Typography>
194                 <DefaultCodeSnippet
195                     className={props.classes.codeSnippet}
196                     lines={[textSSH]} />
197             </CardContent>
198         </Card>
199     </Grid>;
200
201 const textSSH = `Host *.arvados
202     TCPKeepAlive yes
203     ServerAliveInterval 60
204     ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;