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