forked from mikewlange/furan
/
cancel.go
68 lines (57 loc) · 1.88 KB
/
cancel.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
package cmd
import (
"fmt"
"log"
"time"
"github.com/dollarshaveclub/furan/generated/lib"
"github.com/spf13/cobra"
"golang.org/x/net/context"
"google.golang.org/grpc"
)
// triggerCmd represents the trigger command
var cancelCmd = &cobra.Command{
Use: "cancel",
Short: "Cancel a running build",
Long: `Cancel a running build in a remote Furan cluster`,
Run: cancel,
}
var cancelReq = &lib.BuildCancelRequest{}
func init() {
cancelCmd.PersistentFlags().StringVar(&remoteFuranHost, "remote-host", "", "Remote Furan server with gRPC port (eg: furan.me.com:4001)")
cancelCmd.PersistentFlags().BoolVar(&discoverFuranHost, "consul-discovery", false, "Discover Furan hosts via Consul")
cancelCmd.PersistentFlags().StringVar(&consulFuranSvcName, "svc-name", "furan", "Consul service name for Furan hosts")
cancelCmd.PersistentFlags().StringVar(&cancelReq.BuildId, "build-id", "", "Build ID")
RootCmd.AddCommand(cancelCmd)
}
func cancel(cmd *cobra.Command, args []string) {
if remoteFuranHost == "" {
if !discoverFuranHost || consulFuranSvcName == "" {
clierr("remote host or consul discovery is required")
}
}
if cancelReq.BuildId == "" {
clierr("build ID is required")
}
var remoteHost string
if discoverFuranHost {
n, err := getFuranServerFromConsul(consulFuranSvcName)
if err != nil {
clierr("error discovering Furan hosts: %v", err)
}
remoteHost = fmt.Sprintf("%v:%v", n.addr, n.port)
} else {
remoteHost = remoteFuranHost
}
log.Printf("connecting to %v", remoteHost)
conn, err := grpc.Dial(remoteHost, grpc.WithInsecure(), grpc.WithBlock(), grpc.WithTimeout(connTimeoutSecs*time.Second))
if err != nil {
clierr("error connecting to remote host: %v", err)
}
defer conn.Close()
c := lib.NewFuranExecutorClient(conn)
resp, err := c.CancelBuild(context.Background(), cancelReq)
if err != nil {
rpcerr(err, "CancelBuild", nil)
}
log.Printf("%v\n", *resp)
}