Fix profile updates and validate configuration before apply
This commit is contained in:
parent
b552ec49cb
commit
2bf14948d7
1 changed files with 372 additions and 12 deletions
|
|
@ -48,6 +48,11 @@ func (c config) unitProfile(unit string) string {
|
|||
return name
|
||||
}
|
||||
}
|
||||
for _, app := range keys(c.Shared) {
|
||||
if appMatches(app, unit) {
|
||||
return c.Shared[app]
|
||||
}
|
||||
}
|
||||
for _, name := range keys(c.Apps) {
|
||||
for _, app := range c.Apps[name] {
|
||||
if appMatches(app, unit) {
|
||||
|
|
@ -57,10 +62,7 @@ func (c config) unitProfile(unit string) string {
|
|||
}
|
||||
for _, name := range keys(c.Prefixes) {
|
||||
for _, prefix := range c.Prefixes[name] {
|
||||
parts := strings.Split(prefix, ".")
|
||||
stem := strings.Join(parts[:len(parts)-2], ".")
|
||||
kind := "." + parts[len(parts)-2]
|
||||
if strings.HasPrefix(unit, stem) && strings.HasSuffix(unit, kind) {
|
||||
if prefixMatches(prefix, unit) {
|
||||
return name
|
||||
}
|
||||
}
|
||||
|
|
@ -77,16 +79,51 @@ func (c config) unitProfile(unit string) string {
|
|||
return ""
|
||||
}
|
||||
|
||||
func (c config) activeProfile(u unit) string {
|
||||
if name := c.unitProfile(u.Name); name != "" {
|
||||
return name
|
||||
}
|
||||
if strings.HasPrefix(u.Name, "budget-") && strings.HasSuffix(u.Name, ".scope") {
|
||||
if name, ok := strings.CutSuffix(u.Description, " workload"); ok && name != "search" {
|
||||
return name
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func prefixMatches(directory, unit string) bool {
|
||||
for _, kind := range []string{".service", ".scope", ".slice"} {
|
||||
if stem, ok := strings.CutSuffix(directory, kind+".d"); ok {
|
||||
return strings.HasPrefix(unit, stem) && strings.HasSuffix(unit, kind)
|
||||
}
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func dropSection(directory string) (string, error) {
|
||||
for _, kind := range []struct{ suffix, section string }{
|
||||
{".service.d", "Service"}, {".scope.d", "Scope"}, {".slice.d", "Slice"},
|
||||
} {
|
||||
if stem, ok := strings.CutSuffix(directory, kind.suffix); ok && stem != "" {
|
||||
return kind.section, nil
|
||||
}
|
||||
}
|
||||
return "", fmt.Errorf("invalid drop-in directory %q", directory)
|
||||
}
|
||||
|
||||
func (c config) render() (map[string]string, error) {
|
||||
if err := c.validate(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
files := map[string]string{}
|
||||
add := func(directory, name, extra string) error {
|
||||
values, err := c.assignments(name)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
section := "Service"
|
||||
if strings.Contains(directory, ".scope.") {
|
||||
section = "Scope"
|
||||
section, err := dropSection(directory)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
files[directory+"/50-budget.conf"] = "[" + section + "]\n" + strings.Join(values, "\n") + "\n" + extra
|
||||
return nil
|
||||
|
|
|
|||
Loading…
Reference in a new issue