Sets container name based on vagrant root path

Closes #14
This commit is contained in:
Fabio Rehm 2013-03-29 02:17:34 -03:00
parent a33b32c164
commit 976a6ebed7
3 changed files with 11 additions and 6 deletions

View file

@ -3,7 +3,10 @@ module Vagrant
module Action
class Create < BaseAction
def call(env)
machine_id = env[:machine].provider.container.create(env[:machine].box.metadata)
base_name = env[:root_path].basename.to_s
base_name.gsub!(/[^-a-z0-9_]/i, "")
machine_id = env[:machine].provider.container.create(base_name, env[:machine].box.metadata)
env[:machine].id = machine_id
env[:just_created] = true
@app.call env

View file

@ -42,10 +42,10 @@ module Vagrant
Pathname.new("#{base_path}/rootfs")
end
def create(metadata = {})
def create(base_name, metadata = {})
@logger.debug('Creating container using lxc-create...')
@name = SecureRandom.hex(6)
@name = "#{base_name}-#{SecureRandom.hex(6)}"
public_key = Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s
meta_opts = metadata.fetch('template-opts', {}).merge(
'--auth-key' => public_key,

View file

@ -33,7 +33,8 @@ describe Vagrant::LXC::Container do
end
describe 'creation' do
let(:name) { 'random-container-name' }
let(:base_name) { 'container-name' }
let(:suffix) { 'random-suffix' }
let(:template_name) { 'template-name' }
let(:rootfs_cache) { '/path/to/cache' }
let(:public_key_path) { Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s }
@ -42,11 +43,12 @@ describe Vagrant::LXC::Container do
subject { described_class.new(name, cli) }
before do
SecureRandom.stub(hex: name)
subject.create 'template-name' => template_name, 'rootfs-cache-path' => rootfs_cache, 'template-opts' => { '--foo' => 'bar'}
SecureRandom.stub(hex: suffix)
subject.create base_name, 'template-name' => template_name, 'rootfs-cache-path' => rootfs_cache, 'template-opts' => { '--foo' => 'bar'}
end
it 'creates container with the right arguments' do
cli.should have_received(:name=).with("#{base_name}-#{suffix}")
cli.should have_received(:create).with(
template_name,
'--auth-key' => public_key_path,