-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.go
109 lines (89 loc) · 2.06 KB
/
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
package main
import (
"./highbatch"
"bytes"
"fmt"
"github.com/BurntSushi/toml"
"github.com/kardianos/osext"
"io/ioutil"
"os"
"path/filepath"
"runtime"
)
func main() {
fullexecpath, err := osext.Executable()
if err != nil {
fmt.Println(err)
}
dir, _ := filepath.Split(fullexecpath)
if err := os.Chdir(dir); err != nil {
fmt.Println(err)
}
cleanTask()
if bootCheck() && cmdCheck(fullexecpath) {
highbatch.ServiceInit()
}
}
func cleanTask() {
tasks := []string{"tasks.zip", "public/static/file/tasks.zip"}
for _, x := range tasks {
if _, err := os.Stat(x); err == nil {
if err := os.Remove(x); err != nil {
fmt.Println(err)
}
}
}
}
func cmdCheck(path string) bool {
if runtime.GOOS == "windows" {
if _, err := os.Stat("command"); err != nil {
if err := os.Mkdir("command", 0600); err != nil {
fmt.Println(err)
return false
}
}
installCmdFile := "command/win_service_install.bat"
if _, err := os.Stat(installCmdFile); err != nil {
cmd := path + " install"
if err := ioutil.WriteFile(installCmdFile, []byte(cmd), 0644); err != nil {
fmt.Println(err)
return false
}
}
uninstallCmdFile := "command/win_service_uninstall.bat"
if _, err := os.Stat(uninstallCmdFile); err != nil {
cmd := path + " uninstall"
if err := ioutil.WriteFile(uninstallCmdFile, []byte(cmd), 0644); err != nil {
fmt.Println(err)
return false
}
}
}
return true
}
func bootCheck() bool {
configfile := "config.toml"
if _, err := os.Stat(configfile); err != nil {
host, err := os.Hostname()
if err != nil {
fmt.Println(err)
}
var config highbatch.ConfigClient
config.Master.Host = "highbatch"
config.Master.Port = "8081"
config.Worker.Host = host
config.Worker.Port = "8081"
var buffer bytes.Buffer
encoder := toml.NewEncoder(&buffer)
if err := encoder.Encode(config); err != nil {
fmt.Println(err)
}
if err := ioutil.WriteFile(configfile, []byte(buffer.String()), 0644); err != nil {
fmt.Println(err)
}
fmt.Println("create config file")
return false
} else {
return true
}
}