@@ -7,12 +7,9 @@ import (
7
7
"os/exec"
8
8
)
9
9
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.
13
11
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" )
16
13
if err := cmd .Run (); err != nil {
17
14
slog .Info ("Error with systemctl: %w\n " , "error" , err )
18
15
return false
@@ -30,37 +27,32 @@ func InstallDbus() error {
30
27
return nil
31
28
}
32
29
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
60
51
}
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" )
64
56
}
65
57
66
58
slog .Info ("restarting D-Bus" )
0 commit comments