2020-10-21 14:19:46 +00:00
|
|
|
package jobspec2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/hashicorp/nomad/api"
|
2022-08-24 15:43:01 +00:00
|
|
|
"github.com/hashicorp/nomad/helper/pointer"
|
2020-10-21 14:19:46 +00:00
|
|
|
)
|
|
|
|
|
2020-11-09 21:23:09 +00:00
|
|
|
func normalizeJob(jc *jobConfig) {
|
|
|
|
j := jc.Job
|
2020-10-21 14:19:46 +00:00
|
|
|
if j.Name == nil {
|
2020-11-09 21:23:09 +00:00
|
|
|
j.Name = &jc.JobID
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
if j.ID == nil {
|
2020-11-09 21:23:09 +00:00
|
|
|
j.ID = &jc.JobID
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if j.Periodic != nil && j.Periodic.Spec != nil {
|
|
|
|
v := "cron"
|
|
|
|
j.Periodic.SpecType = &v
|
|
|
|
}
|
|
|
|
|
2020-11-09 21:23:09 +00:00
|
|
|
normalizeVault(jc.Vault)
|
2020-10-21 14:19:46 +00:00
|
|
|
|
2020-11-09 21:23:09 +00:00
|
|
|
if len(jc.Tasks) != 0 {
|
|
|
|
alone := make([]*api.TaskGroup, 0, len(jc.Tasks))
|
|
|
|
for _, t := range jc.Tasks {
|
2020-10-21 14:19:46 +00:00
|
|
|
alone = append(alone, &api.TaskGroup{
|
|
|
|
Name: &t.Name,
|
|
|
|
Tasks: []*api.Task{t},
|
|
|
|
})
|
|
|
|
}
|
|
|
|
alone = append(alone, j.TaskGroups...)
|
|
|
|
j.TaskGroups = alone
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, tg := range j.TaskGroups {
|
|
|
|
normalizeNetworkPorts(tg.Networks)
|
|
|
|
for _, t := range tg.Tasks {
|
|
|
|
if t.Resources != nil {
|
|
|
|
normalizeNetworkPorts(t.Resources.Networks)
|
|
|
|
}
|
|
|
|
|
|
|
|
normalizeTemplates(t.Templates)
|
|
|
|
|
|
|
|
// normalize Vault
|
|
|
|
normalizeVault(t.Vault)
|
|
|
|
|
|
|
|
if t.Vault == nil {
|
2020-11-09 21:23:09 +00:00
|
|
|
t.Vault = jc.Vault
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeVault(v *api.Vault) {
|
|
|
|
if v == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if v.Env == nil {
|
2022-08-24 15:43:01 +00:00
|
|
|
v.Env = pointer.Of(true)
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
if v.ChangeMode == nil {
|
2022-08-24 15:43:01 +00:00
|
|
|
v.ChangeMode = pointer.Of("restart")
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeNetworkPorts(networks []*api.NetworkResource) {
|
|
|
|
if networks == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, n := range networks {
|
|
|
|
if len(n.DynamicPorts) == 0 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
dynamic := make([]api.Port, 0, len(n.DynamicPorts))
|
|
|
|
var reserved []api.Port
|
|
|
|
|
|
|
|
for _, p := range n.DynamicPorts {
|
|
|
|
if p.Value > 0 {
|
|
|
|
reserved = append(reserved, p)
|
|
|
|
} else {
|
|
|
|
dynamic = append(dynamic, p)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(dynamic) == 0 {
|
|
|
|
dynamic = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
n.DynamicPorts = dynamic
|
|
|
|
n.ReservedPorts = reserved
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func normalizeTemplates(templates []*api.Template) {
|
|
|
|
if len(templates) == 0 {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, t := range templates {
|
|
|
|
if t.ChangeMode == nil {
|
2022-08-24 15:43:01 +00:00
|
|
|
t.ChangeMode = pointer.Of("restart")
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
if t.Perms == nil {
|
2022-08-24 15:43:01 +00:00
|
|
|
t.Perms = pointer.Of("0644")
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
if t.Splay == nil {
|
2022-08-24 15:43:01 +00:00
|
|
|
t.Splay = pointer.Of(5 * time.Second)
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
2022-08-24 15:43:01 +00:00
|
|
|
normalizeChangeScript(t.ChangeScript)
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:43:01 +00:00
|
|
|
func normalizeChangeScript(ch *api.ChangeScript) {
|
|
|
|
if ch == nil {
|
|
|
|
return
|
|
|
|
}
|
2020-10-21 14:19:46 +00:00
|
|
|
|
2022-08-24 15:43:01 +00:00
|
|
|
if ch.Args == nil {
|
|
|
|
ch.Args = []string{}
|
|
|
|
}
|
2020-10-21 14:19:46 +00:00
|
|
|
|
2022-08-24 15:43:01 +00:00
|
|
|
if ch.Timeout == nil {
|
|
|
|
ch.Timeout = pointer.Of(5 * time.Second)
|
|
|
|
}
|
2020-10-21 14:19:46 +00:00
|
|
|
|
2022-08-24 15:43:01 +00:00
|
|
|
if ch.FailOnError == nil {
|
|
|
|
ch.FailOnError = pointer.Of(false)
|
|
|
|
}
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|