65865370d21147a43dabaf45a590495ca67a0dfa
[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 } 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 { Dispatch, 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 } from '~/models/virtual-machines';
18 import { Routes } from '~/routes/routes';
19
20 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'icon';
21
22 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
23     button: {
24         marginTop: theme.spacing.unit,
25         marginBottom: theme.spacing.unit * 2
26     },
27     codeSnippet: {
28         borderRadius: theme.spacing.unit * 0.5,
29         border: '1px solid',
30         borderColor: theme.palette.grey["400"],
31     },
32     link: {
33         textDecoration: 'none',
34         color: theme.palette.primary.main,
35         "&:hover": {
36             color: theme.palette.primary.dark,
37             transition: 'all 0.5s ease'
38         }
39     },
40     linkIcon: {
41         textDecoration: 'none',
42         color: theme.palette.grey["500"],
43         textAlign: 'right',
44         "&:hover": {
45             color: theme.palette.common.black,
46             transition: 'all 0.5s ease'
47         }
48     },
49     icon: {
50         textAlign: "right"
51     }
52 });
53
54 const mapStateToProps = (state: RootState) => {
55     return {
56         requestedDate: state.virtualMachines.date,
57         virtualMachines: state.virtualMachines.virtualMachines,
58         logins: state.virtualMachines.logins,
59         links: state.virtualMachines.links
60     };
61 };
62
63 const mapDispatchToProps = (dispatch: Dispatch) => ({
64     saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
65     loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesData())
66 });
67
68 interface VirtualMachinesPanelDataProps {
69     requestedDate: string;
70     virtualMachines: ListResults<any>;
71     logins: VirtualMachinesLoginsResource[];
72     links: ListResults<any>;
73 }
74
75 interface VirtualMachinesPanelActionProps {
76     saveRequestedDate: () => void;
77     loadVirtualMachinesData: () => string;
78 }
79
80 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
81
82 export const VirtualMachinePanel = compose(
83     withStyles(styles),
84     connect(mapStateToProps, mapDispatchToProps))(
85         class extends React.Component<VirtualMachineProps> {
86             componentDidMount() {
87                 this.props.loadVirtualMachinesData();
88             }
89
90             render() {
91                 const { classes, saveRequestedDate, requestedDate, virtualMachines, logins, links } = this.props;
92                 return (
93                     <Grid container spacing={16}>
94                         {cardContentWithNoVirtualMachines(requestedDate, saveRequestedDate, classes)}
95                         {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && cardContentWithVirtualMachines(virtualMachines, links, classes)}
96                         {cardSSHSection(classes)}
97                     </Grid>
98                 );
99             }
100         }
101     );
102
103 const cardContentWithNoVirtualMachines = (requestedDate: string, saveRequestedDate: () => void, classes: any) =>
104     <Grid item xs={12}>
105         <Card>
106             <CardContent>
107                 <Typography variant="body2">
108                     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.
109                 </Typography>
110                 <Button variant="contained" color="primary" className={classes.button} onClick={saveRequestedDate}>
111                     SEND REQUEST FOR SHELL ACCESS
112                 </Button>
113                 {requestedDate &&
114                     <Typography variant="body1">
115                         A request for shell access was sent on {requestedDate}
116                     </Typography>}
117             </CardContent>
118         </Card>
119     </Grid>;
120
121 const login = 'pawelkowalczyk';
122
123 const cardContentWithVirtualMachines = (virtualMachines: ListResults<any>, links: ListResults<any>, classes: any) =>
124     <Grid item xs={12}>
125         <Card>
126             <CardContent>
127                 <div className={classes.icon}>
128                     <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" className={classes.linkIcon}>
129                         <Tooltip title="Access VM using webshell">
130                             <HelpIcon />
131                         </Tooltip>
132                     </a>
133                 </div>
134                 <Table>
135                     <TableHead>
136                         <TableRow>
137                             <TableCell>Host name</TableCell>
138                             <TableCell>Login name</TableCell>
139                             <TableCell>Command line</TableCell>
140                             <TableCell>Web shell</TableCell>
141                         </TableRow>
142                     </TableHead>
143                     <TableBody>
144                         {virtualMachines.items.map((it, index) =>
145                             <TableRow key={index}>
146                                 <TableCell>{it.hostname}</TableCell>
147                                 <TableCell>{getUsername(links, it)}</TableCell>
148                                 <TableCell>ssh {getUsername(links, it)}@shell.arvados</TableCell>
149                                 <TableCell>
150                                     <a href={`https://workbench.c97qk.arvadosapi.com${it.href}/webshell/${getUsername(links, it)}`} target="_blank" className={classes.link}>
151                                         Log in as {getUsername(links, it)}
152                                     </a>
153                                 </TableCell>
154                             </TableRow>
155                         )}
156                     </TableBody>
157                 </Table>
158             </CardContent>
159         </Card>
160     </Grid>;
161
162 const getUsername = (links: ListResults<any>, virtualMachine: any) => {
163     const link = links.items.find((item: any) => item.headUuid === virtualMachine.uuid);
164     return link.properties.username || undefined;
165 };
166
167 const cardSSHSection = (classes: any) =>
168     <Grid item xs={12}>
169         <Card>
170             <CardContent>
171                 <Typography variant="body2">
172                     In order to access virtual machines using SSH, <Link to={Routes.SSH_KEYS} className={classes.link}>add an SSH key to your account</Link> and add a section like this to your SSH configuration file ( ~/.ssh/config):
173                 </Typography>
174                 <DefaultCodeSnippet
175                     className={classes.codeSnippet}
176                     lines={[textSSH]} />
177             </CardContent>
178         </Card>
179     </Grid>;
180
181 const textSSH = `Host *.arvados
182     TCPKeepAlive yes
183     ServerAliveInterval 60
184     ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;