From 3d96fb050ddfba23c29cf5017149d518ae54abd3 Mon Sep 17 00:00:00 2001 From: dkeven <82354774+dkeven@users.noreply.github.com> Date: Fri, 13 Dec 2024 15:25:52 +0800 Subject: [PATCH] cherry-pick of #70: fix: install sudo if it's missing (#71) --- pkg/common/loader.go | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/pkg/common/loader.go b/pkg/common/loader.go index f229858..af66313 100755 --- a/pkg/common/loader.go +++ b/pkg/common/loader.go @@ -18,6 +18,7 @@ package common import ( "bufio" + "bytetrade.io/web3os/installer/pkg/core/util" "encoding/json" "fmt" "io/ioutil" @@ -198,6 +199,12 @@ func (d *DefaultLoader) Load() (*kubekeyapiv1alpha2.Cluster, error) { allInOne := &kubekeyapiv1alpha2.Cluster{} + if osType != Darwin && osType != Windows { + if err := installSUDOIfMissing(); err != nil { + return nil, err + } + } + if err := localSSH(osType); err != nil { return nil, err } @@ -468,6 +475,18 @@ func currentUser(osType string) (*user.User, error) { return u, nil } +func installSUDOIfMissing() error { + p, _ := util.GetCommand("sudo") + if p != "" { + return nil + } + output, err := exec.Command("/bin/sh", "-c", "apt install -y sudo").CombinedOutput() + if err != nil { + return errors.Wrapf(err, "failed to install the sudo command that's missing: %s", string(output)) + } + return nil +} + func localSSH(osType string) error { switch osType { case Windows: