Skip to content

Commit 79c9266

Browse files
authored
Merge pull request #60 from germag/clean-up
Clean up and dedup code
2 parents 6f883d9 + 2ac4274 commit 79c9266

File tree

4 files changed

+90
-104
lines changed

4 files changed

+90
-104
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/bootc/bootc_disk.go

Lines changed: 7 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import (
1818
"github.com/containers/podman-bootc/pkg/utils"
1919

2020
"github.com/containers/podman/v5/pkg/bindings/containers"
21-
"github.com/containers/podman/v5/pkg/bindings/images"
2221
"github.com/containers/podman/v5/pkg/domain/entities/types"
2322
"github.com/containers/podman/v5/pkg/specgen"
2423
"github.com/docker/go-units"
@@ -277,32 +276,17 @@ func (p *BootcDisk) bootcInstallImageToDisk(quiet bool, diskConfig DiskImageConf
277276
}
278277

279278
// pullImage fetches the container image if not present
280-
func (p *BootcDisk) pullImage() (err error) {
281-
pullPolicy := "missing"
282-
ids, err := images.Pull(p.Ctx, p.ImageNameOrId, &images.PullOptions{Policy: &pullPolicy})
279+
func (p *BootcDisk) pullImage() error {
280+
imageData, err := utils.PullAndInspect(p.Ctx, p.ImageNameOrId)
283281
if err != nil {
284-
return fmt.Errorf("failed to pull image: %w", err)
285-
}
286-
287-
if len(ids) == 0 {
288-
return fmt.Errorf("no ids returned from image pull")
289-
}
290-
291-
if len(ids) > 1 {
292-
return fmt.Errorf("multiple ids returned from image pull")
293-
}
294-
295-
image, err := images.GetImage(p.Ctx, p.ImageNameOrId, &images.GetOptions{})
296-
if err != nil {
297-
return fmt.Errorf("failed to get image: %w", err)
282+
return err
298283
}
299-
p.imageData = image
300284

301-
imageId := ids[0]
302-
p.ImageId = imageId
303-
p.RepoTag = image.RepoTags[0]
285+
p.imageData = imageData
286+
p.ImageId = imageData.ID
287+
p.RepoTag = imageData.RepoTags[0]
304288

305-
return
289+
return nil
306290
}
307291

308292
// runInstallContainer runs the bootc installer in a container to create a disk image

pkg/utils/podman.go

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

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

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)