balena-io / balena-sdk

The SDK to make balena powered JavaScript applications.
https://www.balena.io/
Apache License 2.0
146 stars 46 forks source link

os.download should support zip images (atm gives only uncompressed OS Images) #730

Open sbousamra opened 5 years ago

sbousamra commented 5 years ago

Expected Behavior

The file streamed from balena().models.os.download to be a compressed version of the OS image.

Actual Behavior

balena().models.os.download(deviceType, version) is giving me an uncompressed version of the OS image (e.g. intel-nuc latest version is ~1.5gb, when compressed it is ~200mb), is there a reason this is uncompressed or is it possible for there to be an option to request it in a compressed format to reduce the download size?

Steps to Reproduce the Problem

balena()
  .models.os.download('intel-nuc', 'latest')
  .then((stream) => stream.pipe(fs.createWriteStream('intel-nuc.img')));

Specifications

References

Uncompressed Screen Shot 2019-08-21 at 3 02 58 pm

Compressed Screen Shot 2019-08-21 at 3 03 09 pm

thgreasi commented 5 years ago

Hi, This sounds like a nice feature to consider adding. I will pass this internally for consideration.

thgreasi commented 3 years ago

Hi, Even though an explicit way to request for a .zip file is still valid, we have some interesting findings to share. After some testing it seems that both current browsers and node will auto-gzip all traffic when the server supports it (balenaCloud does) and auto-expand while saving to disk. As a result even if you end up with a 900MB .img file in your filesystem, the amount of data that goes over-the-wire is actually quite smaller, to a degree that's comparable with the sizes of the respective .zip files.

You can also give this a try by timing how much time your browser needs to download a full vs a zipped image: https://api.balena-cloud.com/download?deviceType=raspberrypi4-64&version=2.75.0%2Brev1.prod https://api.balena-cloud.com/download?deviceType=raspberrypi4-64&version=2.75.0%2Brev1.prod&fileType=.zip

and then in order to compare those with the sdk method as well, you can time the balena-cli's download command in your terminal with something like:

time balena os download raspberrypi4-64 --version 2.75.0+rev1.prod -o ./tmp.img

In my tests all those took about 33 seconds and for each of them my system's data meter couldn't track more than 200MB of overall data traffic during that time window.

For internal future reference: See: https://www.flowdock.com/app/rulemotion/r-beginners/threads/nFpqGUwLNhVUJnPlSTfNU-ixwkN