@@ -3,23 +3,8 @@ package containerd
33import (
44 "fmt"
55 "log/slog"
6- "os"
7- "os/exec"
86)
97
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
13- func UsesSystemd () bool {
14- // Check if is a systemd system
15- cmd := nsenterCmd ("systemctl" , "is-system-running" , "--quiet" )
16- if err := cmd .Run (); err != nil {
17- slog .Info ("Error with systemctl: %w\n " , "error" , err )
18- return false
19- }
20- return true
21- }
22-
238// InstallDbus checks if D-Bus service is installed and active. If not, installs D-Bus
249// and starts the service.
2510// NOTE: this limits support to systems using systemctl to manage systemd.
@@ -30,37 +15,38 @@ func InstallDbus() error {
3015 return nil
3116 }
3217 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 )
18+
19+ type pkgManager struct {
20+ name string
21+ check [] string
22+ update [] string
23+ install [] string
24+ }
25+
26+ managers := [] pkgManager {
27+ { "apt-get" , [] string { "which" , "apt-get" }, [] string { "apt-get" , "update" , "--yes" }, [] string { "apt-get" , " install" , "--yes" , "dbus" }},
28+ { "dnf" , [] string { "which" , "dnf" }, [] string {}, [] string { "dnf" , "install" , "--yes" , "dbus" }},
29+ { "apk" , [] string { "which" , "apk" }, [] string {}, [] string { "apk" , "add" , "dbus" }},
30+ { "yum" , [] string { "which" , "yum" }, [] string {}, [] string { "yum" , "install" , "--yes" , "dbus" }},
31+ }
32+ installed := false
33+ for _ , mgr := range managers {
34+ if err := nsenterCmd ( mgr . check ... ). Run (); err == nil {
35+ if len ( mgr . update ) != 0 {
36+ if err := nsenterCmd ( mgr . update ... ). Run (); err ! = nil {
37+ return fmt . Errorf ( "failed to update package manager %s: %w " , mgr . name , err )
38+ }
39+ }
40+ if err := nsenterCmd ( mgr . install ... ). Run (); err != nil {
41+ return fmt . Errorf ( "failed to install D-Bus with %s: %w" , mgr . name , err )
42+ }
43+ installed = true
44+ break
6045 }
61- } else {
62- slog .Info ("WARNING: Could not install D-Bus. No supported package manager found." )
63- return nil
46+ }
47+
48+ if ! installed {
49+ return fmt .Errorf ("could not install D-Bus as no supported package manager found" )
6450 }
6551
6652 slog .Info ("restarting D-Bus" )
@@ -71,8 +57,3 @@ func InstallDbus() error {
7157
7258 return nil
7359}
74-
75- func nsenterCmd (cmd ... string ) * exec.Cmd {
76- return exec .Command ("nsenter" ,
77- append ([]string {fmt .Sprintf ("-m/%s/proc/1/ns/mnt" , os .Getenv ("HOST_ROOT" )), "--" }, cmd ... )... ) // #nosec G204
78- }
0 commit comments