From 1d8a34a3b627a5310fa74b9d76ab9f68d2eb0c97 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Fri, 14 Jul 2023 10:42:50 +0800 Subject: [PATCH] fix(host): find python path on guestman init Signed-off-by: wanyaoqi --- pkg/hostman/guestman/guestman.go | 35 +++++++++++++++++++++++++++++++- pkg/hostman/guestman/qemu-kvm.go | 2 +- pkg/hostman/host_services.go | 3 +++ 3 files changed, 38 insertions(+), 2 deletions(-) diff --git a/pkg/hostman/guestman/guestman.go b/pkg/hostman/guestman/guestman.go index b53bd0e782..1305ce3f29 100644 --- a/pkg/hostman/guestman/guestman.go +++ b/pkg/hostman/guestman/guestman.go @@ -15,6 +15,7 @@ package guestman import ( + "bytes" "context" "fmt" "io/ioutil" @@ -94,7 +95,8 @@ type SGuestManager struct { qemuMachineCpuMax map[string]uint qemuMaxMem int - cpuSet *CpuSetCounter + cpuSet *CpuSetCounter + pythonPath string } func NewGuestManager(host hostutils.IHost, serversPath string) *SGuestManager { @@ -162,6 +164,37 @@ func (m *SGuestManager) InitQemuMaxMems(maxMems uint) { } } +func (m *SGuestManager) InitPythonPath() error { + defer func() { + log.Infof("Python path %s", m.pythonPath) + }() + out, err := procutils.NewRemoteCommandAsFarAsPossible("which", "python").Output() + if err == nil { + m.pythonPath = string(bytes.TrimSpace(out)) + return nil + } + log.Infof("No python found %s: %s", out, err) + + out, err = procutils.NewRemoteCommandAsFarAsPossible("which", "python3").Output() + if err == nil { + m.pythonPath = string(bytes.TrimSpace(out)) + return nil + } + log.Infof("No python3 found %s: %s", out, err) + + out, err = procutils.NewRemoteCommandAsFarAsPossible("which", "python2").Output() + if err == nil { + m.pythonPath = string(bytes.TrimSpace(out)) + return nil + } + log.Infof("No python2 found %s: %s", out, err) + return errors.Errorf("No python/python2/python3 found in PATH") +} + +func (m *SGuestManager) getPythonPath() string { + return m.pythonPath +} + func (m *SGuestManager) QemuLogDir() string { return path.Join(m.ServersPath, "logs") } diff --git a/pkg/hostman/guestman/qemu-kvm.go b/pkg/hostman/guestman/qemu-kvm.go index 405e3c3a16..a243515d4a 100644 --- a/pkg/hostman/guestman/qemu-kvm.go +++ b/pkg/hostman/guestman/qemu-kvm.go @@ -1756,7 +1756,7 @@ func (s *SKVMGuestInstance) pyLauncherPath() string { } func (s *SKVMGuestInstance) scriptStart(ctx context.Context) error { - output, err := procutils.NewRemoteCommandAsFarAsPossible("python", s.pyLauncherPath()).Output() + output, err := procutils.NewRemoteCommandAsFarAsPossible(s.manager.getPythonPath(), s.pyLauncherPath()).Output() if err != nil { return fmt.Errorf("Start VM Failed %s %s", output, err) } diff --git a/pkg/hostman/host_services.go b/pkg/hostman/host_services.go index 7fb8232ffa..aba27c43be 100644 --- a/pkg/hostman/host_services.go +++ b/pkg/hostman/host_services.go @@ -95,6 +95,9 @@ func (host *SHostService) RunService() { hostInstance.GetQemuMachineInfoList(), hostInstance.GetKVMMaxCpus(), ) guestman.GetGuestManager().InitQemuMaxMems(uint(hostInstance.GetMemoryTotal())) + if err := guestman.GetGuestManager().InitPythonPath(); err != nil { + log.Fatalf("Guest manager init python path %s", err) + } hostInstance.StartRegister(2, func() { guestChan = guestman.GetGuestManager().Bootstrap()