-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnode.go
169 lines (153 loc) · 3.47 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
//nolint
package e2e
import (
"context"
"net/url"
"path/filepath"
"strings"
"sync"
"time"
"github.com/criticalstack/e2d/pkg/client"
"github.com/criticalstack/e2d/pkg/log"
"github.com/criticalstack/e2d/pkg/manager"
snapshotutil "github.com/criticalstack/e2d/pkg/snapshot/util"
)
type Node struct {
*manager.Manager
c *TestCluster
removed bool
started bool
}
func (n *Node) Client() *manager.Client {
scheme := "https"
if n.Config().CAKey == "" {
scheme = "http"
}
clientURL := url.URL{Scheme: scheme, Host: n.Config().ClientAddr.String()}
cfg := &client.Config{
ClientURLs: []string{clientURL.String()},
Timeout: 5 * time.Second,
}
if n.Config().CACert != "" {
dir := filepath.Dir(n.Config().CACert)
cfg.SecurityConfig = client.SecurityConfig{
CertFile: filepath.Join(dir, "client.crt"),
KeyFile: filepath.Join(dir, "client.key"),
CertAuth: true,
TrustedCAFile: n.Config().CACert,
}
}
cc, err := client.New(cfg)
if err != nil {
panic(err)
}
return &manager.Client{
Client: cc,
Timeout: 5 * time.Second,
}
}
func (n *Node) Name() string {
return n.Etcd().Name
}
func (n *Node) Remove() *Node {
if n.started {
n.c.t.Fatalf("cannot remove node %q, must be stopped first", n.Name())
}
n.removed = true
return n
}
func (n *Node) Start() *Node {
if n.removed || n.started {
return nil
}
log.Infof("starting node: %q\n", n.Name())
n.started = true
go func() {
if err := n.Run(); err != nil {
n.c.t.Fatalf("cannot start node %q: %v", n.Name(), err)
}
}()
return n
}
func (n *Node) Stop() *Node {
if !n.started {
return n
}
log.Infof("stopping node: %q\n", n.Name())
n.HardStop()
n.started = false
return n
}
func (n *Node) Restart() *Node {
if !n.started {
return n
}
if err := n.Manager.Restart(); err != nil {
n.c.t.Fatalf("cannot restart node %q: %v", n.Name(), err)
}
return n
}
func (n *Node) Wait() *Node {
switch {
case n.removed:
nodes := n.c.getStarted()
log.Infof("waiting for the node %#v to be removed from the following nodes: %v\n", n.Name(), nodes)
waiting := nodes
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
defer cancel()
var wg sync.WaitGroup
for _, name := range nodes {
wg.Add(1)
go func(name string) {
defer wg.Done()
for {
select {
case removedNode := <-n.c.Node(name).RemoveCh():
if removedNode == n.Name() {
for i, name := range waiting {
if name == removedNode {
waiting = append(waiting[:i], waiting[i+1:]...)
}
}
return
}
case <-ctx.Done():
n.c.t.Fatalf("timed out waiting for nodes to be removed: %q", strings.Join(waiting, ","))
return
}
}
}(name)
}
wg.Wait()
case n.started:
log.Infof("waiting for the node %q to be started\n", n.Name())
for {
if n.Etcd().IsRunning() {
log.Infof("node %q started successfully!\n", n.Name())
return n
}
time.Sleep(100 * time.Millisecond)
}
}
return n
}
func (n *Node) SaveSnapshot() *Node {
data, size, _, err := n.Etcd().CreateSnapshot(0)
if err != nil {
n.c.t.Fatal(err)
}
if n.Config().SnapshotConfiguration.Encryption {
key, err := manager.ReadEncryptionKey(n.Config().CAKey)
if err != nil {
n.c.t.Fatal(err)
}
data = snapshotutil.NewEncrypterReadCloser(data, &key, size)
}
if n.Config().SnapshotConfiguration.Compression {
data = snapshotutil.NewGzipReadCloser(data)
}
if err := n.Snapshotter().Save(data); err != nil {
n.c.t.Fatal(err)
}
return n
}