16181: Support SSHHelpHostSuffix, fix user VM page render issues
[arvados-workbench2.git] / src / views / virtual-machine-panel / virtual-machine-user-panel.tsx
index fbb1f23f51cb33069167cb763ea76833eed6e4bf..5e131861edacf0e87329d37ccec34d33b36f0d3a 100644 (file)
@@ -7,14 +7,12 @@ import { connect } from 'react-redux';
 import { Grid, Typography, Button, Card, CardContent, TableBody, TableCell, TableHead, TableRow, Table, Tooltip } from '@material-ui/core';
 import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles';
 import { ArvadosTheme } from '~/common/custom-theme';
-import { DefaultCodeSnippet } from '~/components/default-code-snippet/default-code-snippet';
-import { Link } from 'react-router-dom';
 import { compose, Dispatch } from 'redux';
 import { saveRequestedDate, loadVirtualMachinesUserData } from '~/store/virtual-machines/virtual-machines-actions';
 import { RootState } from '~/store/store';
 import { ListResults } from '~/services/common-service/common-service';
 import { HelpIcon } from '~/components/icon/icon';
-import { Routes } from '~/routes/routes';
+// import * as CopyToClipboard from 'react-copy-to-clipboard';
 
 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon';
 
@@ -57,10 +55,14 @@ const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
     }
 });
 
-const mapStateToProps = ({ virtualMachines }: RootState) => {
+const mapStateToProps = (state: RootState) => {
     return {
-        requestedDate: virtualMachines.date,
-        ...virtualMachines
+        requestedDate: state.virtualMachines.date,
+        userUuid: state.auth.user!.uuid,
+        helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
+        hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix,
+        webShell: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
+        ...state.virtualMachines
     };
 };
 
@@ -72,7 +74,11 @@ const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelAction
 interface VirtualMachinesPanelDataProps {
     requestedDate: string;
     virtualMachines: ListResults<any>;
+    userUuid: string;
     links: ListResults<any>;
+    helpText: string;
+    hostSuffix: string;
+    webShell: string;
 }
 
 interface VirtualMachinesPanelActionProps {
@@ -108,7 +114,7 @@ const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
         <Card>
             <CardContent className={props.classes.cardWithoutMachines}>
                 <Grid item xs={6}>
-                    <Typography variant="body2">
+                    <Typography variant='body1'>
                         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.
                     </Typography>
                 </Grid>
@@ -147,7 +153,7 @@ const virtualMachineSendRequest = (props: VirtualMachineProps) =>
             SEND REQUEST FOR SHELL ACCESS
         </Button>
         {props.requestedDate &&
-            <Typography variant="body1">
+            <Typography >
                 A request for shell access was sent on {props.requestedDate}
             </Typography>}
     </span>;
@@ -159,44 +165,41 @@ const virtualMachinesTable = (props: VirtualMachineProps) =>
                 <TableCell>Host name</TableCell>
                 <TableCell>Login name</TableCell>
                 <TableCell>Command line</TableCell>
-                <TableCell>Web shell</TableCell>
+                {props.webShell !== "" && <TableCell>Web shell</TableCell>}
             </TableRow>
         </TableHead>
         <TableBody>
-            {props.virtualMachines.items.map((it, index) =>
-                <TableRow key={index}>
-                    <TableCell>{it.hostname}</TableCell>
-                    <TableCell>{getUsername(props.links)}</TableCell>
-                    <TableCell>ssh {getUsername(props.links)}@{it.hostname}.arvados</TableCell>
-                    <TableCell>
-                        <a href={`https://workbench.c97qk.arvadosapi.com${it.href}/webshell/${getUsername(props.links)}`} target="_blank" className={props.classes.link}>
-                            Log in as {getUsername(props.links)}
-                        </a>
-                    </TableCell>
-                </TableRow>
-            )}
+            {props.virtualMachines.items.map(it =>
+                props.links.items.map(lk => {
+                    if (lk.tailUuid === props.userUuid) {
+                        const username = lk.properties.username;
+                        const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
+                        return <TableRow key={lk.uuid}>
+                            <TableCell>{it.hostname}</TableCell>
+                            <TableCell>{username}</TableCell>
+                            <TableCell>
+                                {command}
+                            </TableCell>
+                            {props.webShell !== "" && <TableCell>
+                                <a href={`${props.webShell}${it.href}/webshell/${username}`} target="_blank" className={props.classes.link}>
+                                    Log in as {username}
+                                </a>
+                            </TableCell>}
+                        </TableRow>;
+                    }
+                    return;
+                }
+                ))}
         </TableBody>
     </Table>;
 
-const getUsername = (links: ListResults<any>) => {
-    return links.items[0].properties.username;
-};
-
 const CardSSHSection = (props: VirtualMachineProps) =>
     <Grid item xs={12}>
         <Card>
             <CardContent>
-                <Typography variant="body2">
-                    In order to access virtual machines using SSH, <Link to={Routes.SSH_KEYS_USER} className={props.classes.link}>add an SSH key to your account</Link> and add a section like this to your SSH configuration file ( ~/.ssh/config):
+                <Typography>
+                    <div dangerouslySetInnerHTML={{ __html: props.helpText }} style={{ margin: "1em" }} />
                 </Typography>
-                <DefaultCodeSnippet
-                    className={props.classes.codeSnippet}
-                    lines={[textSSH]} />
             </CardContent>
         </Card>
     </Grid>;
-
-const textSSH = `Host *.arvados
-    TCPKeepAlive yes
-    ServerAliveInterval 60
-    ProxyCommand ssh -p2222 turnout@switchyard.api.ardev.roche.com -x -a $SSH_PROXY_FLAGS %h`;
\ No newline at end of file