parent
c7c54457ce
commit
306deaa0e2
6 changed files with 46 additions and 20 deletions
|
@ -6,7 +6,9 @@ module Vagrant
|
||||||
base_name = env[:root_path].basename.to_s
|
base_name = env[:root_path].basename.to_s
|
||||||
base_name.gsub!(/[^-a-z0-9_]/i, "")
|
base_name.gsub!(/[^-a-z0-9_]/i, "")
|
||||||
|
|
||||||
machine_id = env[:machine].provider.container.create(base_name, env[:machine].box.metadata)
|
target_rootfs_path = env[:machine].provider_config.target_rootfs_path
|
||||||
|
|
||||||
|
machine_id = env[:machine].provider.container.create(base_name, target_rootfs_path, env[:machine].box.metadata)
|
||||||
env[:machine].id = machine_id
|
env[:machine].id = machine_id
|
||||||
env[:just_created] = true
|
env[:just_created] = true
|
||||||
@app.call env
|
@app.call env
|
||||||
|
|
|
@ -6,8 +6,15 @@ module Vagrant
|
||||||
# @return [Array]
|
# @return [Array]
|
||||||
attr_reader :start_opts
|
attr_reader :start_opts
|
||||||
|
|
||||||
|
# Base directory to store container's rootfs
|
||||||
|
#
|
||||||
|
# Defaults to nil, which means it will be stored wherever the lxc template
|
||||||
|
# tells it to be stored
|
||||||
|
attr_accessor :target_rootfs_path
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@start_opts = []
|
@start_opts = []
|
||||||
|
@target_rootfs_path = nil
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -36,10 +36,10 @@ module Vagrant
|
||||||
end
|
end
|
||||||
|
|
||||||
def rootfs_path
|
def rootfs_path
|
||||||
Pathname.new("#{base_path}/rootfs")
|
Pathname.new(base_path.join('config').read.match(/^lxc\.rootfs\s+=\s+(.+)$/)[1])
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(base_name, metadata = {})
|
def create(base_name, target_rootfs_path, metadata = {})
|
||||||
@logger.debug('Creating container using lxc-create...')
|
@logger.debug('Creating container using lxc-create...')
|
||||||
|
|
||||||
@name = "#{base_name}-#{SecureRandom.hex(6)}"
|
@name = "#{base_name}-#{SecureRandom.hex(6)}"
|
||||||
|
@ -50,7 +50,7 @@ module Vagrant
|
||||||
)
|
)
|
||||||
|
|
||||||
@cli.name = @name
|
@cli.name = @name
|
||||||
@cli.create(metadata.fetch('template-name'), meta_opts)
|
@cli.create(metadata.fetch('template-name'), target_rootfs_path, meta_opts)
|
||||||
|
|
||||||
@name
|
@name
|
||||||
end
|
end
|
||||||
|
|
|
@ -31,15 +31,18 @@ module Vagrant
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def create(template, template_opts = {})
|
def create(template, target_rootfs_path, template_opts = {})
|
||||||
extra = template_opts.to_a.flatten
|
extra = template_opts.to_a.flatten
|
||||||
extra.unshift '--' unless extra.empty?
|
extra.unshift '--' unless extra.empty?
|
||||||
|
|
||||||
|
rootfs_args = target_rootfs_path ?
|
||||||
|
['-B', 'dir', '--dir', target_rootfs_path] :
|
||||||
|
[]
|
||||||
|
|
||||||
run :create,
|
run :create,
|
||||||
# lxc-create options
|
|
||||||
'--template', template,
|
'--template', template,
|
||||||
'--name', @name,
|
'--name', @name,
|
||||||
*extra
|
*(rootfs_args + extra)
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy
|
def destroy
|
||||||
|
|
|
@ -33,19 +33,31 @@ describe Vagrant::LXC::Container::CLI do
|
||||||
subject { described_class.new(name) }
|
subject { described_class.new(name) }
|
||||||
|
|
||||||
before do
|
before do
|
||||||
subject.stub(:run)
|
subject.stub(:run) { |*args| @run_args = args }
|
||||||
subject.create(template, template_args)
|
subject.create(template, rootfs, template_args)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'issues a lxc-create with provided template, container name and hash of arguments' do
|
context 'when no rootfs is passed' do
|
||||||
subject.should have_received(:run).with(
|
let(:rootfs) { nil }
|
||||||
:create,
|
|
||||||
'--template', template,
|
it 'issues a lxc-create with provided template, container name and hash of arguments' do
|
||||||
'--name', name,
|
subject.should have_received(:run).with(
|
||||||
'--',
|
:create,
|
||||||
'--extra-param', 'param',
|
'--template', template,
|
||||||
'--other', 'value'
|
'--name', name,
|
||||||
)
|
'--',
|
||||||
|
'--extra-param', 'param',
|
||||||
|
'--other', 'value'
|
||||||
|
)
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'when the rootfs is passed' do
|
||||||
|
let(:rootfs) { 'rootfs_path' }
|
||||||
|
|
||||||
|
it 'issues a lxc-create with the right rootfs arguments' do
|
||||||
|
@run_args.join(' ').should =~ /-B dir --dir #{rootfs}/
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -37,6 +37,7 @@ describe Vagrant::LXC::Container do
|
||||||
let(:suffix) { 'random-suffix' }
|
let(:suffix) { 'random-suffix' }
|
||||||
let(:template_name) { 'template-name' }
|
let(:template_name) { 'template-name' }
|
||||||
let(:rootfs_cache) { '/path/to/cache' }
|
let(:rootfs_cache) { '/path/to/cache' }
|
||||||
|
let(:target_rootfs) { '/path/to/rootfs' }
|
||||||
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 }
|
||||||
let(:cli) { fire_double('Vagrant::LXC::Container::CLI', :create => true, :name= => true) }
|
let(:cli) { fire_double('Vagrant::LXC::Container::CLI', :create => true, :name= => true) }
|
||||||
|
|
||||||
|
@ -44,13 +45,14 @@ describe Vagrant::LXC::Container do
|
||||||
|
|
||||||
before do
|
before do
|
||||||
SecureRandom.stub(hex: suffix)
|
SecureRandom.stub(hex: suffix)
|
||||||
subject.create base_name, 'template-name' => template_name, 'rootfs-cache-path' => rootfs_cache, 'template-opts' => { '--foo' => 'bar'}
|
subject.create base_name, target_rootfs, 'template-name' => template_name, 'rootfs-cache-path' => rootfs_cache, 'template-opts' => { '--foo' => 'bar'}
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'creates container with the right arguments' do
|
it 'creates container with the right arguments' do
|
||||||
cli.should have_received(:name=).with("#{base_name}-#{suffix}")
|
cli.should have_received(:name=).with("#{base_name}-#{suffix}")
|
||||||
cli.should have_received(:create).with(
|
cli.should have_received(:create).with(
|
||||||
template_name,
|
template_name,
|
||||||
|
target_rootfs,
|
||||||
'--auth-key' => public_key_path,
|
'--auth-key' => public_key_path,
|
||||||
'--cache' => rootfs_cache,
|
'--cache' => rootfs_cache,
|
||||||
'--foo' => 'bar'
|
'--foo' => 'bar'
|
||||||
|
|
Loading…
Add table
Reference in a new issue