From 442bd0465246275cbdcf2d2e951912812b6130b0 Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Thu, 23 Jun 2022 18:03:09 +0200 Subject: [PATCH] Try 002 --- cmd/bootstraper/main.go | 137 +++++++++++++++------------ data/config/test-services.json | 92 ++++++++++++++++++ data/templates/grafana.ini.pktpl.hcl | 5 + pkg/configs/files.go | 46 +++++++++ pkg/configs/main.go | 29 ++++-- pkg/configs/system_services.go | 27 ++++++ pkg/templater/main.go | 13 ++- 7 files changed, 273 insertions(+), 76 deletions(-) create mode 100644 data/config/test-services.json create mode 100644 data/templates/grafana.ini.pktpl.hcl create mode 100644 pkg/configs/files.go create mode 100644 pkg/configs/system_services.go diff --git a/cmd/bootstraper/main.go b/cmd/bootstraper/main.go index b017198..7cc6da2 100644 --- a/cmd/bootstraper/main.go +++ b/cmd/bootstraper/main.go @@ -1,88 +1,105 @@ package main import ( + "encoding/json" "fmt" "io/ioutil" "os" - "path/filepath" - "strconv" - "forge.cadoles.com/pcaseiro/templatefile/pkg/templater" - "forge.cadoles.com/pcaseiro/templatefile/pkg/utils" + "forge.cadoles.com/pcaseiro/templatefile/pkg/configs" "github.com/alexflint/go-arg" - "github.com/zclconf/go-cty/cty" - ctyjson "github.com/zclconf/go-cty/cty/json" ) +func processGlobal(global configs.Service, templateDir string) error { + fmt.Printf("FIXME Global %v %s\n", global, templateDir) + return nil +} + +func processConfigFiles(tpls []configs.ConfigFile, variables map[string]interface{}, templateDir string) error { + values, err := json.Marshal(variables) + if err != nil { + return fmt.Errorf("Error unmarshaling values on template process; %v", err) + } + + for _, tpl := range tpls { + if err := tpl.Generate("/tmp/test", templateDir, values); err != nil { + return fmt.Errorf("Template %s generation failed with error %v", tpl.Source, err) + } + + } + return nil +} + +func processDaemons(daemon configs.SystemService) error { + return daemon.Manage() +} + +func processService(conf configs.Service, templateDir string) error { + err := processConfigFiles(conf.ConfigFiles, conf.Vars, templateDir) + if err != nil { + return fmt.Errorf("ProcessingTemplatesFailed with error: %v", err) + } + + err = processDaemons(conf.Daemon) + if err != nil { + return fmt.Errorf("Error managing service daemons: %v", err) + } + return nil +} + +func processConfig(confpath string, templateDir string) (configs.TemplaterConfig, error) { + var cnf configs.TemplaterConfig + + flContent, err := os.ReadFile(confpath) + if err != nil { + return cnf, fmt.Errorf("Confiuration read failed with error: %v", err) + } + + err = json.Unmarshal(flContent, &cnf) + if err != nil { + return cnf, fmt.Errorf("Processing config failed with error: %v", err) + } + + for name, svr := range cnf.Services { + if name == "Global" { + err = processGlobal(svr, templateDir) + if err != nil { + return cnf, fmt.Errorf("Error processing globals: %v", err) + } + } else { + if err = processService(svr, templateDir); err != nil { + return cnf, fmt.Errorf("Error processing service: %v", err) + } + } + } + + return cnf, nil + +} + func main() { var args struct { ConfigDirectory string `arg:"-c,--config-dir,env:CONFIG_DIR" help:"Configuration values directory path" default:"./data/config"` + ConfigFile string `arg:"-f,--config-file,env:CONFIG_FILE" help:"Configuration file to procedss"` 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 args.ConfigFile != "" { + _, err := processConfig(args.ConfigFile, args.TemplateDirectory) if err != nil { panic(err) } - - ctyType, err := ctyjson.ImpliedType(flContent) + } else { + files, err := ioutil.ReadDir(args.ConfigDirectory) 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) - } - } + for _, file := range files { + fname := fmt.Sprintf("%s/%s", args.ConfigDirectory, file.Name()) + processConfig(fname, args.TemplateDirectory) } } - } diff --git a/data/config/test-services.json b/data/config/test-services.json new file mode 100644 index 0000000..2821b4f --- /dev/null +++ b/data/config/test-services.json @@ -0,0 +1,92 @@ +{ + "Name": "LokiStack", + "Services": { + "Global": { + "Vars": { + "EnableLoki": true, + "EnableGrafana": false, + "EnablePrometheus" : false + }, + "ConfigFiles": [ + { + "destination": "/etc/hosts", + "source": "hosts.pktpl.hcl", + "mode": "600", + "owner": "root", + "group": "root" + } + ] + }, + "Loki": { + "EnabledBy": { "var": "EnableLoki", "value": true }, + "ConfigFiles": [ + { + "destination": "/etc/loki/loki-local-config.yaml", + "source": "loki-local-config.pktpl.hcl", + "mode": "600", + "owner": "loki", + "group": "grafana" + } + ], + "Vars": { + "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": "" + } + }, + "Daemon": { + "name": "loki", + "enabled": true + }, + "Users": { + "loki": { + "username": "loki", + "group": "grafana", + "home" : "/srv/loki", + "shell": "/bin/nologin" + } + } + }, + "Grafana": { + "EnabledBy": { "var": "EnableGrafana", "value": true }, + "ConfigFiles": [ + { + "destination": "/etc/grafana.ini", + "source": "grafana.ini.pktpl.hcl", + "mode": "700", + "owner": "grafana", + "group": "grafana" + } + ], + "Vars": { + "AuthEnabled": false, + "User": "toto", + "Group": "grafana" + }, + "Users": { + "grafana": { + "username": "grafana", + "group": "grafana", + "home": "/srv/grafana", + "shell": "/bin/nologin" + } + }, + "Daemon": { + "name": "grafana", + "enabled": true + } + } + } +} \ No newline at end of file diff --git a/data/templates/grafana.ini.pktpl.hcl b/data/templates/grafana.ini.pktpl.hcl new file mode 100644 index 0000000..bd5f3a4 --- /dev/null +++ b/data/templates/grafana.ini.pktpl.hcl @@ -0,0 +1,5 @@ +%{ if AuthEnabled ~} +auth_enabled: true +%{ else } +auth_enabled: false +%{ endif } diff --git a/pkg/configs/files.go b/pkg/configs/files.go new file mode 100644 index 0000000..b5ce526 --- /dev/null +++ b/pkg/configs/files.go @@ -0,0 +1,46 @@ +package configs + +import ( + "fmt" + "os" + "path/filepath" + "strconv" + + "forge.cadoles.com/pcaseiro/templatefile/pkg/templater" +) + +type ConfigFile struct { + Destination string `form:"destination" json:"destination"` + Source string `form:"source" json:"source"` + Mode string `form:"mod" json:"mode"` + Owner string `json:"owner"` + Group string `json:"group"` +} + +func (cf *ConfigFile) Generate(root string, templateDir string, values []byte) error { + dest := filepath.Join(root, cf.Destination) + source := filepath.Join(templateDir, cf.Source) + intMod, err := strconv.ParseInt(cf.Mode, 8, 64) + if err != nil { + return (err) + } + + fmt.Printf("Processing %s\n", source) + fileExtension := filepath.Ext(source) + if fileExtension == ".hcl" { + res, err := templater.ProcessHCLTemplate(source, values) + if err != nil { + return fmt.Errorf("Process templates failed with error: %v", err) + } + dirname := filepath.Dir(dest) + err = os.MkdirAll(dirname, os.FileMode(int(0700))) + if err != nil { + return fmt.Errorf("Process templates failed with error: %v", err) + } + err = os.WriteFile(dest, []byte(res), os.FileMode(intMod)) + if err != nil { + return fmt.Errorf("Process templates failed with error: %v", err) + } + } + return nil +} diff --git a/pkg/configs/main.go b/pkg/configs/main.go index 6dbc5b2..8289d6d 100644 --- a/pkg/configs/main.go +++ b/pkg/configs/main.go @@ -1,15 +1,26 @@ package configs -type ConfigFile struct { - Destination string `form:"destination" json:"destination"` - Source string `form:"source" json:"source"` - Mod string `form:"mod" json:"mod"` +type SimpleCondition struct { + Var string `json:"var"` + Value bool `json:"value"` +} + +type SystemUser struct { + UserName string `json:"username"` + Group string `json:"group"` + Home string `json:"home"` + Shell string `json:"shell"` +} + +type Service struct { + EnabledBy SimpleCondition `json:"EnabledBy"` + ConfigFiles []ConfigFile `json:"ConfigFiles"` + Vars map[string]interface{} `json:"Vars"` + Daemon SystemService `json:"Daemon"` + Users map[string]SystemUser `json:"Users"` } type TemplaterConfig struct { - Name string `json:"Name"` - Config struct { - ConfigFiles []ConfigFile `json:"ConfigFiles"` - Variables map[string]interface{} `form:"-" json:"-"` - } `json:"Config"` + Name string `json:"Name"` + Services map[string]Service `json:"Services"` } diff --git a/pkg/configs/system_services.go b/pkg/configs/system_services.go new file mode 100644 index 0000000..ad39cfa --- /dev/null +++ b/pkg/configs/system_services.go @@ -0,0 +1,27 @@ +package configs + +import "fmt" + +type SystemService struct { + Name string `json:"Name"` + Enabled bool `json:"Enabled"` +} + +func (sys *SystemService) Manage() error { + if sys.Enabled { + fmt.Printf("Processing Daemon %s", sys.Name) + } else { + fmt.Printf("Nothing to do for daemone %s\n", sys.Name) + } + return nil +} + +func (sys *SystemService) Start() error { + fmt.Printf("Starting %s\n", sys.Name) + return nil +} + +func (sys *SystemService) Stop() error { + fmt.Printf("Stoping %s\n", sys.Name) + return nil +} diff --git a/pkg/templater/main.go b/pkg/templater/main.go index c029811..42c1129 100644 --- a/pkg/templater/main.go +++ b/pkg/templater/main.go @@ -15,7 +15,7 @@ import ( "forge.cadoles.com/pcaseiro/templatefile/pkg/utils" ) -func ProcessGoTemplate(file string, config []byte) string { +func ProcessGoTemplate(file string, config []byte) (string, error) { // The JSON configuration var confData map[string]interface{} @@ -33,10 +33,10 @@ func ProcessGoTemplate(file string, config []byte) string { utils.CheckErr(tpl.Execute(&res, confData["Config"])) - return res.String() + return res.String(), nil } -func ProcessHCLTemplate(file string, config []byte) string { +func ProcessHCLTemplate(file string, config []byte) (string, error) { fct, err := os.ReadFile(file) utils.CheckErr(err) @@ -48,7 +48,7 @@ func ProcessHCLTemplate(file string, config []byte) string { var varsVal cty.Value ctyType, err := ctyjson.ImpliedType(config) if err != nil { - panic(err) + return "", err /* Maybe one day cexpr, diags := hclsyntax.ParseExpression(config, "", hcl.Pos{Line: 0, Column: 1}) if diags.HasErrors() { @@ -63,9 +63,8 @@ func ProcessHCLTemplate(file string, config []byte) string { utils.CheckErr(err) } - cnfVals := varsVal.AsValueMap() ctx := &hcl.EvalContext{ - Variables: cnfVals["Config"].AsValueMap(), + Variables: varsVal.AsValueMap(), } for n := range ctx.Variables { @@ -86,5 +85,5 @@ func ProcessHCLTemplate(file string, config []byte) string { panic(diags.Error()) } - return val.AsString() + return val.AsString(), nil }