2017-03-30 19:35:50 +00:00
|
|
|
package consul
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2017-08-23 14:52:48 +00:00
|
|
|
"github.com/hashicorp/consul/acl"
|
2017-12-12 00:38:52 +00:00
|
|
|
"github.com/hashicorp/consul/agent/consul/autopilot"
|
2017-07-06 10:34:00 +00:00
|
|
|
"github.com/hashicorp/consul/agent/structs"
|
2017-03-30 19:35:50 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// AutopilotGetConfiguration is used to retrieve the current Autopilot configuration.
|
2017-12-12 00:38:52 +00:00
|
|
|
func (op *Operator) AutopilotGetConfiguration(args *structs.DCSpecificRequest, reply *autopilot.Config) error {
|
2017-03-30 19:35:50 +00:00
|
|
|
if done, err := op.srv.forward("Operator.AutopilotGetConfiguration", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator read access.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := op.srv.ResolveToken(args.Token)
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-23 14:52:48 +00:00
|
|
|
if rule != nil && !rule.OperatorRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
state := op.srv.fsm.State()
|
|
|
|
_, config, err := state.AutopilotConfig()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-04-13 17:43:07 +00:00
|
|
|
if config == nil {
|
|
|
|
return fmt.Errorf("autopilot config not initialized yet")
|
|
|
|
}
|
2017-03-30 19:35:50 +00:00
|
|
|
|
|
|
|
*reply = *config
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// AutopilotSetConfiguration is used to set the current Autopilot configuration.
|
|
|
|
func (op *Operator) AutopilotSetConfiguration(args *structs.AutopilotSetConfigRequest, reply *bool) error {
|
|
|
|
if done, err := op.srv.forward("Operator.AutopilotSetConfiguration", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator write access.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := op.srv.ResolveToken(args.Token)
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-23 14:52:48 +00:00
|
|
|
if rule != nil && !rule.OperatorWrite() {
|
|
|
|
return acl.ErrPermissionDenied
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Apply the update
|
|
|
|
resp, err := op.srv.raftApply(structs.AutopilotRequestType, args)
|
|
|
|
if err != nil {
|
|
|
|
op.srv.logger.Printf("[ERR] consul.operator: Apply failed: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if respErr, ok := resp.(error); ok {
|
|
|
|
return respErr
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if the return type is a bool.
|
|
|
|
if respBool, ok := resp.(bool); ok {
|
|
|
|
*reply = respBool
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ServerHealth is used to get the current health of the servers.
|
2017-12-12 00:38:52 +00:00
|
|
|
func (op *Operator) ServerHealth(args *structs.DCSpecificRequest, reply *autopilot.OperatorHealthReply) error {
|
2017-03-30 19:35:50 +00:00
|
|
|
// This must be sent to the leader, so we fix the args since we are
|
|
|
|
// re-using a structure where we don't support all the options.
|
|
|
|
args.RequireConsistent = true
|
|
|
|
args.AllowStale = false
|
|
|
|
if done, err := op.srv.forward("Operator.ServerHealth", args, args, reply); done {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// This action requires operator read access.
|
2018-10-19 16:04:07 +00:00
|
|
|
rule, err := op.srv.ResolveToken(args.Token)
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-08-23 14:52:48 +00:00
|
|
|
if rule != nil && !rule.OperatorRead() {
|
|
|
|
return acl.ErrPermissionDenied
|
2017-03-30 19:35:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Exit early if the min Raft version is too low
|
2017-12-12 00:38:52 +00:00
|
|
|
minRaftProtocol, err := op.srv.autopilot.MinRaftProtocol()
|
2017-03-30 19:35:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error getting server raft protocol versions: %s", err)
|
|
|
|
}
|
|
|
|
if minRaftProtocol < 3 {
|
|
|
|
return fmt.Errorf("all servers must have raft_protocol set to 3 or higher to use this endpoint")
|
|
|
|
}
|
|
|
|
|
2017-12-12 00:38:52 +00:00
|
|
|
*reply = op.srv.autopilot.GetClusterHealth()
|
2017-03-30 19:35:50 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|