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 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

View file

@ -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

View file

@ -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