2013-03-02 15:17:04 +00:00
|
|
|
module Vagrant
|
|
|
|
module LXC
|
|
|
|
module Action
|
|
|
|
# Prepare arguments to be used for lxc-create
|
|
|
|
class HandleBoxMetadata < BaseAction
|
|
|
|
LXC_TEMPLATES_PATH = Pathname.new("/usr/share/lxc/templates")
|
2013-03-08 03:54:15 +00:00
|
|
|
TEMP_PREFIX = "vagrant-lxc-rootfs-temp-"
|
2013-03-02 15:17:04 +00:00
|
|
|
|
|
|
|
def initialize(app, env)
|
|
|
|
super
|
|
|
|
@logger = Log4r::Logger.new("vagrant::lxc::action::handle_box_metadata")
|
|
|
|
end
|
|
|
|
|
|
|
|
def call(env)
|
2013-03-11 03:13:48 +00:00
|
|
|
env[:ui].info I18n.t("vagrant.actions.vm.import.importing",
|
|
|
|
:name => env[:machine].box.name)
|
|
|
|
|
2013-03-08 03:54:15 +00:00
|
|
|
rootfs_cache = Dir.mktmpdir(TEMP_PREFIX)
|
2013-03-02 15:17:04 +00:00
|
|
|
box = env[:machine].box
|
2013-03-08 03:54:15 +00:00
|
|
|
template_name = "vagrant-#{box.name}"
|
2013-03-02 15:17:04 +00:00
|
|
|
|
|
|
|
# Prepends "lxc-" to the template file so that `lxc-create` is able to find it
|
2013-03-08 03:54:15 +00:00
|
|
|
lxc_template_src = box.directory.join('lxc-template').to_s
|
|
|
|
unless File.exists?(lxc_template_src)
|
|
|
|
raise Errors::TemplateFileMissing.new name: box.name
|
|
|
|
end
|
|
|
|
dest = LXC_TEMPLATES_PATH.join("lxc-#{template_name}").to_s
|
2013-03-02 15:17:04 +00:00
|
|
|
@logger.debug('Copying LXC template into place')
|
2013-03-08 03:54:15 +00:00
|
|
|
system(%Q[sudo su root -c "cp #{lxc_template_src} #{dest}"])
|
|
|
|
|
|
|
|
@logger.debug('Extracting rootfs')
|
2013-03-30 22:23:06 +00:00
|
|
|
# TODO: Ideally the compressed rootfs should not output errors...
|
|
|
|
system(%Q[sudo su root -c "cd #{box.directory} && tar xfz rootfs.tar.gz -C #{rootfs_cache} 2>/dev/null"])
|
2013-03-08 03:54:15 +00:00
|
|
|
|
|
|
|
box.metadata.merge!(
|
|
|
|
'template-name' => template_name,
|
|
|
|
'rootfs-cache-path' => rootfs_cache
|
|
|
|
)
|
2013-03-02 15:17:04 +00:00
|
|
|
|
|
|
|
@app.call(env)
|
2013-03-08 03:54:15 +00:00
|
|
|
|
|
|
|
ensure
|
|
|
|
system %Q[sudo su root -c "rm -rf #{rootfs_cache}"]
|
2013-03-02 15:17:04 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|