2017-09-17 23:54:23 +02:00
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2017-11-18 20:34:28 +01:00
|
|
|
"bytes"
|
2017-09-17 23:54:23 +02:00
|
|
|
"context"
|
2017-11-18 20:34:28 +01:00
|
|
|
"encoding/json"
|
2017-09-17 23:54:23 +02:00
|
|
|
"github.com/pkg/errors"
|
2018-08-25 21:30:25 +02:00
|
|
|
"github.com/zrepl/zrepl/logger"
|
2017-11-18 20:34:28 +01:00
|
|
|
"io"
|
2017-09-17 23:54:23 +02:00
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ControlJob struct {
|
|
|
|
Name string
|
|
|
|
sockaddr *net.UnixAddr
|
|
|
|
}
|
|
|
|
|
|
|
|
func NewControlJob(name, sockpath string) (j *ControlJob, err error) {
|
|
|
|
j = &ControlJob{Name: name}
|
|
|
|
|
|
|
|
j.sockaddr, err = net.ResolveUnixAddr("unix", sockpath)
|
|
|
|
if err != nil {
|
|
|
|
err = errors.Wrap(err, "cannot resolve unix address")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j *ControlJob) JobName() string {
|
|
|
|
return j.Name
|
|
|
|
}
|
|
|
|
|
2018-04-05 22:22:55 +02:00
|
|
|
func (j *ControlJob) JobType() JobType { return JobTypeControl }
|
|
|
|
|
2017-09-17 23:54:23 +02:00
|
|
|
const (
|
2018-02-17 16:20:10 +01:00
|
|
|
ControlJobEndpointPProf string = "/debug/pprof"
|
2017-11-18 20:34:28 +01:00
|
|
|
ControlJobEndpointVersion string = "/version"
|
2017-12-24 15:35:12 +01:00
|
|
|
ControlJobEndpointStatus string = "/status"
|
2017-09-17 23:54:23 +02:00
|
|
|
)
|
|
|
|
|
|
|
|
func (j *ControlJob) JobStart(ctx context.Context) {
|
|
|
|
|
2018-08-26 14:58:57 +02:00
|
|
|
log := getLogger(ctx)
|
2017-09-23 11:07:08 +02:00
|
|
|
defer log.Info("control job finished")
|
2017-09-17 23:54:23 +02:00
|
|
|
|
2017-12-24 15:35:12 +01:00
|
|
|
daemon := ctx.Value(contextKeyDaemon).(*Daemon)
|
|
|
|
|
2017-09-17 23:54:23 +02:00
|
|
|
l, err := ListenUnixPrivate(j.sockaddr)
|
|
|
|
if err != nil {
|
2017-09-23 11:07:08 +02:00
|
|
|
log.WithError(err).Error("error listening")
|
2017-09-17 23:54:23 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2018-02-17 16:20:10 +01:00
|
|
|
pprofServer := NewPProfServer(ctx)
|
|
|
|
|
2017-09-17 23:54:23 +02:00
|
|
|
mux := http.NewServeMux()
|
2018-02-17 16:20:10 +01:00
|
|
|
mux.Handle(ControlJobEndpointPProf, requestLogger{log: log, handlerFunc: func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
var msg PprofServerControlMsg
|
|
|
|
err := json.NewDecoder(r.Body).Decode(&msg)
|
|
|
|
if err != nil {
|
|
|
|
log.WithError(err).Error("bad pprof request from client")
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
}
|
|
|
|
pprofServer.Control(msg)
|
|
|
|
w.WriteHeader(200)
|
|
|
|
}})
|
2017-12-24 15:05:07 +01:00
|
|
|
mux.Handle(ControlJobEndpointVersion,
|
|
|
|
requestLogger{log: log, handler: jsonResponder{func() (interface{}, error) {
|
|
|
|
return NewZreplVersionInformation(), nil
|
|
|
|
}}})
|
2017-12-24 15:35:12 +01:00
|
|
|
mux.Handle(ControlJobEndpointStatus,
|
|
|
|
requestLogger{log: log, handler: jsonResponder{func() (interface{}, error) {
|
2018-08-26 16:49:40 +02:00
|
|
|
panic("FIXME") // FIXME
|
2017-12-24 15:35:12 +01:00
|
|
|
}}})
|
2018-08-16 01:26:09 +02:00
|
|
|
mux.Handle("/pulljobreport",
|
|
|
|
requestLogger{log: log, handler: jsonResponder{func() (interface{}, error) {
|
|
|
|
j := daemon.conf.Jobs["debian"]
|
|
|
|
return j.(*PullJob).Report(), nil
|
|
|
|
}}})
|
2017-09-17 23:54:23 +02:00
|
|
|
server := http.Server{Handler: mux}
|
|
|
|
|
|
|
|
outer:
|
|
|
|
for {
|
|
|
|
|
|
|
|
served := make(chan error)
|
|
|
|
go func() {
|
|
|
|
served <- server.Serve(l)
|
|
|
|
close(served)
|
|
|
|
}()
|
|
|
|
|
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
2017-10-05 21:20:01 +02:00
|
|
|
log.WithError(ctx.Err()).Info("context done")
|
2017-09-17 23:54:23 +02:00
|
|
|
server.Shutdown(context.Background())
|
|
|
|
break outer
|
|
|
|
case err = <-served:
|
|
|
|
if err != nil {
|
2017-09-22 14:13:58 +02:00
|
|
|
log.WithError(err).Error("error serving")
|
2017-09-17 23:54:23 +02:00
|
|
|
break outer
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2017-12-24 15:05:07 +01:00
|
|
|
type jsonResponder struct {
|
|
|
|
producer func() (interface{}, error)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (j jsonResponder) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
res, err := j.producer()
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
io.WriteString(w, err.Error())
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var buf bytes.Buffer
|
|
|
|
err = json.NewEncoder(&buf).Encode(res)
|
|
|
|
if err != nil {
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
io.WriteString(w, err.Error())
|
|
|
|
} else {
|
|
|
|
io.Copy(w, &buf)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-17 23:54:23 +02:00
|
|
|
type requestLogger struct {
|
2018-08-10 17:06:00 +02:00
|
|
|
log logger.Logger
|
2017-12-24 15:05:07 +01:00
|
|
|
handler http.Handler
|
|
|
|
handlerFunc http.HandlerFunc
|
2017-09-17 23:54:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l requestLogger) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
2017-09-22 14:13:58 +02:00
|
|
|
log := l.log.WithField("method", r.Method).WithField("url", r.URL)
|
2017-09-23 11:07:08 +02:00
|
|
|
log.Info("start")
|
2017-12-24 15:05:07 +01:00
|
|
|
if l.handlerFunc != nil {
|
|
|
|
l.handlerFunc(w, r)
|
|
|
|
} else if l.handler != nil {
|
|
|
|
l.handler.ServeHTTP(w, r)
|
|
|
|
} else {
|
|
|
|
log.Error("no handler or handlerFunc configured")
|
|
|
|
}
|
2017-09-23 11:07:08 +02:00
|
|
|
log.Info("finish")
|
2017-09-17 23:54:23 +02:00
|
|
|
}
|