virtual-machines-panel-init
[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 } 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, loadRequestedDate } from '~/store/virtual-machines/virtual-machines-actions';
14 import { RootState } from '~/store/store';
15
16 type CssRules = 'button' | 'codeSnippet' | 'link';
17
18 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
19     button: {
20         marginTop: theme.spacing.unit,
21         marginBottom: theme.spacing.unit * 2
22     },
23     codeSnippet: {
24         borderRadius: theme.spacing.unit * 0.5,
25         border: '1px solid',
26         borderColor: theme.palette.grey["400"],
27         maxHeight: '400px'
28     },
29     link: {
30         textDecoration: 'none',
31         color: theme.palette.primary.main
32     },
33 });
34
35 const mapStateToProps = (state: RootState) => {
36     return {
37         requestedDate: state.virtualMachines.date
38     };
39 };
40
41 const mapDispatchToProps = (dispatch: Dispatch) => ({
42     saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
43     loadRequestedDate: () => dispatch<any>(loadRequestedDate())
44 });
45
46 interface VirtualMachinesPanelDataProps {
47     requestedDate: string;
48 }
49
50 interface VirtualMachinesPanelActionProps {
51     saveRequestedDate: () => void;
52     loadRequestedDate: () => string;
53 }
54
55 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
56
57 export const VirtualMachinePanel = withStyles(styles)(connect(mapStateToProps, mapDispatchToProps)(
58     class extends React.Component<VirtualMachineProps> {
59         componentDidMount() {
60             this.props.loadRequestedDate();
61         }
62
63         render() {
64             const { classes, saveRequestedDate, requestedDate } = this.props;
65             return (
66                 <Grid container spacing={16}>
67                     <Grid item xs={12}>
68                         <Card>
69                             <CardContent>
70                                 <Typography variant="body2">
71                                     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.
72                                 </Typography>
73                                 <Button variant="contained" color="primary" className={classes.button} onClick={saveRequestedDate}>
74                                     SEND REQUEST FOR SHELL ACCESS
75                                 </Button>
76                                 {requestedDate &&
77                                     <Typography variant="body1">
78                                         A request for shell access was sent on {requestedDate}
79                                     </Typography>}
80                             </CardContent>
81                         </Card>
82                     </Grid>
83                     <Grid item xs={12}>
84                         <Card>
85                             <CardContent>
86                                 <Typography variant="body2">
87                                     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):
88                                 </Typography>
89                                 <DefaultCodeSnippet
90                                     className={classes.codeSnippet}
91                                     lines={[textSSH]} />
92                             </CardContent>
93                         </Card>
94                     </Grid>
95                 </Grid >
96             );
97         }
98     }));
99
100
101
102 const textSSH = `Host *.arvados
103     TCPKeepAlive yes
104     ServerAliveInterval 60
105     ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;