Try 001
This commit is contained in:
parent
5085bd4d69
commit
784b6c59c8
4
Makefile
4
Makefile
|
@ -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
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
|
@ -1,26 +1,28 @@
|
|||
{
|
||||
"Name": "loki",
|
||||
"ConfigFiles": [
|
||||
{
|
||||
"destination": "/etc/loki/loki-local-config.yaml",
|
||||
"source": "loki-local-config.pktpl.hcl",
|
||||
"mod": "600"
|
||||
"Config": {
|
||||
"ConfigFiles": [
|
||||
{
|
||||
"destination": "/etc/loki/loki-local-config.yaml",
|
||||
"source": "loki-local-config.pktpl.hcl",
|
||||
"mod": "600"
|
||||
}
|
||||
],
|
||||
"AuthEnabled": false,
|
||||
"User": "loki",
|
||||
"Group": "grafana",
|
||||
"HTTPPort": "3100",
|
||||
"GRPCPort": "9096",
|
||||
"AlertManagerURL": "http://localhost:9093",
|
||||
"StorageRoot": "/var/loki",
|
||||
"SharedStore": "filesystem",
|
||||
"ObjectStore": "filesystem",
|
||||
"LogLevel": "error",
|
||||
"S3": {
|
||||
"URL": "",
|
||||
"BucketName": "",
|
||||
"APIKey": "",
|
||||
"APISecretKey": ""
|
||||
}
|
||||
],
|
||||
"AuthEnabled": false,
|
||||
"User": "loki",
|
||||
"Group": "grafana",
|
||||
"HTTPPort": "3100",
|
||||
"GRPCPort": "9096",
|
||||
"AlertManagerURL": "http://localhost:9093",
|
||||
"StorageRoot": "/var/loki",
|
||||
"SharedStore": "filesystem",
|
||||
"ObjectStore": "filesystem",
|
||||
"LogLevel": "error",
|
||||
"S3": {
|
||||
"URL": "",
|
||||
"BucketName": "",
|
||||
"APIKey": "",
|
||||
"APISecretKey": ""
|
||||
}
|
||||
}
|
|
@ -0,0 +1,15 @@
|
|||
{
|
||||
"Name": "toto",
|
||||
"Config": {
|
||||
"ConfigFiles": [
|
||||
{
|
||||
"destination": "/etc/hosts",
|
||||
"source": "hosts.pktpl.hcl",
|
||||
"mod": "600"
|
||||
}
|
||||
],
|
||||
"AuthEnabled": false,
|
||||
"User": "toto",
|
||||
"Group": "grafana"
|
||||
}
|
||||
}
|
|
@ -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"`
|
||||
}
|
|
@ -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 {
|
||||
|
|
Loading…
Reference in New Issue