Complete rewrite of the bootstraper tool

This commit is contained in:
Philippe Caseiro 2022-06-15 17:29:45 +02:00
parent 5085bd4d69
commit d5436077da
11 changed files with 348 additions and 28 deletions

View File

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

105
cmd/bootstraper/main.go Normal file
View File

@ -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("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)
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)
}
}
}

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

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

15
data/config/test2.json Normal file
View File

@ -0,0 +1,15 @@
{
"Name": "toto",
"Config": {
"ConfigFiles": [
{
"destination": "/etc/hosts",
"source": "hosts.pktpl.hcl",
"mod": "600"
}
],
"AuthEnabled": false,
"User": "toto",
"Group": "grafana"
}
}

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
}

26
pkg/configs/main.go Normal file
View File

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

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"
)
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
}