-
Notifications
You must be signed in to change notification settings - Fork 54
/
startuato_linux.go
170 lines (148 loc) · 3.48 KB
/
startuato_linux.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
// +build !windows
package main
import (
"fmt"
"io"
"io/ioutil"
"log"
"net"
"net/http"
"os"
"os/exec"
"syscall"
"github.com/gin-gonic/gin"
"github.com/rcrowley/goagain"
schiavo "zxq.co/ripple/schiavolib"
)
var l net.Listener
func startuato(engine *gin.Engine) bool {
engine.GET("/51/update", updateFromRemote)
returnCh := make(chan bool)
// whether it was from this very thing or not
var iZingri bool
hs := func(l net.Listener, h http.Handler) {
err := http.Serve(l, h)
if f, ok := err.(*net.OpError); ok && f.Err.Error() == "use of closed network connection" && !iZingri {
returnCh <- true
}
}
var err error
// Inherit a net.Listener from our parent process or listen anew.
l, err = goagain.Listener()
if err != nil {
// Listen on a TCP or a UNIX domain socket (TCP here).
if config.Unix {
l, err = net.Listen("unix", config.ListenTo)
} else {
l, err = net.Listen("tcp", config.ListenTo)
}
if err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}
schiavo.Bunker.Send(fmt.Sprint("LISTENINGU STARTUATO ON ", l.Addr()))
// Accept connections in a new goroutine.
go hs(l, engine)
} else {
// Resume accepting connections in a new goroutine.
schiavo.Bunker.Send(fmt.Sprint("LISTENINGU RESUMINGU ON ", l.Addr()))
go hs(l, engine)
// Kill the parent, now that the child has started successfully.
if err := goagain.Kill(); err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}
}
go func() {
// Block the main goroutine awaiting signals.
if _, err := goagain.Wait(l); err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}
// Do whatever's necessary to ensure a graceful exit like waiting for
// goroutines to terminate or a channel to become closed.
//
// In this case, we'll simply stop listening and wait one second.
iZingri = true
if err := l.Close(); err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}
if err := db.Close(); err != nil {
schiavo.Bunker.Send(err.Error())
log.Fatalln(err)
}
returnCh <- false
}()
return <-returnCh
}
func updateFromRemote(c *gin.Context) {
if c.Query("hanayokey") != config.APISecret {
c.String(403, "nope")
return
}
if f, err := os.Stat(".git"); err == os.ErrNotExist || !f.IsDir() {
c.String(500, "not git ffs")
return
}
br := c.Query("branch")
if br == "" {
br = "master"
}
c.String(200, "all right")
go func() {
if !execCommand("git", "fetch", "origin", br) {
return
}
if !execCommand("git", "checkout", "origin/"+br) {
return
}
if !execCommand("git", "submodule", "update") {
return
}
// will ensure dependencies are up-to-date
if !execCommand("go", "get", "-v", "-d") {
return
}
if !execCommand("bash", "-c", "go build -v") {
return
}
proc, err := os.FindProcess(syscall.Getpid())
if err != nil {
log.Println(err)
return
}
proc.Signal(syscall.SIGUSR2)
}()
}
func execCommand(command string, args ...string) bool {
cmd := exec.Command(command, args...)
cmd.Env = os.Environ()
stdout, err := cmd.StdoutPipe()
if err != nil {
log.Println(err)
return false
}
stderr, err := cmd.StderrPipe()
if err != nil {
log.Println(err)
return false
}
if err := cmd.Start(); err != nil {
log.Println(err)
return false
}
data, err := ioutil.ReadAll(stderr)
if err != nil {
log.Println(err)
return false
}
// Bob. We got a problem.
if len(data) != 0 {
log.Println(string(data))
}
io.Copy(os.Stdout, stdout)
cmd.Wait()
stdout.Close()
return true
}