This commit is contained in:
Philippe Caseiro 2022-06-15 17:29:45 +02:00
parent 5085bd4d69
commit 784b6c59c8
7 changed files with 147 additions and 24 deletions

View File

@ -2,12 +2,14 @@ LINT_ARGS ?= ./...
DESTDIR ?= "/usr/local"
bin:
GOOS=linux go build -o bin/templater-linux cmd/templater.go
GOOS=linux go build -o bin/templater-linux cmd/templater/main.go
GOOS=linux go build -o bin/bootstraper-linux cmd/bootstraper/main.go
upx bin/templater-linux
upx bin/templaster-server
install:
cp bin/templater-linux $(DESTDIR)/bin/templater
cp bin/bootstraper-linux $(DESTDIR)/bin/bootstraper
uninstall:
rm $(DESTDIR)/bin/templater

88
cmd/bootstraper/main.go Normal file
View File

@ -0,0 +1,88 @@
package main
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strconv"
"forge.cadoles.com/pcaseiro/templatefile/pkg/templater"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
"github.com/alexflint/go-arg"
"github.com/zclconf/go-cty/cty"
ctyjson "github.com/zclconf/go-cty/cty/json"
)
func main() {
var args struct {
ConfigDirectory string `arg:"-c,--config-dir,env:CONFIG_DIR" help:"Configuration values directory path" default:"./data/config"`
TemplateDirectory string `arg:"-t,--template-dir,env:TEMPLATE_DIR" help:"Template directory path" default:"./data/templates"`
}
arg.MustParse(&args)
files, err := ioutil.ReadDir(args.ConfigDirectory)
if err != nil {
panic(err)
}
for _, file := range files {
var varsVal cty.Value
fname := fmt.Sprintf("%s/%s", args.ConfigDirectory, file.Name())
flContent, err := os.ReadFile(fname)
if err != nil {
panic(err)
}
ctyType, err := ctyjson.ImpliedType(flContent)
if err != nil {
panic(err)
/* Maybe one day
cexpr, diags := hclsyntax.ParseExpression(config, "", hcl.Pos{Line: 0, Column: 1})
if diags.HasErrors() {
panic(diags.Error())
}
varsVal, diags = cexpr.Value(&hcl.EvalContext{})
fmt.Println(cexpr.Variables())
checkDiags(diags)
*/
} else {
varsVal, err = ctyjson.Unmarshal(flContent, ctyType)
utils.CheckErr(err)
}
cnfVals := varsVal.AsValueMap()
config := cnfVals["Config"].AsValueMap()
fls := config["ConfigFiles"].AsValueSlice()
for _, fl := range fls {
data := fl.AsValueMap()
dest := filepath.Join("/tmp/test", data["destination"].AsString())
source := filepath.Join(args.TemplateDirectory, data["source"].AsString())
mod := data["mod"].AsString()
intMod, err := strconv.Atoi(mod)
if err != nil {
panic(err)
}
fmt.Printf("Processing %s\n", source)
fileExtension := filepath.Ext(source)
if fileExtension == ".hcl" {
res := templater.ProcessHCLTemplate(source, flContent)
dirname := filepath.Dir(dest)
err = os.MkdirAll(dirname, os.FileMode(int(0700)))
if err != nil {
panic(err)
}
err = os.WriteFile(dest, []byte(res), os.FileMode(intMod))
if err != nil {
panic(err)
}
}
}
}
}

View File

@ -1,5 +1,6 @@
{
"Name": "loki",
"Config": {
"ConfigFiles": [
{
"destination": "/etc/loki/loki-local-config.yaml",
@ -23,4 +24,5 @@
"APIKey": "",
"APISecretKey": ""
}
}
}

15
data/config/test2.json Normal file
View File

@ -0,0 +1,15 @@
{
"Name": "toto",
"Config": {
"ConfigFiles": [
{
"destination": "/etc/hosts",
"source": "hosts.pktpl.hcl",
"mod": "600"
}
],
"AuthEnabled": false,
"User": "toto",
"Group": "grafana"
}
}

15
pkg/configs/main.go Normal file
View File

@ -0,0 +1,15 @@
package configs
type ConfigFile struct {
Destination string `form:"destination" json:"destination"`
Source string `form:"source" json:"source"`
Mod string `form:"mod" json:"mod"`
}
type TemplaterConfig struct {
Name string `json:"Name"`
Config struct {
ConfigFiles []ConfigFile `json:"ConfigFiles"`
Variables map[string]interface{} `form:"-" json:"-"`
} `json:"Config"`
}

View File

@ -31,7 +31,7 @@ func ProcessGoTemplate(file string, config []byte) string {
tpl, err := template.New("conf").Parse(string(data))
utils.CheckErr(err)
utils.CheckErr(tpl.Execute(&res, confData))
utils.CheckErr(tpl.Execute(&res, confData["Config"]))
return res.String()
}
@ -63,8 +63,9 @@ func ProcessHCLTemplate(file string, config []byte) string {
utils.CheckErr(err)
}
cnfVals := varsVal.AsValueMap()
ctx := &hcl.EvalContext{
Variables: varsVal.AsValueMap(),
Variables: cnfVals["Config"].AsValueMap(),
}
for n := range ctx.Variables {