11package utils
22
33import (
4+ "context"
45 "encoding/json"
56 "errors"
67 "fmt"
8+ "os"
79 "os/exec"
810 "strings"
911
12+ "github.com/containers/podman/v5/pkg/bindings"
1013 "github.com/containers/podman/v5/pkg/machine"
1114 "github.com/containers/podman/v5/pkg/machine/define"
1215 "github.com/containers/podman/v5/pkg/machine/env"
1316 "github.com/containers/podman/v5/pkg/machine/provider"
1417 "github.com/containers/podman/v5/pkg/machine/vmconfigs"
1518)
1619
17- type MachineInfo struct {
18- PodmanSocket string
20+ type MachineContext struct {
21+ Ctx context. Context
1922 SSHIdentityPath string
20- Rootful bool
2123}
2224
23- func GetMachineInfo () (* MachineInfo , error ) {
24- minfo , err := getMachineInfo ()
25+ type machineInfo struct {
26+ podmanSocket string
27+ sshIdentityPath string
28+ rootful bool
29+ }
30+
31+ func GetMachineContext () (* MachineContext , error ) {
32+ //podman machine connection
33+ machineInfo , err := getMachineInfo ()
34+ if err != nil {
35+ return nil , fmt .Errorf ("unable to get podman machine info: %w" , err )
36+ }
37+
38+ if machineInfo == nil {
39+ return nil , errors .New ("rootful podman machine is required, please run 'podman machine init --rootful'" )
40+ }
41+
42+ if ! machineInfo .rootful {
43+ return nil , errors .New ("rootful podman machine is required, please run 'podman machine set --rootful'" )
44+ }
45+
46+ if _ , err := os .Stat (machineInfo .podmanSocket ); err != nil {
47+ return nil , fmt .Errorf ("podman machine socket is missing: %w" , err )
48+ }
49+
50+ ctx , err := bindings .NewConnectionWithIdentity (
51+ context .Background (),
52+ fmt .Sprintf ("unix://%s" , machineInfo .podmanSocket ),
53+ machineInfo .sshIdentityPath ,
54+ true )
55+ if err != nil {
56+ return nil , fmt .Errorf ("failed to connect to the podman socket: %w" , err )
57+ }
58+
59+ mc := & MachineContext {
60+ Ctx : ctx ,
61+ SSHIdentityPath : machineInfo .sshIdentityPath ,
62+ }
63+ return mc , nil
64+ }
65+
66+ func getMachineInfo () (* machineInfo , error ) {
67+ minfo , err := getPv5MachineInfo ()
2568 if err != nil {
2669 var errIncompatibleMachineConfig * define.ErrIncompatibleMachineConfig
2770 var errVMDoesNotExist * define.ErrVMDoesNotExist
@@ -39,7 +82,7 @@ func GetMachineInfo() (*MachineInfo, error) {
3982}
4083
4184// Get podman v5 machine info
42- func getMachineInfo () (* MachineInfo , error ) {
85+ func getPv5MachineInfo () (* machineInfo , error ) {
4386 prov , err := provider .Get ()
4487 if err != nil {
4588 return nil , fmt .Errorf ("getting podman machine provider: %w" , err )
@@ -60,16 +103,16 @@ func getMachineInfo() (*MachineInfo, error) {
60103 return nil , fmt .Errorf ("getting podman machine connection info: %w" , err )
61104 }
62105
63- pmi := MachineInfo {
64- PodmanSocket : podmanSocket .GetPath (),
65- SSHIdentityPath : pm .SSH .IdentityPath ,
66- Rootful : pm .HostUser .Rootful ,
106+ pmi := machineInfo {
107+ podmanSocket : podmanSocket .GetPath (),
108+ sshIdentityPath : pm .SSH .IdentityPath ,
109+ rootful : pm .HostUser .Rootful ,
67110 }
68111 return & pmi , nil
69112}
70113
71114// Just to support podman v4.9, it will be removed in the future
72- func getPv4MachineInfo () (* MachineInfo , error ) {
115+ func getPv4MachineInfo () (* machineInfo , error ) {
73116 //check if a default podman machine exists
74117 listCmd := exec .Command ("podman" , "machine" , "list" , "--format" , "json" )
75118 var listCmdOutput strings.Builder
@@ -135,9 +178,9 @@ func getPv4MachineInfo() (*MachineInfo, error) {
135178 return nil , errors .New ("no podman machine found" )
136179 }
137180
138- return & MachineInfo {
139- PodmanSocket : machineInspect [0 ].ConnectionInfo .PodmanSocket .Path ,
140- SSHIdentityPath : machineInspect [0 ].SSHConfig .IdentityPath ,
141- Rootful : machineInspect [0 ].Rootful ,
181+ return & machineInfo {
182+ podmanSocket : machineInspect [0 ].ConnectionInfo .PodmanSocket .Path ,
183+ sshIdentityPath : machineInspect [0 ].SSHConfig .IdentityPath ,
184+ rootful : machineInspect [0 ].Rootful ,
142185 }, nil
143186}
0 commit comments