add hook update support

master
codeskyblue 8 years ago
parent 4cf18bb3f1
commit fb35cdce5a

@ -17,7 +17,7 @@ Features
* [x] Memory and CPU monitor
* [x] HTTP Basic auth
* [ ] Github webhook
* [x] Github webhook
* [ ] Single log page, include search support
* [ ] 中文文档

@ -82,7 +82,7 @@
<span class="glyphicon glyphicon-text-size"></span> Tail
</button>
<a href="/settings/{{p.program.name}}" class="btn btn-default btn-xs">
<span class="glyphicon glyphicon-cog"></span> Setting
<span class="glyphicon glyphicon-cog"></span> More
</a>
</td>
</tr>

@ -4,6 +4,7 @@ import (
"encoding/json"
"errors"
"fmt"
"io"
"io/ioutil"
"net/http"
"os"
@ -17,6 +18,7 @@ import (
"time"
sigar "github.com/cloudfoundry/gosigar"
"github.com/codeskyblue/kexec"
"github.com/go-yaml/yaml"
"github.com/gorilla/mux"
"github.com/gorilla/websocket"
@ -398,6 +400,43 @@ func (s *Supervisor) hStopProgram(w http.ResponseWriter, r *http.Request) {
w.Write(data)
}
func (s *Supervisor) hWebhook(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
name, category := vars["name"], vars["category"]
proc, ok := s.procMap[name]
if !ok {
http.Error(w, fmt.Sprintf("proc %s not exist", strconv.Quote(name)), http.StatusForbidden)
return
}
hook := proc.Program.WebHook
if category == "github" {
gh := hook.Github
_ = gh.Secret
isRunning := proc.IsRunning()
s.stopAndWait(name)
go func() {
cmd := kexec.CommandString(hook.Command)
cmd.Dir = proc.Program.Dir
cmd.Stdout = proc.Output
cmd.Stderr = proc.Output
err := GoTimeout(cmd.Run, time.Duration(hook.Timeout)*time.Second)
if err == ErrGoTimeout {
cmd.Terminate(syscall.SIGTERM)
}
if err != nil {
log.Warnf("webhook command error: %v", err)
// Trigger pushover notification
}
if isRunning {
proc.Operate(StartEvent)
}
}()
io.WriteString(w, "success triggered")
} else {
log.Warnf("unknown webhook category: %v", category)
}
}
var upgrader = websocket.Upgrader{}
func (s *Supervisor) wsEvents(w http.ResponseWriter, r *http.Request) {
@ -620,5 +659,7 @@ func newSupervisorHandler() (suv *Supervisor, hdlr http.Handler, err error) {
r.HandleFunc("/ws/logs/{name}", suv.wsLog)
r.HandleFunc("/ws/perfs/{name}", suv.wsPerf)
r.HandleFunc("/webhooks/{name}/{category}", suv.hWebhook).Methods("POST")
return suv, r, nil
}

Loading…
Cancel
Save