// Copyright (C) The Arvados Authors. All rights reserved. // // SPDX-License-Identifier: AGPL-3.0 import React from 'react'; import { connect } from 'react-redux'; import { Grid, Typography, Button, Card, CardContent, TableBody, TableCell, TableHead, TableRow, Table, Tooltip, Chip } from '@material-ui/core'; import { StyleRulesCallback, WithStyles, withStyles } from '@material-ui/core/styles'; import { ArvadosTheme } from 'common/custom-theme'; 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 { SESSION_STORAGE } from "services/auth-service/auth-service"; // import * as CopyToClipboard from 'react-copy-to-clipboard'; import parse from "parse-duration"; import { CopyIcon } from 'components/icon/icon'; import CopyToClipboard from 'react-copy-to-clipboard'; import { snackbarActions, SnackbarKind } from 'store/snackbar/snackbar-actions'; type CssRules = 'button' | 'codeSnippet' | 'link' | 'linkIcon' | 'rightAlign' | 'cardWithoutMachines' | 'icon' | 'chipsRoot' | 'copyIcon' | 'webshellButton'; const EXTRA_TOKEN = "exraToken"; const styles: StyleRulesCallback = (theme: ArvadosTheme) => ({ button: { marginTop: theme.spacing.unit, marginBottom: theme.spacing.unit }, codeSnippet: { borderRadius: theme.spacing.unit * 0.5, border: '1px solid', borderColor: theme.palette.grey["400"], }, link: { textDecoration: 'none', color: theme.palette.primary.main, "&:hover": { color: theme.palette.primary.dark, transition: 'all 0.5s ease' } }, linkIcon: { textDecoration: 'none', color: theme.palette.grey["500"], textAlign: 'right', "&:hover": { color: theme.palette.common.black, transition: 'all 0.5s ease' } }, rightAlign: { textAlign: "right" }, cardWithoutMachines: { display: 'flex' }, icon: { textAlign: "right", marginTop: theme.spacing.unit }, chipsRoot: { margin: `0px -${theme.spacing.unit / 2}px`, }, copyIcon: { marginLeft: theme.spacing.unit, color: theme.palette.grey["500"], cursor: 'pointer', display: 'inline', '& svg': { fontSize: '1rem' } }, webshellButton: { textTransform: "initial", }, }); const mapStateToProps = (state: RootState) => { return { requestedDate: state.virtualMachines.date, userUuid: state.auth.user!.uuid, helpText: state.auth.config.clusterConfig.Workbench.SSHHelpPageHTML, hostSuffix: state.auth.config.clusterConfig.Workbench.SSHHelpHostSuffix || "", token: state.auth.extraApiToken || state.auth.apiToken || '', tokenLocation: state.auth.extraApiToken ? EXTRA_TOKEN : (state.auth.apiTokenLocation || ''), webshellUrl: state.auth.config.clusterConfig.Services.WebShell.ExternalURL, idleTimeout: parse(state.auth.config.clusterConfig.Workbench.IdleTimeout, 's') || 0, ...state.virtualMachines }; }; const mapDispatchToProps = (dispatch: Dispatch): Pick => ({ saveRequestedDate: () => dispatch(saveRequestedDate()), loadVirtualMachinesData: () => dispatch(loadVirtualMachinesUserData()), onCopy: (message: string) => { dispatch(snackbarActions.OPEN_SNACKBAR({ message, hideDuration: 2000, kind: SnackbarKind.SUCCESS })); }, }); interface VirtualMachinesPanelDataProps { requestedDate: string; virtualMachines: ListResults; userUuid: string; links: ListResults; helpText: string; hostSuffix: string; token: string; tokenLocation: string; webshellUrl: string; idleTimeout: number; } interface VirtualMachinesPanelActionProps { saveRequestedDate: () => void; loadVirtualMachinesData: () => string; onCopy: (message: string) => void; } type VirtualMachineProps = VirtualMachinesPanelActionProps & VirtualMachinesPanelDataProps & WithStyles; export const VirtualMachineUserPanel = compose( withStyles(styles), connect(mapStateToProps, mapDispatchToProps))( class extends React.Component { componentDidMount() { this.props.loadVirtualMachinesData(); } render() { const { virtualMachines, links } = this.props; return ( {virtualMachines.itemsAvailable === 0 && } {virtualMachines.itemsAvailable > 0 && links.itemsAvailable > 0 && } {} ); } } ); const CardContentWithoutVirtualMachines = (props: VirtualMachineProps) => 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. {virtualMachineSendRequest(props)} ; const CardContentWithVirtualMachines = (props: VirtualMachineProps) =>
{virtualMachineSendRequest(props)}
{virtualMachinesTable(props)}
; const virtualMachineSendRequest = (props: VirtualMachineProps) => {props.requestedDate && A request for shell access was sent on {props.requestedDate} } ; const virtualMachinesTable = (props: VirtualMachineProps) => Host name Login name Groups Command line Web shell {props.virtualMachines.items.map(it => props.links.items.map(lk => { if (lk.tailUuid === props.userUuid && lk.headUuid === it.uuid) { const username = lk.properties.username; const command = `ssh ${username}@${it.hostname}${props.hostSuffix}`; let tokenParam = ""; if (props.tokenLocation === SESSION_STORAGE || props.tokenLocation === EXTRA_TOKEN) { tokenParam = `&token=${encodeURIComponent(props.token)}`; } const loginHref = `/webshell/?host=${encodeURIComponent(props.webshellUrl + '/' + it.hostname)}&timeout=${props.idleTimeout}&login=${encodeURIComponent(username)}${tokenParam}`; return {it.hostname} {username} { (lk.properties.groups || []).map((group, i) => ( )) } {command} props.onCopy!("Copied")}> ; } return null; } ))}
; const CardSSHSection = (props: VirtualMachineProps) =>
;