/
node.go
271 lines (254 loc) · 6.83 KB
/
node.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
package rpctest
import (
"fmt"
"io/ioutil"
"os"
"os/exec"
"path/filepath"
"runtime"
"time"
rpc "github.com/p9c/monorepo/pkg/rpcclient"
"github.com/p9c/monorepo/pkg/util"
)
// nodeConfig contains all the args and data required to launch a pod process and connect the rpc client to it.
type nodeConfig struct {
rpcUser string
rpcPass string
listen string
rpcListen string
rpcConnect string
dataDir string
logDir string
profile string
debugLevel string
extra []string
prefix string
exe string
endpoint string
certFile string
keyFile string
certificates []byte
}
// newConfig returns a newConfig with all default values.
func newConfig(prefix, certFile, keyFile string, extra []string) (*nodeConfig, error) {
podPath, e := podExecutablePath()
if e != nil {
podPath = "pod"
}
a := &nodeConfig{
listen: "127.0.0.1:41047",
rpcListen: "127.0.0.1:41048",
rpcUser: "user",
rpcPass: "pass",
extra: extra,
prefix: prefix,
exe: podPath,
endpoint: "ws",
certFile: certFile,
keyFile: keyFile,
}
if e := a.setDefaults(); E.Chk(e) {
return nil, e
}
return a, nil
}
// setDefaults sets the default values of the config. It also creates the temporary data, and log directories which must
// be cleaned up with a call to cleanup().
func (n *nodeConfig) setDefaults() (e error) {
datadir, e := ioutil.TempDir("", n.prefix+"-data")
if e != nil {
return e
}
n.dataDir = datadir
logdir, e := ioutil.TempDir("", n.prefix+"-logs")
if e != nil {
return e
}
n.logDir = logdir
cert, e := ioutil.ReadFile(n.certFile)
if e != nil {
return e
}
n.certificates = cert
return nil
}
// arguments returns an array of arguments that be used to launch the pod process.
func (n *nodeConfig) arguments() []string {
args := []string{}
if n.rpcUser != "" {
// --rpcuser
args = append(args, fmt.Sprintf("--rpcuser=%s", n.rpcUser))
}
if n.rpcPass != "" {
// --rpcpass
args = append(args, fmt.Sprintf("--rpcpass=%s", n.rpcPass))
}
if n.listen != "" {
// --listen
args = append(args, fmt.Sprintf("--listen=%s", n.listen))
}
if n.rpcListen != "" {
// --rpclisten
args = append(args, fmt.Sprintf("--rpclisten=%s", n.rpcListen))
}
if n.rpcConnect != "" {
// --rpcconnect
args = append(args, fmt.Sprintf("--rpcconnect=%s", n.rpcConnect))
}
// --rpccert
args = append(args, fmt.Sprintf("--rpccert=%s", n.certFile))
// --rpckey
args = append(args, fmt.Sprintf("--rpckey=%s", n.keyFile))
if n.dataDir != "" {
// --datadir
args = append(args, fmt.Sprintf("--datadir=%s", n.dataDir))
}
if n.logDir != "" {
// --logdir
args = append(args, fmt.Sprintf("--logdir=%s", n.logDir))
}
if n.profile != "" {
// --profile
args = append(args, fmt.Sprintf("--profile=%s", n.profile))
}
if n.debugLevel != "" {
// --debuglevel
args = append(args, fmt.Sprintf("--debuglevel=%s", n.debugLevel))
}
args = append(args, n.extra...)
return args
}
// command returns the exec.Cmd which will be used to start the pod process.
func (n *nodeConfig) command() *exec.Cmd {
return exec.Command(n.exe, n.arguments()...)
}
// rpcConnConfig returns the rpc connection config that can be used to connect to the pod process that is launched via
// Start().
func (n *nodeConfig) rpcConnConfig() rpc.ConnConfig {
return rpc.ConnConfig{
Host: n.rpcListen,
Endpoint: n.endpoint,
User: n.rpcUser,
Pass: n.rpcPass,
Certificates: n.certificates,
DisableAutoReconnect: true,
}
}
// String returns the string representation of this nodeConfig.
func (n *nodeConfig) String() string {
return n.prefix
}
// cleanup removes the tmp data and log directories.
func (n *nodeConfig) cleanup() (e error) {
dirs := []string{
n.logDir,
n.dataDir,
}
for _, dir := range dirs {
if e = os.RemoveAll(dir); E.Chk(e) {
E.F("Cannot remove dir %s: %v", dir, e)
}
}
return e
}
// node houses the necessary state required to configure, launch, and manage a pod process.
type node struct {
config *nodeConfig
cmd *exec.Cmd
pidFile string
dataDir string
}
// newNode creates a new node instance according to the passed config. dataDir will be used to hold a file recording the
// pid of the launched process, and as the base for the log and data directories for pod.
func newNode(config *nodeConfig, dataDir string) (*node, error) {
return &node{
config: config,
dataDir: dataDir,
cmd: config.command(),
}, nil
}
// start creates a new pod process and writes its pid in a file reserved for recording the pid of the launched process.
// This file can be used to terminate the process in case of a hang or panic. In the case of a failing test case, or
// panic, it is important that the process be stopped via stop( ) otherwise it will persist unless explicitly killed.
func (n *node) start() (e error) {
if e = n.cmd.Start(); E.Chk(e) {
return e
}
pid, e := os.Create(
filepath.Join(
n.dataDir,
fmt.Sprintf("%s.pid", n.config),
),
)
if e != nil {
return e
}
n.pidFile = pid.Name()
if _, e = fmt.Fprintf(pid, "%d\n", n.cmd.Process.Pid); E.Chk(e) {
return e
}
if e = pid.Close(); E.Chk(e) {
return e
}
return nil
}
// stop interrupts the running pod process process, and waits until it exits properly. On windows, interrupt is not
// supported so a kill signal is used instead
func (n *node) stop() (e error) {
if n.cmd == nil || n.cmd.Process == nil {
// return if not properly initialized or error starting the process
return nil
}
defer func() {
e := n.cmd.Wait()
if e != nil {
}
}()
if runtime.GOOS == "windows" {
return n.cmd.Process.Signal(os.Kill)
}
return n.cmd.Process.Signal(os.Interrupt)
}
// cleanup cleanups process and args files. The file housing the pid of the created process will be deleted as well as
// any directories created by the process.
func (n *node) cleanup() (e error) {
if n.pidFile != "" {
if e := os.Remove(n.pidFile); E.Chk(e) {
E.F("unable to remove file %s: %v", n.pidFile, e)
}
}
return n.config.cleanup()
}
// shutdown terminates the running pod process and cleans up all file/directories created by node.
func (n *node) shutdown() (e error) {
if e := n.stop(); E.Chk(e) {
return e
}
if e := n.cleanup(); E.Chk(e) {
return e
}
return nil
}
// genCertPair generates a key/cert pair to the paths provided.
func genCertPair(certFile, keyFile string) (e error) {
org := "rpctest autogenerated cert"
validUntil := time.Now().Add(10 * 365 * 24 * time.Hour)
var key []byte
var cert []byte
cert, key, e = util.NewTLSCertPair(org, validUntil, nil)
if e != nil {
return e
}
// Write cert and key files.
if e = ioutil.WriteFile(certFile, cert, 0666); E.Chk(e) {
return e
}
if e = ioutil.WriteFile(keyFile, key, 0600); E.Chk(e) {
defer func() {
if e = os.Remove(certFile); E.Chk(e) {
}
}()
return e
}
return nil
}