210 lines
5.3 KiB
Go
210 lines
5.3 KiB
Go
package data
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
|
|
"kra/internal/conf"
|
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
|
"google.golang.org/protobuf/proto"
|
|
"gopkg.in/yaml.v3"
|
|
)
|
|
|
|
func protoMap(message proto.Message) (map[string]any, error) {
|
|
raw, err := protojson.MarshalOptions{UseProtoNames: true}.Marshal(message)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var value map[string]any
|
|
if err = json.Unmarshal(raw, &value); err != nil {
|
|
return nil, err
|
|
}
|
|
delete(value, "config_path")
|
|
return value, nil
|
|
}
|
|
|
|
func setYAMLMapping(node *yaml.Node, key string, value any) error {
|
|
if node.Kind == yaml.DocumentNode {
|
|
node = node.Content[0]
|
|
}
|
|
if node.Kind != yaml.MappingNode {
|
|
return fmt.Errorf("configuration root is not a mapping")
|
|
}
|
|
raw, err := yaml.Marshal(value)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var replacement yaml.Node
|
|
if err = yaml.Unmarshal(raw, &replacement); err != nil {
|
|
return err
|
|
}
|
|
for i := 0; i < len(node.Content); i += 2 {
|
|
if node.Content[i].Value == key {
|
|
node.Content[i+1] = replacement.Content[0]
|
|
return nil
|
|
}
|
|
}
|
|
node.Content = append(node.Content, &yaml.Node{Kind: yaml.ScalarNode, Value: key}, replacement.Content[0])
|
|
return nil
|
|
}
|
|
|
|
func (d *Data) persistConfig() error {
|
|
dataConfig, adminConfig := d.runtime.Values()
|
|
return d.persistConfigValues(dataConfig, adminConfig)
|
|
}
|
|
|
|
func (d *Data) persistConfigValues(dataConfig *conf.Data, adminConfig *conf.AdminBackend) error {
|
|
d.configMu.Lock()
|
|
defer d.configMu.Unlock()
|
|
return d.persistConfigValuesLocked(dataConfig, adminConfig)
|
|
}
|
|
|
|
func (d *Data) persistConfigValuesLocked(dataConfig *conf.Data, adminConfig *conf.AdminBackend) error {
|
|
if adminConfig == nil || adminConfig.ConfigPath == "" {
|
|
return nil
|
|
}
|
|
configPath := adminConfig.ConfigPath
|
|
raw, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var document yaml.Node
|
|
if err = yaml.Unmarshal(raw, &document); err != nil {
|
|
return err
|
|
}
|
|
dataValue, err := protoMap(dataConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
adminValue, err := protoMap(adminConfig)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = setYAMLMapping(&document, "data", dataValue); err != nil {
|
|
return err
|
|
}
|
|
if err = setYAMLMapping(&document, "admin", adminValue); err != nil {
|
|
return err
|
|
}
|
|
output, err := yaml.Marshal(&document)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if err = os.MkdirAll(filepath.Dir(configPath), 0o755); err != nil {
|
|
return err
|
|
}
|
|
temporary, err := os.CreateTemp(filepath.Dir(configPath), ".kra-config-*.yaml")
|
|
if err != nil {
|
|
return err
|
|
}
|
|
tempName := temporary.Name()
|
|
defer os.Remove(tempName)
|
|
if _, err = temporary.Write(output); err != nil {
|
|
_ = temporary.Close()
|
|
return err
|
|
}
|
|
if err = temporary.Chmod(0o600); err != nil {
|
|
_ = temporary.Close()
|
|
return err
|
|
}
|
|
if err = temporary.Close(); err != nil {
|
|
return err
|
|
}
|
|
return os.Rename(tempName, configPath)
|
|
}
|
|
|
|
func (d *Data) reloadConfig(ctx context.Context) error {
|
|
d.configMu.Lock()
|
|
defer d.configMu.Unlock()
|
|
configPath := d.runtime.ConfigPath()
|
|
if configPath == "" {
|
|
return fmt.Errorf("configuration path is not set")
|
|
}
|
|
next, err := readBootstrap(configPath)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if next.Data == nil || next.Data.Database == nil || next.Admin == nil {
|
|
return fmt.Errorf("data.database and admin configuration are required")
|
|
}
|
|
next.Admin.ConfigPath = configPath
|
|
candidateDB, err := openDatabase(next.Data.Database, false, "")
|
|
if err != nil {
|
|
return fmt.Errorf("reload database: %w", err)
|
|
}
|
|
closeCandidate := true
|
|
defer func() {
|
|
if closeCandidate {
|
|
if sqlDB, closeErr := candidateDB.DB(); closeErr == nil {
|
|
_ = sqlDB.Close()
|
|
}
|
|
}
|
|
}()
|
|
if sqlDB, dbErr := candidateDB.DB(); dbErr != nil {
|
|
return dbErr
|
|
} else if err = sqlDB.PingContext(ctx); err != nil {
|
|
return fmt.Errorf("reload database: %w", err)
|
|
}
|
|
if next.Admin.System == nil || !next.Admin.System.DisableAutoMigrate {
|
|
if err = migrateAll(candidateDB.WithContext(ctx)); err != nil {
|
|
return fmt.Errorf("reload database migrations: %w", err)
|
|
}
|
|
}
|
|
candidateStorage, err := buildFileStorage(next.Admin)
|
|
if err != nil {
|
|
return fmt.Errorf("reload storage: %w", err)
|
|
}
|
|
useRedis := next.Admin.System != nil && next.Admin.System.UseRedis
|
|
candidateRedis := openRedis(next.Data.Redis, useRedis)
|
|
useMongo := next.Admin.System != nil && next.Admin.System.UseMongo
|
|
candidateMongo, mongoErr := openMongo(next.Data.Mongo, useMongo)
|
|
mongoAccepted := false
|
|
defer func() {
|
|
if !mongoAccepted && candidateMongo != nil {
|
|
_ = candidateMongo.Disconnect(context.Background())
|
|
}
|
|
}()
|
|
candidateDBList, err := openDatabaseList(next.Data.DatabaseList)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
|
|
d.gormDB.replace(candidateDB)
|
|
d.replaceDatabaseList(candidateDBList)
|
|
d.redis.replace(candidateRedis)
|
|
if mongoErr == nil {
|
|
d.mongo.replace(candidateMongo)
|
|
mongoAccepted = true
|
|
}
|
|
d.runtime.Replace(next.Data, next.Admin)
|
|
if d.storage != nil {
|
|
d.storage.replace(candidateStorage)
|
|
}
|
|
closeCandidate = false
|
|
return nil
|
|
}
|
|
|
|
func readBootstrap(configPath string) (*conf.Bootstrap, error) {
|
|
raw, err := os.ReadFile(configPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var plain map[string]any
|
|
if err = yaml.Unmarshal(raw, &plain); err != nil {
|
|
return nil, err
|
|
}
|
|
encoded, err := json.Marshal(plain)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var next conf.Bootstrap
|
|
if err = (protojson.UnmarshalOptions{DiscardUnknown: true}).Unmarshal(encoded, &next); err != nil {
|
|
return nil, err
|
|
}
|
|
return &next, nil
|
|
}
|