action/handle_box_metadata: Implement support for 1.0.0 base boxes

This commit is contained in:
Fabio Rehm 2014-03-09 18:11:40 -03:00
parent d5faf1db66
commit deac5930cc
2 changed files with 93 additions and 16 deletions

View file

@ -3,7 +3,8 @@ module Vagrant
module Action
# Prepare arguments to be used for lxc-create
class HandleBoxMetadata
SUPPORTED_VERSIONS = [2, 3]
SUPPORTED_VERSIONS = ['1.0.0', '2', '3']
def initialize(app, env)
@app = app
@logger = Log4r::Logger.new("vagrant::lxc::action::handle_box_metadata")
@ -16,15 +17,25 @@ module Vagrant
@env[:ui].info I18n.t("vagrant.actions.vm.import.importing",
:name => @env[:machine].box.name)
@logger.debug 'Validating box contents'
@logger.info 'Validating box contents'
validate_box
@logger.debug 'Setting box options on environment'
@env[:lxc_template_opts] = template_opts
@logger.info 'Setting box options on environment'
@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
if template_config_file.exist?
@env[:lxc_template_config] = template_config_file.to_s
@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
@app.call env
@ -35,15 +46,17 @@ module Vagrant
end
def template_config_file
@template_config_file ||= @box.directory.join('lxc.conf')
@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
def template_opts
@template_opts ||= @box.metadata.fetch('template-opts', {}).dup.merge!(
'--tarball' => rootfs_tarball,
# TODO: Deprecate this, the rootfs should be ready for vagrant-lxc
# SSH access at this point
'--auth-key' => Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s
'--tarball' => rootfs_tarball
)
end
@ -52,10 +65,10 @@ module Vagrant
end
def validate_box
unless SUPPORTED_VERSIONS.include? @box.metadata.fetch('version').to_i
unless SUPPORTED_VERSIONS.include? box_version
raise Errors::IncompatibleBox.new name: @box.name,
found: @box.metadata.fetch('version').to_i,
supported: SUPPORTED_VERSIONS.join(' and ')
found: box_version,
supported: SUPPORTED_VERSIONS.join(', ')
end
unless File.exists?(template_src)
@ -66,6 +79,10 @@ module Vagrant
raise Errors::RootFSTarballMissing.new name: @box.name
end
end
def box_version
@box.metadata.fetch('version')
end
end
end
end

View file

@ -6,7 +6,7 @@ require 'vagrant-lxc/action/handle_box_metadata'
describe Vagrant::LXC::Action::HandleBoxMetadata do
let(:app) { double(:app, call: true) }
let(:env) { {machine: machine, ui: double(info: true)} }
let(:env) { {machine: machine, ui: double(info: true, warn: true)} }
let(:machine) { double(:machine, box: box) }
let(:box) { double(:box, name: 'box-name', metadata: metadata, directory: box_directory) }
let(:box_directory) { Pathname.new('/path/to/box') }
@ -16,9 +16,54 @@ describe Vagrant::LXC::Action::HandleBoxMetadata do
subject { described_class.new(app, env) }
context 'with valid contents' do
context 'with 1.0.0 box' do
let(:version) { '1.0.0' }
before do
File.stub(exists?: true)
# REFACTOR: This is pretty bad
subject.stub_chain(:template_config_file, :exist?).and_return(true)
subject.stub_chain(:template_config_file, :to_s).and_return(box_directory.join('lxc-config').to_s)
subject.call(env)
end
it 'sets the tarball argument for the template' do
env[:lxc_template_opts].should include(
'--tarball' => box_directory.join('rootfs.tar.gz').to_s
)
end
it 'sets the template --config parameter' do
env[:lxc_template_opts].should include(
'--config' => box_directory.join('lxc-config').to_s
)
end
it 'does not set the auth key argument for the template' do
env[:lxc_template_opts].should_not include(
'--auth-key' => vagrant_key
)
end
it 'sets the template options from metadata on env hash' do
env[:lxc_template_opts].should include(metadata['template-opts'])
end
it 'sets the template source path on env hash' do
env[:lxc_template_src].should == box_directory.join('lxc-template').to_s
end
it 'does not warn about deprecation' do
env[:ui].should_not have_received(:warn)
end
end
context 'with valid pre 1.0.0 box' do
before do
File.stub(exists?: true)
# REFACTOR: This is pretty bad
subject.stub_chain(:old_template_config_file, :exist?).and_return(true)
subject.stub_chain(:old_template_config_file, :to_s).and_return(box_directory.join('lxc.conf').to_s)
subject.call(env)
end
@ -34,6 +79,10 @@ describe Vagrant::LXC::Action::HandleBoxMetadata do
)
end
it 'sets the lxc config file parameter' do
env[:lxc_template_config].should == box_directory.join('lxc.conf').to_s
end
it 'sets the template options from metadata on env hash' do
env[:lxc_template_opts].should include(metadata['template-opts'])
end
@ -41,12 +90,23 @@ describe Vagrant::LXC::Action::HandleBoxMetadata do
it 'sets the template source path on env hash' do
env[:lxc_template_src].should == box_directory.join('lxc-template').to_s
end
it 'warns about deprecation' do
env[:ui].should have_received(:warn)
end
end
describe 'with invalid contents' do
before { File.stub(exists?: true) }
it 'raises an error if the version is != 2' do
it 'validates box versions' do
%w( 2 3 1.0.0 ).each do |v|
metadata['version'] = v
expect {
subject.call(env)
}.to_not raise_error(Vagrant::LXC::Errors::IncompatibleBox)
end
metadata['version'] = '1'
expect {
subject.call(env)