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