Download image if not exists on the system
This commit is contained in:
parent
2840b6e221
commit
f8d67e621b
3 changed files with 23 additions and 5 deletions
7
bin/vm
7
bin/vm
|
|
@ -52,8 +52,8 @@ when :archive
|
|||
options[:distro] = parameter
|
||||
VirtualMachine.archive(options)
|
||||
|
||||
when :appy
|
||||
puts "appy chnages from VM to the host os"
|
||||
when :apply
|
||||
puts "apply chnages from VM to the host os"
|
||||
when :restore
|
||||
options[:distro] = parameter
|
||||
VirtualMachine.restore(options)
|
||||
|
|
@ -61,6 +61,9 @@ when :restore
|
|||
when :daemon
|
||||
puts "setup a pprocess so i can attach to it"
|
||||
# paramaters exatly as run/setup
|
||||
when :execute
|
||||
puts "vm execute debian --file example.sh --attach (block terminal till done otherwiese notifi when done)"
|
||||
puts "if name not provided then use the last accessed"
|
||||
when :status
|
||||
puts "Print all images and status stopped/daemon(port)/running(port/)"
|
||||
when :attach
|
||||
|
|
|
|||
|
|
@ -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)
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue