Download image if not exists on the system

This commit is contained in:
Artur Gurgul 2025-08-14 08:03:10 +02:00
parent 2840b6e221
commit f8d67e621b
3 changed files with 23 additions and 5 deletions

View file

@ -5,6 +5,7 @@ require 'fileutils'
require 'open-uri'
module Downloader
# brew install gnu-tar
# use_cache => save in the home and return path to it
# forced => download even if the file exists in the cache, saves to chace if use_cache == true
def self.get(url, use_cache = true, forced = false)

View file

@ -9,16 +9,20 @@ module VirtualMachine
ISO_URLS[name][arch][type]
end
def self.image_id_dir(options)
File.join("#{options[:distro].to_s}-#{options[:arch].to_s}", options[:name])
end
def self.vm_root_path
ENV["DAT_VM_DATA"] || File.join(User.cache_path, "vm")
end
def self.vm_dir(options)
File.join(vm_root_path, "image", "#{options[:distro].to_s}-#{options[:arch].to_s}", options[:name])
File.join(vm_root_path, "image", image_id_dir(options))
end
def self.archive_dir(options)
File.join(vm_root_path, "archive", "#{options[:distro].to_s}-#{options[:arch].to_s}", options[:name])
File.join(vm_root_path, "archive", image_id_dir(options))
end
def self.create_archive_path(options)
@ -34,7 +38,10 @@ module VirtualMachine
def self.vm_archive_url(options)
return nil unless ENV["DAT_VM_REPO_URL"]
image_id = image_id_dir(options)
uri = Addressable::URI.parse(ENV["DAT_VM_REPO_URL"])
uri.path = File.join(uri.path, image_id, "archive.tar.zst")
return uri.to_s
end
def self.root_img_path(options)
@ -67,6 +74,13 @@ module VirtualMachine
fill_defaults(options)
disk_img_path = root_img_path(options)
unless File.exist?(disk_img_path)
url = vm_archive_url(options)
Downloader.get(url, use_cache: false) do |path|
Archive.restore(path, vm_dir(options))
end
#raise "file do not exists"
end
# TODO:
# - if image path not exists, check the cache
# - if cache do not exists try to download