-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This PR changes the behavior of the noopImagePuller. As this puller does not actually pull any image as it is used when an image is already in the local system, it just clones the image in a dedicated folder so that the original image does not get alterated when started by macadam. Signed-off-by: lstocchi <[email protected]>
- Loading branch information
Showing
2 changed files
with
75 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,23 +1,86 @@ | ||
package imagepullers | ||
|
||
import ( | ||
"bufio" | ||
"fmt" | ||
"io" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/containers/podman/v5/pkg/machine/define" | ||
|
||
"github.com/containers/podman/v5/pkg/machine/env" | ||
) | ||
|
||
type NoopImagePuller struct { | ||
localPath string | ||
localPath *define.VMFile | ||
sourceURI string | ||
vmType define.VMType | ||
machineDirs *define.MachineDirs | ||
machineName string | ||
} | ||
|
||
func NewNoopImagePuller(machineName string, vmType define.VMType) (*NoopImagePuller, error) { | ||
dirs, err := env.GetMachineDirs(vmType) | ||
if err != nil { | ||
return nil, err | ||
} | ||
|
||
puller := NoopImagePuller{ | ||
machineDirs: dirs, | ||
machineName: machineName, | ||
vmType: vmType, | ||
} | ||
|
||
return &puller, nil | ||
} | ||
|
||
var _ define.ImagePuller = &NoopImagePuller{} | ||
func (puller *NoopImagePuller) SetSourceURI(sourcePath string) { | ||
puller.sourceURI = sourcePath | ||
} | ||
|
||
func (puller *NoopImagePuller) SetSourceURI(localPath string) { | ||
puller.localPath = localPath | ||
func imageExtension(sourceURI string) string { | ||
if strings.HasSuffix(sourceURI, ".tar.gz") { | ||
return "tar.gz" | ||
} | ||
return filepath.Ext(sourceURI) | ||
} | ||
|
||
func (puller *NoopImagePuller) LocalPath() (*define.VMFile, error) { | ||
return define.NewMachineFile(puller.localPath, nil) | ||
vmFile, err := puller.machineDirs.DataDir.AppendToNewVMFile(fmt.Sprintf("%s-%s.%s", puller.machineName, puller.vmType.String(), imageExtension(puller.sourceURI)), nil) | ||
if err != nil { | ||
return nil, err | ||
} | ||
puller.localPath = vmFile | ||
return vmFile, nil | ||
} | ||
|
||
/* | ||
The noopImageBuilder does not actually download any image as the image is already stored locally. | ||
The download func is used to make a copy of the source image so that the user image is not modified | ||
by macadam | ||
*/ | ||
func (puller *NoopImagePuller) Download() error { | ||
return nil | ||
return copyFile(puller.sourceURI, puller.localPath.Path) | ||
} | ||
|
||
func copyFile(src, dst string) error { | ||
in, err := os.Open(src) | ||
if err != nil { | ||
return err | ||
} | ||
defer in.Close() | ||
|
||
out, err := os.Create(dst) | ||
if err != nil { | ||
return err | ||
} | ||
defer out.Close() | ||
|
||
bufferedWriter := bufio.NewWriter(out) | ||
defer bufferedWriter.Flush() | ||
|
||
_, err = io.Copy(bufferedWriter, in) | ||
return err | ||
} |