Linting fixes + FileManager share start refactor

This commit is contained in:
AlexSSD7 2023-09-02 12:07:30 +01:00
commit b15e2df3d3
11 changed files with 66 additions and 120 deletions

View file

@ -2,6 +2,7 @@ package storage
import (
"bytes"
"context"
"crypto/sha256"
"encoding/hex"
"fmt"
@ -15,7 +16,7 @@ import (
"github.com/pkg/errors"
)
func (s *Storage) download(url string, hash []byte, out string, applyReaderMiddleware func(io.Reader) io.Reader) error {
func (s *Storage) download(ctx context.Context, url string, hash []byte, out string, applyReaderMiddleware func(io.Reader) io.Reader) error {
var created, success bool
defer func() {
@ -40,9 +41,14 @@ func (s *Storage) download(url string, hash []byte, out string, applyReaderMiddl
defer func() { _ = f.Close() }()
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return errors.Wrap(err, "create new http get request")
}
s.logger.Info("Starting to download file", "from", url, "to", out)
resp, err := http.Get(url)
resp, err := http.DefaultClient.Do(req)
if err != nil {
return errors.Wrap(err, "http get")
}

View file

@ -55,13 +55,13 @@ func (s *Storage) ReleaseNetTapAllocation(tapName string) error {
return nil
}
func (s *Storage) ListNetTapAllocations() ([]nettap.NetTapAlloc, error) {
func (s *Storage) ListNetTapAllocations() ([]nettap.Alloc, error) {
dirEntries, err := os.ReadDir(s.path)
if err != nil {
return nil, errors.Wrap(err, "read data dir")
}
var ret []nettap.NetTapAlloc
var ret []nettap.Alloc
for _, entry := range dirEntries {
if strings.HasPrefix(entry.Name(), tapAllocPrefix) {
@ -84,7 +84,7 @@ func (s *Storage) ListNetTapAllocations() ([]nettap.NetTapAlloc, error) {
return nil, errors.Wrapf(err, "parse pid (alloc file '%v')", entryPath)
}
ret = append(ret, nettap.NetTapAlloc{
ret = append(ret, nettap.Alloc{
TapName: tapName,
PID: int(pid),
})

View file

@ -2,6 +2,7 @@ package storage
import (
"compress/bzip2"
"context"
"fmt"
"log/slog"
"os"
@ -34,7 +35,7 @@ func NewStorage(logger *slog.Logger, dataDir string) (*Storage, error) {
}, nil
}
func (s *Storage) CheckDownloadBaseImage() (string, error) {
func (s *Storage) CheckDownloadBaseImage(ctx context.Context) (string, error) {
baseImagePath := filepath.Join(s.path, constants.GetAlpineBaseImageFileName())
_, err := os.Stat(baseImagePath)
if err != nil {
@ -43,7 +44,7 @@ func (s *Storage) CheckDownloadBaseImage() (string, error) {
}
// Image doesn't exist. Download one.
err := s.download(constants.GetAlpineBaseImageURL(), constants.GetAlpineBaseImageHash(), baseImagePath, nil)
err := s.download(ctx, constants.GetAlpineBaseImageURL(), constants.GetAlpineBaseImageHash(), baseImagePath, nil)
if err != nil {
return "", errors.Wrap(err, "download base alpine image")
}
@ -76,13 +77,16 @@ func (s *Storage) RunCLIImageBuild(showBuilderVMDisplay bool, overwrite bool) in
return 1
}
baseImagePath, err := s.CheckDownloadBaseImage()
// We're using context.Background() everywhere because this is intended
// to be executed as a blocking CLI command.
baseImagePath, err := s.CheckDownloadBaseImage(context.Background())
if err != nil {
slog.Error("Failed to check or download base VM image", "error", err.Error())
return 1
}
biosPath, err := s.CheckDownloadVMBIOS()
biosPath, err := s.CheckDownloadVMBIOS(context.Background())
if err != nil {
slog.Error("Failed to check or download VM BIOS", "error", err.Error())
return 1
@ -131,9 +135,9 @@ func (s *Storage) DataDirPath() string {
return s.path
}
func (s *Storage) CheckDownloadVMBIOS() (string, error) {
func (s *Storage) CheckDownloadVMBIOS(ctx context.Context) (string, error) {
if runtime.GOARCH == "arm64" {
p, err := s.CheckDownloadAarch64EFIImage()
p, err := s.CheckDownloadAarch64EFIImage(ctx)
if err != nil {
return "", errors.Wrap(err, "check/download aarch64 efi image")
}
@ -146,7 +150,7 @@ func (s *Storage) CheckDownloadVMBIOS() (string, error) {
return "", nil
}
func (s *Storage) CheckDownloadAarch64EFIImage() (string, error) {
func (s *Storage) CheckDownloadAarch64EFIImage(ctx context.Context) (string, error) {
efiImagePath := s.GetAarch64EFIImagePath()
_, err := os.Stat(efiImagePath)
if err != nil {
@ -155,7 +159,7 @@ func (s *Storage) CheckDownloadAarch64EFIImage() (string, error) {
}
// EFI image doesn't exist. Download one.
err := s.download(constants.GetAarch64EFIImageBZ2URL(), constants.GetAarch64EFIImageHash(), efiImagePath, bzip2.NewReader)
err := s.download(ctx, constants.GetAarch64EFIImageBZ2URL(), constants.GetAarch64EFIImageHash(), efiImagePath, bzip2.NewReader)
if err != nil {
return "", errors.Wrap(err, "download base alpine image")
}