2014-01-30 23:35:38 +00:00
|
|
|
package agent
|
|
|
|
|
2014-02-03 23:15:35 +00:00
|
|
|
import (
|
2017-05-15 19:49:13 +00:00
|
|
|
"time"
|
|
|
|
|
2017-04-19 23:00:11 +00:00
|
|
|
"github.com/hashicorp/consul/api"
|
2014-02-03 23:15:35 +00:00
|
|
|
"github.com/hashicorp/consul/consul/structs"
|
2016-06-06 20:19:31 +00:00
|
|
|
"github.com/hashicorp/consul/types"
|
2014-02-03 23:15:35 +00:00
|
|
|
)
|
|
|
|
|
2014-01-30 23:35:38 +00:00
|
|
|
// ServiceDefinition is used to JSON decode the Service definitions
|
|
|
|
type ServiceDefinition struct {
|
2015-09-11 15:35:29 +00:00
|
|
|
ID string
|
|
|
|
Name string
|
|
|
|
Tags []string
|
|
|
|
Address string
|
|
|
|
Port int
|
|
|
|
Check CheckType
|
|
|
|
Checks CheckTypes
|
|
|
|
Token string
|
|
|
|
EnableTagOverride bool
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *ServiceDefinition) NodeService() *structs.NodeService {
|
|
|
|
ns := &structs.NodeService{
|
2015-09-11 15:35:29 +00:00
|
|
|
ID: s.ID,
|
|
|
|
Service: s.Name,
|
|
|
|
Tags: s.Tags,
|
|
|
|
Address: s.Address,
|
|
|
|
Port: s.Port,
|
|
|
|
EnableTagOverride: s.EnableTagOverride,
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
|
|
|
if ns.ID == "" && ns.Service != "" {
|
|
|
|
ns.ID = ns.Service
|
|
|
|
}
|
|
|
|
return ns
|
|
|
|
}
|
|
|
|
|
2015-01-14 01:52:17 +00:00
|
|
|
func (s *ServiceDefinition) CheckTypes() (checks CheckTypes) {
|
|
|
|
s.Checks = append(s.Checks, &s.Check)
|
|
|
|
for _, check := range s.Checks {
|
2015-01-29 20:11:42 +00:00
|
|
|
if check.Valid() {
|
2015-01-14 01:52:17 +00:00
|
|
|
checks = append(checks, check)
|
|
|
|
}
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
2015-01-14 01:52:17 +00:00
|
|
|
return
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
2016-06-07 20:24:51 +00:00
|
|
|
// CheckDefinition is used to JSON decode the Check definitions
|
2014-01-30 23:35:38 +00:00
|
|
|
type CheckDefinition struct {
|
2016-06-06 20:19:31 +00:00
|
|
|
ID types.CheckID
|
2014-02-03 23:15:35 +00:00
|
|
|
Name string
|
|
|
|
Notes string
|
2015-01-14 01:52:17 +00:00
|
|
|
ServiceID string
|
2015-04-28 19:44:46 +00:00
|
|
|
Token string
|
2015-04-12 00:53:48 +00:00
|
|
|
Status string
|
2017-05-15 19:49:13 +00:00
|
|
|
|
|
|
|
// Copied fields from CheckType without the fields
|
|
|
|
// already present in CheckDefinition:
|
|
|
|
//
|
|
|
|
// ID (CheckID), Name, Status, Notes
|
|
|
|
//
|
|
|
|
Script string
|
|
|
|
HTTP string
|
|
|
|
TCP string
|
|
|
|
Interval time.Duration
|
|
|
|
DockerContainerID string
|
|
|
|
Shell string
|
|
|
|
TLSSkipVerify bool
|
|
|
|
Timeout time.Duration
|
|
|
|
TTL time.Duration
|
|
|
|
DeregisterCriticalServiceAfter time.Duration
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
|
|
|
|
2014-02-03 23:15:35 +00:00
|
|
|
func (c *CheckDefinition) HealthCheck(node string) *structs.HealthCheck {
|
|
|
|
health := &structs.HealthCheck{
|
2015-01-14 01:52:17 +00:00
|
|
|
Node: node,
|
|
|
|
CheckID: c.ID,
|
|
|
|
Name: c.Name,
|
2017-04-19 23:00:11 +00:00
|
|
|
Status: api.HealthCritical,
|
2015-01-14 01:52:17 +00:00
|
|
|
Notes: c.Notes,
|
|
|
|
ServiceID: c.ServiceID,
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
2015-04-12 00:53:48 +00:00
|
|
|
if c.Status != "" {
|
|
|
|
health.Status = c.Status
|
|
|
|
}
|
2014-02-03 23:15:35 +00:00
|
|
|
if health.CheckID == "" && health.Name != "" {
|
2016-06-06 20:19:31 +00:00
|
|
|
health.CheckID = types.CheckID(health.Name)
|
2014-02-03 23:15:35 +00:00
|
|
|
}
|
|
|
|
return health
|
2014-01-30 23:35:38 +00:00
|
|
|
}
|
2015-04-28 02:01:02 +00:00
|
|
|
|
2017-05-15 19:49:13 +00:00
|
|
|
func (c *CheckDefinition) CheckType() *CheckType {
|
|
|
|
return &CheckType{
|
|
|
|
CheckID: c.ID,
|
|
|
|
Name: c.Name,
|
|
|
|
Script: c.Script,
|
|
|
|
HTTP: c.HTTP,
|
|
|
|
TCP: c.TCP,
|
|
|
|
Interval: c.Interval,
|
|
|
|
DockerContainerID: c.DockerContainerID,
|
|
|
|
Shell: c.Shell,
|
|
|
|
TLSSkipVerify: c.TLSSkipVerify,
|
|
|
|
Timeout: c.Timeout,
|
|
|
|
TTL: c.TTL,
|
|
|
|
DeregisterCriticalServiceAfter: c.DeregisterCriticalServiceAfter,
|
|
|
|
Status: c.Status,
|
|
|
|
Notes: c.Notes,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-28 02:01:02 +00:00
|
|
|
// persistedService is used to wrap a service definition and bundle it
|
|
|
|
// with an ACL token so we can restore both at a later agent start.
|
|
|
|
type persistedService struct {
|
|
|
|
Token string
|
|
|
|
Service *structs.NodeService
|
|
|
|
}
|