|
1 | 1 | package imagepullers
|
2 | 2 |
|
3 | 3 | import (
|
| 4 | + "bufio" |
| 5 | + "fmt" |
| 6 | + "io" |
| 7 | + "os" |
| 8 | + "path/filepath" |
| 9 | + "strings" |
| 10 | + |
4 | 11 | "github.com/containers/podman/v5/pkg/machine/define"
|
| 12 | + |
| 13 | + "github.com/containers/podman/v5/pkg/machine/env" |
5 | 14 | )
|
6 | 15 |
|
7 | 16 | type NoopImagePuller struct {
|
8 |
| - localPath string |
| 17 | + localPath *define.VMFile |
| 18 | + sourceURI string |
| 19 | + vmType define.VMType |
| 20 | + machineDirs *define.MachineDirs |
| 21 | + machineName string |
| 22 | +} |
| 23 | + |
| 24 | +func NewNoopImagePuller(machineName string, vmType define.VMType) (*NoopImagePuller, error) { |
| 25 | + dirs, err := env.GetMachineDirs(vmType) |
| 26 | + if err != nil { |
| 27 | + return nil, err |
| 28 | + } |
| 29 | + |
| 30 | + puller := NoopImagePuller{ |
| 31 | + machineDirs: dirs, |
| 32 | + machineName: machineName, |
| 33 | + vmType: vmType, |
| 34 | + } |
| 35 | + |
| 36 | + return &puller, nil |
9 | 37 | }
|
10 | 38 |
|
11 |
| -var _ define.ImagePuller = &NoopImagePuller{} |
| 39 | +func (puller *NoopImagePuller) SetSourceURI(sourcePath string) { |
| 40 | + puller.sourceURI = sourcePath |
| 41 | +} |
12 | 42 |
|
13 |
| -func (puller *NoopImagePuller) SetSourceURI(localPath string) { |
14 |
| - puller.localPath = localPath |
| 43 | +func imageExtension(sourceURI string) string { |
| 44 | + if strings.HasSuffix(sourceURI, ".tar.gz") { |
| 45 | + return "tar.gz" |
| 46 | + } |
| 47 | + return filepath.Ext(sourceURI) |
15 | 48 | }
|
16 | 49 |
|
17 | 50 | func (puller *NoopImagePuller) LocalPath() (*define.VMFile, error) {
|
18 |
| - return define.NewMachineFile(puller.localPath, nil) |
| 51 | + vmFile, err := puller.machineDirs.DataDir.AppendToNewVMFile(fmt.Sprintf("%s-%s.%s", puller.machineName, puller.vmType.String(), imageExtension(puller.sourceURI)), nil) |
| 52 | + if err != nil { |
| 53 | + return nil, err |
| 54 | + } |
| 55 | + puller.localPath = vmFile |
| 56 | + return vmFile, nil |
19 | 57 | }
|
20 | 58 |
|
| 59 | +/* |
| 60 | +The noopImageBuilder does not actually download any image as the image is already stored locally. |
| 61 | +The download func is used to make a copy of the source image so that the user image is not modified |
| 62 | +by macadam |
| 63 | +*/ |
21 | 64 | func (puller *NoopImagePuller) Download() error {
|
22 |
| - return nil |
| 65 | + return copyFile(puller.sourceURI, puller.localPath.Path) |
| 66 | +} |
| 67 | + |
| 68 | +func copyFile(src, dst string) error { |
| 69 | + in, err := os.Open(src) |
| 70 | + if err != nil { |
| 71 | + return err |
| 72 | + } |
| 73 | + defer in.Close() |
| 74 | + |
| 75 | + out, err := os.Create(dst) |
| 76 | + if err != nil { |
| 77 | + return err |
| 78 | + } |
| 79 | + defer out.Close() |
| 80 | + |
| 81 | + bufferedWriter := bufio.NewWriter(out) |
| 82 | + defer bufferedWriter.Flush() |
| 83 | + |
| 84 | + _, err = io.Copy(bufferedWriter, in) |
| 85 | + return err |
23 | 86 | }
|
0 commit comments