From 661b00ea22e67503637a9661a384881a114b511f Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sat, 2 Mar 2013 00:55:45 -0300 Subject: [PATCH] Remove LXC::Container dependency on a vagrant machine --- lib/vagrant-lxc/container.rb | 20 ++++++++++---------- lib/vagrant-lxc/provider.rb | 2 +- spec/unit/container_spec.rb | 27 ++++++++++++--------------- 3 files changed, 23 insertions(+), 26 deletions(-) diff --git a/lib/vagrant-lxc/container.rb b/lib/vagrant-lxc/container.rb index 40ae6d0..3db6265 100644 --- a/lib/vagrant-lxc/container.rb +++ b/lib/vagrant-lxc/container.rb @@ -12,22 +12,22 @@ module Vagrant # Include this so we can use `Subprocess` more easily. include Vagrant::Util::Retryable - def initialize(machine) - @machine = machine - @logger = Log4r::Logger.new("vagrant::provider::lxc::container") + def initialize(name) + @name = name + @logger = Log4r::Logger.new("vagrant::provider::lxc::container") end def create # FIXME: Ruby 1.8 users dont have SecureRandom - machine_id = SecureRandom.hex(6) + name = SecureRandom.hex(6) public_key = Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s - log, status = lxc :create, '--template', 'ubuntu-cloud', '--name', machine_id, '--', '-S', public_key + log, status = lxc :create, '--template', 'ubuntu-cloud', '--name', name, '--', '-S', public_key # TODO: Handle errors - machine_id + @name = name end def start - lxc :start, '-d', '--name', @machine.id + lxc :start, '-d', '--name', @name wait_until :running end @@ -41,7 +41,7 @@ module Vagrant end def wait_until(state) - lxc :wait, '--name', @machine.id, '--state', state.to_s.upcase + lxc :wait, '--name', @name, '--state', state.to_s.upcase end def lxc(command, *args) @@ -53,9 +53,9 @@ module Vagrant end def state - if lxc(:info, '--name', @machine.id) =~ /^state:[^A-Z]+([A-Z]+)$/ + if lxc(:info, '--name', @name) =~ /^state:[^A-Z]+([A-Z]+)$/ $1.downcase.to_sym - elsif @machine.id + elsif @name :unknown end end diff --git a/lib/vagrant-lxc/provider.rb b/lib/vagrant-lxc/provider.rb index 9f49caf..87bb07c 100644 --- a/lib/vagrant-lxc/provider.rb +++ b/lib/vagrant-lxc/provider.rb @@ -13,7 +13,7 @@ module Vagrant def initialize(machine) @logger = Log4r::Logger.new("vagrant::provider::lxc") @machine = machine - @container = Container.new(@machine) + @container = Container.new(@machine.id) end # @see Vagrant::Plugin::V1::Provider#action diff --git a/spec/unit/container_spec.rb b/spec/unit/container_spec.rb index 7503119..2550ab3 100644 --- a/spec/unit/container_spec.rb +++ b/spec/unit/container_spec.rb @@ -3,9 +3,9 @@ require 'unit_helper' require 'vagrant-lxc/container' describe Vagrant::LXC::Container do - # Default subject and machine for specs - let(:machine) { fire_double('Vagrant::Machine') } - subject { described_class.new(machine) } + # Default subject and container name for specs + let(:name) { nil } + subject { described_class.new(name) } describe 'lxc commands execution' do let(:args) { @args } @@ -30,8 +30,7 @@ describe Vagrant::LXC::Container do end describe 'guard for container state' do - let(:machine_id) { 'random-machine-id' } - let(:machine) { fire_double('Vagrant::Machine', id: machine_id) } + let(:name) { 'random-container-name' } before do subject.stub :lxc @@ -41,19 +40,19 @@ describe Vagrant::LXC::Container do it 'runs lxc-wait with the machine id and upcased state' do subject.should have_received(:lxc).with( :wait, - '--name', machine_id, + '--name', name, '--state', 'RUNNING' ) end end describe 'creation' do - let(:new_machine_id) { 'random-machine-id' } + let(:name) { 'random-container-name' } let(:public_key_path) { Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s } before do subject.stub(:lxc) - SecureRandom.stub(hex: new_machine_id) + SecureRandom.stub(hex: name) subject.create end @@ -61,7 +60,7 @@ describe Vagrant::LXC::Container do subject.should have_received(:lxc).with( :create, '--template', 'ubuntu-cloud', - '--name', new_machine_id, + '--name', name, '--', '-S', public_key_path ) @@ -69,8 +68,7 @@ describe Vagrant::LXC::Container do end describe 'start' do - let(:machine_id) { 'random-machine-id' } - let(:machine) { fire_double('Vagrant::Machine', id: machine_id) } + let(:name) { 'container-name' } before do subject.stub(lxc: true, wait_until: true) @@ -81,7 +79,7 @@ describe Vagrant::LXC::Container do subject.should have_received(:lxc).with( :start, '-d', - '--name', machine.id + '--name', name ) end @@ -91,8 +89,7 @@ describe Vagrant::LXC::Container do end describe 'state' do - let(:machine_id) { 'random-machine-id' } - let(:machine) { fire_double('Vagrant::Machine', id: machine_id) } + let(:name) { 'random-container-name' } before do subject.stub(lxc: "state: STOPPED\npid: 2") @@ -102,7 +99,7 @@ describe Vagrant::LXC::Container do subject.state subject.should have_received(:lxc).with( :info, - '--name', machine_id + '--name', name ) end