2014-01-04 01:15:51 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
2014-01-30 23:35:38 +00:00
|
|
|
"fmt"
|
2014-01-30 23:18:05 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2015-07-30 19:02:37 +00:00
|
|
|
"github.com/hashicorp/serf/coordinate"
|
2014-05-27 22:09:28 +00:00
|
|
|
"github.com/hashicorp/serf/serf"
|
2014-01-04 01:15:51 +00:00
|
|
|
"net/http"
|
2015-01-21 17:53:31 +00:00
|
|
|
"strconv"
|
2014-01-04 01:15:51 +00:00
|
|
|
"strings"
|
|
|
|
)
|
|
|
|
|
2014-05-27 22:09:28 +00:00
|
|
|
type AgentSelf struct {
|
|
|
|
Config *Config
|
2015-07-30 19:02:37 +00:00
|
|
|
Coord *coordinate.Coordinate
|
2014-05-27 22:09:28 +00:00
|
|
|
Member serf.Member
|
|
|
|
}
|
|
|
|
|
2014-05-25 23:59:48 +00:00
|
|
|
func (s *HTTPServer) AgentSelf(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2015-10-16 02:28:31 +00:00
|
|
|
var c *coordinate.Coordinate
|
2015-07-30 19:02:37 +00:00
|
|
|
if !s.agent.config.DisableCoordinates {
|
|
|
|
var err error
|
2015-10-16 02:28:31 +00:00
|
|
|
if c, err = s.agent.GetCoordinate(); err != nil {
|
2015-07-30 19:02:37 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-05-27 22:09:28 +00:00
|
|
|
return AgentSelf{
|
|
|
|
Config: s.agent.config,
|
2015-10-16 02:28:31 +00:00
|
|
|
Coord: c,
|
2014-05-27 22:09:28 +00:00
|
|
|
Member: s.agent.LocalMember(),
|
|
|
|
}, nil
|
2014-05-25 23:59:48 +00:00
|
|
|
}
|
|
|
|
|
2014-01-04 01:15:51 +00:00
|
|
|
func (s *HTTPServer) AgentServices(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2014-01-21 19:52:25 +00:00
|
|
|
services := s.agent.state.Services()
|
2014-01-21 01:00:52 +00:00
|
|
|
return services, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentChecks(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2014-01-21 19:52:25 +00:00
|
|
|
checks := s.agent.state.Checks()
|
2014-01-21 01:00:52 +00:00
|
|
|
return checks, nil
|
2014-01-04 01:15:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentMembers(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
// Check if the WAN is being queried
|
|
|
|
wan := false
|
|
|
|
if other := req.URL.Query().Get("wan"); other != "" {
|
|
|
|
wan = true
|
|
|
|
}
|
|
|
|
if wan {
|
|
|
|
return s.agent.WANMembers(), nil
|
|
|
|
} else {
|
|
|
|
return s.agent.LANMembers(), nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentJoin(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
// Check if the WAN is being queried
|
|
|
|
wan := false
|
|
|
|
if other := req.URL.Query().Get("wan"); other != "" {
|
|
|
|
wan = true
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the address
|
|
|
|
addr := strings.TrimPrefix(req.URL.Path, "/v1/agent/join/")
|
|
|
|
if wan {
|
|
|
|
_, err := s.agent.JoinWAN([]string{addr})
|
2014-02-19 22:27:01 +00:00
|
|
|
return nil, err
|
2014-01-04 01:15:51 +00:00
|
|
|
} else {
|
|
|
|
_, err := s.agent.JoinLAN([]string{addr})
|
2014-02-19 22:27:01 +00:00
|
|
|
return nil, err
|
2014-01-04 01:15:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentForceLeave(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
addr := strings.TrimPrefix(req.URL.Path, "/v1/agent/force-leave/")
|
2014-02-19 22:27:01 +00:00
|
|
|
return nil, s.agent.ForceLeave(addr)
|
2014-01-04 01:15:51 +00:00
|
|
|
}
|
2014-01-30 22:58:36 +00:00
|
|
|
|
2016-01-27 16:20:19 +00:00
|
|
|
const invalidCheckMessage = "Must provide TTL or Script/DockerContainerID/HTTP/TCP and Interval"
|
|
|
|
|
2014-01-30 22:58:36 +00:00
|
|
|
func (s *HTTPServer) AgentRegisterCheck(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2014-01-30 23:35:38 +00:00
|
|
|
var args CheckDefinition
|
2014-04-21 22:02:36 +00:00
|
|
|
// Fixup the type decode of TTL or Interval
|
|
|
|
decodeCB := func(raw interface{}) error {
|
|
|
|
return FixupCheckType(raw)
|
|
|
|
}
|
|
|
|
if err := decodeBody(req, &args, decodeCB); err != nil {
|
2014-01-30 23:35:38 +00:00
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err)))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the check has a name
|
|
|
|
if args.Name == "" {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Missing check name"))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-04-12 00:53:48 +00:00
|
|
|
if args.Status != "" && !structs.ValidStatus(args.Status) {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Bad check status"))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-01-30 23:35:38 +00:00
|
|
|
// Construct the health check
|
2014-02-03 23:15:35 +00:00
|
|
|
health := args.HealthCheck(s.agent.config.NodeName)
|
2014-01-30 23:35:38 +00:00
|
|
|
|
|
|
|
// Verify the check type
|
|
|
|
chkType := &args.CheckType
|
|
|
|
if !chkType.Valid() {
|
|
|
|
resp.WriteHeader(400)
|
2016-01-27 16:20:19 +00:00
|
|
|
resp.Write([]byte(invalidCheckMessage))
|
2014-01-30 23:35:38 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-04-28 01:26:23 +00:00
|
|
|
// Get the provided token, if any
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2014-01-30 23:35:38 +00:00
|
|
|
// Add the check
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := s.agent.AddCheck(health, chkType, true, token); err != nil {
|
2015-02-20 23:45:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentDeregisterCheck(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2014-01-30 23:18:05 +00:00
|
|
|
checkID := strings.TrimPrefix(req.URL.Path, "/v1/agent/check/deregister/")
|
2015-02-20 23:45:06 +00:00
|
|
|
if err := s.agent.RemoveCheck(checkID, true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentCheckPass(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2014-01-30 23:18:05 +00:00
|
|
|
checkID := strings.TrimPrefix(req.URL.Path, "/v1/agent/check/pass/")
|
|
|
|
note := req.URL.Query().Get("note")
|
2015-02-20 23:45:06 +00:00
|
|
|
if err := s.agent.UpdateCheck(checkID, structs.HealthPassing, note); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentCheckWarn(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2014-01-30 23:18:05 +00:00
|
|
|
checkID := strings.TrimPrefix(req.URL.Path, "/v1/agent/check/warn/")
|
|
|
|
note := req.URL.Query().Get("note")
|
2015-02-20 23:45:06 +00:00
|
|
|
if err := s.agent.UpdateCheck(checkID, structs.HealthWarning, note); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentCheckFail(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2014-01-30 23:18:05 +00:00
|
|
|
checkID := strings.TrimPrefix(req.URL.Path, "/v1/agent/check/fail/")
|
|
|
|
note := req.URL.Query().Get("note")
|
2015-02-20 23:45:06 +00:00
|
|
|
if err := s.agent.UpdateCheck(checkID, structs.HealthCritical, note); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
2016-03-03 01:08:06 +00:00
|
|
|
// checkUpdate is the payload for a PUT to AgentCheckUpdate.
|
|
|
|
type checkUpdate struct {
|
|
|
|
// Status us one of the structs.Health* states, "passing", "warning", or
|
|
|
|
// "critical".
|
|
|
|
Status string
|
|
|
|
|
|
|
|
// Output is the information to post to the UI for operators as the
|
|
|
|
// output of the process that decided to hit the TTL check. This is
|
|
|
|
// different from the note field that's associated with the check
|
|
|
|
// itself.
|
|
|
|
Output string
|
|
|
|
}
|
|
|
|
|
|
|
|
// AgentCheckUpdate is a PUT-based alternative to the GET-based Pass/Warn/Fail
|
|
|
|
// APIs.
|
|
|
|
func (s *HTTPServer) AgentCheckUpdate(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
if req.Method != "PUT" {
|
|
|
|
resp.WriteHeader(405)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var update checkUpdate
|
|
|
|
if err := decodeBody(req, &update, nil); err != nil {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err)))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
switch update.Status {
|
|
|
|
case structs.HealthPassing:
|
|
|
|
case structs.HealthWarning:
|
|
|
|
case structs.HealthCritical:
|
|
|
|
default:
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte(fmt.Sprintf("Invalid check status: '%s'", update.Status)))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
total := len(update.Output)
|
|
|
|
if total > CheckBufSize {
|
2016-03-03 03:47:00 +00:00
|
|
|
update.Output = fmt.Sprintf("%s ... (captured %d of %d bytes)",
|
2016-03-03 01:08:06 +00:00
|
|
|
update.Output[:CheckBufSize], CheckBufSize, total)
|
|
|
|
}
|
|
|
|
|
|
|
|
checkID := strings.TrimPrefix(req.URL.Path, "/v1/agent/check/update/")
|
|
|
|
if err := s.agent.UpdateCheck(checkID, update.Status, update.Output); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-01-30 22:58:36 +00:00
|
|
|
func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2014-01-30 23:35:38 +00:00
|
|
|
var args ServiceDefinition
|
2014-04-21 22:02:36 +00:00
|
|
|
// Fixup the type decode of TTL or Interval if a check if provided
|
|
|
|
decodeCB := func(raw interface{}) error {
|
|
|
|
rawMap, ok := raw.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return nil
|
|
|
|
}
|
2014-04-25 02:44:27 +00:00
|
|
|
|
|
|
|
for k, v := range rawMap {
|
2015-01-14 03:08:30 +00:00
|
|
|
switch strings.ToLower(k) {
|
|
|
|
case "check":
|
|
|
|
if err := FixupCheckType(v); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
case "checks":
|
|
|
|
chkTypes, ok := v.([]interface{})
|
|
|
|
if !ok {
|
2015-01-24 02:50:51 +00:00
|
|
|
continue
|
2015-01-14 03:08:30 +00:00
|
|
|
}
|
|
|
|
for _, chkType := range chkTypes {
|
|
|
|
if err := FixupCheckType(chkType); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
2014-04-25 02:44:27 +00:00
|
|
|
}
|
|
|
|
}
|
2015-01-14 03:08:30 +00:00
|
|
|
return nil
|
2014-04-21 22:02:36 +00:00
|
|
|
}
|
|
|
|
if err := decodeBody(req, &args, decodeCB); err != nil {
|
2014-01-30 23:35:38 +00:00
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte(fmt.Sprintf("Request decode failed: %v", err)))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Verify the service has a name
|
|
|
|
if args.Name == "" {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Missing service name"))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2014-02-03 23:15:35 +00:00
|
|
|
// Get the node service
|
|
|
|
ns := args.NodeService()
|
2014-01-30 23:35:38 +00:00
|
|
|
|
|
|
|
// Verify the check type
|
2015-01-14 01:52:17 +00:00
|
|
|
chkTypes := args.CheckTypes()
|
|
|
|
for _, check := range chkTypes {
|
2015-04-12 00:53:48 +00:00
|
|
|
if check.Status != "" && !structs.ValidStatus(check.Status) {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Status for checks must 'passing', 'warning', 'critical', 'unknown'"))
|
|
|
|
return nil, nil
|
|
|
|
}
|
2015-01-14 01:52:17 +00:00
|
|
|
if !check.Valid() {
|
|
|
|
resp.WriteHeader(400)
|
2016-01-27 16:20:19 +00:00
|
|
|
resp.Write([]byte(invalidCheckMessage))
|
2015-01-14 01:52:17 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
2015-04-28 01:26:23 +00:00
|
|
|
// Get the provided token, if any
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2014-01-30 23:35:38 +00:00
|
|
|
// Add the check
|
2015-05-05 00:36:17 +00:00
|
|
|
if err := s.agent.AddService(ns, chkTypes, true, token); err != nil {
|
2015-02-20 23:45:06 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *HTTPServer) AgentDeregisterService(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2014-01-30 23:18:05 +00:00
|
|
|
serviceID := strings.TrimPrefix(req.URL.Path, "/v1/agent/service/deregister/")
|
2015-02-20 23:45:06 +00:00
|
|
|
if err := s.agent.RemoveService(serviceID, true); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
s.syncChanges()
|
|
|
|
return nil, nil
|
2014-01-30 22:58:36 +00:00
|
|
|
}
|
2015-01-15 08:16:34 +00:00
|
|
|
|
|
|
|
func (s *HTTPServer) AgentServiceMaintenance(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
2015-01-15 09:17:35 +00:00
|
|
|
// Only PUT supported
|
|
|
|
if req.Method != "PUT" {
|
|
|
|
resp.WriteHeader(405)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-01-15 08:16:34 +00:00
|
|
|
// Ensure we have a service ID
|
|
|
|
serviceID := strings.TrimPrefix(req.URL.Path, "/v1/agent/service/maintenance/")
|
|
|
|
if serviceID == "" {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Missing service ID"))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have some action
|
|
|
|
params := req.URL.Query()
|
|
|
|
if _, ok := params["enable"]; !ok {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Missing value for enable"))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := params.Get("enable")
|
2015-01-21 17:53:31 +00:00
|
|
|
enable, err := strconv.ParseBool(raw)
|
|
|
|
if err != nil {
|
2015-01-15 08:16:34 +00:00
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte(fmt.Sprintf("Invalid value for enable: %q", raw)))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:43:59 +00:00
|
|
|
// Get the provided token, if any
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2015-01-15 08:16:34 +00:00
|
|
|
if enable {
|
2015-01-21 20:21:57 +00:00
|
|
|
reason := params.Get("reason")
|
2015-09-10 18:43:59 +00:00
|
|
|
if err = s.agent.EnableServiceMaintenance(serviceID, reason, token); err != nil {
|
2015-01-15 18:51:00 +00:00
|
|
|
resp.WriteHeader(404)
|
2015-01-15 09:17:35 +00:00
|
|
|
resp.Write([]byte(err.Error()))
|
2015-01-21 21:28:26 +00:00
|
|
|
return nil, nil
|
2015-01-15 09:17:35 +00:00
|
|
|
}
|
2015-01-15 08:16:34 +00:00
|
|
|
} else {
|
2015-01-15 09:17:35 +00:00
|
|
|
if err = s.agent.DisableServiceMaintenance(serviceID); err != nil {
|
2015-01-15 18:51:00 +00:00
|
|
|
resp.WriteHeader(404)
|
2015-01-15 09:17:35 +00:00
|
|
|
resp.Write([]byte(err.Error()))
|
2015-01-21 21:28:26 +00:00
|
|
|
return nil, nil
|
2015-01-15 09:17:35 +00:00
|
|
|
}
|
2015-01-15 08:16:34 +00:00
|
|
|
}
|
2015-02-20 23:45:06 +00:00
|
|
|
s.syncChanges()
|
2015-01-21 21:28:26 +00:00
|
|
|
return nil, nil
|
2015-01-15 08:16:34 +00:00
|
|
|
}
|
2015-01-15 19:20:22 +00:00
|
|
|
|
|
|
|
func (s *HTTPServer) AgentNodeMaintenance(resp http.ResponseWriter, req *http.Request) (interface{}, error) {
|
|
|
|
// Only PUT supported
|
|
|
|
if req.Method != "PUT" {
|
|
|
|
resp.WriteHeader(405)
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ensure we have some action
|
|
|
|
params := req.URL.Query()
|
|
|
|
if _, ok := params["enable"]; !ok {
|
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte("Missing value for enable"))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
raw := params.Get("enable")
|
2015-01-21 17:53:31 +00:00
|
|
|
enable, err := strconv.ParseBool(raw)
|
|
|
|
if err != nil {
|
2015-01-15 19:20:22 +00:00
|
|
|
resp.WriteHeader(400)
|
|
|
|
resp.Write([]byte(fmt.Sprintf("Invalid value for enable: %q", raw)))
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2015-09-10 18:43:59 +00:00
|
|
|
// Get the provided token, if any
|
|
|
|
var token string
|
|
|
|
s.parseToken(req, &token)
|
|
|
|
|
2015-01-15 19:20:22 +00:00
|
|
|
if enable {
|
2015-09-10 18:43:59 +00:00
|
|
|
s.agent.EnableNodeMaintenance(params.Get("reason"), token)
|
2015-01-15 19:20:22 +00:00
|
|
|
} else {
|
|
|
|
s.agent.DisableNodeMaintenance()
|
|
|
|
}
|
2015-02-20 23:45:06 +00:00
|
|
|
s.syncChanges()
|
2015-01-15 19:20:22 +00:00
|
|
|
return nil, nil
|
|
|
|
}
|
2015-02-20 23:45:06 +00:00
|
|
|
|
|
|
|
// syncChanges is a helper function which wraps a blocking call to sync
|
|
|
|
// services and checks to the server. If the operation fails, we only
|
|
|
|
// only warn because the write did succeed and anti-entropy will sync later.
|
|
|
|
func (s *HTTPServer) syncChanges() {
|
|
|
|
if err := s.agent.state.syncChanges(); err != nil {
|
|
|
|
s.logger.Printf("[ERR] agent: failed to sync changes: %v", err)
|
|
|
|
}
|
|
|
|
}
|