Skip to content

Commit 5001fd9

Browse files
committed
Add function to connect to podman machine
let's remove the code duplication to connect to the podman machine. Signed-off-by: German Maglione <[email protected]>
1 parent 6f883d9 commit 5001fd9

File tree

3 files changed

+65
-81
lines changed

3 files changed

+65
-81
lines changed

cmd/run.go

Lines changed: 4 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package cmd
22

33
import (
4-
"context"
5-
"errors"
64
"fmt"
7-
"os"
85
"time"
96

107
"github.com/containers/podman-bootc/pkg/bootc"
@@ -13,7 +10,6 @@ import (
1310
"github.com/containers/podman-bootc/pkg/utils"
1411
"github.com/containers/podman-bootc/pkg/vm"
1512

16-
"github.com/containers/podman/v5/pkg/bindings"
1713
"github.com/sirupsen/logrus"
1814
"github.com/spf13/cobra"
1915
)
@@ -66,42 +62,16 @@ func doRun(flags *cobra.Command, args []string) error {
6662
return fmt.Errorf("unable to get user: %w", err)
6763
}
6864

69-
//podman machine connection
70-
machineInfo, err := utils.GetMachineInfo()
71-
if err != nil {
72-
return err
73-
}
74-
75-
if machineInfo == nil {
76-
println(utils.PodmanMachineErrorMessage)
77-
return errors.New("rootful podman machine is required, please run 'podman machine init --rootful'")
78-
}
79-
80-
if !machineInfo.Rootful {
81-
println(utils.PodmanMachineErrorMessage)
82-
return errors.New("rootful podman machine is required, please run 'podman machine set --rootful'")
83-
}
84-
85-
if _, err := os.Stat(machineInfo.PodmanSocket); err != nil {
86-
println(utils.PodmanMachineErrorMessage)
87-
logrus.Errorf("podman machine socket is missing. Is podman machine running?\n%s", err)
88-
return err
89-
}
90-
91-
ctx, err := bindings.NewConnectionWithIdentity(
92-
context.Background(),
93-
fmt.Sprintf("unix://%s", machineInfo.PodmanSocket),
94-
machineInfo.SSHIdentityPath,
95-
true)
65+
machine, err := utils.GetMachineContext()
9666
if err != nil {
9767
println(utils.PodmanMachineErrorMessage)
98-
logrus.Errorf("failed to connect to the podman socket. Is podman machine running?\n%s", err)
68+
logrus.Errorf("failed to connect to podman machine. Is podman machine running?\n%s", err)
9969
return err
10070
}
10171

10272
// create the disk image
10373
idOrName := args[0]
104-
bootcDisk := bootc.NewBootcDisk(idOrName, ctx, user)
74+
bootcDisk := bootc.NewBootcDisk(idOrName, machine.Ctx, user)
10575
err = bootcDisk.Install(vmConfig.Quiet, diskImageConfigInstance)
10676

10777
if err != nil {
@@ -143,7 +113,7 @@ func doRun(flags *cobra.Command, args []string) error {
143113
RemoveVm: vmConfig.RemoveVm,
144114
Background: vmConfig.Background,
145115
SSHPort: sshPort,
146-
SSHIdentity: machineInfo.SSHIdentityPath,
116+
SSHIdentity: machine.SSHIdentityPath,
147117
VMUser: vmConfig.User,
148118
})
149119

pkg/utils/podman.go

Lines changed: 58 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,70 @@
11
package utils
22

33
import (
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
}

podman-bootc.go

Lines changed: 3 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package main
22

33
import (
4-
"context"
5-
"fmt"
64
"os"
75
"os/signal"
86
"syscall"
@@ -12,7 +10,6 @@ import (
1210
"github.com/containers/podman-bootc/pkg/user"
1311
"github.com/containers/podman-bootc/pkg/utils"
1412

15-
"github.com/containers/podman/v5/pkg/bindings"
1613
"github.com/sirupsen/logrus"
1714
)
1815

@@ -23,41 +20,15 @@ func cleanup() {
2320
os.Exit(0)
2421
}
2522

26-
//podman machine connection
27-
machineInfo, err := utils.GetMachineInfo()
28-
if err != nil {
29-
logrus.Errorf("unable to get podman machine info: %s", err)
30-
os.Exit(1)
31-
}
32-
33-
if machineInfo == nil {
34-
logrus.Errorf("rootful podman machine is required, please run 'podman machine init --rootful'")
35-
os.Exit(1)
36-
}
37-
38-
if !machineInfo.Rootful {
39-
logrus.Errorf("rootful podman machine is required, please run 'podman machine set --rootful'")
40-
os.Exit(1)
41-
}
42-
43-
if _, err := os.Stat(machineInfo.PodmanSocket); err != nil {
44-
logrus.Errorf("podman machine socket is missing. Is podman machine running?\n%s", err)
45-
os.Exit(1)
46-
}
47-
48-
ctx, err := bindings.NewConnectionWithIdentity(
49-
context.Background(),
50-
fmt.Sprintf("unix://%s", machineInfo.PodmanSocket),
51-
machineInfo.SSHIdentityPath,
52-
true)
23+
machine, err := utils.GetMachineContext()
5324
if err != nil {
5425
println(utils.PodmanMachineErrorMessage)
55-
logrus.Errorf("failed to connect to the podman socket. Is podman machine running?\n%s", err)
26+
logrus.Errorf("failed to connect to podman machine. Is podman machine running?\n%s", err)
5627
os.Exit(1)
5728
}
5829

5930
//delete the disk image
60-
err = bootc.NewBootcDisk("", ctx, user).Cleanup()
31+
err = bootc.NewBootcDisk("", machine.Ctx, user).Cleanup()
6132
if err != nil {
6233
logrus.Errorf("unable to get podman machine info: %s", err)
6334
os.Exit(0)

0 commit comments

Comments
 (0)