-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlisten-main.go
128 lines (105 loc) · 2.57 KB
/
listen-main.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
package main
import (
"encoding/json"
"flag"
"fmt"
"github.com/sevlyar/go-daemon"
"log"
"net/http"
"os"
)
var addr = flag.String("addr", websocketServer, "http service address")
var mode = flag.String("mode", "", "update or not")
func main() {
var args []string
flag.Parse()
args = append(args, "[k8sMLer daemon]")
if *mode != "" {
args = append(args, *mode)
}
cntxt := &daemon.Context{
PidFileName: "k8sMLer.pid",
PidFilePerm: 0644,
LogFileName: "k8sMLer.log",
LogFilePerm: 0640,
WorkDir: "./",
Env: nil,
Args: args,
Umask: 027,
}
d, err := cntxt.Reborn() // like fork
if err != nil {
log.Fatalln("Unable to run: ", err)
}
if d != nil {
return // child is ready, return parent
}
defer cntxt.Release()
var mod string
if len(os.Args) > 1 {
// update
mod = os.Args[1]
}
log.Print("- - - - - - - - - - - - - - -")
log.Print("K8sMLer daemon started")
listen_main(mod)
}
func listen_main(mod string) {
QUEUELIST = make([]*headNode, 0)
IP_POOL = make(map[string]bool)
if mod == MOD_UPDATE {
Trace.Println("update mode start up, recovery IP-POOL...")
tmpbyte := make([]byte, 4096)
file, error := os.OpenFile(".ippool", os.O_RDONLY, 0766)
if error != nil {
fmt.Println(error)
}
defer file.Close()
total, err := file.Read(tmpbyte)
if err != nil {
Error.Println(err)
}
err = json.Unmarshal(tmpbyte[:total], &IP_POOL) // tmpbyte[:total] for error invalid character '\x00' after top-level value
if err != nil {
Error.Println(err)
}
/*//validation
fmt.Println("'''''''''''''''''''''''''''''''''")
for k, v := range IP_POOL {
fmt.Println(k, v)
}
fmt.Println("'''''''''''''''''''''''''''''''''")*/
}
hub := newHub()
go hub.run()
UPDATEMAP = make(map[string][]string)
http.HandleFunc("/", func(writer http.ResponseWriter, request *http.Request) {
serveWs(hub, writer, request, mod)
})
err := http.ListenAndServe(*addr, nil)
if err != nil {
Error.Println("ListenAndServe: ", err)
}
}
func init() {
/*file, err := os.OpenFile("k8sMLer.err",
os.O_CREATE|os.O_WRONLY|os.O_APPEND, 0666)
if err != nil {
log.Fatalln("Failed to open error log file:", err)
}*/
Trace = log.New(os.Stdout,
"TRACE: ",
log.Ldate|log.Ltime|log.Lshortfile)
Info = log.New(os.Stdout,
"INFO: ",
log.Ldate|log.Ltime|log.Lshortfile)
Warning = log.New(os.Stdout,
"WARNING: ",
log.Ldate|log.Ltime|log.Lshortfile)
/*Error = log.New(io.MultiWriter(file, os.Stderr),
"ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile)*/
Error = log.New(os.Stdout,
"ERROR: ",
log.Ldate|log.Ltime|log.Lshortfile)
}