This commit is contained in:
Philippe Caseiro 2022-06-23 18:03:09 +02:00
parent 784b6c59c8
commit 442bd04652
7 changed files with 273 additions and 76 deletions

View File

@ -1,88 +1,105 @@
package main package main
import ( import (
"encoding/json"
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os" "os"
"path/filepath"
"strconv"
"forge.cadoles.com/pcaseiro/templatefile/pkg/templater" "forge.cadoles.com/pcaseiro/templatefile/pkg/configs"
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils"
"github.com/alexflint/go-arg" "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() { func main() {
var args struct { var args struct {
ConfigDirectory string `arg:"-c,--config-dir,env:CONFIG_DIR" help:"Configuration values directory path" default:"./data/config"` 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"` TemplateDirectory string `arg:"-t,--template-dir,env:TEMPLATE_DIR" help:"Template directory path" default:"./data/templates"`
} }
arg.MustParse(&args) arg.MustParse(&args)
if args.ConfigFile != "" {
_, err := processConfig(args.ConfigFile, args.TemplateDirectory)
if err != nil {
panic(err)
}
} else {
files, err := ioutil.ReadDir(args.ConfigDirectory) files, err := ioutil.ReadDir(args.ConfigDirectory)
if err != nil { if err != nil {
panic(err) panic(err)
} }
for _, file := range files { for _, file := range files {
var varsVal cty.Value
fname := fmt.Sprintf("%s/%s", args.ConfigDirectory, file.Name()) fname := fmt.Sprintf("%s/%s", args.ConfigDirectory, file.Name())
flContent, err := os.ReadFile(fname) processConfig(fname, args.TemplateDirectory)
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

@ -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
}
}
}
}

View File

@ -0,0 +1,5 @@
%{ if AuthEnabled ~}
auth_enabled: true
%{ else }
auth_enabled: false
%{ endif }

46
pkg/configs/files.go Normal file
View File

@ -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
}

View File

@ -1,15 +1,26 @@
package configs package configs
type ConfigFile struct { type SimpleCondition struct {
Destination string `form:"destination" json:"destination"` Var string `json:"var"`
Source string `form:"source" json:"source"` Value bool `json:"value"`
Mod string `form:"mod" json:"mod"` }
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 { type TemplaterConfig struct {
Name string `json:"Name"` Name string `json:"Name"`
Config struct { Services map[string]Service `json:"Services"`
ConfigFiles []ConfigFile `json:"ConfigFiles"`
Variables map[string]interface{} `form:"-" json:"-"`
} `json:"Config"`
} }

View File

@ -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
}

View File

@ -15,7 +15,7 @@ import (
"forge.cadoles.com/pcaseiro/templatefile/pkg/utils" "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 // The JSON configuration
var confData map[string]interface{} var confData map[string]interface{}
@ -33,10 +33,10 @@ func ProcessGoTemplate(file string, config []byte) string {
utils.CheckErr(tpl.Execute(&res, confData["Config"])) 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) fct, err := os.ReadFile(file)
utils.CheckErr(err) utils.CheckErr(err)
@ -48,7 +48,7 @@ func ProcessHCLTemplate(file string, config []byte) string {
var varsVal cty.Value var varsVal cty.Value
ctyType, err := ctyjson.ImpliedType(config) ctyType, err := ctyjson.ImpliedType(config)
if err != nil { if err != nil {
panic(err) return "", err
/* Maybe one day /* Maybe one day
cexpr, diags := hclsyntax.ParseExpression(config, "", hcl.Pos{Line: 0, Column: 1}) cexpr, diags := hclsyntax.ParseExpression(config, "", hcl.Pos{Line: 0, Column: 1})
if diags.HasErrors() { if diags.HasErrors() {
@ -63,9 +63,8 @@ func ProcessHCLTemplate(file string, config []byte) string {
utils.CheckErr(err) utils.CheckErr(err)
} }
cnfVals := varsVal.AsValueMap()
ctx := &hcl.EvalContext{ ctx := &hcl.EvalContext{
Variables: cnfVals["Config"].AsValueMap(), Variables: varsVal.AsValueMap(),
} }
for n := range ctx.Variables { for n := range ctx.Variables {
@ -86,5 +85,5 @@ func ProcessHCLTemplate(file string, config []byte) string {
panic(diags.Error()) panic(diags.Error())
} }
return val.AsString() return val.AsString(), nil
} }