This repository has been archived by the owner on Aug 7, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 18
/
node_web_remote_client.go
167 lines (141 loc) · 3.97 KB
/
node_web_remote_client.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
// Copyright 2016 CoreOS, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package proc
import (
"crypto/tls"
"fmt"
"net/url"
"sync"
"time"
"github.com/coreos/etcd/tools/functional-tester/etcd-agent/client"
)
type NodeWebRemoteClient struct {
mu sync.Mutex // guards the following
Flags *Flags
TLSCertPath string
TLSKeyPath string
TLSConfig *tls.Config
Agent client.Agent
active bool
limitInterval time.Duration
lastTerminated time.Time
lastRestarted time.Time
}
func (nd *NodeWebRemoteClient) Endpoint() string {
es := ""
for k := range nd.Flags.ListenClientURLs {
es = k
break
}
s, _ := url.Parse(es)
return s.Host
}
func (nd *NodeWebRemoteClient) StatusEndpoint() string {
es := ""
for k := range nd.Flags.ListenClientURLs {
es = k
break
}
return es
}
func (nd *NodeWebRemoteClient) IsActive() bool {
nd.mu.Lock()
defer nd.mu.Unlock()
return nd.active
}
func (nd *NodeWebRemoteClient) Start() error {
nd.mu.Lock()
defer nd.mu.Unlock()
if nd.active {
return fmt.Errorf("%s is already running or requested to restart", nd.Flags.Name)
}
flagSlice, err := nd.Flags.StringSlice()
if err != nil {
return err
}
if _, err := nd.Agent.Start(flagSlice...); err != nil {
return err
}
// if nd.TLSConfig == nil {
// tlsConfig, err := autoSelfCert(nd.TLSCertPath, nd.TLSKeyPath)
// if err != nil {
// return err
// }
// nd.TLSConfig = tlsConfig
// }
nd.active = true
return nil
}
func (nd *NodeWebRemoteClient) Restart() error {
nd.mu.Lock()
defer nd.mu.Unlock()
lastTerminated := nd.lastTerminated
lastRestarted := nd.lastRestarted
if nd.active {
return fmt.Errorf("%s is already running or requested to restart", nd.Flags.Name)
}
// TODO: better way to wait resource release?
// (we can scan the /proc to see if ports are still bind)
//
// restart, 2nd restart term should be more than limit interval
sub := time.Now().Sub(lastRestarted)
if sub < nd.limitInterval {
return fmt.Errorf("Somebody restarted the same node (only %v ago)! Retry in %v!", sub, nd.limitInterval)
}
// terminate, and immediate restart term should be more than limit interval
subt := time.Now().Sub(lastTerminated)
if subt < nd.limitInterval {
return fmt.Errorf("Somebody terminated the node (only %v ago)! Retry in %v!", subt, nd.limitInterval)
}
if _, err := nd.Agent.Restart(); err != nil {
return err
}
nd.lastRestarted = time.Now()
nd.active = true
return nil
}
func (nd *NodeWebRemoteClient) Terminate() error {
nd.mu.Lock()
defer nd.mu.Unlock()
lastTerminated := nd.lastTerminated
lastRestarted := nd.lastRestarted
if !nd.active {
return fmt.Errorf("%s is already terminated or requested to terminate", nd.Flags.Name)
}
// terminate, 2nd terminate term should be more than limit interval
sub := time.Now().Sub(lastTerminated)
if sub < nd.limitInterval {
return fmt.Errorf("Somebody terminated the same node (only %v ago)! Retry in %v!", sub, nd.limitInterval)
}
// restart, and immediate terminate term should be more than limit interval
subt := time.Now().Sub(lastRestarted)
if subt < nd.limitInterval {
return fmt.Errorf("Somebody restarted the node (only %v ago)! Retry in %v!", subt, nd.limitInterval)
}
if err := nd.Agent.Stop(); err != nil {
return err
}
nd.lastTerminated = time.Now()
nd.active = false
return nil
}
func (nd *NodeWebRemoteClient) Clean() error {
if err := nd.Agent.Cleanup(); err != nil {
return err
}
return nil
}
func (nd *NodeWebRemoteClient) TLS() *tls.Config {
return nd.TLSConfig
}