vagrant-lxc-ng/lib/vagrant-lxc/action/handle_box_metadata.rb

95 lines
3 KiB
Ruby
Raw Normal View History

module Vagrant
module LXC
module Action
# Prepare arguments to be used for lxc-create
2013-04-02 00:05:19 +00:00
class HandleBoxMetadata
SUPPORTED_VERSIONS = ['1.0.0', '2', '3']
def initialize(app, env)
2013-04-02 00:05:19 +00:00
@app = app
@logger = Log4r::Logger.new("vagrant::lxc::action::handle_box_metadata")
end
def call(env)
2013-04-09 01:06:13 +00:00
@env = env
@box = @env[:machine].box
2013-03-11 03:13:48 +00:00
2013-04-09 01:06:13 +00:00
@env[:ui].info I18n.t("vagrant.actions.vm.import.importing",
:name => @env[:machine].box.name)
@logger.info 'Validating box contents'
2013-04-09 01:06:13 +00:00
validate_box
@logger.info 'Setting box options on environment'
2013-04-09 01:06:13 +00:00
@env[:lxc_template_src] = template_src
@env[:lxc_template_opts] = template_opts
# FIXME: Remove support for pre 1.0.0 boxes
if box_version != '1.0.0'
@env[:ui].warn "WARNING: You are using a base box that has a format that has been deprecated, please upgrade to a new one."
@env[:lxc_template_opts].merge!(
'--auth-key' => Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s
)
end
2013-04-09 01:06:13 +00:00
if template_config_file.exist?
@env[:lxc_template_opts].merge!('--config' => template_config_file.to_s)
elsif old_template_config_file.exist?
@env[:lxc_template_config] = old_template_config_file.to_s
end
2013-04-09 01:06:13 +00:00
@app.call env
end
2013-04-09 01:06:13 +00:00
def template_src
@template_src ||=
if (box_template = @box.directory.join('lxc-template')).exist?
box_template.to_s
else
Vagrant::LXC.source_root.join('scripts/lxc-template').to_s
end
2013-04-09 01:06:13 +00:00
end
def template_config_file
@template_config_file ||= @box.directory.join('lxc-config')
end
# TODO: Remove this once we remove compatibility for < 1.0.0 boxes
def old_template_config_file
@old_template_config_file ||= @box.directory.join('lxc.conf')
end
2013-04-09 01:06:13 +00:00
def template_opts
@template_opts ||= @box.metadata.fetch('template-opts', {}).dup.merge!(
'--tarball' => rootfs_tarball
)
2013-04-09 01:06:13 +00:00
end
2013-04-09 01:06:13 +00:00
def rootfs_tarball
@rootfs_tarball ||= @box.directory.join('rootfs.tar.gz').to_s
end
2013-04-09 01:06:13 +00:00
def validate_box
unless SUPPORTED_VERSIONS.include? box_version
2013-06-08 04:13:51 +00:00
raise Errors::IncompatibleBox.new name: @box.name,
found: box_version,
supported: SUPPORTED_VERSIONS.join(', ')
2013-04-09 01:06:13 +00:00
end
unless File.exists?(template_src)
raise Errors::TemplateFileMissing.new name: @box.name
end
unless File.exists?(rootfs_tarball)
raise Errors::RootFSTarballMissing.new name: @box.name
end
end
def box_version
@box.metadata.fetch('version')
end
end
end
end
end