The new Ripple frontend.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

startuato_linux.go 3.5KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // +build !windows
  2. package main
  3. import (
  4. "fmt"
  5. "io"
  6. "io/ioutil"
  7. "log"
  8. "net"
  9. "net/http"
  10. "os"
  11. "os/exec"
  12. "syscall"
  13. "github.com/gin-gonic/gin"
  14. "github.com/rcrowley/goagain"
  15. "zxq.co/ripple/schiavolib"
  16. )
  17. var l net.Listener
  18. func startuato(engine *gin.Engine) bool {
  19. engine.GET("/51/update", updateFromRemote)
  20. returnCh := make(chan bool)
  21. // whether it was from this very thing or not
  22. var iZingri bool
  23. hs := func(l net.Listener, h http.Handler) {
  24. err := http.Serve(l, h)
  25. if f, ok := err.(*net.OpError); ok && f.Err.Error() == "use of closed network connection" && !iZingri {
  26. returnCh <- true
  27. }
  28. }
  29. var err error
  30. // Inherit a net.Listener from our parent process or listen anew.
  31. l, err = goagain.Listener()
  32. if err != nil {
  33. // Listen on a TCP or a UNIX domain socket (TCP here).
  34. if config.Unix {
  35. l, err = net.Listen("unix", config.ListenTo)
  36. } else {
  37. l, err = net.Listen("tcp", config.ListenTo)
  38. }
  39. if err != nil {
  40. schiavo.Bunker.Send(err.Error())
  41. log.Fatalln(err)
  42. }
  43. schiavo.Bunker.Send(fmt.Sprint("LISTENINGU STARTUATO ON ", l.Addr()))
  44. // Accept connections in a new goroutine.
  45. go hs(l, engine)
  46. } else {
  47. // Resume accepting connections in a new goroutine.
  48. schiavo.Bunker.Send(fmt.Sprint("LISTENINGU RESUMINGU ON ", l.Addr()))
  49. go hs(l, engine)
  50. // Kill the parent, now that the child has started successfully.
  51. if err := goagain.Kill(); err != nil {
  52. schiavo.Bunker.Send(err.Error())
  53. log.Fatalln(err)
  54. }
  55. }
  56. go func() {
  57. // Block the main goroutine awaiting signals.
  58. if _, err := goagain.Wait(l); err != nil {
  59. schiavo.Bunker.Send(err.Error())
  60. log.Fatalln(err)
  61. }
  62. // Do whatever's necessary to ensure a graceful exit like waiting for
  63. // goroutines to terminate or a channel to become closed.
  64. //
  65. // In this case, we'll simply stop listening and wait one second.
  66. iZingri = true
  67. if err := l.Close(); err != nil {
  68. schiavo.Bunker.Send(err.Error())
  69. log.Fatalln(err)
  70. }
  71. if err := db.Close(); err != nil {
  72. schiavo.Bunker.Send(err.Error())
  73. log.Fatalln(err)
  74. }
  75. returnCh <- false
  76. }()
  77. return <-returnCh
  78. }
  79. func updateFromRemote(c *gin.Context) {
  80. if c.Query("hanayokey") != config.APISecret {
  81. c.String(403, "nope")
  82. return
  83. }
  84. if f, err := os.Stat(".git"); err == os.ErrNotExist || !f.IsDir() {
  85. c.String(500, "not git ffs")
  86. return
  87. }
  88. br := c.Query("branch")
  89. if br == "" {
  90. br = "master"
  91. }
  92. c.String(200, "all right")
  93. go func() {
  94. if !execCommand("git", "fetch", "origin", br) {
  95. return
  96. }
  97. if !execCommand("git", "checkout", "origin/"+br) {
  98. return
  99. }
  100. if !execCommand("git", "submodule", "update") {
  101. return
  102. }
  103. // go get
  104. // -u: update all dependencies
  105. // -d: stop after downloading deps
  106. if !execCommand("go", "get", "-v", "-u", "-d") {
  107. return
  108. }
  109. if !execCommand("bash", "-c", "go build -v") {
  110. return
  111. }
  112. proc, err := os.FindProcess(syscall.Getpid())
  113. if err != nil {
  114. log.Println(err)
  115. return
  116. }
  117. proc.Signal(syscall.SIGUSR2)
  118. }()
  119. }
  120. func execCommand(command string, args ...string) bool {
  121. cmd := exec.Command(command, args...)
  122. cmd.Env = os.Environ()
  123. stdout, err := cmd.StdoutPipe()
  124. if err != nil {
  125. log.Println(err)
  126. return false
  127. }
  128. stderr, err := cmd.StderrPipe()
  129. if err != nil {
  130. log.Println(err)
  131. return false
  132. }
  133. if err := cmd.Start(); err != nil {
  134. log.Println(err)
  135. return false
  136. }
  137. data, err := ioutil.ReadAll(stderr)
  138. if err != nil {
  139. log.Println(err)
  140. return false
  141. }
  142. // Bob. We got a problem.
  143. if len(data) != 0 {
  144. log.Println(string(data))
  145. }
  146. io.Copy(os.Stdout, stdout)
  147. cmd.Wait()
  148. stdout.Close()
  149. return true
  150. }