docs(README): update installation instructions for various distributions
This commit is contained in:
@@ -13,11 +13,11 @@ const systemUnitTemplate = `# /etc/systemd/system/{{.ServiceName}}
|
||||
|
||||
[Unit]
|
||||
Description=Unitdore: {{.Unit.Name}} ({{.Unit.Runtime}})
|
||||
After=network.target
|
||||
After=network.target{{with .RuntimeService}} {{.}}
|
||||
Requires={{.}}{{end}}
|
||||
|
||||
[Service]
|
||||
Type=simple
|
||||
User={{.ServiceUser}}
|
||||
ExecStart={{.ExecStart}}
|
||||
ExecStop={{.ExecStop}}
|
||||
Restart=on-failure
|
||||
@@ -46,11 +46,11 @@ WantedBy=default.target
|
||||
`
|
||||
|
||||
type templateData struct {
|
||||
ServiceName string
|
||||
Unit config.Unit
|
||||
ExecStart string
|
||||
ExecStop string
|
||||
ServiceUser string
|
||||
ServiceName string
|
||||
Unit config.Unit
|
||||
ExecStart string
|
||||
ExecStop string
|
||||
RuntimeService string
|
||||
}
|
||||
|
||||
// ServiceName returns the systemd service name for a unit, with optional prefix/suffix.
|
||||
@@ -59,15 +59,15 @@ func ServiceName(u config.Unit, prefix, suffix string) string {
|
||||
}
|
||||
|
||||
// Generate produces the .service file content for a unit.
|
||||
func Generate(u config.Unit, prefix, suffix, serviceUser string) (string, error) {
|
||||
func Generate(u config.Unit, prefix, suffix string) (string, error) {
|
||||
execStart, execStop := buildExecCommands(u)
|
||||
|
||||
data := templateData{
|
||||
ServiceName: ServiceName(u, prefix, suffix),
|
||||
Unit: u,
|
||||
ExecStart: execStart,
|
||||
ExecStop: execStop,
|
||||
ServiceUser: serviceUser,
|
||||
ServiceName: ServiceName(u, prefix, suffix),
|
||||
Unit: u,
|
||||
ExecStart: execStart,
|
||||
ExecStop: execStop,
|
||||
RuntimeService: runtimeService(u.Runtime),
|
||||
}
|
||||
|
||||
tmplStr := systemUnitTemplate
|
||||
@@ -87,6 +87,15 @@ func Generate(u config.Unit, prefix, suffix, serviceUser string) (string, error)
|
||||
return buf.String(), nil
|
||||
}
|
||||
|
||||
// runtimeService returns the systemd service name that must be running for the
|
||||
// given container runtime, or "" if none is required (e.g. daemonless podman).
|
||||
func runtimeService(runtime string) string {
|
||||
if runtime == "docker" {
|
||||
return "docker.service"
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func buildExecCommands(u config.Unit) (start, stop string) {
|
||||
// If a custom command is provided, use it directly
|
||||
if u.Command != "" {
|
||||
|
||||
@@ -35,7 +35,7 @@ func TestGenerate_SystemUnit(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "", "", "unitdore")
|
||||
content, err := Generate(u, "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error: %v", err)
|
||||
}
|
||||
@@ -59,6 +59,13 @@ func TestGenerate_SystemUnit(t *testing.T) {
|
||||
t.Errorf("Generate() missing %q in output:\n%s", check, content)
|
||||
}
|
||||
}
|
||||
|
||||
if strings.Contains(content, "User=") {
|
||||
t.Errorf("Generate() system unit should not contain User= directive:\n%s", content)
|
||||
}
|
||||
if strings.Contains(content, "Requires=") {
|
||||
t.Errorf("Generate() podman system unit should not contain Requires= (podman is daemonless):\n%s", content)
|
||||
}
|
||||
}
|
||||
|
||||
func TestGenerate_UserUnit(t *testing.T) {
|
||||
@@ -70,7 +77,7 @@ func TestGenerate_UserUnit(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "", "", "unitdore")
|
||||
content, err := Generate(u, "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error: %v", err)
|
||||
}
|
||||
@@ -101,7 +108,7 @@ func TestGenerate_CustomCommand(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "", "", "unitdore")
|
||||
content, err := Generate(u, "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error: %v", err)
|
||||
}
|
||||
@@ -118,16 +125,21 @@ func TestGenerate_DockerRuntime(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "", "", "unitdore")
|
||||
content, err := Generate(u, "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error: %v", err)
|
||||
}
|
||||
|
||||
if !strings.Contains(content, "ExecStart=/usr/bin/docker start redis") {
|
||||
t.Errorf("Generate() wrong ExecStart for docker:\n%s", content)
|
||||
checks := []string{
|
||||
"ExecStart=/usr/bin/docker start redis",
|
||||
"ExecStop=/usr/bin/docker stop redis",
|
||||
"After=network.target docker.service",
|
||||
"Requires=docker.service",
|
||||
}
|
||||
if !strings.Contains(content, "ExecStop=/usr/bin/docker stop redis") {
|
||||
t.Errorf("Generate() wrong ExecStop for docker:\n%s", content)
|
||||
for _, check := range checks {
|
||||
if !strings.Contains(content, check) {
|
||||
t.Errorf("Generate() docker runtime missing %q in output:\n%s", check, content)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -138,7 +150,7 @@ func TestGenerate_UnknownRuntime(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "", "", "unitdore")
|
||||
content, err := Generate(u, "", "")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() should not error on unknown runtime: %v", err)
|
||||
}
|
||||
@@ -155,7 +167,7 @@ func TestGenerate_WithPrefixSuffix(t *testing.T) {
|
||||
Enabled: true,
|
||||
}
|
||||
|
||||
content, err := Generate(u, "prod-", "-web", "unitdore")
|
||||
content, err := Generate(u, "prod-", "-web")
|
||||
if err != nil {
|
||||
t.Fatalf("Generate() error: %v", err)
|
||||
}
|
||||
|
||||
@@ -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.
|
||||
func Install(u config.Unit, prefix, suffix, serviceUser string) error {
|
||||
content, err := Generate(u, prefix, suffix, serviceUser)
|
||||
func Install(u config.Unit, prefix, suffix string) error {
|
||||
content, err := Generate(u, prefix, suffix)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user