Rework templating for bootstrap

This commit is contained in:
Tom Wiesing 2022-09-15 16:57:15 +02:00
parent 08d1840a63
commit 47aeb05c82
No known key found for this signature in database
4 changed files with 108 additions and 43 deletions

View file

@ -0,0 +1,91 @@
package config
import (
"bytes"
"io"
"path/filepath"
"reflect"
"github.com/FAU-CDI/wisski-distillery/internal/core"
"github.com/FAU-CDI/wisski-distillery/pkg/hostname"
"github.com/FAU-CDI/wisski-distillery/pkg/password"
"github.com/FAU-CDI/wisski-distillery/pkg/unpack"
_ "embed"
)
// Template is a template for the cofiguration file
type Template struct {
DeployRoot string `env:"DEPLOY_ROOT"`
DefaultDomain string `env:"DEFAULT_DOMAIN"`
SelfOverridesFile string `env:"SELF_OVERRIDES_FILE"`
AuthorizedKeys string `env:"AUTHORIZED_KEYS_FILE"`
TriplestoreAdminUser string `env:"GRAPHDB_ADMIN_USER"`
TriplestoreAdminPassword string `env:"GRAPHDB_ADMIN_PASSWORD"`
MysqlAdminUsername string `env:"MYSQL_ADMIN_USER"`
MysqlAdminPassword string `env:"MYSQL_ADMIN_PASSWORD"`
}
// SetDefaults sets defaults on the template
func (tpl *Template) SetDefaults() (err error) {
if tpl.DeployRoot == "" {
tpl.DeployRoot = core.BaseDirectoryDefault
}
if tpl.DefaultDomain == "" {
tpl.DefaultDomain = hostname.FQDN()
}
if tpl.SelfOverridesFile == "" {
tpl.SelfOverridesFile = filepath.Join(tpl.DeployRoot, core.OverridesJSON)
}
if tpl.AuthorizedKeys == "" {
tpl.AuthorizedKeys = filepath.Join(tpl.DeployRoot, core.AuthorizedKeys)
}
if tpl.TriplestoreAdminUser == "" {
tpl.TriplestoreAdminUser = "admin"
}
if tpl.TriplestoreAdminPassword == "" {
tpl.TriplestoreAdminPassword, err = password.Password(64)
if err != nil {
return err
}
}
if tpl.MysqlAdminUsername == "" {
tpl.MysqlAdminUsername = "admin"
}
if tpl.MysqlAdminPassword == "" {
tpl.MysqlAdminPassword, err = password.Password(64)
if err != nil {
return err
}
}
return nil
}
//go:embed config_template
var templateBytes []byte
// MarshalTo marshals this template into dst
func (tpl Template) MarshalTo(dst io.Writer) error {
tplVal := reflect.ValueOf(tpl)
tplType := reflect.TypeOf(tpl)
context := make(map[string]string, tplType.NumField())
for i := 0; i < tplType.NumField(); i++ {
field := tplType.Field(i)
key := field.Tag.Get("env")
value := tplVal.FieldByName(field.Name).String()
context[key] = value
}
return unpack.WriteTemplate(dst, context, bytes.NewReader(templateBytes))
}

View file

@ -15,10 +15,6 @@ const Executable = "wdcli"
// It should be located inside the deployment directory.
const ConfigFile = ".env"
// ConfigFileTemplate contains a template for a new configuration file
//go:embed bootstrap/env
var ConfigFileTemplate []byte
// OverridesJSON is the name of the json overrides file.
// It should be located inside the deployment directory.
const OverridesJSON = "overrides.json"