From 976a6ebed77f8fce87878869c8becdd82a7106ef Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Fri, 29 Mar 2013 02:17:34 -0300 Subject: [PATCH] Sets container name based on vagrant root path Closes #14 --- lib/vagrant-lxc/action/create.rb | 5 ++++- lib/vagrant-lxc/container.rb | 4 ++-- spec/unit/container_spec.rb | 8 +++++--- 3 files changed, 11 insertions(+), 6 deletions(-) diff --git a/lib/vagrant-lxc/action/create.rb b/lib/vagrant-lxc/action/create.rb index bbb2d63..eba7b75 100644 --- a/lib/vagrant-lxc/action/create.rb +++ b/lib/vagrant-lxc/action/create.rb @@ -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 diff --git a/lib/vagrant-lxc/container.rb b/lib/vagrant-lxc/container.rb index 4c6f10e..e5e1a32 100644 --- a/lib/vagrant-lxc/container.rb +++ b/lib/vagrant-lxc/container.rb @@ -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, diff --git a/spec/unit/container_spec.rb b/spec/unit/container_spec.rb index 5a316f0..6573e96 100644 --- a/spec/unit/container_spec.rb +++ b/spec/unit/container_spec.rb @@ -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,