-
Notifications
You must be signed in to change notification settings - Fork 116
/
Copy pathcontrol.go
129 lines (104 loc) · 3.04 KB
/
control.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// Package control implements the control sub-commands.
package control
import (
"context"
"os"
"github.com/spf13/cobra"
"google.golang.org/grpc"
"github.com/oasislabs/oasis-core/go/common/logging"
controlGrpc "github.com/oasislabs/oasis-core/go/grpc/control"
cmdCommon "github.com/oasislabs/oasis-core/go/oasis-node/cmd/common"
cmdGrpc "github.com/oasislabs/oasis-core/go/oasis-node/cmd/common/grpc"
)
var (
shutdownWait = false
controlCmd = &cobra.Command{
Use: "control",
Short: "node control interface utilities",
}
controlIsSyncedCmd = &cobra.Command{
Use: "is-synced",
Short: "exit with 0 if the node completed initial syncing, 1 if not",
Run: doIsSynced,
}
controlWaitSyncCmd = &cobra.Command{
Use: "wait-sync",
Short: "wait for the node to complete initial syncing",
Run: doWaitSync,
}
controlShutdownCmd = &cobra.Command{
Use: "shutdown",
Short: "request node shutdown on next epoch transition",
Run: doShutdown,
}
logger = logging.GetLogger("cmd/control")
)
// DoConnect connects to the runtime client grpc server.
func DoConnect(cmd *cobra.Command) (*grpc.ClientConn, controlGrpc.ControlClient) {
if err := cmdCommon.Init(); err != nil {
cmdCommon.EarlyLogAndExit(err)
}
conn, err := cmdGrpc.NewClient(cmd)
if err != nil {
logger.Error("failed to establish connection with node",
"err", err,
)
os.Exit(1)
}
client := controlGrpc.NewControlClient(conn)
return conn, client
}
func doIsSynced(cmd *cobra.Command, args []string) {
conn, client := DoConnect(cmd)
defer conn.Close()
logger.Debug("querying synced status")
// Use background context to block until the result comes in.
result, err := client.IsSynced(context.Background(), &controlGrpc.IsSyncedRequest{})
if err != nil {
logger.Error("failed to query synced status",
"err", err,
)
os.Exit(128)
}
if result.Synced {
os.Exit(0)
} else {
os.Exit(1)
}
}
func doWaitSync(cmd *cobra.Command, args []string) {
conn, client := DoConnect(cmd)
defer conn.Close()
logger.Debug("waiting for sync status")
// Use background context to block until the result comes in.
_, err := client.WaitSync(context.Background(), &controlGrpc.WaitSyncRequest{})
if err != nil {
logger.Error("failed to wait for sync status",
"err", err,
)
os.Exit(1)
}
}
func doShutdown(cmd *cobra.Command, args []string) {
conn, client := DoConnect(cmd)
defer conn.Close()
req := &controlGrpc.ShutdownRequest{
Wait: shutdownWait,
}
_, err := client.RequestShutdown(context.Background(), req)
if err != nil {
logger.Error("failed to send shutdown request",
"err", err,
)
os.Exit(1)
}
}
// Register registers the client sub-command and all of it's children.
func Register(parentCmd *cobra.Command) {
controlCmd.PersistentFlags().AddFlagSet(cmdGrpc.ClientFlags)
controlShutdownCmd.Flags().BoolVarP(&shutdownWait, "wait", "w", false, "wait for the node to finish shutdown")
controlCmd.AddCommand(controlIsSyncedCmd)
controlCmd.AddCommand(controlWaitSyncCmd)
controlCmd.AddCommand(controlShutdownCmd)
parentCmd.AddCommand(controlCmd)
}