config: Properly use yaml template

This commit is contained in:
Tom Wiesing 2023-02-25 20:26:54 +01:00
parent 945329a080
commit 30c25b8e2a
No known key found for this signature in database
9 changed files with 168 additions and 78 deletions

View file

@ -9,6 +9,10 @@ import (
"time"
"github.com/FAU-CDI/wisski-distillery/pkg/pools"
"github.com/tkw1536/pkglib/yamlx"
"gopkg.in/yaml.v3"
_ "embed"
)
// Config represents the configuration of a WissKI Distillery.
@ -41,12 +45,47 @@ type Config struct {
SessionSecret string `yaml:"session_secret" default:"" validate:"nonempty"`
// interval to trigger distillery cron tasks in
CronInterval time.Duration `env:"cron_interval" default:"10m" validate:"duration"`
CronInterval time.Duration `yaml:"cron_interval" default:"10m" validate:"duration"`
// ConfigPath is the path this configuration was loaded from (if any)
ConfigPath string `yaml:"-"`
}
//go:embed config.yml
var configBytes []byte
// Marshal marshals this configuration in nicely formatted form.
// Where possible, this will provided yaml comments.
//
// Previous may optionally provide the bytes of a previous configuration file to replace settings in.
// The previous yaml file must be a valid configuration yaml, meaning all fields should be set.
// When previous is of length 0, the default configuration yaml will be used instead.
func Marshal(config *Config, previous []byte) ([]byte, error) {
if len(previous) == 0 {
previous = configBytes
}
// load the template yaml
template := new(yaml.Node)
if err := yaml.Unmarshal(previous, template); err != nil {
return nil, err
}
// load the config yaml
cfg, err := yamlx.Marshal(config)
if err != nil {
return nil, err
}
// transplant the configuration yaml into the template
if err := yamlx.Transplant(template, cfg); err != nil {
return nil, err
}
// marshal it again as a set of bytes
return yaml.Marshal(template)
}
// CSRFSecret return the csrfSecret derived from the session secret
func (config *Config) CSRFSecret() []byte {
// take the hash of the secret

View file

@ -1,17 +1,19 @@
paths:
# Several files are required to manage the system
# On top of this all real-system space will be created under this directory.
root: ${DEPLOY_ROOT}
# A WissKI Distillery needs to store a lot of data on disk.
# This setting defines a root folder all of these will be placed in.
# On top of this all real-system space will be created under this directory
root: ""
# You can override individual URLS in the homepage.
# Do this by adding URLs (without trailing '/'s) into a JSON file.
# This is the path to that file.
overrides: ${SELF_OVERRIDES_FILE}
# Individual paths on the root domain can be overwritten.
# This can be achieved by adding URLs (without trailing '/'s) into a JSON file.
# This setting defines the path to that file.
overrides: ""
# You can block specific prefixes within Triplestore from showing up in the global resolver.
# Do this by adding one prefix per line in this file.
# Lines starting with '#' and blank lines are ignored.
blocks: ${SELF_RESOLVER_BLOCK_FILE}
# This setting defines the path to that file.
blocks: ""
http:
# Each created Drupal Instance corresponds to a single domain name.
@ -30,7 +32,8 @@ http:
# By default, the default domain redirects to the distillery repository.
# If you want to change this, set an alternate domain name here.
home: ""
theme:
home: ""
docker:
# The name of the (global) docker network to run the distillery services in.

View file

@ -8,7 +8,7 @@ type DatabaseConfig struct {
// Prefix for new users and data setss
UserPrefix string `yaml:"user_prefix" default:"wisski-distillery-" validate:"slug"`
DataPrefix string `yaml:"fragment_prefix" default:"wisski-distillery-" validate:"slug"`
DataPrefix string `yaml:"data_prefix" default:"wisski-distillery-" validate:"slug"`
}
type SQLConfig struct {

View file

@ -21,7 +21,12 @@ func (config *Config) Unmarshal(env environment.Environment, src io.Reader) erro
}
}
// do the validator
// TODO: should this be done seperatly?
return config.Validate(env)
}
// Validate validates this configuration file and sets appropriate defaults
func (config *Config) Validate(env environment.Environment) error {
return validator.Validate(config, validators.New(env))
}

View file

@ -1,19 +1,13 @@
package config
import (
"bytes"
"io"
"path/filepath"
"reflect"
"time"
"github.com/FAU-CDI/wisski-distillery/internal/bootstrap"
"github.com/FAU-CDI/wisski-distillery/pkg/environment"
"github.com/FAU-CDI/wisski-distillery/pkg/hostname"
"github.com/FAU-CDI/wisski-distillery/pkg/password"
"github.com/FAU-CDI/wisski-distillery/pkg/unpack"
"gopkg.in/yaml.v3"
_ "embed"
)
// Template is a template for the configuration file
@ -88,28 +82,47 @@ func (tpl *Template) SetDefaults(env environment.Environment) (err error) {
return nil
}
//go:embed config_template.yml
var templateBytes []byte
// Generate generates a configuration file for this configuration
func (tpl Template) Generate() Config {
return Config{
Paths: PathsConfig{
Root: tpl.DeployRoot,
OverridesJSON: tpl.SelfOverridesFile,
ResolverBlocks: tpl.SelfResolverBlockFile,
},
HTTP: HTTPConfig{
PrimaryDomain: tpl.DefaultDomain,
ExtraDomains: []string{},
},
Docker: DockerConfig{
tpl.DockerNetworkName,
},
SQL: SQLConfig{
DatabaseConfig: DatabaseConfig{
AdminUsername: tpl.MysqlAdminUsername,
AdminPassword: tpl.MysqlAdminPassword,
// MarshalTo marshals this template into dst
func (tpl Template) MarshalTo(dst io.Writer) error {
tplVal := reflect.ValueOf(tpl)
tplType := reflect.TypeOf(tpl)
UserPrefix: "mysql-factory-",
DataPrefix: "mysql-factory-",
},
context := make(map[string]string, tplType.NumField())
for i := 0; i < tplType.NumField(); i++ {
field := tplType.Field(i)
Database: "distillery",
},
TS: TSConfig{
DatabaseConfig: DatabaseConfig{
AdminUsername: tpl.TriplestoreAdminUser,
AdminPassword: tpl.TriplestoreAdminPassword,
key := field.Tag.Get("env")
value := tplVal.FieldByName(field.Name).Interface()
UserPrefix: "graphdb-factory-",
DataPrefix: "graphdb-factory-",
},
},
MaxBackupAge: 30 * 24 * time.Hour, // 1 month
PasswordLength: 64,
bytes, err := yaml.Marshal(value)
if err != nil {
return err
}
context[key] = string(bytes)
PublicSSHPort: 2222,
SessionSecret: tpl.SessionSecret,
CronInterval: 10 * time.Minute,
}
// TODO: CONFIG: Update template writing
return unpack.WriteTemplate(dst, context, bytes.NewReader(templateBytes))
}