mirror of
https://github.com/podman-container-tools/podman.git
synced 2026-08-11 19:35:43 +00:00
The following PR is the leading PR for refactoring podman machine with the following goals: * less duplication/more re-use * common configuration file between providers * more consistentency in how machines are handled by providers The goal of this PR is the rough refactor. There are still rough spots for sure, specifically around the podman socket and pipe. This implemention is only for Linux. All other providers are still present but will not compile or work. This is why tests for them have been temporarily suspended. The ready socket code is another area that needs to be smoothed over. Right now, the ready socket code is still in QEMU. Preferably it would be moved to a generic spot where all three approaches to readiness socket use can be defined. It should also be noted: * all machine related tests pass. * make validate for Linux passes * Apple QEMU was largely removed * More code pruning is possible; will become clearer when other providers are complete. the dir pkg/machine/p5 is not permanent. i had to seperate this from machine initially due to circular import problems. i think when all providers are done (or nearly done), it can be placed and named properly. Signed-off-by: Brent Baude <bbaude@redhat.com>
111 lines
2.4 KiB
Go
111 lines
2.4 KiB
Go
package stdpull
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"io/fs"
|
|
"net/http"
|
|
url2 "net/url"
|
|
"os"
|
|
"path"
|
|
"path/filepath"
|
|
|
|
"github.com/containers/podman/v4/pkg/machine/compression"
|
|
"github.com/containers/podman/v4/pkg/machine/define"
|
|
"github.com/containers/podman/v4/utils"
|
|
"github.com/sirupsen/logrus"
|
|
)
|
|
|
|
type DiskFromURL struct {
|
|
u *url2.URL
|
|
finalPath *define.VMFile
|
|
tempLocation *define.VMFile
|
|
}
|
|
|
|
func NewDiskFromURL(inputPath string, finalPath *define.VMFile, tempDir *define.VMFile) (*DiskFromURL, error) {
|
|
var (
|
|
err error
|
|
)
|
|
u, err := url2.Parse(inputPath)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
// Make sure the temporary location exists before we get too deep
|
|
if _, err := os.Stat(tempDir.GetPath()); err != nil {
|
|
if errors.Is(err, fs.ErrNotExist) {
|
|
return nil, fmt.Errorf("temporary download directory %s does not exist", tempDir.GetPath())
|
|
}
|
|
}
|
|
|
|
remoteImageName := path.Base(inputPath)
|
|
if remoteImageName == "" {
|
|
return nil, fmt.Errorf("invalid url: unable to determine image name in %q", inputPath)
|
|
}
|
|
|
|
tempLocation, err := tempDir.AppendToNewVMFile(remoteImageName, nil)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
return &DiskFromURL{
|
|
u: u,
|
|
finalPath: finalPath,
|
|
tempLocation: tempLocation,
|
|
}, nil
|
|
}
|
|
|
|
func (d *DiskFromURL) Get() error {
|
|
// this fetches the image and writes it to the temporary location
|
|
if err := d.pull(); err != nil {
|
|
return err
|
|
}
|
|
logrus.Debugf("decompressing %s to %s", d.tempLocation.GetPath(), d.finalPath.GetPath())
|
|
return compression.Decompress(d.tempLocation, d.finalPath.GetPath())
|
|
}
|
|
|
|
func (d *DiskFromURL) pull() error {
|
|
out, err := os.Create(d.tempLocation.GetPath())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if err := out.Close(); err != nil {
|
|
logrus.Error(err)
|
|
}
|
|
}()
|
|
|
|
resp, err := http.Get(d.u.String())
|
|
if err != nil {
|
|
return err
|
|
}
|
|
defer func() {
|
|
if err := resp.Body.Close(); err != nil {
|
|
logrus.Error(err)
|
|
}
|
|
}()
|
|
|
|
if resp.StatusCode != http.StatusOK {
|
|
return fmt.Errorf("downloading VM image %s: %s", d.u.String(), resp.Status)
|
|
}
|
|
size := resp.ContentLength
|
|
prefix := "Downloading VM image: " + filepath.Base(d.tempLocation.GetPath())
|
|
onComplete := prefix + ": done"
|
|
|
|
p, bar := utils.ProgressBar(prefix, size, onComplete)
|
|
|
|
proxyReader := bar.ProxyReader(resp.Body)
|
|
defer func() {
|
|
if err := proxyReader.Close(); err != nil {
|
|
logrus.Error(err)
|
|
}
|
|
}()
|
|
|
|
if _, err := io.Copy(out, proxyReader); err != nil {
|
|
return err
|
|
}
|
|
|
|
p.Wait()
|
|
return nil
|
|
}
|