/
quit.go
73 lines (64 loc) · 1.72 KB
/
quit.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
69
70
71
72
73
package cli
import (
"fmt"
"os"
"time"
"github.com/QMSTR/qmstr/lib/go-qmstr/docker"
"github.com/docker/docker/client"
"golang.org/x/net/context"
pb "github.com/QMSTR/qmstr/lib/go-qmstr/service"
"github.com/spf13/cobra"
)
var force, nocopy bool
// quitCmd represents the quit command
var quitCmd = &cobra.Command{
Use: "quit",
Short: "Quit QMSTR master",
Long: `Quit the running QMSTR master. This will copy the results produced by the master to the user's workspace.`,
Run: func(cmd *cobra.Command, args []string) {
if !nocopy {
if err := copyResults(); err != nil {
Log.Fatalf("copying results failed: %v", err)
}
}
setUpControlService()
quitServer()
tearDownServer()
stopMasterContainer()
},
}
func init() {
rootCmd.AddCommand(quitCmd)
quitCmd.Flags().BoolVarP(&force, "force", "f", false, "force quit")
quitCmd.Flags().BoolVar(&nocopy, "no-copy", false, "Do not copy results")
}
func quitServer() {
resp, err := controlServiceClient.Quit(context.Background(), &pb.QuitMessage{Kill: force})
if err != nil {
Log.Printf("Failed to communicate with qmstr-master server. %v\n", err)
os.Exit(ReturnCodeServerCommunicationError)
}
if !resp.Success {
Log.Println("Server responded unsuccessful")
os.Exit(ReturnCodeResponseFalseError)
}
}
func stopMasterContainer() {
ctx := context.Background()
cli, err := client.NewEnvClient()
if err != nil {
Log.Fatalf("Failed to create docker client %v", err)
}
mID, _, err := docker.GetMasterInfo(ctx, cli)
if err != nil {
Log.Fatal(err)
}
d := time.Duration(2) * time.Second
err = cli.ContainerStop(ctx, mID, &d)
if err != nil {
err1 := cli.ContainerKill(ctx, mID, "SIGKILL")
if err1 != nil {
Log.Fatal(fmt.Errorf("%v : %v", err1, err))
}
}
}