19093: Add process details attriutes warning for container retry
[arvados.git] / src / views / virtual-machine-panel / virtual-machine-user-panel.tsx
1 // Copyright (C) The Arvados Authors. All rights reserved.
2 //
3 // SPDX-License-Identifier: AGPL-3.0
4
5 import React from 'react';
6 import { connect } from 'react-redux';
7 import { Grid, Typography, Button, Card, CardContent, TableBody, TableCell, TableHead, TableRow, Table, Tooltip, Chip } from '@material-ui/core';
8 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
9 import { ArvadosTheme } from 'common/custom-theme';
10 import { compose, Dispatch } from 'redux';
11 import { saveRequestedDate, loadVirtualMachinesUserData } from 'store/virtual-machines/virtual-machines-actions';
12 import { RootState } from 'store/store';
13 import { ListResults } from 'services/common-service/common-service';
14 import { HelpIcon } from 'components/icon/icon';
15 import { SESSION_STORAGE } from "services/auth-service/auth-service";
16 // import * as CopyToClipboard from 'react-copy-to-clipboard';
17 import parse from "parse-duration";
18 import { CopyIcon } from 'components/icon/icon';
19 import CopyToClipboard from 'react-copy-to-clipboard';
20 import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions';
21
22 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon' | 'chipsRoot' | 'copyIcon' | 'webshellButton';
23
24 const EXTRA_TOKEN = "exraToken";
25
26 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
27     button: {
28         marginTop: theme.spacing.unit,
29         marginBottom: theme.spacing.unit
30     },
31     codeSnippet: {
32         borderRadius: theme.spacing.unit * 0.5,
33         border: '1px solid',
34         borderColor: theme.palette.grey["400"],
35     },
36     link: {
37         textDecoration: 'none',
38         color: theme.palette.primary.main,
39         "&:hover": {
40             color: theme.palette.primary.dark,
41             transition: 'all 0.5s ease'
42         }
43     },
44     linkIcon: {
45         textDecoration: 'none',
46         color: theme.palette.grey["500"],
47         textAlign: 'right',
48         "&:hover": {
49             color: theme.palette.common.black,
50             transition: 'all 0.5s ease'
51         }
52     },
53     rightAlign: {
54         textAlign: "right"
55     },
56     cardWithoutMachines: {
57         display: 'flex'
58     },
59     icon: {
60         textAlign: "right",
61         marginTop: theme.spacing.unit
62     },
63     chipsRoot: {
64         margin: `0px -${theme.spacing.unit / 2}px`,
65     },
66     copyIcon: {
67         marginLeft: theme.spacing.unit,
68         color: theme.palette.grey["500"],
69         cursor: 'pointer',
70         display: 'inline',
71         '& svg': {
72             fontSize: '1rem'
73         }
74     },
75     webshellButton: {
76         textTransform: "initial",
77     },
78 });
79
80 const mapStateToProps = (state: RootState) => {
81     return {
82         requestedDate: state.virtualMachines.date,
83         userUuid: state.auth.user!.uuid,
84         helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
85         hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "",
86         token: state.auth.extraApiToken || state.auth.apiToken || '',
87         tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''),
88         webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
89         idleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
90         ...state.virtualMachines
91     };
92 };
93
94 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate' | 'onCopy'> => ({
95     saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
96     loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
97     onCopy: (message: string) => {
98         dispatch(snackbarActions.OPEN_SNACKBAR({
99             message,
100             hideDuration: 2000,
101             kind: SnackbarKind.SUCCESS
102         }));
103     },
104 });
105
106 interface VirtualMachinesPanelDataProps {
107     requestedDate: string;
108     virtualMachines: ListResults<any>;
109     userUuid: string;
110     links: ListResults<any>;
111     helpText: string;
112     hostSuffix: string;
113     token: string;
114     tokenLocation: string;
115     webshellUrl: string;
116     idleTimeout: number;
117 }
118
119 interface VirtualMachinesPanelActionProps {
120     saveRequestedDate: () => void;
121     loadVirtualMachinesData: () => string;
122     onCopy: (message: string) => void;
123 }
124
125 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
126
127 export const VirtualMachineUserPanel = compose(
128     withStyles(styles),
129     connect(mapStateToProps, mapDispatchToProps))(
130         class extends React.Component<VirtualMachineProps> {
131             componentDidMount() {
132                 this.props.loadVirtualMachinesData();
133             }
134
135             render() {
136                 const { virtualMachines, links } = this.props;
137                 return (
138                     <Grid container spacing={16} data-cy="vm-user-panel">
139                         {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
140                         {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
141                         {<CardSSHSection {...this.props} />}
142                     </Grid>
143                 );
144             }
145         }
146     );
147
148 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
149     <Grid item xs={12}>
150         <Card>
151             <CardContent className={props.classes.cardWithoutMachines}>
152                 <Grid item xs={6}>
153                     <Typography variant='body1'>
154                         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.
155                     </Typography>
156                 </Grid>
157                 <Grid item xs={6} className={props.classes.rightAlign}>
158                     {virtualMachineSendRequest(props)}
159                 </Grid>
160             </CardContent>
161         </Card>
162     </Grid>;
163
164 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
165     <Grid item xs={12}>
166         <Card>
167             <CardContent>
168                 <span>
169                     <div className={props.classes.rightAlign}>
170                         {virtualMachineSendRequest(props)}
171                     </div>
172                     <div className={props.classes.icon}>
173                         <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" rel="noopener noreferrer" className={props.classes.linkIcon}>
174                             <Tooltip title="Access VM using webshell">
175                                 <HelpIcon />
176                             </Tooltip>
177                         </a>
178                     </div>
179                     {virtualMachinesTable(props)}
180                 </span>
181
182             </CardContent>
183         </Card>
184     </Grid>;
185
186 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
187     <span>
188         <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
189             SEND REQUEST FOR SHELL ACCESS
190         </Button>
191         {props.requestedDate &&
192             <Typography >
193                 A request for shell access was sent on {props.requestedDate}
194             </Typography>}
195     </span>;
196
197 const virtualMachinesTable = (props: VirtualMachineProps) =>
198     <Table data-cy="vm-user-table">
199         <TableHead>
200             <TableRow>
201                 <TableCell>Host name</TableCell>
202                 <TableCell>Login name</TableCell>
203                 <TableCell>Groups</TableCell>
204                 <TableCell>Command line</TableCell>
205                 <TableCell>Web shell</TableCell>
206             </TableRow>
207         </TableHead>
208         <TableBody>
209             {props.virtualMachines.items.map(it =>
210                 props.links.items.map(lk => {
211                     if (lk.tailUuid === props.userUuid && lk.headUuid === it.uuid) {
212                         const username = lk.properties.username;
213                         const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
214                         let tokenParam = "";
215                         if (props.tokenLocation === SESSION_STORAGE || props.tokenLocation === EXTRA_TOKEN) {
216                           tokenParam = `&token=${encodeURIComponent(props.token)}`;
217                         }
218                         const loginHref = `/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&timeout=${props.idleTimeout}&login=${encodeURIComponent(username)}${tokenParam}`;
219                         return <TableRow key={lk.uuid}>
220                             <TableCell>{it.hostname}</TableCell>
221                             <TableCell>{username}</TableCell>
222                             <TableCell>
223                                 <Grid container spacing={8} className={props.classes.chipsRoot}>
224                                     {
225                                     (lk.properties.groups || []).map((group, i) => (
226                                         <Grid item key={i}>
227                                             <Chip label={group} />
228                                         </Grid>
229                                     ))
230                                     }
231                                 </Grid>
232                             </TableCell>
233                             <TableCell>
234                                 {command}
235                                 <Tooltip title="Copy to clipboard">
236                                     <span className={props.classes.copyIcon}>
237                                         <CopyToClipboard text={command || ""} onCopy={() => props.onCopy!("Copied")}>
238                                             <CopyIcon />
239                                         </CopyToClipboard>
240                                     </span>
241                                 </Tooltip>
242                             </TableCell>
243                             <TableCell>
244                                 <Button
245                                     className={props.classes.webshellButton}
246                                     variant="contained"
247                                     size="small"
248                                     href={loginHref}
249                                     target="_blank"
250                                     rel="noopener noreferrer">
251                                         Log in as {username}
252                                 </Button>
253                             </TableCell>
254                         </TableRow>;
255                     }
256                     return null;
257                 }
258                 ))}
259         </TableBody>
260     </Table>;
261
262 const CardSSHSection = (props: VirtualMachineProps) =>
263     <Grid item xs={12}>
264         <Card>
265             <CardContent>
266                 <Typography>
267                     <div dangerouslySetInnerHTML={{ __html: props.helpText }} style={{ margin: "1em" }} />
268                 </Typography>
269             </CardContent>
270         </Card>
271     </Grid>;