11package utils
22
33import (
4+ "context"
45 "encoding/json"
56 "errors"
67 "fmt"
8+ "github.com/containers/podman/v5/pkg/bindings/images"
9+ "github.com/containers/podman/v5/pkg/domain/entities/types"
10+ "os"
711 "os/exec"
812 "strings"
913
14+ "github.com/containers/podman/v5/pkg/bindings"
1015 "github.com/containers/podman/v5/pkg/machine"
1116 "github.com/containers/podman/v5/pkg/machine/define"
1217 "github.com/containers/podman/v5/pkg/machine/env"
1318 "github.com/containers/podman/v5/pkg/machine/provider"
1419 "github.com/containers/podman/v5/pkg/machine/vmconfigs"
1520)
1621
17- type MachineInfo struct {
18- PodmanSocket string
22+ type MachineContext struct {
23+ Ctx context. Context
1924 SSHIdentityPath string
20- Rootful bool
2125}
2226
23- func GetMachineInfo () (* MachineInfo , error ) {
24- minfo , err := getMachineInfo ()
27+ type machineInfo struct {
28+ podmanSocket string
29+ sshIdentityPath string
30+ rootful bool
31+ }
32+
33+ // PullAndInspect inpects the image, pulling in if the image if required
34+ func PullAndInspect (ctx context.Context , imageNameOrId string ) (* types.ImageInspectReport , error ) {
35+ pullPolicy := "missing"
36+ _ , err := images .Pull (ctx , imageNameOrId , & images.PullOptions {Policy : & pullPolicy })
37+ if err != nil {
38+ return nil , fmt .Errorf ("failed to pull image: %w" , err )
39+ }
40+
41+ imageInfo , err := images .GetImage (ctx , imageNameOrId , & images.GetOptions {})
42+ if err != nil {
43+ return nil , fmt .Errorf ("failed to inspect image: %w" , err )
44+ }
45+
46+ return imageInfo , nil
47+ }
48+
49+ func GetMachineContext () (* MachineContext , error ) {
50+ //podman machine connection
51+ machineInfo , err := getMachineInfo ()
52+ if err != nil {
53+ return nil , fmt .Errorf ("unable to get podman machine info: %w" , err )
54+ }
55+
56+ if machineInfo == nil {
57+ return nil , errors .New ("rootful podman machine is required, please run 'podman machine init --rootful'" )
58+ }
59+
60+ if ! machineInfo .rootful {
61+ return nil , errors .New ("rootful podman machine is required, please run 'podman machine set --rootful'" )
62+ }
63+
64+ if _ , err := os .Stat (machineInfo .podmanSocket ); err != nil {
65+ return nil , fmt .Errorf ("podman machine socket is missing: %w" , err )
66+ }
67+
68+ ctx , err := bindings .NewConnectionWithIdentity (
69+ context .Background (),
70+ fmt .Sprintf ("unix://%s" , machineInfo .podmanSocket ),
71+ machineInfo .sshIdentityPath ,
72+ true )
73+ if err != nil {
74+ return nil , fmt .Errorf ("failed to connect to the podman socket: %w" , err )
75+ }
76+
77+ mc := & MachineContext {
78+ Ctx : ctx ,
79+ SSHIdentityPath : machineInfo .sshIdentityPath ,
80+ }
81+ return mc , nil
82+ }
83+
84+ func getMachineInfo () (* machineInfo , error ) {
85+ minfo , err := getPv5MachineInfo ()
2586 if err != nil {
2687 var errIncompatibleMachineConfig * define.ErrIncompatibleMachineConfig
2788 var errVMDoesNotExist * define.ErrVMDoesNotExist
@@ -39,7 +100,7 @@ func GetMachineInfo() (*MachineInfo, error) {
39100}
40101
41102// Get podman v5 machine info
42- func getMachineInfo () (* MachineInfo , error ) {
103+ func getPv5MachineInfo () (* machineInfo , error ) {
43104 prov , err := provider .Get ()
44105 if err != nil {
45106 return nil , fmt .Errorf ("getting podman machine provider: %w" , err )
@@ -60,16 +121,16 @@ func getMachineInfo() (*MachineInfo, error) {
60121 return nil , fmt .Errorf ("getting podman machine connection info: %w" , err )
61122 }
62123
63- pmi := MachineInfo {
64- PodmanSocket : podmanSocket .GetPath (),
65- SSHIdentityPath : pm .SSH .IdentityPath ,
66- Rootful : pm .HostUser .Rootful ,
124+ pmi := machineInfo {
125+ podmanSocket : podmanSocket .GetPath (),
126+ sshIdentityPath : pm .SSH .IdentityPath ,
127+ rootful : pm .HostUser .Rootful ,
67128 }
68129 return & pmi , nil
69130}
70131
71132// Just to support podman v4.9, it will be removed in the future
72- func getPv4MachineInfo () (* MachineInfo , error ) {
133+ func getPv4MachineInfo () (* machineInfo , error ) {
73134 //check if a default podman machine exists
74135 listCmd := exec .Command ("podman" , "machine" , "list" , "--format" , "json" )
75136 var listCmdOutput strings.Builder
@@ -135,9 +196,9 @@ func getPv4MachineInfo() (*MachineInfo, error) {
135196 return nil , errors .New ("no podman machine found" )
136197 }
137198
138- return & MachineInfo {
139- PodmanSocket : machineInspect [0 ].ConnectionInfo .PodmanSocket .Path ,
140- SSHIdentityPath : machineInspect [0 ].SSHConfig .IdentityPath ,
141- Rootful : machineInspect [0 ].Rootful ,
199+ return & machineInfo {
200+ podmanSocket : machineInspect [0 ].ConnectionInfo .PodmanSocket .Path ,
201+ sshIdentityPath : machineInspect [0 ].SSHConfig .IdentityPath ,
202+ rootful : machineInspect [0 ].Rootful ,
142203 }, nil
143204}
0 commit comments