Skip to content

Commit

Permalink
fix(agent): Create the rootfs tarball in the download function (#843)
Browse files Browse the repository at this point in the history
Well, the download function might now be doing more than it should.
But WSL imposes the precondition that nobody else holds the rootfs file
open when registering the distro instance. So we must ensure the file is
closed by the time we call import. This is the most readable way to do
so IMHO.

We could have wrapped the file creation and the call to download in a
closure. It would be more compliant to SRP, but less readable.

---
UDENG-3590
CarlosNihelton authored Jul 18, 2024
2 parents ca2716b + 195025e commit 2042e21
Showing 1 changed file with 8 additions and 9 deletions.
17 changes: 8 additions & 9 deletions windows-agent/internal/proservices/landscape/executor.go
Original file line number Diff line number Diff line change
@@ -251,13 +251,7 @@ func installFromURL(ctx context.Context, homeDir string, downloadDir string, dis

tarball := filepath.Join(tmpDir, distro.Name()+".tar.gz")

f, err := os.Create(tarball)
if err != nil {
return err
}
defer f.Close()

err = download(ctx, f, rootfsURL)
err = download(ctx, rootfsURL, tarball)
if err != nil {
return err
}
@@ -278,9 +272,9 @@ func installFromURL(ctx context.Context, homeDir string, downloadDir string, dis
return nil
}

// download downloads the rootfs from the given URL and writes it to the given writer while verifying its checksum.
// download downloads the rootfs from the given URL and writes it to the given destination while verifying its checksum.
// The checksum is read from the SHA256SUMS file found alongside the rootfs URL, as done in cloud-images.ubuntu.com.
func download(ctx context.Context, f io.Writer, u *url.URL) (err error) {
func download(ctx context.Context, u *url.URL, destination string) (err error) {
defer decorate.OnError(&err, "could not download %q", u)

checksum, err := wantRootfsChecksum(ctx, u)
@@ -297,6 +291,11 @@ func download(ctx context.Context, f io.Writer, u *url.URL) (err error) {
return fmt.Errorf("http request failed with code %d", resp.StatusCode)
}

f, err := os.Create(destination)
if err != nil {
return err
}
defer f.Close()
// Verify checksum and write file to disk
r := io.TeeReader(resp.Body, f)
if checksum != "" {

0 comments on commit 2042e21

Please sign in to comment.