Merge pull request #17192 from baude/wslfcosreorg

WSL refactoring
This commit is contained in:
OpenShift Merge Robot
2023-01-26 08:30:56 -05:00
committed by GitHub
6 changed files with 29 additions and 28 deletions

View File

@ -61,7 +61,7 @@ func NewFcosDownloader(vmType, vmName, imageStream string) (DistributionDownload
fcd := FcosDownload{
Download: Download{
Arch: getFcosArch(),
Arch: GetFcosArch(),
Artifact: artifact,
CacheDir: cacheDir,
Format: Format,
@ -76,7 +76,7 @@ func NewFcosDownloader(vmType, vmName, imageStream string) (DistributionDownload
if err != nil {
return nil, err
}
fcd.Download.LocalUncompressedFile = fcd.getLocalUncompressedFile(dataDir)
fcd.Download.LocalUncompressedFile = fcd.GetLocalUncompressedFile(dataDir)
return fcd, nil
}
@ -118,10 +118,10 @@ func (f FcosDownload) CleanCache() error {
// Set cached image to expire after 2 weeks
// FCOS refreshes around every 2 weeks, assume old images aren't needed
expire := 14 * 24 * time.Hour
return removeImageAfterExpire(f.CacheDir, expire)
return RemoveImageAfterExpire(f.CacheDir, expire)
}
func getFcosArch() string {
func GetFcosArch() string {
var arch string
// TODO fill in more architectures
switch runtime.GOARCH {
@ -189,7 +189,7 @@ func GetFCOSDownload(imageStream string) (*FcosDownloadInfo, error) {
return nil, err
}
arches, ok := altMeta.Architectures[getFcosArch()]
arches, ok := altMeta.Architectures[GetFcosArch()]
if !ok {
return nil, fmt.Errorf("unable to pull VM image: no targetArch in stream")
}
@ -209,7 +209,7 @@ func GetFCOSDownload(imageStream string) (*FcosDownloadInfo, error) {
if err := json.Unmarshal(body, &fcosstable); err != nil {
return nil, err
}
arch, ok := fcosstable.Architectures[getFcosArch()]
arch, ok := fcosstable.Architectures[GetFcosArch()]
if !ok {
return nil, fmt.Errorf("unable to pull VM image: no targetArch in stream")
}

View File

@ -7,6 +7,6 @@ import (
"runtime"
)
func determineFedoraArch() string {
func DetermineMachineArch() string {
return runtime.GOARCH
}

View File

@ -8,7 +8,7 @@ import (
"golang.org/x/sys/windows"
)
func determineFedoraArch() string {
func DetermineMachineArch() string {
const fallbackMsg = "this may result in the wrong Linux arch under emulation"
var machine, native uint16
current, _ := syscall.GetCurrentProcess()

View File

@ -68,7 +68,7 @@ func NewGenericDownloader(vmType, vmName, pullPath string) (DistributionDownload
return gd, nil
}
func (d Download) getLocalUncompressedFile(dataDir string) string {
func (d Download) GetLocalUncompressedFile(dataDir string) string {
var (
extension string
)
@ -273,7 +273,7 @@ func decompressEverythingElse(src string, output io.WriteCloser) error {
return err
}
func removeImageAfterExpire(dir string, expire time.Duration) error {
func RemoveImageAfterExpire(dir string, expire time.Duration) error {
now := time.Now()
err := filepath.Walk(dir, func(path string, info os.FileInfo, err error) error {
// Delete any cache files that are older than expiry date

View File

@ -1,20 +1,21 @@
//go:build amd64 || arm64
// +build amd64 arm64
package machine
package wsl
import (
"errors"
"fmt"
"io"
"os"
"path"
"strings"
"net/http"
"net/url"
"os"
"path"
"path/filepath"
"strings"
"time"
"github.com/containers/podman/v4/pkg/machine"
)
const (
@ -23,16 +24,16 @@ const (
)
type FedoraDownload struct {
Download
machine.Download
}
func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDownload, error) {
func NewFedoraDownloader(vmType, vmName, releaseStream string) (machine.DistributionDownload, error) {
downloadURL, version, arch, size, err := getFedoraDownload()
if err != nil {
return nil, err
}
cacheDir, err := GetCacheDir(vmType)
cacheDir, err := machine.GetCacheDir(vmType)
if err != nil {
return nil, err
}
@ -40,11 +41,11 @@ func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDown
imageName := fmt.Sprintf("fedora-podman-%s-%s.tar.xz", arch, version)
f := FedoraDownload{
Download: Download{
Arch: getFcosArch(),
Artifact: artifact,
Download: machine.Download{
Arch: machine.GetFcosArch(),
Artifact: "",
CacheDir: cacheDir,
Format: Format,
Format: machine.Format,
ImageName: imageName,
LocalPath: filepath.Join(cacheDir, imageName),
URL: downloadURL,
@ -52,15 +53,15 @@ func NewFedoraDownloader(vmType, vmName, releaseStream string) (DistributionDown
Size: size,
},
}
dataDir, err := GetDataDir(vmType)
dataDir, err := machine.GetDataDir(vmType)
if err != nil {
return nil, err
}
f.Download.LocalUncompressedFile = f.getLocalUncompressedFile(dataDir)
f.Download.LocalUncompressedFile = f.GetLocalUncompressedFile(dataDir)
return f, nil
}
func (f FedoraDownload) Get() *Download {
func (f FedoraDownload) Get() *machine.Download {
return &f.Download
}
@ -78,12 +79,12 @@ func (f FedoraDownload) HasUsableCache() (bool, error) {
func (f FedoraDownload) CleanCache() error {
// Set cached image to expire after 2 weeks
expire := 14 * 24 * time.Hour
return removeImageAfterExpire(f.CacheDir, expire)
return machine.RemoveImageAfterExpire(f.CacheDir, expire)
}
func getFedoraDownload() (*url.URL, string, string, int64, error) {
var releaseURL string
arch := determineFedoraArch()
arch := machine.DetermineMachineArch()
switch arch {
case "arm64":
releaseURL = githubArmReleaseURL

View File

@ -415,7 +415,7 @@ func downloadDistro(v *MachineVM, opts machine.InitOptions) error {
if _, e := strconv.Atoi(opts.ImagePath); e == nil {
v.ImageStream = opts.ImagePath
dd, err = machine.NewFedoraDownloader(vmtype, v.Name, opts.ImagePath)
dd, err = NewFedoraDownloader(vmtype, v.Name, opts.ImagePath)
} else {
v.ImageStream = "custom"
dd, err = machine.NewGenericDownloader(vmtype, v.Name, opts.ImagePath)