@@ -11,6 +11,7 @@ import (
1111 "os"
1212 "os/exec"
1313 "path/filepath"
14+ "sync"
1415
1516 "bauklotze/pkg/machine/define"
1617 "bauklotze/pkg/machine/fs"
@@ -29,7 +30,9 @@ type VMState struct {
2930}
3031
3132var (
32- Workspace string
33+ Workspace string
34+ binDir string
35+ binDirLocker sync.Mutex
3336)
3437
3538type VMProvider interface { //nolint:interfacebloat
@@ -131,9 +134,6 @@ type MachineConfig struct {
131134 // RestAPISocks is the socks for rest api, it is used by appliance to connect to query the status of vm
132135 // exec cmdline in vm etc...
133136 RestAPISocks string `json:"restAPISocks" validate:"required"`
134- KrunKitBin string `json:"krunKitBin" validate:"required"`
135- VFKitBin string `json:"vfKitBin" validate:"required"`
136- GVProxyBin string `json:"gvProxyBin" validate:"required"`
137137}
138138
139139type SSHAuthSocks struct {
@@ -267,3 +267,53 @@ func (mc *MachineConfig) Write() error {
267267 }
268268 return ioutils .AtomicWriteFile (mc .ConfigFile , b , define .DefaultFilePerm ) //nolint:wrapcheck
269269}
270+
271+ // GetLocationDir return the installation dir of ovm
272+ func (mc * MachineConfig ) getBinDir () (string , error ) {
273+ binDirLocker .Lock ()
274+ defer binDirLocker .Unlock ()
275+
276+ if binDir != "" {
277+ return binDir , nil
278+ }
279+
280+ execPath , err := os .Executable ()
281+ if err != nil {
282+ return "" , fmt .Errorf ("unable to get executable path: %w" , err )
283+ }
284+
285+ execPath , err = filepath .EvalSymlinks (execPath )
286+ if err != nil {
287+ return "" , fmt .Errorf ("unable to eval symlinks: %w" , err )
288+ }
289+ binDir = filepath .Dir (filepath .Dir (execPath ))
290+
291+ return binDir , nil
292+ }
293+
294+ func (mc * MachineConfig ) GetKrunkitBin () (string , error ) {
295+ dir , err := mc .getBinDir ()
296+ if err != nil {
297+ return "" , fmt .Errorf ("get bin dir err: %w" , err )
298+ }
299+
300+ return filepath .Join (dir , define .Libexec , define .KrunkitBinaryName ), nil
301+ }
302+
303+ func (mc * MachineConfig ) GetVfkitBin () (string , error ) {
304+ dir , err := mc .getBinDir ()
305+ if err != nil {
306+ return "" , fmt .Errorf ("get bin dir err: %w" , err )
307+ }
308+
309+ return filepath .Join (dir , define .Libexec , define .VfkitBinaryName ), nil
310+ }
311+
312+ func (mc * MachineConfig ) GetGVProxyBin () (string , error ) {
313+ dir , err := mc .getBinDir ()
314+ if err != nil {
315+ return "" , fmt .Errorf ("get bin dir err: %w" , err )
316+ }
317+
318+ return filepath .Join (dir , define .Libexec , define .GvProxyBinaryName ), nil
319+ }
0 commit comments