Remove LXC::Container dependency on a vagrant machine

This commit is contained in:
Fabio Rehm 2013-03-02 00:55:45 -03:00
parent 8ca90f1071
commit 661b00ea22
3 changed files with 23 additions and 26 deletions

View file

@ -12,22 +12,22 @@ module Vagrant
# Include this so we can use `Subprocess` more easily. # Include this so we can use `Subprocess` more easily.
include Vagrant::Util::Retryable include Vagrant::Util::Retryable
def initialize(machine) def initialize(name)
@machine = machine @name = name
@logger = Log4r::Logger.new("vagrant::provider::lxc::container") @logger = Log4r::Logger.new("vagrant::provider::lxc::container")
end end
def create def create
# FIXME: Ruby 1.8 users dont have SecureRandom # 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 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 # TODO: Handle errors
machine_id @name = name
end end
def start def start
lxc :start, '-d', '--name', @machine.id lxc :start, '-d', '--name', @name
wait_until :running wait_until :running
end end
@ -41,7 +41,7 @@ module Vagrant
end end
def wait_until(state) def wait_until(state)
lxc :wait, '--name', @machine.id, '--state', state.to_s.upcase lxc :wait, '--name', @name, '--state', state.to_s.upcase
end end
def lxc(command, *args) def lxc(command, *args)
@ -53,9 +53,9 @@ module Vagrant
end end
def state 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 $1.downcase.to_sym
elsif @machine.id elsif @name
:unknown :unknown
end end
end end

View file

@ -13,7 +13,7 @@ module Vagrant
def initialize(machine) def initialize(machine)
@logger = Log4r::Logger.new("vagrant::provider::lxc") @logger = Log4r::Logger.new("vagrant::provider::lxc")
@machine = machine @machine = machine
@container = Container.new(@machine) @container = Container.new(@machine.id)
end end
# @see Vagrant::Plugin::V1::Provider#action # @see Vagrant::Plugin::V1::Provider#action

View file

@ -3,9 +3,9 @@ require 'unit_helper'
require 'vagrant-lxc/container' require 'vagrant-lxc/container'
describe Vagrant::LXC::Container do describe Vagrant::LXC::Container do
# Default subject and machine for specs # Default subject and container name for specs
let(:machine) { fire_double('Vagrant::Machine') } let(:name) { nil }
subject { described_class.new(machine) } subject { described_class.new(name) }
describe 'lxc commands execution' do describe 'lxc commands execution' do
let(:args) { @args } let(:args) { @args }
@ -30,8 +30,7 @@ describe Vagrant::LXC::Container do
end end
describe 'guard for container state' do describe 'guard for container state' do
let(:machine_id) { 'random-machine-id' } let(:name) { 'random-container-name' }
let(:machine) { fire_double('Vagrant::Machine', id: machine_id) }
before do before do
subject.stub :lxc subject.stub :lxc
@ -41,19 +40,19 @@ describe Vagrant::LXC::Container do
it 'runs lxc-wait with the machine id and upcased state' do it 'runs lxc-wait with the machine id and upcased state' do
subject.should have_received(:lxc).with( subject.should have_received(:lxc).with(
:wait, :wait,
'--name', machine_id, '--name', name,
'--state', 'RUNNING' '--state', 'RUNNING'
) )
end end
end end
describe 'creation' do 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 } let(:public_key_path) { Vagrant.source_root.join('keys', 'vagrant.pub').expand_path.to_s }
before do before do
subject.stub(:lxc) subject.stub(:lxc)
SecureRandom.stub(hex: new_machine_id) SecureRandom.stub(hex: name)
subject.create subject.create
end end
@ -61,7 +60,7 @@ describe Vagrant::LXC::Container do
subject.should have_received(:lxc).with( subject.should have_received(:lxc).with(
:create, :create,
'--template', 'ubuntu-cloud', '--template', 'ubuntu-cloud',
'--name', new_machine_id, '--name', name,
'--', '--',
'-S', public_key_path '-S', public_key_path
) )
@ -69,8 +68,7 @@ describe Vagrant::LXC::Container do
end end
describe 'start' do describe 'start' do
let(:machine_id) { 'random-machine-id' } let(:name) { 'container-name' }
let(:machine) { fire_double('Vagrant::Machine', id: machine_id) }
before do before do
subject.stub(lxc: true, wait_until: true) subject.stub(lxc: true, wait_until: true)
@ -81,7 +79,7 @@ describe Vagrant::LXC::Container do
subject.should have_received(:lxc).with( subject.should have_received(:lxc).with(
:start, :start,
'-d', '-d',
'--name', machine.id '--name', name
) )
end end
@ -91,8 +89,7 @@ describe Vagrant::LXC::Container do
end end
describe 'state' do describe 'state' do
let(:machine_id) { 'random-machine-id' } let(:name) { 'random-container-name' }
let(:machine) { fire_double('Vagrant::Machine', id: machine_id) }
before do before do
subject.stub(lxc: "state: STOPPED\npid: 2") subject.stub(lxc: "state: STOPPED\npid: 2")
@ -102,7 +99,7 @@ describe Vagrant::LXC::Container do
subject.state subject.state
subject.should have_received(:lxc).with( subject.should have_received(:lxc).with(
:info, :info,
'--name', machine_id '--name', name
) )
end end