From 67c61482395352d16aa00339bee8b1f13b45c455 Mon Sep 17 00:00:00 2001 From: Evan Parker Date: Tue, 10 Jul 2018 10:16:33 -0700 Subject: [PATCH] Add start button to restart stopped clusters. (#457) Add a button that will start any cluster whose current state is "Stopped". Rather than adding a dedicated start/stop button, the new button replaces the "Connect" button for clusters in the "Stopped" state. --- ui/src/components/ClusterCard.js | 36 ++++++++++++++++----- ui/src/components/StartButton.js | 54 ++++++++++++++++++++++++++++++++ 2 files changed, 82 insertions(+), 8 deletions(-) create mode 100644 ui/src/components/StartButton.js diff --git a/ui/src/components/ClusterCard.js b/ui/src/components/ClusterCard.js index e702ed29dec..894b806ae2b 100644 --- a/ui/src/components/ClusterCard.js +++ b/ui/src/components/ClusterCard.js @@ -10,6 +10,7 @@ import { withStyles } from '@material-ui/core/styles'; import ConnectButton from './ConnectButton' import DeleteDialog from './DeleteDialog' +import StartButton from './StartButton' import StatusIcon from './StatusIcon' @@ -28,6 +29,7 @@ const unknownStatus = "Unknown"; const runningStatus = "Running"; const deletingStatus = "Deleting"; const deletedStatus = "Deleted"; +const stoppedStatus = "Stopped"; const errorString404 = "404/Not Found"; @@ -79,7 +81,12 @@ class ClusterCard extends React.Component { setClusterStatusDeleting = () => { this.setState({ clusterStatus: "Deleting" }); - this.checkForUpdatedState() + setTimeout(this.checkForUpdatedState, fastStatusCheckInterval) + } + + setClusterStatusStarting = () => { + this.setState({ clusterStatus: "Starting" }); + setTimeout(this.checkForUpdatedState, fastStatusCheckInterval) } /** @@ -193,6 +200,25 @@ class ClusterCard extends React.Component { var classes = this.props.classes; var model = this.props.clusterModel; var machineCfg = model.machineConfig; + var startOrConnect = null; + if (this.state.clusterStatus === stoppedStatus) { + startOrConnect = ( + ); + } else { + startOrConnect = ( + ); + } return ( @@ -230,13 +256,7 @@ class ClusterCard extends React.Component { - + {startOrConnect} { + var model = this.props.clusterModel; + // Fetch auth info which includes the Google Client ID. + var notebooksUrl = createApiUrl( + model.googleProject, model.clusterName); + fetch( + notebooksUrl + "/start", + { + method: "POST", + headers: { + "Authorization": "Bearer " + this.props.googleAuthToken + }, + credentials: "include" + }) + .then((response) => { + if (response.status == 409) { + throw new Error("Cluster cannot be started.") + } + if (response.status == 404) { + throw new Error("Cluster not found.") + } + if (response.status >= 400) { + console.log("Unexpected response, logging response object.") + console.log(response) + throw new Error("Bad response from server. Cluster may not start."); + } + return response; + }) + .then((response) => this.props.updateStatusToStarting()) + .catch((error) => { + this.props.errorHandler(error.toString()); + }); + } + + render() { + return ; + } +} + +export default StartButton;