1
1
package utils
2
2
3
3
import (
4
+ "context"
4
5
"encoding/json"
5
6
"errors"
6
7
"fmt"
8
+ "os"
7
9
"os/exec"
8
10
"strings"
9
11
12
+ "github.com/containers/podman/v5/pkg/bindings"
10
13
"github.com/containers/podman/v5/pkg/machine"
11
14
"github.com/containers/podman/v5/pkg/machine/define"
12
15
"github.com/containers/podman/v5/pkg/machine/env"
13
16
"github.com/containers/podman/v5/pkg/machine/provider"
14
17
"github.com/containers/podman/v5/pkg/machine/vmconfigs"
15
18
)
16
19
17
- type MachineInfo struct {
18
- PodmanSocket string
20
+ type MachineContext struct {
21
+ Ctx context. Context
19
22
SSHIdentityPath string
20
- Rootful bool
21
23
}
22
24
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 ()
25
68
if err != nil {
26
69
var errIncompatibleMachineConfig * define.ErrIncompatibleMachineConfig
27
70
var errVMDoesNotExist * define.ErrVMDoesNotExist
@@ -39,7 +82,7 @@ func GetMachineInfo() (*MachineInfo, error) {
39
82
}
40
83
41
84
// Get podman v5 machine info
42
- func getMachineInfo () (* MachineInfo , error ) {
85
+ func getPv5MachineInfo () (* machineInfo , error ) {
43
86
prov , err := provider .Get ()
44
87
if err != nil {
45
88
return nil , fmt .Errorf ("getting podman machine provider: %w" , err )
@@ -60,16 +103,16 @@ func getMachineInfo() (*MachineInfo, error) {
60
103
return nil , fmt .Errorf ("getting podman machine connection info: %w" , err )
61
104
}
62
105
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 ,
67
110
}
68
111
return & pmi , nil
69
112
}
70
113
71
114
// Just to support podman v4.9, it will be removed in the future
72
- func getPv4MachineInfo () (* MachineInfo , error ) {
115
+ func getPv4MachineInfo () (* machineInfo , error ) {
73
116
//check if a default podman machine exists
74
117
listCmd := exec .Command ("podman" , "machine" , "list" , "--format" , "json" )
75
118
var listCmdOutput strings.Builder
@@ -135,9 +178,9 @@ func getPv4MachineInfo() (*MachineInfo, error) {
135
178
return nil , errors .New ("no podman machine found" )
136
179
}
137
180
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 ,
142
185
}, nil
143
186
}
0 commit comments