2015-09-09 20:02:39 +00:00
|
|
|
package api
|
|
|
|
|
2015-10-30 23:32:05 +00:00
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2015-11-02 21:24:59 +00:00
|
|
|
// RestartPolicy defines how the Nomad client restarts
|
|
|
|
// tasks in a taskgroup when they fail
|
2015-10-30 23:32:05 +00:00
|
|
|
type RestartPolicy struct {
|
2016-02-02 23:08:07 +00:00
|
|
|
Interval time.Duration
|
|
|
|
Attempts int
|
|
|
|
Delay time.Duration
|
|
|
|
Mode string
|
2015-10-31 04:28:56 +00:00
|
|
|
}
|
|
|
|
|
2015-11-17 07:20:35 +00:00
|
|
|
// The ServiceCheck data model represents the consul health check that
|
|
|
|
// Nomad registers for a Task
|
|
|
|
type ServiceCheck struct {
|
|
|
|
Id string
|
|
|
|
Name string
|
|
|
|
Type string
|
2015-11-17 22:21:14 +00:00
|
|
|
Script string
|
2015-11-18 21:02:25 +00:00
|
|
|
Path string
|
2015-11-17 22:21:14 +00:00
|
|
|
Protocol string
|
2015-11-17 07:20:35 +00:00
|
|
|
Interval time.Duration
|
|
|
|
Timeout time.Duration
|
|
|
|
}
|
|
|
|
|
|
|
|
// The Service model represents a Consul service defintion
|
|
|
|
type Service struct {
|
|
|
|
Id string
|
|
|
|
Name string
|
|
|
|
Tags []string
|
|
|
|
PortLabel string `mapstructure:"port"`
|
|
|
|
Checks []ServiceCheck
|
|
|
|
}
|
|
|
|
|
2015-09-09 20:02:39 +00:00
|
|
|
// TaskGroup is the unit of scheduling.
|
|
|
|
type TaskGroup struct {
|
2015-10-30 23:32:05 +00:00
|
|
|
Name string
|
|
|
|
Count int
|
|
|
|
Constraints []*Constraint
|
|
|
|
Tasks []*Task
|
|
|
|
RestartPolicy *RestartPolicy
|
|
|
|
Meta map[string]string
|
2015-09-09 20:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTaskGroup creates a new TaskGroup.
|
2015-09-10 00:59:18 +00:00
|
|
|
func NewTaskGroup(name string, count int) *TaskGroup {
|
2015-09-09 20:02:39 +00:00
|
|
|
return &TaskGroup{
|
2015-12-18 20:17:13 +00:00
|
|
|
Name: name,
|
|
|
|
Count: count,
|
2015-09-09 20:02:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Constrain is used to add a constraint to a task group.
|
|
|
|
func (g *TaskGroup) Constrain(c *Constraint) *TaskGroup {
|
|
|
|
g.Constraints = append(g.Constraints, c)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddMeta is used to add a meta k/v pair to a task group
|
|
|
|
func (g *TaskGroup) SetMeta(key, val string) *TaskGroup {
|
|
|
|
if g.Meta == nil {
|
|
|
|
g.Meta = make(map[string]string)
|
|
|
|
}
|
|
|
|
g.Meta[key] = val
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
|
|
|
// AddTask is used to add a new task to a task group.
|
|
|
|
func (g *TaskGroup) AddTask(t *Task) *TaskGroup {
|
|
|
|
g.Tasks = append(g.Tasks, t)
|
|
|
|
return g
|
|
|
|
}
|
|
|
|
|
2016-02-05 07:54:15 +00:00
|
|
|
// LogConfig provides configuration for log rotation
|
|
|
|
type LogConfig struct {
|
2016-02-11 00:40:36 +00:00
|
|
|
MaxFiles int
|
|
|
|
MaxFileSizeMB int
|
2016-02-05 07:54:15 +00:00
|
|
|
}
|
|
|
|
|
2015-09-09 20:02:39 +00:00
|
|
|
// Task is a single process in a task group.
|
|
|
|
type Task struct {
|
|
|
|
Name string
|
|
|
|
Driver string
|
2015-11-14 04:51:30 +00:00
|
|
|
Config map[string]interface{}
|
2015-09-09 20:02:39 +00:00
|
|
|
Constraints []*Constraint
|
2015-09-30 16:18:43 +00:00
|
|
|
Env map[string]string
|
2015-11-17 07:20:35 +00:00
|
|
|
Services []Service
|
2015-09-09 20:02:39 +00:00
|
|
|
Resources *Resources
|
|
|
|
Meta map[string]string
|
2015-12-23 00:10:30 +00:00
|
|
|
KillTimeout time.Duration
|
2016-02-05 07:54:15 +00:00
|
|
|
LogConfig *LogConfig
|
2016-03-14 22:46:06 +00:00
|
|
|
Artifacts []*TaskArtifact
|
|
|
|
}
|
|
|
|
|
|
|
|
type TaskArtifact struct {
|
|
|
|
GetterSource string
|
|
|
|
GetterOptions map[string]string
|
2015-09-09 20:02:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewTask creates and initializes a new Task.
|
|
|
|
func NewTask(name, driver string) *Task {
|
|
|
|
return &Task{
|
|
|
|
Name: name,
|
|
|
|
Driver: driver,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure is used to configure a single k/v pair on
|
|
|
|
// the task.
|
|
|
|
func (t *Task) SetConfig(key, val string) *Task {
|
|
|
|
if t.Config == nil {
|
2015-11-14 04:51:30 +00:00
|
|
|
t.Config = make(map[string]interface{})
|
2015-09-09 20:02:39 +00:00
|
|
|
}
|
|
|
|
t.Config[key] = val
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// SetMeta is used to add metadata k/v pairs to the task.
|
|
|
|
func (t *Task) SetMeta(key, val string) *Task {
|
|
|
|
if t.Meta == nil {
|
|
|
|
t.Meta = make(map[string]string)
|
|
|
|
}
|
|
|
|
t.Meta[key] = val
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
|
|
|
// Require is used to add resource requirements to a task.
|
|
|
|
func (t *Task) Require(r *Resources) *Task {
|
|
|
|
t.Resources = r
|
|
|
|
return t
|
|
|
|
}
|
2015-09-10 00:29:43 +00:00
|
|
|
|
|
|
|
// Constraint adds a new constraints to a single task.
|
|
|
|
func (t *Task) Constrain(c *Constraint) *Task {
|
|
|
|
t.Constraints = append(t.Constraints, c)
|
|
|
|
return t
|
|
|
|
}
|
2015-11-12 23:28:22 +00:00
|
|
|
|
2016-02-10 21:36:47 +00:00
|
|
|
// SetLogConfig sets a log config to a task
|
|
|
|
func (t *Task) SetLogConfig(l *LogConfig) *Task {
|
|
|
|
t.LogConfig = l
|
|
|
|
return t
|
|
|
|
}
|
|
|
|
|
2015-11-12 23:28:22 +00:00
|
|
|
// TaskState tracks the current state of a task and events that caused state
|
|
|
|
// transistions.
|
|
|
|
type TaskState struct {
|
|
|
|
State string
|
|
|
|
Events []*TaskEvent
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
2015-11-14 22:13:32 +00:00
|
|
|
TaskDriverFailure = "Driver Failure"
|
2016-02-29 00:56:05 +00:00
|
|
|
TaskReceived = "Received"
|
2015-11-14 22:13:32 +00:00
|
|
|
TaskStarted = "Started"
|
|
|
|
TaskTerminated = "Terminated"
|
|
|
|
TaskKilled = "Killed"
|
2016-02-29 00:56:05 +00:00
|
|
|
TaskRestarting = "Restarting"
|
|
|
|
TaskNotRestarting = "Restarts Exceeded"
|
2015-11-12 23:28:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TaskEvent is an event that effects the state of a task and contains meta-data
|
|
|
|
// appropriate to the events type.
|
|
|
|
type TaskEvent struct {
|
2015-11-14 22:13:32 +00:00
|
|
|
Type string
|
2015-11-12 23:28:22 +00:00
|
|
|
Time int64
|
2015-11-16 20:10:29 +00:00
|
|
|
DriverError string
|
2015-11-12 23:28:22 +00:00
|
|
|
ExitCode int
|
|
|
|
Signal int
|
|
|
|
Message string
|
2015-11-14 06:07:13 +00:00
|
|
|
KillError string
|
2016-02-29 00:56:05 +00:00
|
|
|
StartDelay int64
|
2015-11-12 23:28:22 +00:00
|
|
|
}
|