mirror of
https://github.com/vagrant-libvirt/vagrant-libvirt.git
synced 2025-02-25 18:55:27 -06:00
port tools/create_box.sh to package action. close #11
This commit is contained in:
parent
9fcd9f5873
commit
08037d1941
@ -144,8 +144,10 @@ module VagrantPlugins
|
|||||||
|
|
||||||
# not implemented and looks like not require
|
# not implemented and looks like not require
|
||||||
def self.action_package
|
def self.action_package
|
||||||
lambda do |env|
|
Vagrant::Action::Builder.new.tap do |b|
|
||||||
raise Errors::PackageNotSupported
|
b.use ConfigValidate
|
||||||
|
b.use ConnectLibvirt
|
||||||
|
b.use PackageDomain
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -317,6 +319,7 @@ module VagrantPlugins
|
|||||||
|
|
||||||
action_root = Pathname.new(File.expand_path('../action', __FILE__))
|
action_root = Pathname.new(File.expand_path('../action', __FILE__))
|
||||||
autoload :ConnectLibvirt, action_root.join('connect_libvirt')
|
autoload :ConnectLibvirt, action_root.join('connect_libvirt')
|
||||||
|
autoload :PackageDomain, action_root.join('package_domain')
|
||||||
autoload :CreateDomain, action_root.join('create_domain')
|
autoload :CreateDomain, action_root.join('create_domain')
|
||||||
autoload :CreateDomainVolume, action_root.join('create_domain_volume')
|
autoload :CreateDomainVolume, action_root.join('create_domain_volume')
|
||||||
autoload :CreateNetworkInterfaces, action_root.join('create_network_interfaces')
|
autoload :CreateNetworkInterfaces, action_root.join('create_network_interfaces')
|
||||||
|
@ -41,14 +41,14 @@ module VagrantPlugins
|
|||||||
next if disk[:allow_existing]
|
next if disk[:allow_existing]
|
||||||
diskname = libvirt_domain.name + '-' + disk[:device] + '.' + disk[:type].to_s
|
diskname = libvirt_domain.name + '-' + disk[:device] + '.' + disk[:type].to_s
|
||||||
# diskname is uniq
|
# diskname is uniq
|
||||||
libvirt_disk = env[:libvirt_compute].volumes.all.select do |x|
|
libvirt_disk = domain.volumes.select do |x|
|
||||||
x.name == diskname
|
x.name == diskname
|
||||||
end.first
|
end.first
|
||||||
if libvirt_disk
|
if libvirt_disk
|
||||||
libvirt_disk.destroy
|
libvirt_disk.destroy
|
||||||
elsif disk[:path]
|
elsif disk[:path]
|
||||||
poolname = env[:machine].provider_config.storage_pool_name
|
poolname = env[:machine].provider_config.storage_pool_name
|
||||||
libvirt_disk = env[:libvirt_compute].volumes.all.select do |x|
|
libvirt_disk = domain.volumes.select do |x|
|
||||||
# FIXME can remove pool/target.img and pool/123/target.img
|
# FIXME can remove pool/target.img and pool/123/target.img
|
||||||
x.path =~ /\/#{disk[:path]}$/ && x.pool_name == poolname
|
x.path =~ /\/#{disk[:path]}$/ && x.pool_name == poolname
|
||||||
end.first
|
end.first
|
||||||
@ -57,7 +57,7 @@ module VagrantPlugins
|
|||||||
end
|
end
|
||||||
|
|
||||||
# remove root storage
|
# remove root storage
|
||||||
root_disk = env[:libvirt_compute].volumes.all.select do |x|
|
root_disk = domain.volumes.select do |x|
|
||||||
x.name == libvirt_domain.name + '.img'
|
x.name == libvirt_domain.name + '.img'
|
||||||
end.first
|
end.first
|
||||||
root_disk.destroy if root_disk
|
root_disk.destroy if root_disk
|
||||||
|
79
lib/vagrant-libvirt/action/package_domain.rb
Normal file
79
lib/vagrant-libvirt/action/package_domain.rb
Normal file
@ -0,0 +1,79 @@
|
|||||||
|
require 'log4r'
|
||||||
|
|
||||||
|
module VagrantPlugins
|
||||||
|
module ProviderLibvirt
|
||||||
|
module Action
|
||||||
|
# Action for create new box for libvirt provider
|
||||||
|
class PackageDomain
|
||||||
|
def initialize(app, env)
|
||||||
|
@logger = Log4r::Logger.new('vagrant_libvirt::action::package_domain')
|
||||||
|
@app = app
|
||||||
|
env['package.files'] ||= {}
|
||||||
|
env['package.output'] ||= 'package.box'
|
||||||
|
end
|
||||||
|
|
||||||
|
def call(env)
|
||||||
|
env[:ui].info(I18n.t('vagrant_libvirt.package_domain'))
|
||||||
|
libvirt_domain = env[:libvirt_compute].client.lookup_domain_by_uuid(
|
||||||
|
env[:machine].id)
|
||||||
|
domain = env[:libvirt_compute].servers.get(env[:machine].id.to_s)
|
||||||
|
root_disk = domain.volumes.select do |x|
|
||||||
|
x.name == libvirt_domain.name + '.img'
|
||||||
|
end.first
|
||||||
|
boxname = env['package.output']
|
||||||
|
raise "#{boxname}: Already exists" if File.exists?(boxname)
|
||||||
|
@tmp_dir = Dir.pwd + '/_tmp_package'
|
||||||
|
@tmp_img = @tmp_dir + '/box.img'
|
||||||
|
Dir.mkdir(@tmp_dir)
|
||||||
|
if File.readable?(root_disk.path)
|
||||||
|
backing = `qemu-img info "#{root_disk.path}" | grep 'backing file:' | cut -d ':' -f2`.chomp
|
||||||
|
else
|
||||||
|
env[:ui].error("Require set read access to #{root_disk.path}. sudo chmod a+r #{root_disk.path}")
|
||||||
|
FileUtils.rm_rf(@tmp_dir)
|
||||||
|
raise 'Have no access'
|
||||||
|
end
|
||||||
|
env[:ui].info('Image has backing image, copying image and rebasing ...')
|
||||||
|
FileUtils.cp(root_disk.path, @tmp_img)
|
||||||
|
`qemu-img rebase -p -b "" #{@tmp_img}`
|
||||||
|
Dir.chdir(@tmp_dir)
|
||||||
|
img_size = `qemu-img info #{@tmp_img} | grep 'virtual size' | awk '{print $3;}' | tr -d 'G'`.chomp
|
||||||
|
File.write(@tmp_dir + '/metadata.json', metadata_content(img_size))
|
||||||
|
File.write(@tmp_dir + '/Vagrantfile',vagrantfile_content)
|
||||||
|
assebmle_box(boxname)
|
||||||
|
FileUtils.mv(@tmp_dir + '/' + boxname, '../' + boxname)
|
||||||
|
env[:ui].info('Box created')
|
||||||
|
env[:ui].info('You can now add the box:')
|
||||||
|
env[:ui].info("vagrant box add #{boxname} --name any_comfortable_name")
|
||||||
|
@app.call(env)
|
||||||
|
end
|
||||||
|
|
||||||
|
def assebmle_box(boxname)
|
||||||
|
`tar cvzf "#{boxname}" --totals ./metadata.json ./Vagrantfile ./box.img`
|
||||||
|
end
|
||||||
|
|
||||||
|
def vagrantfile_content
|
||||||
|
<<-EOF
|
||||||
|
Vagrant.configure("2") do |config|
|
||||||
|
config.vm.provider :libvirt do |libvirt|
|
||||||
|
libvirt.driver = "kvm"
|
||||||
|
libvirt.host = ""
|
||||||
|
libvirt.connect_via_ssh = false
|
||||||
|
libvirt.storage_pool_name = "default"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
EOF
|
||||||
|
end
|
||||||
|
|
||||||
|
def metadata_content(filesize)
|
||||||
|
<<-EOF
|
||||||
|
{
|
||||||
|
"provider": "libvirt",
|
||||||
|
"format": "qcow2",
|
||||||
|
"virtual_size": #{filesize}
|
||||||
|
}
|
||||||
|
EOF
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue
Block a user