Add login in/out handling, fix async validation
[arvados-workbench2.git] / src / views / virtual-machine-panel / virtual-machine-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, 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';
20
21 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon' | 'moreOptionsButton' | 'moreOptions';
22
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
24     button: {
25         marginTop: theme.spacing.unit,
26         marginBottom: theme.spacing.unit
27     },
28     codeSnippet: {
29         borderRadius: theme.spacing.unit * 0.5,
30         border: '1px solid',
31         borderColor: theme.palette.grey["400"],
32     },
33     link: {
34         textDecoration: 'none',
35         color: theme.palette.primary.main,
36         "&:hover": {
37             color: theme.palette.primary.dark,
38             transition: 'all 0.5s ease'
39         }
40     },
41     linkIcon: {
42         textDecoration: 'none',
43         color: theme.palette.grey["500"],
44         textAlign: 'right',
45         "&:hover": {
46             color: theme.palette.common.black,
47             transition: 'all 0.5s ease'
48         }
49     },
50     rightAlign: {
51         textAlign: "right"
52     },
53     cardWithoutMachines: {
54         display: 'flex'
55     },
56     icon: {
57         textAlign: "right",
58         marginTop: theme.spacing.unit
59     },
60     moreOptionsButton: {
61         padding: 0
62     },
63     moreOptions: {
64         textAlign: 'right',
65         '&:last-child': {
66             paddingRight: 0
67         }
68     },
69 });
70
71 const mapStateToProps = ({ virtualMachines, auth }: RootState) => {
72     return {
73         requestedDate: virtualMachines.date,
74         isAdmin: auth.user!.isAdmin,
75         logins: virtualMachines.logins,
76         ...virtualMachines
77     };
78 };
79
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));
85     },
86 });
87
88 interface VirtualMachinesPanelDataProps {
89     requestedDate: string;
90     virtualMachines: ListResults<any>;
91     logins: VirtualMachineLogins;
92     links: ListResults<any>;
93     isAdmin: boolean;
94 }
95
96 interface VirtualMachinesPanelActionProps {
97     saveRequestedDate: () => void;
98     loadVirtualMachinesData: () => string;
99     onOptionsMenuOpen: (event: React.MouseEvent<HTMLElement>, virtualMachine: VirtualMachinesResource) => void;
100 }
101
102 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
103
104 export const VirtualMachinePanel = compose(
105     withStyles(styles),
106     connect(mapStateToProps, mapDispatchToProps))(
107         class extends React.Component<VirtualMachineProps> {
108             componentDidMount() {
109                 this.props.loadVirtualMachinesData();
110             }
111
112             render() {
113                 const { virtualMachines, links, isAdmin } = this.props;
114                 return (
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} />}
119                     </Grid>
120                 );
121             }
122         }
123     );
124
125 const CardContentWithNoVirtualMachines = (props: VirtualMachineProps) =>
126     <Grid item xs={12}>
127         <Card>
128             <CardContent className={props.classes.cardWithoutMachines}>
129                 <Grid item xs={6}>
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.
132                     </Typography>
133                 </Grid>
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
137                     </Button>
138                     {props.requestedDate &&
139                         <Typography variant="body1">
140                             A request for shell access was sent on {props.requestedDate}
141                         </Typography>}
142                 </Grid>
143             </CardContent>
144         </Card>
145     </Grid>;
146
147 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
148     <Grid item xs={12}>
149         <Card>
150             <CardContent>
151                 {props.isAdmin ? <span>{adminVirtualMachinesTable(props)}</span>
152                     : <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
156                                 </Button>
157                             {props.requestedDate &&
158                                 <Typography variant="body1">
159                                     A request for shell access was sent on {props.requestedDate}
160                                 </Typography>}
161                         </div>
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">
165                                     <HelpIcon />
166                                 </Tooltip>
167                             </a>
168                         </div>
169                         {userVirtualMachinesTable(props)}
170                     </span>
171                 }
172             </CardContent>
173         </Card>
174     </Grid>;
175
176 const userVirtualMachinesTable = (props: VirtualMachineProps) =>
177     <Table>
178         <TableHead>
179             <TableRow>
180                 <TableCell>Host name</TableCell>
181                 <TableCell>Login name</TableCell>
182                 <TableCell>Command line</TableCell>
183                 <TableCell>Web shell</TableCell>
184             </TableRow>
185         </TableHead>
186         <TableBody>
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>
192                     <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)}
195                         </a>
196                     </TableCell>
197                 </TableRow>
198             )}
199         </TableBody>
200     </Table>;
201
202 const adminVirtualMachinesTable = (props: VirtualMachineProps) =>
203     <Table>
204         <TableHead>
205             <TableRow>
206                 <TableCell>Uuid</TableCell>
207                 <TableCell>Host name</TableCell>
208                 <TableCell>Logins</TableCell>
209                 <TableCell />
210             </TableRow>
211         </TableHead>
212         <TableBody>
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}>
221                                 <MoreOptionsIcon />
222                             </IconButton>
223                         </Tooltip>
224                     </TableCell>
225                 </TableRow>
226             )}
227         </TableBody>
228     </Table>;
229
230 const getUsername = (links: ListResults<any>) => {
231     return links.items[0].properties.username;
232 };
233
234 const CardSSHSection = (props: VirtualMachineProps) =>
235     <Grid item xs={12}>
236         <Card>
237             <CardContent>
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):
240                 </Typography>
241                 <DefaultCodeSnippet
242                     className={props.classes.codeSnippet}
243                     lines={[textSSH]} />
244             </CardContent>
245         </Card>
246     </Grid>;
247
248 const textSSH = `Host *.arvados
249     TCPKeepAlive yes
250     ServerAliveInterval 60
251     ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;