1 // Copyright (C) The Arvados Authors. All rights reserved.
3 // SPDX-License-Identifier: AGPL-3.0
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";
19 type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon' | 'chipsRoot';
21 const EXTRA_TOKEN = "exraToken";
23 const styles: StyleRulesCallback<CssRules> = (theme: ArvadosTheme) => ({
25 marginTop: theme.spacing.unit,
26 marginBottom: theme.spacing.unit
29 borderRadius: theme.spacing.unit * 0.5,
31 borderColor: theme.palette.grey["400"],
34 textDecoration: 'none',
35 color: theme.palette.primary.main,
37 color: theme.palette.primary.dark,
38 transition: 'all 0.5s ease'
42 textDecoration: 'none',
43 color: theme.palette.grey["500"],
46 color: theme.palette.common.black,
47 transition: 'all 0.5s ease'
53 cardWithoutMachines: {
58 marginTop: theme.spacing.unit
61 margin: `0px -${theme.spacing.unit / 2}px`,
65 const mapStateToProps = (state: RootState) => {
67 requestedDate: state.virtualMachines.date,
68 userUuid: state.auth.user!.uuid,
69 helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML,
70 hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "",
71 token: state.auth.extraApiToken || state.auth.apiToken || '',
72 tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''),
73 webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL,
74 idleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0,
75 ...state.virtualMachines
79 const mapDispatchToProps = (dispatch: Dispatch): Pick<VirtualMachinesPanelActionProps, 'loadVirtualMachinesData' | 'saveRequestedDate'> => ({
80 saveRequestedDate: () => dispatch<any>(saveRequestedDate()),
81 loadVirtualMachinesData: () => dispatch<any>(loadVirtualMachinesUserData()),
84 interface VirtualMachinesPanelDataProps {
85 requestedDate: string;
86 virtualMachines: ListResults<any>;
88 links: ListResults<any>;
92 tokenLocation: string;
97 interface VirtualMachinesPanelActionProps {
98 saveRequestedDate: () => void;
99 loadVirtualMachinesData: () => string;
102 type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles<CssRules>;
104 export const VirtualMachineUserPanel = compose(
106 connect(mapStateToProps, mapDispatchToProps))(
107 class extends React.Component<VirtualMachineProps> {
108 componentDidMount() {
109 this.props.loadVirtualMachinesData();
113 const { virtualMachines, links } = this.props;
115 <Grid container spacing={16}>
116 {virtualMachines.itemsAvailable === 0 && <CardContentWithoutVirtualMachines {...this.props} />}
117 {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && <CardContentWithVirtualMachines {...this.props} />}
118 {<CardSSHSection {...this.props} />}
125 const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) =>
128 <CardContent className={props.classes.cardWithoutMachines}>
130 <Typography variant='body1'>
131 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.
134 <Grid item xs={6} className={props.classes.rightAlign}>
135 {virtualMachineSendRequest(props)}
141 const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
146 <div className={props.classes.rightAlign}>
147 {virtualMachineSendRequest(props)}
149 <div className={props.classes.icon}>
150 <a href="https://doc.arvados.org/user/getting_started/vm-login-with-webshell.html" target="_blank" rel="noopener noreferrer" className={props.classes.linkIcon}>
151 <Tooltip title="Access VM using webshell">
156 {virtualMachinesTable(props)}
163 const virtualMachineSendRequest = (props: VirtualMachineProps) =>
165 <Button variant="contained" color="primary" className={props.classes.button} onClick={props.saveRequestedDate}>
166 SEND REQUEST FOR SHELL ACCESS
168 {props.requestedDate &&
170 A request for shell access was sent on {props.requestedDate}
174 const virtualMachinesTable = (props: VirtualMachineProps) =>
178 <TableCell>Host name</TableCell>
179 <TableCell>Login name</TableCell>
180 <TableCell>Groups</TableCell>
181 <TableCell>Command line</TableCell>
182 <TableCell>Web shell</TableCell>
186 {props.virtualMachines.items.map(it =>
187 props.links.items.map(lk => {
188 if (lk.tailUuid === props.userUuid && lk.headUuid === it.uuid) {
189 const username = lk.properties.username;
190 const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`;
192 if (props.tokenLocation === SESSION_STORAGE || props.tokenLocation === EXTRA_TOKEN) {
193 tokenParam = `&token=${encodeURIComponent(props.token)}`;
195 return <TableRow key={lk.uuid}>
196 <TableCell>{it.hostname}</TableCell>
197 <TableCell>{username}</TableCell>
199 <Grid container spacing={8} className={props.classes.chipsRoot}>
201 (lk.properties.groups || []).map((group, i) => (
203 <Chip label={group} />
213 <a href={`/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&timeout=${props.idleTimeout}&login=${encodeURIComponent(username)}${tokenParam}`} target="_blank" rel="noopener noreferrer" className={props.classes.link}>
225 const CardSSHSection = (props: VirtualMachineProps) =>
230 <div dangerouslySetInnerHTML={{ __html: props.helpText }} style={{ margin: "1em" }} />