2019-09-04 12:15:40 +02:00
|
|
|
// Copyright (C) 2016-2019 Vincent Ambo <mail@tazj.in>
|
2017-11-21 11:24:04 +01:00
|
|
|
//
|
|
|
|
// This file is part of Kontemplate.
|
|
|
|
//
|
|
|
|
// Kontemplate is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2017-02-08 11:50:49 +01:00
|
|
|
package templater
|
|
|
|
|
|
|
|
import (
|
2017-02-08 11:55:59 +01:00
|
|
|
"bytes"
|
2017-02-08 14:37:36 +01:00
|
|
|
"encoding/json"
|
2017-02-08 11:50:49 +01:00
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
2018-03-29 19:12:46 +02:00
|
|
|
"os/exec"
|
2017-02-08 11:50:49 +01:00
|
|
|
"path"
|
2017-02-08 11:55:59 +01:00
|
|
|
"strings"
|
2017-02-08 11:50:49 +01:00
|
|
|
"text/template"
|
|
|
|
|
2017-02-08 14:37:36 +01:00
|
|
|
"github.com/Masterminds/sprig"
|
2017-02-08 11:55:59 +01:00
|
|
|
"github.com/tazjin/kontemplate/context"
|
2017-02-10 20:53:59 +01:00
|
|
|
"github.com/tazjin/kontemplate/util"
|
2017-02-08 11:50:49 +01:00
|
|
|
)
|
|
|
|
|
2017-04-04 11:02:34 +02:00
|
|
|
const failOnMissingKeys string = "missingkey=error"
|
|
|
|
|
2017-06-11 21:25:56 +02:00
|
|
|
type RenderedResource struct {
|
|
|
|
Filename string
|
|
|
|
Rendered string
|
|
|
|
}
|
|
|
|
|
|
|
|
type RenderedResourceSet struct {
|
|
|
|
Name string
|
|
|
|
Resources []RenderedResource
|
2018-11-15 13:49:23 +01:00
|
|
|
Args []string
|
2017-06-11 21:25:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func LoadAndApplyTemplates(include *[]string, exclude *[]string, c *context.Context) ([]RenderedResourceSet, error) {
|
2017-02-08 16:44:55 +01:00
|
|
|
limitedResourceSets := applyLimits(&c.ResourceSets, include, exclude)
|
2017-06-11 21:43:02 +02:00
|
|
|
renderedResourceSets := make([]RenderedResourceSet, 0)
|
2017-02-08 12:58:53 +01:00
|
|
|
|
2017-05-04 17:24:36 +02:00
|
|
|
if len(*limitedResourceSets) == 0 {
|
2017-06-11 21:25:56 +02:00
|
|
|
return renderedResourceSets, fmt.Errorf("No valid resource sets included!")
|
2017-05-04 17:24:36 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:44:55 +01:00
|
|
|
for _, rs := range *limitedResourceSets {
|
2017-06-11 21:25:56 +02:00
|
|
|
set, err := processResourceSet(c, &rs)
|
2017-02-08 11:50:49 +01:00
|
|
|
|
2017-02-08 16:44:55 +01:00
|
|
|
if err != nil {
|
2017-06-11 21:25:56 +02:00
|
|
|
return nil, err
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
2017-06-11 21:25:56 +02:00
|
|
|
|
|
|
|
renderedResourceSets = append(renderedResourceSets, *set)
|
2017-02-08 12:58:53 +01:00
|
|
|
}
|
|
|
|
|
2017-06-11 21:25:56 +02:00
|
|
|
return renderedResourceSets, nil
|
2017-02-08 12:58:53 +01:00
|
|
|
}
|
2017-02-08 11:50:49 +01:00
|
|
|
|
2018-06-09 21:15:18 +02:00
|
|
|
func processResourceSet(ctx *context.Context, rs *context.ResourceSet) (*RenderedResourceSet, error) {
|
2017-02-08 12:58:53 +01:00
|
|
|
fmt.Fprintf(os.Stderr, "Loading resources for %s\n", rs.Name)
|
2017-02-08 11:50:49 +01:00
|
|
|
|
2019-09-04 11:56:07 +02:00
|
|
|
fileInfo, err := os.Stat(rs.Path)
|
2017-02-08 12:58:53 +01:00
|
|
|
if err != nil {
|
2018-03-09 15:17:54 +01:00
|
|
|
return nil, err
|
2017-02-08 12:58:53 +01:00
|
|
|
}
|
|
|
|
|
2018-06-09 21:15:18 +02:00
|
|
|
var files []os.FileInfo
|
|
|
|
var resources []RenderedResource
|
|
|
|
|
|
|
|
// Treat single-file resource paths separately from resource
|
|
|
|
// sets containing multiple templates
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
// Explicitly discard this error, which will give us an empty
|
|
|
|
// list of files instead.
|
|
|
|
// This will end up printing a warning to the user, but it
|
|
|
|
// won't stop the rest of the process.
|
2019-09-04 11:56:07 +02:00
|
|
|
files, _ = ioutil.ReadDir(rs.Path)
|
2018-06-09 21:15:18 +02:00
|
|
|
resources, err = processFiles(ctx, rs, files)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2019-09-04 11:56:07 +02:00
|
|
|
resource, err := templateFile(ctx, rs, rs.Path)
|
2018-06-09 21:15:18 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
resources = []RenderedResource{resource}
|
|
|
|
}
|
|
|
|
|
2017-06-11 21:25:56 +02:00
|
|
|
return &RenderedResourceSet{
|
|
|
|
Name: rs.Name,
|
|
|
|
Resources: resources,
|
2018-11-15 13:49:23 +01:00
|
|
|
Args: rs.Args,
|
2017-06-11 21:25:56 +02:00
|
|
|
}, nil
|
2017-02-08 12:58:53 +01:00
|
|
|
}
|
|
|
|
|
2018-06-09 21:15:18 +02:00
|
|
|
func processFiles(ctx *context.Context, rs *context.ResourceSet, files []os.FileInfo) ([]RenderedResource, error) {
|
2017-06-11 21:43:02 +02:00
|
|
|
resources := make([]RenderedResource, 0)
|
2017-06-11 21:25:56 +02:00
|
|
|
|
2017-02-08 12:58:53 +01:00
|
|
|
for _, file := range files {
|
|
|
|
if !file.IsDir() && isResourceFile(file) {
|
2019-09-04 11:56:07 +02:00
|
|
|
path := path.Join(rs.Path, file.Name())
|
2018-06-09 21:15:18 +02:00
|
|
|
res, err := templateFile(ctx, rs, path)
|
2017-02-08 12:58:53 +01:00
|
|
|
|
|
|
|
if err != nil {
|
2017-06-11 21:25:56 +02:00
|
|
|
return resources, err
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
2017-02-08 12:58:53 +01:00
|
|
|
|
2017-06-11 21:25:56 +02:00
|
|
|
resources = append(resources, res)
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-11 21:25:56 +02:00
|
|
|
return resources, nil
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
|
|
|
|
2018-06-09 21:15:18 +02:00
|
|
|
func templateFile(ctx *context.Context, rs *context.ResourceSet, filepath string) (RenderedResource, error) {
|
|
|
|
var resource RenderedResource
|
2017-02-08 11:50:49 +01:00
|
|
|
|
2018-06-09 21:15:18 +02:00
|
|
|
tpl, err := template.New(path.Base(filepath)).Funcs(templateFuncs(ctx, rs)).Option(failOnMissingKeys).ParseFiles(filepath)
|
2017-02-08 11:50:49 +01:00
|
|
|
if err != nil {
|
2018-06-09 21:15:18 +02:00
|
|
|
return resource, fmt.Errorf("Could not load template %s: %v", filepath, err)
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
var b bytes.Buffer
|
|
|
|
err = tpl.Execute(&b, rs.Values)
|
|
|
|
if err != nil {
|
2018-06-09 21:15:18 +02:00
|
|
|
return resource, fmt.Errorf("Error while templating %s: %v", filepath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
resource = RenderedResource{
|
|
|
|
Filename: path.Base(filepath),
|
|
|
|
Rendered: b.String(),
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
|
|
|
|
2018-06-09 21:15:18 +02:00
|
|
|
return resource, nil
|
2017-02-08 11:50:49 +01:00
|
|
|
}
|
|
|
|
|
2017-02-08 16:44:55 +01:00
|
|
|
// Applies the limits of explicitly included or excluded resources and returns the updated resource set.
|
|
|
|
// Exclude takes priority over include
|
|
|
|
func applyLimits(rs *[]context.ResourceSet, include *[]string, exclude *[]string) *[]context.ResourceSet {
|
|
|
|
if len(*include) == 0 && len(*exclude) == 0 {
|
|
|
|
return rs
|
|
|
|
}
|
|
|
|
|
|
|
|
// Exclude excluded resource sets
|
|
|
|
excluded := make([]context.ResourceSet, 0)
|
|
|
|
for _, r := range *rs {
|
2017-02-08 17:14:40 +01:00
|
|
|
if !matchesResourceSet(exclude, &r) {
|
2017-02-08 16:44:55 +01:00
|
|
|
excluded = append(excluded, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Include included resource sets
|
|
|
|
if len(*include) == 0 {
|
|
|
|
return &excluded
|
|
|
|
}
|
|
|
|
included := make([]context.ResourceSet, 0)
|
|
|
|
for _, r := range excluded {
|
2017-02-08 17:14:40 +01:00
|
|
|
if matchesResourceSet(include, &r) {
|
2017-02-08 16:44:55 +01:00
|
|
|
included = append(included, r)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return &included
|
|
|
|
}
|
|
|
|
|
2017-02-08 17:14:40 +01:00
|
|
|
// Check whether an include/exclude string slice matches a resource set
|
|
|
|
func matchesResourceSet(s *[]string, rs *context.ResourceSet) bool {
|
2017-02-08 16:44:55 +01:00
|
|
|
for _, r := range *s {
|
2017-06-11 22:27:20 +02:00
|
|
|
r = strings.TrimSuffix(r, "/")
|
2017-02-08 17:32:19 +01:00
|
|
|
if r == rs.Name || r == rs.Parent {
|
2017-02-08 16:44:55 +01:00
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2018-05-03 19:50:57 +02:00
|
|
|
func templateFuncs(c *context.Context, rs *context.ResourceSet) template.FuncMap {
|
2017-02-08 14:37:36 +01:00
|
|
|
m := sprig.TxtFuncMap()
|
|
|
|
m["json"] = func(data interface{}) string {
|
|
|
|
b, _ := json.Marshal(data)
|
|
|
|
return string(b)
|
|
|
|
}
|
2017-02-09 15:33:03 +01:00
|
|
|
m["passLookup"] = GetFromPass
|
2018-03-29 19:12:46 +02:00
|
|
|
m["gitHEAD"] = func() (string, error) {
|
2018-05-03 19:50:57 +02:00
|
|
|
out, err := exec.Command("git", "-C", c.BaseDir, "rev-parse", "HEAD").Output()
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
2018-03-29 19:12:46 +02:00
|
|
|
}
|
2018-05-03 19:50:57 +02:00
|
|
|
output := strings.TrimSpace(string(out))
|
|
|
|
return output, nil
|
|
|
|
}
|
2017-10-17 22:49:42 +02:00
|
|
|
m["lookupIPAddr"] = GetIPsFromDNS
|
2018-03-09 14:49:33 +01:00
|
|
|
m["insertFile"] = func(file string) (string, error) {
|
|
|
|
data, err := ioutil.ReadFile(path.Join(rs.Path, file))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return string(data), nil
|
|
|
|
}
|
2019-04-30 00:13:15 +02:00
|
|
|
m["insertTemplate"] = func(file string) (string, error) {
|
|
|
|
data, err := templateFile(c, rs, path.Join(rs.Path, file))
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
|
|
|
|
return data.Rendered, nil
|
|
|
|
}
|
2018-06-27 23:37:16 +02:00
|
|
|
m["default"] = func(defaultVal interface{}, varName string) interface{} {
|
|
|
|
if val, ok := rs.Values[varName]; ok {
|
|
|
|
return val
|
|
|
|
}
|
2017-02-08 14:37:36 +01:00
|
|
|
|
2018-06-27 23:37:16 +02:00
|
|
|
return defaultVal
|
|
|
|
}
|
2017-02-08 14:37:36 +01:00
|
|
|
return m
|
|
|
|
}
|
|
|
|
|
2017-04-04 14:36:27 +02:00
|
|
|
// Checks whether a file is a resource file (i.e. is YAML or JSON) and not a default values file.
|
2017-02-08 11:50:49 +01:00
|
|
|
func isResourceFile(f os.FileInfo) bool {
|
2017-08-31 18:37:57 +02:00
|
|
|
for _, defaultFile := range util.DefaultFilenames {
|
|
|
|
if f.Name() == defaultFile {
|
|
|
|
return false
|
|
|
|
}
|
2017-04-04 14:36:27 +02:00
|
|
|
}
|
|
|
|
|
2017-02-08 11:50:49 +01:00
|
|
|
return strings.HasSuffix(f.Name(), "yaml") ||
|
|
|
|
strings.HasSuffix(f.Name(), "yml") ||
|
|
|
|
strings.HasSuffix(f.Name(), "json")
|
|
|
|
}
|