fixed conflicts
[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
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["500"],
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 = compose(
80     withStyles(styles),
81     connect(mapStateToProps, mapDispatchToProps))(
82         class extends React.Component<VirtualMachineProps> {
83             componentDidMount() {
84                 this.props.loadVirtualMachinesData();
85             }
86
87             render() {
88                 const { classes, saveRequestedDate, requestedDate, virtualMachines, logins } = this.props;
89                 return (
90                     <Grid container spacing={16}>
91                         {cardContentWithNoVirtualMachines(requestedDate, saveRequestedDate, classes)}
92                         {virtualMachines.itemsAvailable > 0 && cardContentWithVirtualMachines(virtualMachines, classes)}
93                         {cardSSHSection(classes)}
94                     </Grid>
95                 );
96             }
97         }
98     );
99
100 const cardContentWithNoVirtualMachines = (requestedDate: string, saveRequestedDate: () => void, classes: any) =>
101     <Grid item xs={12}>
102         <Card>
103             <CardContent>
104                 <Typography variant="body2">
105                     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.
106                 </Typography>
107                 <Button variant="contained" color="primary" className={classes.button} onClick={saveRequestedDate}>
108                     SEND REQUEST FOR SHELL ACCESS
109                 </Button>
110                 {requestedDate &&
111                     <Typography variant="body1">
112                         A request for shell access was sent on {requestedDate}
113                     </Typography>}
114             </CardContent>
115         </Card>
116     </Grid>;
117
118 const login = 'pawelkowalczyk';
119
120 const cardContentWithVirtualMachines = (virtualMachines: ListResults<any>, classes: any) =>
121     <Grid item xs={12}>
122         <Card>
123             <CardContent>
124                 <div className={classes.icon}>
125                     <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" className={classes.linkIcon}>
126                         <Tooltip title="Access VM using webshell">
127                             <HelpIcon />
128                         </Tooltip>
129                     </a>
130                 </div>
131                 <Table>
132                     <TableHead>
133                         <TableRow>
134                             <TableCell>Host name</TableCell>
135                             <TableCell>Login name</TableCell>
136                             <TableCell>Command line</TableCell>
137                             <TableCell>Web shell</TableCell>
138                         </TableRow>
139                     </TableHead>
140                     <TableBody>
141                         {virtualMachines.items.map((it, index) =>
142                             <TableRow key={index}>
143                                 <TableCell>{it.hostname}</TableCell>
144                                 <TableCell>{login}</TableCell>
145                                 <TableCell>ssh {login}@shell.arvados</TableCell>
146                                 <TableCell>
147                                     <a href={`https://workbench.c97qk.arvadosapi.com${it.href}/webshell/${login}`} target="_blank" className={classes.link}>
148                                         Log in as {login}
149                                     </a>
150                                 </TableCell>
151                             </TableRow>
152                         )}
153                     </TableBody>
154                 </Table>
155             </CardContent>
156         </Card>
157     </Grid>;
158
159 // dodac link do ssh panelu jak juz bedzie
160 const cardSSHSection = (classes: any) =>
161     <Grid item xs={12}>
162         <Card>
163             <CardContent>
164                 <Typography variant="body2">
165                     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):
166                 </Typography>
167                 <DefaultCodeSnippet
168                     className={classes.codeSnippet}
169                     lines={[textSSH]} />
170             </CardContent>
171         </Card>
172     </Grid>;
173
174 const textSSH = `Host *.arvados
175     TCPKeepAlive yes
176     ServerAliveInterval 60
177     ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;