fix(systemd): include service user in service file generation

* Add ServiceUser field to Config struct for user specification.
* Update Generate and Install functions to accept service user.
* Modify tests to reflect changes in service user handling.
This commit is contained in:
Hein
2026-04-08 13:30:07 +02:00
parent e4d6f3a4a2
commit d8c90e4fff
5 changed files with 31 additions and 15 deletions

View File

@@ -31,7 +31,7 @@ func runInstall(cmd *cobra.Command, args []string) error {
return err return err
} }
prefix, suffix := cfg.Prefix, cfg.Suffix prefix, suffix, serviceUser := cfg.Prefix, cfg.Suffix, cfg.EffectiveServiceUser()
installed := 0 installed := 0
skipped := 0 skipped := 0
removed := 0 removed := 0
@@ -57,7 +57,7 @@ func runInstall(cmd *cobra.Command, args []string) error {
} }
if dryRun { if dryRun {
content, err := systemd.Generate(u, prefix, suffix) content, err := systemd.Generate(u, prefix, suffix, serviceUser)
if err != nil { if err != nil {
fmt.Printf(" ✗ %s: %v\n", u.Name, err) fmt.Printf(" ✗ %s: %v\n", u.Name, err)
continue continue
@@ -68,7 +68,7 @@ func runInstall(cmd *cobra.Command, args []string) error {
continue continue
} }
if err := systemd.Install(u, prefix, suffix); err != nil { if err := systemd.Install(u, prefix, suffix, serviceUser); err != nil {
fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err) fmt.Printf(" ✗ failed: %s: %v\n", u.Name, err)
} else { } else {
path, _ := systemd.UnitPath(u, prefix, suffix) path, _ := systemd.UnitPath(u, prefix, suffix)

View File

@@ -3,6 +3,7 @@ package config
import ( import (
"fmt" "fmt"
"os" "os"
"os/user"
"path/filepath" "path/filepath"
"gopkg.in/yaml.v3" "gopkg.in/yaml.v3"
@@ -24,9 +25,21 @@ type Unit struct {
// Config is the root config structure. // Config is the root config structure.
type Config struct { type Config struct {
Units []Unit `yaml:"units"` Units []Unit `yaml:"units"`
Prefix string `yaml:"prefix,omitempty"` // prepended to generated service name, e.g. "prod-" Prefix string `yaml:"prefix,omitempty"` // prepended to generated service name, e.g. "prod-"
Suffix string `yaml:"suffix,omitempty"` // appended to generated service name, e.g. "-svc" Suffix string `yaml:"suffix,omitempty"` // appended to generated service name, e.g. "-svc"
ServiceUser string `yaml:"service_user,omitempty"` // User= in [Service] section; defaults to "unitdore"
}
// EffectiveServiceUser returns the configured service user, or the current OS user if unset.
func (c *Config) EffectiveServiceUser() string {
if c.ServiceUser != "" {
return c.ServiceUser
}
if u, err := user.Current(); err == nil {
return u.Username
}
return "root"
} }
// Load reads and parses the config file at path. // Load reads and parses the config file at path.

View File

@@ -17,6 +17,7 @@ After=network.target
[Service] [Service]
Type=simple Type=simple
User={{.ServiceUser}}
ExecStart={{.ExecStart}} ExecStart={{.ExecStart}}
ExecStop={{.ExecStop}} ExecStop={{.ExecStop}}
Restart=on-failure Restart=on-failure
@@ -49,6 +50,7 @@ type templateData struct {
Unit config.Unit Unit config.Unit
ExecStart string ExecStart string
ExecStop string ExecStop string
ServiceUser string
} }
// ServiceName returns the systemd service name for a unit, with optional prefix/suffix. // ServiceName returns the systemd service name for a unit, with optional prefix/suffix.
@@ -57,7 +59,7 @@ func ServiceName(u config.Unit, prefix, suffix string) string {
} }
// Generate produces the .service file content for a unit. // Generate produces the .service file content for a unit.
func Generate(u config.Unit, prefix, suffix string) (string, error) { func Generate(u config.Unit, prefix, suffix, serviceUser string) (string, error) {
execStart, execStop := buildExecCommands(u) execStart, execStop := buildExecCommands(u)
data := templateData{ data := templateData{
@@ -65,6 +67,7 @@ func Generate(u config.Unit, prefix, suffix string) (string, error) {
Unit: u, Unit: u,
ExecStart: execStart, ExecStart: execStart,
ExecStop: execStop, ExecStop: execStop,
ServiceUser: serviceUser,
} }
tmplStr := systemUnitTemplate tmplStr := systemUnitTemplate

View File

@@ -35,7 +35,7 @@ func TestGenerate_SystemUnit(t *testing.T) {
Enabled: true, Enabled: true,
} }
content, err := Generate(u, "", "") content, err := Generate(u, "", "", "unitdore")
if err != nil { if err != nil {
t.Fatalf("Generate() error: %v", err) t.Fatalf("Generate() error: %v", err)
} }
@@ -70,7 +70,7 @@ func TestGenerate_UserUnit(t *testing.T) {
Enabled: true, Enabled: true,
} }
content, err := Generate(u, "", "") content, err := Generate(u, "", "", "unitdore")
if err != nil { if err != nil {
t.Fatalf("Generate() error: %v", err) t.Fatalf("Generate() error: %v", err)
} }
@@ -101,7 +101,7 @@ func TestGenerate_CustomCommand(t *testing.T) {
Enabled: true, Enabled: true,
} }
content, err := Generate(u, "", "") content, err := Generate(u, "", "", "unitdore")
if err != nil { if err != nil {
t.Fatalf("Generate() error: %v", err) t.Fatalf("Generate() error: %v", err)
} }
@@ -118,7 +118,7 @@ func TestGenerate_DockerRuntime(t *testing.T) {
Enabled: true, Enabled: true,
} }
content, err := Generate(u, "", "") content, err := Generate(u, "", "", "unitdore")
if err != nil { if err != nil {
t.Fatalf("Generate() error: %v", err) t.Fatalf("Generate() error: %v", err)
} }
@@ -138,7 +138,7 @@ func TestGenerate_UnknownRuntime(t *testing.T) {
Enabled: true, Enabled: true,
} }
content, err := Generate(u, "", "") content, err := Generate(u, "", "", "unitdore")
if err != nil { if err != nil {
t.Fatalf("Generate() should not error on unknown runtime: %v", err) t.Fatalf("Generate() should not error on unknown runtime: %v", err)
} }
@@ -155,7 +155,7 @@ func TestGenerate_WithPrefixSuffix(t *testing.T) {
Enabled: true, Enabled: true,
} }
content, err := Generate(u, "prod-", "-web") content, err := Generate(u, "prod-", "-web", "unitdore")
if err != nil { if err != nil {
t.Fatalf("Generate() error: %v", err) t.Fatalf("Generate() error: %v", err)
} }

View File

@@ -27,8 +27,8 @@ func UnitPath(u config.Unit, prefix, suffix string) (string, error) {
} }
// Install writes the .service file for a unit and reloads systemd. // Install writes the .service file for a unit and reloads systemd.
func Install(u config.Unit, prefix, suffix string) error { func Install(u config.Unit, prefix, suffix, serviceUser string) error {
content, err := Generate(u, prefix, suffix) content, err := Generate(u, prefix, suffix, serviceUser)
if err != nil { if err != nil {
return err return err
} }