Skip to content

Commit fe6d153

Browse files
Reduce complexity of install D-Bus function
Signed-off-by: Kate Goldenring <kate.goldenring@fermyon.com>
1 parent e44beb5 commit fe6d153

File tree

1 file changed

+27
-35
lines changed

1 file changed

+27
-35
lines changed

internal/containerd/install_dbus.go

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,9 @@ import (
77
"os/exec"
88
)
99

10-
// UsesSystemd checks if the system is using systemd
11-
// by running the "systemctl is-system-running" command.
12-
// NOTE: this limits support to systems using systemctl to manage systemd
10+
// UsesSystemd checks if the system is using systemd.
1311
func UsesSystemd() bool {
14-
// Check if is a systemd system
15-
cmd := nsenterCmd("systemctl", "is-system-running", "--quiet")
12+
cmd := nsenterCmd("systemctl", "list-units", "|", "grep", "-q", "containerd.service")
1613
if err := cmd.Run(); err != nil {
1714
slog.Info("Error with systemctl: %w\n", "error", err)
1815
return false
@@ -30,37 +27,32 @@ func InstallDbus() error {
3027
return nil
3128
}
3229
slog.Info("installing D-Bus")
33-
whichApt := nsenterCmd("which", "apt-get")
34-
whichYum := nsenterCmd("which", "yum")
35-
whichDnf := nsenterCmd("which", "dnf")
36-
whichApk := nsenterCmd("which", "apk")
37-
if err := whichApt.Run(); err == nil {
38-
cmd = nsenterCmd("apt-get", "update", "--yes")
39-
if err := cmd.Run(); err != nil {
40-
return fmt.Errorf("failed to update apt: %w", err)
41-
}
42-
cmd = nsenterCmd("apt-get", "install", "--yes", "dbus")
43-
if err := cmd.Run(); err != nil {
44-
return fmt.Errorf("failed to install D-Bus with apt: %w", err)
45-
}
46-
} else if err = whichDnf.Run(); err == nil {
47-
cmd = nsenterCmd("dnf", "install", "--yes", "dbus")
48-
if err := cmd.Run(); err != nil {
49-
return fmt.Errorf("failed to install D-Bus with dnf: %w", err)
50-
}
51-
} else if err = whichApk.Run(); err == nil {
52-
cmd = nsenterCmd("apk", "add", "dbus")
53-
if err := cmd.Run(); err != nil {
54-
return fmt.Errorf("failed to install D-Bus with apk: %w", err)
55-
}
56-
} else if err = whichYum.Run(); err == nil {
57-
cmd = nsenterCmd("yum", "install", "--yes", "dbus")
58-
if err := cmd.Run(); err != nil {
59-
return fmt.Errorf("failed to install D-Bus with yum: %w", err)
30+
31+
type pkgManager struct {
32+
name string
33+
check []string
34+
install []string
35+
}
36+
37+
managers := []pkgManager{
38+
{"apt-get", []string{"which", "apt-get"}, []string{"apt-get", "update", "--yes", "&&", "apt-get", "install", "--yes", "dbus"}},
39+
{"dnf", []string{"which", "dnf"}, []string{"dnf", "install", "--yes", "dbus"}},
40+
{"apk", []string{"which", "apk"}, []string{"apk", "add", "dbus"}},
41+
{"yum", []string{"which", "yum"}, []string{"yum", "install", "--yes", "dbus"}},
42+
}
43+
installed := false
44+
for _, mgr := range managers {
45+
if err := nsenterCmd(mgr.check...).Run(); err == nil {
46+
if err := nsenterCmd(mgr.install...).Run(); err != nil {
47+
return fmt.Errorf("failed to install D-Bus with %s: %w", mgr.name, err)
48+
}
49+
installed = true
50+
break
6051
}
61-
} else {
62-
slog.Info("WARNING: Could not install D-Bus. No supported package manager found.")
63-
return nil
52+
}
53+
54+
if !installed {
55+
return fmt.Errorf("could not install D-Bus as no supported package manager found")
6456
}
6557

6658
slog.Info("restarting D-Bus")

0 commit comments

Comments
 (0)