From c68202a5f1afc2db408a723e11186da26fff9846 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Magnus=20Furug=C3=A5rd?= Date: Thu, 5 Nov 2020 19:04:27 +0100 Subject: [PATCH] stop command --- cluster/new.go | 2 ++ cmd/stop.go | 2 +- cmd/stopCluster.go | 28 ++++++++++++++++++++++++---- cmd/stopJob.go | 29 +++++++++++++++++++++++++---- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/cluster/new.go b/cluster/new.go index 1ee4961..bcdd96e 100644 --- a/cluster/new.go +++ b/cluster/new.go @@ -11,6 +11,7 @@ import ( type Cluster struct { HostURL url.URL ConfigURL url.URL + ClusterURL url.URL DatasetsURL url.URL OverviewURL url.URL Jobs struct { @@ -47,6 +48,7 @@ func New(hostURL string) Cluster { cl.HostURL = h cl.ConfigURL = tools.UrlOrFail(h, "/config") + cl.ClusterURL = tools.UrlOrFail(h, "/cluster") cl.OverviewURL = tools.UrlOrFail(h, "/overview") cl.DatasetsURL = tools.UrlOrFail(h, "/datasets") diff --git a/cmd/stop.go b/cmd/stop.go index 7c85063..f8503be 100644 --- a/cmd/stop.go +++ b/cmd/stop.go @@ -22,7 +22,7 @@ import ( // stopCmd represents the stop command var stopCmd = &cobra.Command{ Use: "stop", - Short: "A brief description of your command", + Short: "Stop a running job, or the entire cluster.", } func init() { diff --git a/cmd/stopCluster.go b/cmd/stopCluster.go index d93205b..1803e7d 100644 --- a/cmd/stopCluster.go +++ b/cmd/stopCluster.go @@ -17,19 +17,39 @@ package cmd import ( "fmt" + "io/ioutil" + "github.com/parnurzeal/gorequest" "github.com/spf13/cobra" ) +var iKnowWhatImDoing bool + // stopClusterCmd represents the stopCluster command var stopClusterCmd = &cobra.Command{ - Use: "cluster", - Short: "A brief description of your command", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("stopCluster called") + Use: "cluster", + Short: "shut down the cluster", + PreRun: func(cmd *cobra.Command, args []string) { InitCluster() }, + RunE: func(cmd *cobra.Command, args []string) error { + if !iKnowWhatImDoing { + return fmt.Errorf("you don't know what you're doing") + } + resp, _, _ := gorequest. + New(). + Delete(cl.ClusterURL.String()). + End() + //TODO: Error management + body, err := ioutil.ReadAll(resp.Body) + if err != nil { + return err + } + msg := string(body) + Print(msg) + return nil }, } func init() { stopCmd.AddCommand(stopClusterCmd) + stopClusterCmd.Flags().BoolVar(&iKnowWhatImDoing, "i-know-what-im-doing", false, "you need to pass this flag for the call to work :)") } diff --git a/cmd/stopJob.go b/cmd/stopJob.go index 3a69c6b..424fadc 100644 --- a/cmd/stopJob.go +++ b/cmd/stopJob.go @@ -18,15 +18,36 @@ package cmd import ( "fmt" + "github.com/parnurzeal/gorequest" "github.com/spf13/cobra" ) // stopJobCmd represents the stopJob command var stopJobCmd = &cobra.Command{ - Use: "job", - Short: "A brief description of your command", - Run: func(cmd *cobra.Command, args []string) { - fmt.Println("stopJob called") + Use: "job ", + Short: "Stop a currently running job", + Args: cobra.ExactArgs(1), + PreRun: func(cmd *cobra.Command, args []string) { InitCluster() }, + RunE: func(cmd *cobra.Command, args []string) error { + jid := args[0] + if len(jid) != 32 { + return fmt.Errorf("`%v` is not a valid job id", jid) + } + + //TODO: Currently uses yarn-cancel as opposed to just /stop (which doesn't seem to work) + stopURL := fmt.Sprintf("%v/%v/yarn-cancel", cl.Jobs.URL.String(), jid) + resp, body, _ := gorequest. + New(). + Get(stopURL). + End() + + if resp.StatusCode == 202 { + fmt.Printf("Successfully cancelled job %v\n", jid) + } else { + fmt.Println("Failed to cancelled job: " + body) + } + + return nil }, }