Handle new box metadata

This commit is contained in:
Fabio Rehm 2013-03-07 02:09:09 -03:00
parent 75378caf01
commit cf2da28625
4 changed files with 21 additions and 28 deletions

View file

@ -13,26 +13,21 @@ module Vagrant
def call(env) def call(env)
box = env[:machine].box box = env[:machine].box
metadata = box.metadata metadata = box.metadata
template_name = metadata['template-name']
after_create = metadata['after-create-script'] ?
box.directory.join(metadata['after-create-script']).to_s :
nil
metadata.merge!( metadata.merge!(
'template-name' => "vagrant-#{box.name}-#{template_name}", 'template-name' => "vagrant-#{box.name}",
'lxc-cache-path' => box.directory.to_s, 'lxc-cache-path' => box.directory.to_s
'after-create-script' => after_create
) )
# Prepends "lxc-" to the template file so that `lxc-create` is able to find it # Prepends "lxc-" to the template file so that `lxc-create` is able to find it
src = box.directory.join('lxc-template').to_s
dest = LXC_TEMPLATES_PATH.join("lxc-#{metadata['template-name']}").to_s dest = LXC_TEMPLATES_PATH.join("lxc-#{metadata['template-name']}").to_s
src = box.directory.join(template_name).to_s
@logger.debug('Copying LXC template into place') @logger.debug('Copying LXC template into place')
# This should only ask for administrative permission once, even # This should only ask for administrative permission once, even
# though its executed in multiple subshells. # though its executed in multiple subshells.
system(%Q[sudo su root -c "cp #{src} #{dest}"]) system(%Q[sudo su root -c "cp #{src} #{dest}"])
system(%Q[sudo su root -c "cd #{box.directory} && tar xfz rootfs.tar.gz"])
@app.call(env) @app.call(env)
end end

View file

@ -37,6 +37,10 @@ module Vagrant
@name = SecureRandom.hex(6) @name = SecureRandom.hex(6)
public_key = Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s public_key = Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s
meta_opts = metadata.fetch('template-opts', {}).map do |opt, value|
[opt, value]
end.flatten
# TODO: Handle errors # TODO: Handle errors
lxc :create, lxc :create,
# lxc-create options # lxc-create options
@ -44,9 +48,9 @@ module Vagrant
'--name', @name, '--name', @name,
'--', '--',
# Template options # Template options
'-S', public_key, '--auth-key', public_key,
'--cache-path', metadata['lxc-cache-path'], '--cache', metadata['lxc-cache-path'],
'-T', metadata['tar-cache'] *meta_opts
@name @name
end end

View file

@ -4,10 +4,7 @@ require 'vagrant-lxc/action/base_action'
require 'vagrant-lxc/action/handle_box_metadata' require 'vagrant-lxc/action/handle_box_metadata'
describe Vagrant::LXC::Action::HandleBoxMetadata do describe Vagrant::LXC::Action::HandleBoxMetadata do
let(:tar_cache) { 'template.zip' } let(:metadata) { {'template-opts' => {'--foo' => 'bar'}} }
let(:template_name) { 'ubuntu-lts' }
let(:after_create) { 'setup-vagrant-user.sh' }
let(:metadata) { {'template-name' => template_name, 'tar-cache' => tar_cache, 'after-create-script' => after_create} }
let(:box) { mock(:box, name: 'box-name', metadata: metadata, directory: Pathname.new('/path/to/box')) } let(:box) { mock(:box, name: 'box-name', metadata: metadata, directory: Pathname.new('/path/to/box')) }
let(:machine) { mock(:machine, box: box) } let(:machine) { mock(:machine, box: box) }
let(:app) { mock(:app, call: true) } let(:app) { mock(:app, call: true) }
@ -24,17 +21,15 @@ describe Vagrant::LXC::Action::HandleBoxMetadata do
metadata['lxc-cache-path'].should == box.directory.to_s metadata['lxc-cache-path'].should == box.directory.to_s
end end
it 'prepends box directory to after-create-script' do
metadata['after-create-script'].should == "#{box.directory.to_s}/#{after_create}"
end
it 'prepends vagrant and box name to template-name' do it 'prepends vagrant and box name to template-name' do
metadata['template-name'].should == "vagrant-#{box.name}-#{template_name}" metadata['template-name'].should == "vagrant-#{box.name}"
end end
it 'copies box template file to the right folder' do it 'copies box template file to the right folder' do
src = box.directory.join(template_name).to_s src = box.directory.join('lxc-template').to_s
dest = "/usr/share/lxc/templates/lxc-#{metadata['template-name']}" dest = "/usr/share/lxc/templates/lxc-#{metadata['template-name']}"
subject.should have_received(:system).with("sudo su root -c \"cp #{src} #{dest}\"") subject.should have_received(:system).with("sudo su root -c \"cp #{src} #{dest}\"")
end end
pending 'extracts rootfs'
end end

View file

@ -78,14 +78,13 @@ describe Vagrant::LXC::Container do
describe 'creation' do describe 'creation' do
let(:name) { 'random-container-name' } let(:name) { 'random-container-name' }
let(:template_name) { 'template-name' } let(:template_name) { 'template-name' }
let(:tar_cache_path) { '/path/to/tar/cache' }
let(:lxc_cache) { '/path/to/cache' } let(:lxc_cache) { '/path/to/cache' }
let(:public_key_path) { Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s } let(:public_key_path) { Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s }
before do before do
subject.stub(lxc: true) subject.stub(lxc: true)
SecureRandom.stub(hex: name) SecureRandom.stub(hex: name)
subject.create 'template-name' => template_name, 'tar-cache' => tar_cache_path, 'lxc-cache-path' => lxc_cache subject.create 'template-name' => template_name, 'lxc-cache-path' => lxc_cache, 'template-opts' => { '--foo' => 'bar'}
end end
it 'calls lxc-create with the right arguments' do it 'calls lxc-create with the right arguments' do
@ -94,9 +93,9 @@ describe Vagrant::LXC::Container do
'--template', template_name, '--template', template_name,
'--name', name, '--name', name,
'--', '--',
'-S', public_key_path, '--auth-key', public_key_path,
'--cache-path', lxc_cache, '--cache', lxc_cache,
'-T', tar_cache_path '--foo', 'bar'
) )
end end
end end