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..a5fa738 --- /dev/null +++ b/cmd/bootstraper/main.go @@ -0,0 +1,105 @@ +package main + +import ( + "encoding/json" + "fmt" + "io/ioutil" + "os" + + "forge.cadoles.com/pcaseiro/templatefile/pkg/configs" + "github.com/alexflint/go-arg" +) + +func processGlobal(global configs.Service, templateDir string) error { + fmt.Printf("\nFIXME 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) + + if args.ConfigFile != "" { + _, err := processConfig(args.ConfigFile, args.TemplateDirectory) + if err != nil { + panic(err) + } + } else { + files, err := ioutil.ReadDir(args.ConfigDirectory) + 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/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-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/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/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 new file mode 100644 index 0000000..8289d6d --- /dev/null +++ b/pkg/configs/main.go @@ -0,0 +1,26 @@ +package configs + +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"` + 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 f1fba18..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{} @@ -31,12 +31,12 @@ 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() + 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() { @@ -85,5 +85,5 @@ func ProcessHCLTemplate(file string, config []byte) string { panic(diags.Error()) } - return val.AsString() + return val.AsString(), nil }