-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgbat.go
More file actions
202 lines (173 loc) · 4.6 KB
/
gbat.go
File metadata and controls
202 lines (173 loc) · 4.6 KB
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
package main
import (
"github.com/wooddeep/gbat/worker/parser"
"github.com/wooddeep/gbat/worker/server"
"github.com/wooddeep/gbat/worker/client"
"github.com/wooddeep/gbat/worker/action"
"github.com/wooddeep/gbat/cluster"
"github.com/wooddeep/gbat/utils"
"github.com/robertkrimen/otto/repl"
"github.com/robertkrimen/otto"
"github.com/Jeffail/gabs"
"os/signal"
"syscall"
"strings"
"strconv"
"regexp"
"bytes"
"flag"
"fmt"
"net"
"os"
)
const (
STANDALONE_NODE = 0
CLUSTER_NODE_WITH_SHELL = 1
CLUSTER_NODE_WITHOUT_SHELL = 2
)
var workMode = STANDALONE_NODE // 0 ~ standalone, 1 ~ node with shell, 2 ~ node without shell
var autoPort = 0
var localIp = ""
/*
* gbat --etcd 127.0.0.1:2379
* gbat --etcd 127.0.0.1:2379 --commit f:/work/xx.exe
*/
func startRepl () {
vm := otto.New()
vm.Run(parser.Prompt)
vm.Set("exit", func(call otto.FunctionCall) otto.Value {
os.RemoveAll("./" + strconv.Itoa(autoPort))
os.Exit(0)
return otto.Value{}
})
vm.Set("lsnode", func(call otto.FunctionCall) otto.Value {
if workMode == STANDALONE_NODE {
return otto.Value{}
}
cluster.GetWokerList()
return otto.Value{}
})
vm.Set("Goprompt", func(call otto.FunctionCall) otto.Value {
path := call.Argument(0).String()
query := call.Argument(1).String()
_, err := os.Stat(path)
if err != nil {
fmt.Printf("# path <%s> not found!\n", path);
return otto.Value{}
}
if workMode == CLUSTER_NODE_WITH_SHELL {
workers := cluster.GetWokerList() // 查询集群节点 通知各个节点查询 //v: /heros/127.0.0.1:56126
collector := make(chan string, len(workers))
//fmt.Printf("## collector addr : %v\n", &collector)
colladdr := fmt.Sprintf("%v", &collector) // 获取collector的地址转化为字符串
for _, work := range workers {
re := regexp.MustCompile(`heros.([0-9:\.]+)`)
match := re.FindStringSubmatch(work)
if match != nil {
url := match[1]
body := gabs.New()
body.Set(path, "path")
body.Set(query, "query")
body.Set(colladdr, "colladdr") // 把collector的地址序列化, 传到每一个worker
body.Set(localIp + ":" + strconv.Itoa(autoPort), "url")
client.HttpPost("http://" + url + "/start", body.String())
}
}
buffer := bytes.Buffer{}
buffer.WriteString(`[`)
for i := 0; i < len(workers); i++ {
ret := <- collector
buffer.WriteString(ret)
buffer.WriteString(`,`)
}
buffer.WriteString(`{}]`)
out, _ := vm.ToValue(buffer.String())
//fmt.Println("## out \n", buffer.String())
return out
}
if workMode == STANDALONE_NODE {
buffer := bytes.Buffer{}
buffer.WriteString(`[`)
collector := make(chan string)
notifier := make(chan bool)
go action.FileParseMain(notifier, collector, path, query)
for {
exitFlag := false
select {
case str := <-collector:
fmt.Println(str)
buffer.WriteString(str)
buffer.WriteString(`,`)
case exitFlag = <-notifier:
fmt.Println(`exit`)
}
if exitFlag {
break
}
}
buffer.WriteString(`{}]`)
out, _ := vm.ToValue(buffer.String())
return out
}
return otto.Value{}
})
if err := repl.Run(vm); err != nil {
panic(err)
}
}
func cleanup() {
os.RemoveAll("./" + strconv.Itoa(autoPort))
os.Exit(0)
}
var etcdaddr string
func init() {
flag.StringVar(&etcdaddr, "etcdaddr", "127.0.0.1:2379", "help message for flagname")
//flag.Var(&shell, "shell", "xxxx")
}
func main() {
//flag.Parse()
argn := len(os.Args)
if argn == 1 { // standalone mode!
startRepl()
return
}
rip, rport, shell := "", 0, ""
args := strings.Join(os.Args, " ")
re := regexp.MustCompile(`\-\-etcd\s+([\d\.]+):(\d+)\s?(\-\-shell)?\s?`)
match := re.FindStringSubmatch(args)
if match != nil {
rip = match[1]
rport, _ = strconv.Atoi(match[2])
shell = match[3]
} else {
fmt.Println("sub parameter error, usage:")
fmt.Println("\tgbat --etcd 127.0.0.1:2379")
fmt.Println("\tgbat --etcd 127.0.0.1:2379 --shell")
return
}
workMode = CLUSTER_NODE_WITHOUT_SHELL
if strings.Count(shell, "") > 1 { // shell mode
workMode = CLUSTER_NODE_WITH_SHELL
go startRepl()
}
cluster.InitApi(rip, rport)
lip, err := utils.GetLocalIp(rip)
if err != nil {
lip = "127.0.0.1"
localIp = lip
}
lsn := server.StartHttpServ() // start the http server
port := lsn.Addr().(*net.TCPAddr).Port // get the dynamic generated http port
autoPort = port
cluster.Register(lip, rip, port, rport)
cluster.Keepalive("/heros/"+lip+":"+strconv.Itoa(port), "", 5)
sig := make(chan os.Signal, 2)
signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
go func() {
<-sig
cleanup()
os.Exit(1)
}()
c := make(chan int)
<-c // wait forerver
}