-
Notifications
You must be signed in to change notification settings - Fork 0
/
client.go
317 lines (266 loc) · 7.91 KB
/
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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* lvcl is a simple program clustering libvirt servers
* Copyright (C) 2020 Adam Prycki (email: adam.prycki@gmail.com)
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
package main
//import "time"
import "fmt"
import "flag"
import "os"
import "runtime"
import "net"
import "math/rand"
import "time"
import "crypto/sha256"
import "io"
import "strconv"
func clogger(l int, s string){
if l==0 {
fmt.Println(s)
}else if l<=cliLogLevel {
fmt.Fprintln(os.Stderr, s)}}
func cerr(s string, e error){
pc := make([]uintptr, 10) // at least 1 entry needed
runtime.Callers(2, pc)
f := runtime.FuncForPC(pc[0])
file, line := f.FileLine(pc[0])
lines := fmt.Sprintf("%s:%d %s\n", file, line, f.Name())
fmt.Fprintln(os.Stderr, lines, s, e, "\n")}
var cliLogLevel int
var ec eclient
var confFileHash string
var confFileHashRaw []byte
var myHostname string
var clientID string
var clientIDSet bool = false
// var cincoming <-chan message = make(chan message)
// var coutgoing chan<- message = make(chan message)
var cincoming chan message
var coutgoing chan message
var exitAfterTextReply bool = false
func waitForClientID(){
for i:=0; i<100; i++ {
if ! clientIDSet {
time.Sleep(time.Duration(5) * time.Millisecond)
}else{
return}
}
clogger(1, "client didn't receive ID from cluster")
os.Exit(3)}
func clientMessageHandler(){
var m message
for {
m = <-cincoming
if m.msg_handle_cluste_assigned_id() {continue}
if m.msg_handle_client_print() {continue}
if m.msg_handle_clientPrintTextStatus() {continue}
if m.msg_handle_clientAskResStateChangeReply() {continue}
clogger(1, fmt.Sprintf("DEBUG client unhandled message %+v", m))}}
func formatMsg() *message {
return &message{
SrcHost: clientID,
SrcMod: msgModClient,
ConfHash: confFileHash,
Time: time.Now(),
Argv: make([]string,1),
}}
func clusterStatus(delay int){
var m *message
waitForClientID()
for{
m=formatMsg()
m.DestHost = "__any__"
m.DestMod = msgModBrain
m.RpcFunc = clientAskAboutStatus
coutgoing <- *m
clogger(3, "send message asking for cluster status")
if delay == -1 {
exitAfterTextReply = true
return
}else{
time.Sleep(time.Duration(delay) * time.Second)}}}
func attachToClusterLogger(verbosity int){
var m message
waitForClientID()
m = *formatMsg()
m.DestHost = "__any__"
m.RpcFunc = clientListenToClusterLogger
m.Argv[0] = strconv.Itoa(verbosity)
coutgoing <- m}
func (m *message)msg_handle_cluste_assigned_id() bool {
// TODO better validation
if m.RpcFunc == exchangeSendClientID {
clientID = m.Argv[0]
clogger(2, "Received client Id=" + clientID)
clientIDSet = true
return true}
return false}
func (m *message)msg_handle_client_print() bool {
if m.ConfHash == confFileHash &&
m.RpcFunc == clientPrintTextLogger &&
m.SrcMod == msgModLoggr &&
m.DestMod == msgModClient {
fmt.Println(m.SrcHost, m.Time, "\t==> ", m.Argv[0])
if exitAfterTextReply {
os.Exit(0)}
return true}
return false}
func (m *message)msg_handle_clientPrintTextStatus() bool {
if m.RpcFunc == clientPrintTextStatus &&
m.SrcMod == msgModBrain &&
m.DestMod == msgModClient &&
m.DestHost == clientID {
clogger(0, m.Argv[0])
return true}
return false}
var resValidStates []string = []string{
"on",
"off",
"pause",
"reboot",
"nuke",
}
func validateStateFlag(s *string) bool {
for _,v := range resValidStates{
if v == *s {
return true}}
return false}
func resourceMod(resName *string, resDesiredState *string) {
var callID string = strconv.Itoa(rand.Int())
var m message
clogger(3, "resourceMod runs")
if validateStateFlag(resDesiredState) == false {
clogger(0, "-state received wrong argument")
os.Exit(3)
}
waitForClientID()
m = *formatMsg()
//os.Exit(1)
m.DestHost = "__any__"
m.DestMod = msgModConfig
m.SrcMod = msgModClient
m.RpcFunc = clientAskResStateChange
m.Argv = []string{
*resName,
*resDesiredState,
callID,}
coutgoing <- m
clogger(3, "resourceMod message sent")
time.Sleep(time.Second)
os.Exit(0)
}
func (m *message)msg_handle_clientAskResStateChangeReply() bool {
if m.SrcMod == msgModConfig &&
m.DestMod == msgModClient &&
m.DestHost == clientID &&
m.RpcFunc == clientAskResStateChangeReply {
clogger(0, m.Argv[3])
os.Exit(int(m.Epoch))
return true}
return false}
func client(){
// cli arguments
var resName string
var resUUID string
var resDesiredState string
var clusterOp string
var socketPath string
var statusInterval int
var clusterLogLevel int
var statusFlag bool
var loggerFlag bool
var resModFlag bool
var unixConn net.Conn
var err error
cincoming = make(chan message)
coutgoing = make(chan message)
//var clientID uint = uint(rand.Uint32())
flag.StringVar(&resName, "res", "VM_ubuntu_20.04",
"name of resource you're trying to modify")
flag.StringVar(&resUUID, "UUID",
"8c56a50e-edf5-43f7-915e-5f34101bf4bf",
"UUID of resource you're trying to modify")
flag.StringVar(&resDesiredState, "state",
"null", "state of resource you want to set")
flag.StringVar(&clusterOp, "op", "maintenance",
"cluster command you want to execute")
flag.StringVar(&socketPath, "socket", "/run/lvcl.sock",
"path to lvcl socket")
flag.IntVar(&cliLogLevel, "v", 5,
"verbosity of the client logger ")
flag.IntVar(&clusterLogLevel, "cv", 1,
"verbosity of the cluster logger ")
flag.BoolVar(&statusFlag, "s", false,
"display cluster status, (use -st to control delay or display once")
flag.BoolVar(&loggerFlag, "w", false,
"display cluster log")
flag.BoolVar(&resModFlag, "r", false,
"modify resource state")
flag.IntVar(&statusInterval, "st", 1,
"sleep between printing status (-1 displays status once) ")
flag.Parse()
fmt.Println(resName, resUUID, resDesiredState,
clusterOp, socketPath, cliLogLevel)
clogger(1, "lvcl started in client mode")
clogger(2,fmt.Sprintln("resName", resName))
clogger(2,fmt.Sprintln("resUUID", resUUID))
clogger(2,fmt.Sprintln("resDesiredState", resDesiredState))
clogger(2,fmt.Sprintln("clusterOp", clusterOp))
clogger(2,fmt.Sprintln("socketPath", socketPath))
clogger(2,fmt.Sprintln("statusFlag", statusFlag))
clogger(2,fmt.Sprintln("resModFlag", resModFlag))
clogger(2,fmt.Sprintln("statusInterval", statusInterval))
myHostname,err=os.Hostname()
if(err != nil){
panic(err)}
file, err := os.Open("cluster.json")
if err != nil {
fmt.Println(err)
os.Exit(10)}
var h=sha256.New()
if _,err:=io.Copy(h,file);err!=nil {
fmt.Println("ERR hashing cluster.json")}
confFileHash = fmt.Sprintf("%x",h.Sum(nil))
confFileHashRaw = h.Sum(nil)
file.Close()
clogger(2,"config hash: " + confFileHash)
err=nil
unixConn,err = net.Dial("unix", socketPath)
if err!=nil {
cerr("failed to connect to socket",err)
os.Exit(2)}
ec=eclient{
hostname: myHostname,
originLocal: true,
outgoing: coutgoing,
incoming: cincoming,
conn: unixConn,
}
go ec.forward()
go ec.listen()
if statusFlag && ! loggerFlag && ! resModFlag {
go clusterStatus(statusInterval)
}else if loggerFlag && ! statusFlag && ! resModFlag {
go attachToClusterLogger(clusterLogLevel)
}else if resModFlag && ! statusFlag && ! loggerFlag {
go resourceMod(&resName, &resDesiredState)
}else {
os.Exit(1)
}
clientMessageHandler()
time.Sleep(time.Millisecond * time.Duration(1000))
}