2013-12-23 19:38:51 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"encoding/json"
|
2014-02-05 22:36:13 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2013-12-23 19:38:51 +00:00
|
|
|
"io"
|
|
|
|
"log"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
2014-03-20 00:50:57 +00:00
|
|
|
"net/http/pprof"
|
2014-02-05 22:36:13 +00:00
|
|
|
"strconv"
|
2013-12-23 22:26:34 +00:00
|
|
|
"time"
|
2013-12-23 19:38:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// HTTPServer is used to wrap an Agent and expose various API's
|
|
|
|
// in a RESTful manner
|
|
|
|
type HTTPServer struct {
|
|
|
|
agent *Agent
|
|
|
|
mux *http.ServeMux
|
|
|
|
listener net.Listener
|
|
|
|
logger *log.Logger
|
|
|
|
}
|
|
|
|
|
2014-01-02 19:45:58 +00:00
|
|
|
// NewHTTPServer starts a new HTTP server to provide an interface to
|
2013-12-23 19:38:51 +00:00
|
|
|
// the agent.
|
2014-03-20 00:50:57 +00:00
|
|
|
func NewHTTPServer(agent *Agent, enableDebug bool, logOutput io.Writer, bind string) (*HTTPServer, error) {
|
2013-12-23 19:38:51 +00:00
|
|
|
// Create the mux
|
|
|
|
mux := http.NewServeMux()
|
|
|
|
|
|
|
|
// Create listener
|
|
|
|
list, err := net.Listen("tcp", bind)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create the server
|
|
|
|
srv := &HTTPServer{
|
|
|
|
agent: agent,
|
|
|
|
mux: mux,
|
|
|
|
listener: list,
|
|
|
|
logger: log.New(logOutput, "", log.LstdFlags),
|
|
|
|
}
|
2014-03-20 00:50:57 +00:00
|
|
|
srv.registerHandlers(enableDebug)
|
2013-12-23 19:38:51 +00:00
|
|
|
|
|
|
|
// Start the server
|
|
|
|
go http.Serve(list, mux)
|
|
|
|
return srv, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Shutdown is used to shutdown the HTTP server
|
|
|
|
func (s *HTTPServer) Shutdown() {
|
|
|
|
s.listener.Close()
|
|
|
|
}
|
|
|
|
|
|
|
|
// registerHandlers is used to attach our handlers to the mux
|
2014-03-20 00:50:57 +00:00
|
|
|
func (s *HTTPServer) registerHandlers(enableDebug bool) {
|
2013-12-25 01:09:51 +00:00
|
|
|
s.mux.HandleFunc("/", s.Index)
|
|
|
|
|
2013-12-23 19:38:51 +00:00
|
|
|
s.mux.HandleFunc("/v1/status/leader", s.wrap(s.StatusLeader))
|
|
|
|
s.mux.HandleFunc("/v1/status/peers", s.wrap(s.StatusPeers))
|
2013-12-23 22:26:34 +00:00
|
|
|
|
2013-12-24 19:55:14 +00:00
|
|
|
s.mux.HandleFunc("/v1/catalog/register", s.wrap(s.CatalogRegister))
|
|
|
|
s.mux.HandleFunc("/v1/catalog/deregister", s.wrap(s.CatalogDeregister))
|
2013-12-23 22:26:34 +00:00
|
|
|
s.mux.HandleFunc("/v1/catalog/datacenters", s.wrap(s.CatalogDatacenters))
|
2014-02-05 22:36:13 +00:00
|
|
|
s.mux.HandleFunc("/v1/catalog/nodes", s.wrapQuery(s.CatalogNodes))
|
|
|
|
s.mux.HandleFunc("/v1/catalog/services", s.wrapQuery(s.CatalogServices))
|
|
|
|
s.mux.HandleFunc("/v1/catalog/service/", s.wrapQuery(s.CatalogServiceNodes))
|
|
|
|
s.mux.HandleFunc("/v1/catalog/node/", s.wrapQuery(s.CatalogNodeServices))
|
2014-01-04 01:15:51 +00:00
|
|
|
|
2014-02-05 22:36:13 +00:00
|
|
|
s.mux.HandleFunc("/v1/health/node/", s.wrapQuery(s.HealthNodeChecks))
|
|
|
|
s.mux.HandleFunc("/v1/health/checks/", s.wrapQuery(s.HealthServiceChecks))
|
|
|
|
s.mux.HandleFunc("/v1/health/state/", s.wrapQuery(s.HealthChecksInState))
|
|
|
|
s.mux.HandleFunc("/v1/health/service/", s.wrapQuery(s.HealthServiceNodes))
|
2014-01-10 23:13:37 +00:00
|
|
|
|
2014-01-04 01:15:51 +00:00
|
|
|
s.mux.HandleFunc("/v1/agent/services", s.wrap(s.AgentServices))
|
2014-01-21 01:00:52 +00:00
|
|
|
s.mux.HandleFunc("/v1/agent/checks", s.wrap(s.AgentChecks))
|
2014-01-04 01:15:51 +00:00
|
|
|
s.mux.HandleFunc("/v1/agent/members", s.wrap(s.AgentMembers))
|
|
|
|
s.mux.HandleFunc("/v1/agent/join/", s.wrap(s.AgentJoin))
|
|
|
|
s.mux.HandleFunc("/v1/agent/force-leave/", s.wrap(s.AgentForceLeave))
|
2014-01-30 22:58:36 +00:00
|
|
|
|
|
|
|
s.mux.HandleFunc("/v1/agent/check/register", s.wrap(s.AgentRegisterCheck))
|
2014-04-18 17:56:00 +00:00
|
|
|
s.mux.HandleFunc("/v1/agent/check/deregister/", s.wrap(s.AgentDeregisterCheck))
|
2014-01-30 22:58:36 +00:00
|
|
|
s.mux.HandleFunc("/v1/agent/check/pass/", s.wrap(s.AgentCheckPass))
|
|
|
|
s.mux.HandleFunc("/v1/agent/check/warn/", s.wrap(s.AgentCheckWarn))
|
|
|
|
s.mux.HandleFunc("/v1/agent/check/fail/", s.wrap(s.AgentCheckFail))
|
|
|
|
|
|
|
|
s.mux.HandleFunc("/v1/agent/service/register", s.wrap(s.AgentRegisterService))
|
2014-04-18 17:54:18 +00:00
|
|
|
s.mux.HandleFunc("/v1/agent/service/deregister/", s.wrap(s.AgentDeregisterService))
|
2014-03-20 00:50:57 +00:00
|
|
|
|
2014-04-01 00:12:10 +00:00
|
|
|
s.mux.HandleFunc("/v1/kv/", s.wrap(s.KVSEndpoint))
|
|
|
|
|
2014-03-20 00:50:57 +00:00
|
|
|
if enableDebug {
|
|
|
|
s.mux.HandleFunc("/debug/pprof/", pprof.Index)
|
|
|
|
s.mux.HandleFunc("/debug/pprof/cmdline", pprof.Cmdline)
|
|
|
|
s.mux.HandleFunc("/debug/pprof/profile", pprof.Profile)
|
|
|
|
s.mux.HandleFunc("/debug/pprof/symbol", pprof.Symbol)
|
|
|
|
}
|
2013-12-23 19:38:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// wrap is used to wrap functions to make them more convenient
|
2013-12-24 00:20:51 +00:00
|
|
|
func (s *HTTPServer) wrap(handler func(resp http.ResponseWriter, req *http.Request) (interface{}, error)) func(resp http.ResponseWriter, req *http.Request) {
|
2013-12-23 19:38:51 +00:00
|
|
|
f := func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
// Invoke the handler
|
2013-12-23 22:26:34 +00:00
|
|
|
start := time.Now()
|
|
|
|
defer func() {
|
2014-01-03 01:58:58 +00:00
|
|
|
s.logger.Printf("[DEBUG] http: Request %v (%v)", req.URL, time.Now().Sub(start))
|
2013-12-23 22:26:34 +00:00
|
|
|
}()
|
2013-12-24 00:20:51 +00:00
|
|
|
obj, err := handler(resp, req)
|
2013-12-23 19:38:51 +00:00
|
|
|
|
|
|
|
// Check for an error
|
|
|
|
HAS_ERR:
|
|
|
|
if err != nil {
|
2014-01-03 01:58:58 +00:00
|
|
|
s.logger.Printf("[ERR] http: Request %v, error: %v", req.URL, err)
|
2013-12-23 19:38:51 +00:00
|
|
|
resp.WriteHeader(500)
|
|
|
|
resp.Write([]byte(err.Error()))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write out the JSON object
|
2013-12-24 00:20:51 +00:00
|
|
|
if obj != nil {
|
|
|
|
var buf bytes.Buffer
|
|
|
|
enc := json.NewEncoder(&buf)
|
|
|
|
if err = enc.Encode(obj); err != nil {
|
|
|
|
goto HAS_ERR
|
|
|
|
}
|
2014-04-17 18:38:14 +00:00
|
|
|
resp.Header().Set("Content-Type", "application/json")
|
2014-04-17 18:53:07 +00:00
|
|
|
resp.Write(buf.Bytes())
|
2013-12-23 19:38:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return f
|
|
|
|
}
|
2013-12-24 00:20:51 +00:00
|
|
|
|
2014-02-05 22:36:13 +00:00
|
|
|
// wrapQuery is used to wrap query functions to make them more convenient
|
|
|
|
func (s *HTTPServer) wrapQuery(handler func(resp http.ResponseWriter, req *http.Request) (uint64, interface{}, error)) func(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
f := func(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
idx, obj, err := handler(resp, req)
|
|
|
|
setIndex(resp, idx)
|
|
|
|
return obj, err
|
|
|
|
}
|
|
|
|
return s.wrap(f)
|
|
|
|
}
|
|
|
|
|
2013-12-25 01:09:51 +00:00
|
|
|
// Renders a simple index page
|
|
|
|
func (s *HTTPServer) Index(resp http.ResponseWriter, req *http.Request) {
|
|
|
|
if req.URL.Path == "/" {
|
|
|
|
resp.Write([]byte("Consul Agent"))
|
|
|
|
} else {
|
|
|
|
resp.WriteHeader(404)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-12-24 00:20:51 +00:00
|
|
|
// decodeBody is used to decode a JSON request body
|
|
|
|
func decodeBody(req *http.Request, out interface{}) error {
|
|
|
|
dec := json.NewDecoder(req.Body)
|
|
|
|
return dec.Decode(out)
|
|
|
|
}
|
2014-02-05 22:36:13 +00:00
|
|
|
|
|
|
|
// setIndex is used to set the index response header
|
|
|
|
func setIndex(resp http.ResponseWriter, index uint64) {
|
|
|
|
resp.Header().Add("X-Consul-Index", strconv.FormatUint(index, 10))
|
|
|
|
}
|
|
|
|
|
|
|
|
// parseWait is used to parse the ?wait and ?index query params
|
|
|
|
// Returns true on error
|
2014-04-21 19:26:12 +00:00
|
|
|
func parseWait(resp http.ResponseWriter, req *http.Request, b *structs.QueryOptions) bool {
|
2014-02-05 22:36:13 +00:00
|
|
|
query := req.URL.Query()
|
|
|
|
if wait := query.Get("wait"); wait != "" {
|
|
|
|
dur, err := time.ParseDuration(wait)
|
|
|
|
if err != nil {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Invalid wait time"))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
b.MaxQueryTime = dur
|
|
|
|
}
|
|
|
|
if idx := query.Get("index"); idx != "" {
|
|
|
|
index, err := strconv.ParseUint(idx, 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Invalid index"))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
b.MinQueryIndex = index
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-04-21 19:26:12 +00:00
|
|
|
// parseConsistency is used to parse the ?stale and ?consistent query params.
|
|
|
|
// Returns true on error
|
|
|
|
func parseConsistency(resp http.ResponseWriter, req *http.Request, b *structs.QueryOptions) bool {
|
|
|
|
query := req.URL.Query()
|
|
|
|
if _, ok := query["stale"]; ok {
|
|
|
|
b.AllowStale = true
|
|
|
|
}
|
|
|
|
if _, ok := query["consistent"]; ok {
|
|
|
|
b.RequireConsistent = true
|
|
|
|
}
|
|
|
|
if b.AllowStale && b.RequireConsistent {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Cannot specify ?stale with ?consistent, conflicting semantics."))
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2014-02-05 22:36:13 +00:00
|
|
|
// parseDC is used to parse the ?dc query param
|
|
|
|
func (s *HTTPServer) parseDC(req *http.Request, dc *string) {
|
|
|
|
if other := req.URL.Query().Get("dc"); other != "" {
|
|
|
|
*dc = other
|
|
|
|
} else if *dc == "" {
|
|
|
|
*dc = s.agent.config.Datacenter
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// parse is a convenience method for endpoints that need
|
|
|
|
// to use both parseWait and parseDC.
|
2014-04-21 19:26:12 +00:00
|
|
|
func (s *HTTPServer) parse(resp http.ResponseWriter, req *http.Request, dc *string, b *structs.QueryOptions) bool {
|
2014-02-05 22:36:13 +00:00
|
|
|
s.parseDC(req, dc)
|
2014-04-21 19:26:12 +00:00
|
|
|
if parseConsistency(resp, req, b) {
|
|
|
|
return true
|
|
|
|
}
|
2014-02-05 22:36:13 +00:00
|
|
|
return parseWait(resp, req, b)
|
|
|
|
}
|