diff --git a/Makefile b/Makefile index 254cad2..631899d 100644 --- a/Makefile +++ b/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 diff --git a/cmd/bootstraper/main.go b/cmd/bootstraper/main.go new file mode 100644 index 0000000..b017198 --- /dev/null +++ b/cmd/bootstraper/main.go @@ -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) + } + } + } + } + +} diff --git a/cmd/templater.go b/cmd/templater/main.go similarity index 100% rename from cmd/templater.go rename to cmd/templater/main.go diff --git a/data/config/test.json b/data/config/test.json index 14035cb..4a4b44f 100644 --- a/data/config/test.json +++ b/data/config/test.json @@ -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": "" } } \ No newline at end of file diff --git a/data/config/test2.json b/data/config/test2.json new file mode 100644 index 0000000..66fc663 --- /dev/null +++ b/data/config/test2.json @@ -0,0 +1,15 @@ +{ + "Name": "toto", + "Config": { + "ConfigFiles": [ + { + "destination": "/etc/hosts", + "source": "hosts.pktpl.hcl", + "mod": "600" + } + ], + "AuthEnabled": false, + "User": "toto", + "Group": "grafana" + } +} \ No newline at end of file diff --git a/pkg/configs/main.go b/pkg/configs/main.go new file mode 100644 index 0000000..6dbc5b2 --- /dev/null +++ b/pkg/configs/main.go @@ -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"` +} diff --git a/pkg/templater/main.go b/pkg/templater/main.go index f1fba18..c029811 100644 --- a/pkg/templater/main.go +++ b/pkg/templater/main.go @@ -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 {