2023-04-10 15:36:59 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2020-10-21 14:19:46 +00:00
|
|
|
package jobspec2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"errors"
|
2020-11-09 19:58:57 +00:00
|
|
|
"fmt"
|
2020-10-21 14:19:46 +00:00
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/hashicorp/hcl/v2"
|
|
|
|
"github.com/hashicorp/hcl/v2/hclsyntax"
|
|
|
|
hcljson "github.com/hashicorp/hcl/v2/json"
|
|
|
|
"github.com/hashicorp/nomad/api"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Parse(path string, r io.Reader) (*api.Job, error) {
|
|
|
|
if path == "" {
|
|
|
|
if f, ok := r.(*os.File); ok {
|
|
|
|
path = f.Name()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var buf bytes.Buffer
|
2020-11-09 19:58:57 +00:00
|
|
|
_, err := io.Copy(&buf, r)
|
|
|
|
if err != nil {
|
2020-10-21 14:19:46 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-09 19:58:57 +00:00
|
|
|
return ParseWithConfig(&ParseConfig{
|
|
|
|
Path: path,
|
|
|
|
Body: buf.Bytes(),
|
|
|
|
AllowFS: false,
|
|
|
|
Strict: true,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseWithConfig(args *ParseConfig) (*api.Job, error) {
|
|
|
|
args.normalize()
|
|
|
|
|
2020-11-09 21:27:06 +00:00
|
|
|
c := newJobConfig(args)
|
2020-11-09 19:58:57 +00:00
|
|
|
err := decode(c)
|
2020-10-21 14:19:46 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-11-09 19:58:57 +00:00
|
|
|
normalizeJob(c)
|
|
|
|
return c.Job, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
type ParseConfig struct {
|
|
|
|
Path string
|
|
|
|
BaseDir string
|
|
|
|
|
|
|
|
// Body is the HCL body
|
|
|
|
Body []byte
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// AllowFS enables HCL functions that require file system access
|
2020-11-09 19:58:57 +00:00
|
|
|
AllowFS bool
|
|
|
|
|
|
|
|
// ArgVars is the CLI -var arguments
|
|
|
|
ArgVars []string
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// VarFiles is the paths of variable data files that should be read during
|
|
|
|
// parsing.
|
2020-11-09 19:58:57 +00:00
|
|
|
VarFiles []string
|
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
// VarContent is the content of variable data known without reading an
|
|
|
|
// actual var file during parsing.
|
|
|
|
VarContent string
|
|
|
|
|
2020-11-09 19:58:57 +00:00
|
|
|
// Envs represent process environment variable
|
|
|
|
Envs []string
|
|
|
|
|
|
|
|
Strict bool
|
|
|
|
|
|
|
|
// parsedVarFiles represent parsed HCL AST of the passed EnvVars
|
|
|
|
parsedVarFiles []*hcl.File
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *ParseConfig) normalize() {
|
|
|
|
if c.BaseDir == "" {
|
|
|
|
c.BaseDir = filepath.Dir(c.Path)
|
|
|
|
}
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
|
|
|
|
2020-11-09 19:58:57 +00:00
|
|
|
func decode(c *jobConfig) error {
|
2020-12-16 15:49:09 +00:00
|
|
|
config := c.ParseConfig
|
2020-10-21 14:19:46 +00:00
|
|
|
|
2020-12-16 15:49:09 +00:00
|
|
|
file, diags := parseHCLOrJSON(config.Body, config.Path)
|
2020-10-21 14:19:46 +00:00
|
|
|
|
2020-12-16 15:49:09 +00:00
|
|
|
for _, varFile := range config.VarFiles {
|
|
|
|
parsedVarFile, ds := parseFile(varFile)
|
2021-05-12 13:08:59 +00:00
|
|
|
if parsedVarFile == nil || ds.HasErrors() {
|
|
|
|
return fmt.Errorf("unable to parse var file: %v", ds.Error())
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:49:09 +00:00
|
|
|
config.parsedVarFiles = append(config.parsedVarFiles, parsedVarFile)
|
|
|
|
diags = append(diags, ds...)
|
2020-10-21 14:19:46 +00:00
|
|
|
}
|
2020-11-09 20:02:21 +00:00
|
|
|
|
2023-04-11 13:45:08 +00:00
|
|
|
if config.VarContent != "" {
|
|
|
|
hclFile, hclDiagnostics := parseHCLOrJSON([]byte(config.VarContent), "input.hcl")
|
|
|
|
if hclDiagnostics.HasErrors() {
|
|
|
|
return fmt.Errorf("unable to parse var content: %v", hclDiagnostics.Error())
|
|
|
|
}
|
|
|
|
config.parsedVarFiles = append(config.parsedVarFiles, hclFile)
|
|
|
|
}
|
|
|
|
|
2022-02-01 23:54:53 +00:00
|
|
|
// Return early if the input job or variable files are not valid.
|
|
|
|
// Decoding and evaluating invalid files may result in unexpected results.
|
|
|
|
if diags.HasErrors() {
|
|
|
|
return diags
|
|
|
|
}
|
|
|
|
|
2020-12-16 15:49:09 +00:00
|
|
|
diags = append(diags, c.decodeBody(file.Body)...)
|
2020-10-21 14:19:46 +00:00
|
|
|
|
|
|
|
if diags.HasErrors() {
|
|
|
|
var str strings.Builder
|
|
|
|
for i, diag := range diags {
|
|
|
|
if i != 0 {
|
|
|
|
str.WriteByte('\n')
|
|
|
|
}
|
|
|
|
str.WriteString(diag.Error())
|
|
|
|
}
|
|
|
|
return errors.New(str.String())
|
|
|
|
}
|
2020-12-16 15:49:09 +00:00
|
|
|
|
2020-11-12 16:35:39 +00:00
|
|
|
diags = append(diags, decodeMapInterfaceType(&c.Job, c.EvalContext())...)
|
|
|
|
diags = append(diags, decodeMapInterfaceType(&c.Tasks, c.EvalContext())...)
|
|
|
|
diags = append(diags, decodeMapInterfaceType(&c.Vault, c.EvalContext())...)
|
2020-10-21 14:19:46 +00:00
|
|
|
|
2020-12-16 15:49:09 +00:00
|
|
|
if diags.HasErrors() {
|
|
|
|
return diags
|
2020-11-09 20:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 15:49:09 +00:00
|
|
|
return nil
|
|
|
|
}
|
2020-11-09 20:02:21 +00:00
|
|
|
|
2020-12-16 15:49:09 +00:00
|
|
|
func parseFile(path string) (*hcl.File, hcl.Diagnostics) {
|
2023-03-08 15:39:03 +00:00
|
|
|
body, err := os.ReadFile(path)
|
2020-12-16 15:49:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, hcl.Diagnostics{
|
|
|
|
&hcl.Diagnostic{
|
2020-11-09 20:02:21 +00:00
|
|
|
Severity: hcl.DiagError,
|
|
|
|
Summary: "Failed to read file",
|
2020-12-16 15:49:09 +00:00
|
|
|
Detail: fmt.Sprintf("failed to read %q: %v", path, err),
|
|
|
|
},
|
2020-11-09 20:02:21 +00:00
|
|
|
}
|
2020-12-16 15:49:09 +00:00
|
|
|
}
|
2020-11-09 20:02:21 +00:00
|
|
|
|
2020-12-16 15:49:09 +00:00
|
|
|
return parseHCLOrJSON(body, path)
|
|
|
|
}
|
2020-11-09 20:02:21 +00:00
|
|
|
|
2020-12-16 15:49:09 +00:00
|
|
|
func parseHCLOrJSON(src []byte, filename string) (*hcl.File, hcl.Diagnostics) {
|
|
|
|
if isJSON(src) {
|
|
|
|
return hcljson.Parse(src, filename)
|
2020-11-09 20:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-12-16 15:49:09 +00:00
|
|
|
return hclsyntax.ParseConfig(src, filename, hcl.Pos{Line: 1, Column: 1})
|
2020-11-09 20:02:21 +00:00
|
|
|
}
|
|
|
|
|
2020-10-21 14:19:46 +00:00
|
|
|
func isJSON(src []byte) bool {
|
|
|
|
for _, c := range src {
|
|
|
|
if c == ' ' {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
return c == '{'
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|