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

View File

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

View File

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

View File

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

View File

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

View File

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