-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathmain.go
195 lines (182 loc) · 5.03 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
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
package main
import (
"context"
"encoding/json"
"flag"
"fmt"
"github.com/hashicorp/go-version"
"github.com/zhenorzz/goploy-agent/config"
"github.com/zhenorzz/goploy-agent/core"
"github.com/zhenorzz/goploy-agent/model"
"github.com/zhenorzz/goploy-agent/route"
"github.com/zhenorzz/goploy-agent/task"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"path"
"strconv"
"syscall"
"time"
)
var (
help bool
v bool
s string
)
const appVersion = "1.3.1"
func init() {
flag.StringVar(&core.AssetDir, "asset-dir", "", "default: ./")
flag.StringVar(&s, "s", "", "stop")
flag.BoolVar(&help, "help", false, "list available subcommands and some concept guides")
flag.BoolVar(&v, "version", false, "show goploy-agent version")
// 改变默认的 Usage
flag.Usage = usage
}
func usage() {
_, _ = fmt.Fprintf(os.Stderr, "Options:\n")
flag.PrintDefaults()
}
func main() {
flag.Parse()
if help {
flag.Usage()
return
}
if v {
println(appVersion)
return
}
handleClientSignal()
println(`
______ __
/ ____/___ ____ / /___ __ __
/ / __/ __ \/ __ \/ / __ \/ / / /
/ /_/ / /_/ / /_/ / / /_/ / /_/ /
\____/\____/ .___/_/\____/\__, /
/_/ /____/ ` + appVersion + "\n")
install()
config.Create(core.GetConfigFile())
pid := strconv.Itoa(os.Getpid())
_ = ioutil.WriteFile(path.Join(core.GetAssetDir(), "goploy-agent.pid"), []byte(pid), 0755)
println("Start at " + time.Now().String())
println("goploy-agent -h for more help")
println("Current pid: " + pid)
println("Config Loaded: " + core.GetConfigFile())
println("UID type: " + config.Toml.Goploy.UIDType)
println("UID: " + config.Toml.Goploy.UID)
println("Env: " + config.Toml.Env)
println("Report to: " + config.Toml.Goploy.ReportURL)
println("Log: " + config.Toml.Log.Path)
if config.Toml.Web.Port != "" {
println("Listen: " + config.Toml.Web.Port)
}
core.CreateValidator()
model.Init()
route.Init()
task.Init()
go checkUpdate()
// server
srv := http.Server{
Addr: ":" + config.Toml.Web.Port,
}
core.Gwg.Add(1)
go func() {
defer core.Gwg.Done()
c := make(chan os.Signal, 1)
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
println("Received the signal: " + (<-c).String())
println("Server is trying to shutdown, wait for a minute")
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
if err := srv.Shutdown(ctx); err != nil {
fmt.Printf("Task shutdown failed, err: %s\n", err.Error())
}
println("Server shutdown gracefully")
println("Task is trying to shutdown, wait for a minute")
if err := task.Shutdown(ctx); err != nil {
fmt.Printf("Task shutdown failed, err: %s\n", err.Error())
}
println("Task shutdown gracefully")
println("SQLite is trying to shutdown, wait for a minute")
if err := model.Shutdown(); err != nil {
fmt.Printf("SQLite shutdown failed, err: %s\n", err.Error())
}
println("SQLite shutdown gracefully")
}()
if config.Toml.Web.Port != "" {
if err := srv.ListenAndServe(); err != nil && err != http.ErrServerClosed {
log.Fatal("ListenAndServe: ", err.Error())
}
}
core.Gwg.Wait()
_ = os.Remove(path.Join(core.GetAssetDir(), "goploy-agent.pid"))
println("Goroutine shutdown gracefully")
println("Success")
return
}
func install() {
_, err := os.Stat(core.GetConfigFile())
if err == nil || os.IsExist(err) {
println("The configuration file already exists, no need to reinstall (if you need to reinstall, please delete the .env file, then restart.)")
return
} else {
println("The configuration file is not exists, please copy goploy-agent.example.toml to goploy-agent.toml")
os.Exit(1)
}
}
func handleClientSignal() {
switch s {
case "stop":
pidStr, err := ioutil.ReadFile(path.Join(core.GetAssetDir(), "goploy-agent.pid"))
if err != nil {
log.Fatal("handle stop, ", err.Error(), ", may be the server not start")
}
pid, _ := strconv.Atoi(string(pidStr))
process, err := os.FindProcess(pid)
if err != nil {
log.Fatal("handle stop, ", err.Error(), ", may be the server not start")
}
err = process.Signal(syscall.SIGTERM)
if err != nil {
log.Fatal("handle stop, ", err.Error())
}
os.Exit(1)
}
}
func checkUpdate() {
resp, err := http.Get("https://api.github.com/repos/zhenorzz/goploy-agent/releases/latest")
if err != nil {
println("Check failed")
println(err.Error())
return
}
defer resp.Body.Close()
body, err := ioutil.ReadAll(resp.Body)
if err != nil {
println("Check failed")
println(err.Error())
return
}
var result map[string]interface{}
if err := json.Unmarshal(body, &result); err != nil {
println("Check failed")
println(err.Error())
return
}
if _, ok := result["tag_name"]; ok {
tagName := result["tag_name"].(string)
tagVer, err := version.NewVersion(tagName)
if err != nil {
println("Check version error")
println(err.Error())
return
}
currentVer, _ := version.NewVersion(appVersion)
if tagVer.GreaterThan(currentVer) {
println("New release available")
println(result["html_url"].(string))
}
}
}