Use lxc-info to determine machine state
This commit is contained in:
parent
ce8902574c
commit
e128b59241
2 changed files with 24 additions and 9 deletions
|
@ -12,8 +12,6 @@ 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
|
||||||
|
|
||||||
CONTAINER_STATE_FILE_PATH = '/tmp/vagrant-lxc-container-state-%<id>s'
|
|
||||||
|
|
||||||
def initialize(machine)
|
def initialize(machine)
|
||||||
@machine = machine
|
@machine = machine
|
||||||
@logger = Log4r::Logger.new("vagrant::provider::lxc::container")
|
@logger = Log4r::Logger.new("vagrant::provider::lxc::container")
|
||||||
|
@ -54,18 +52,14 @@ module Vagrant
|
||||||
File.open(state_file_path, 'w') { |f| f.print state }
|
File.open(state_file_path, 'w') { |f| f.print state }
|
||||||
end
|
end
|
||||||
|
|
||||||
def read_state_from_file
|
def state
|
||||||
if File.exists?(state_file_path)
|
if lxc(:info, '--name', @machine.id) =~ /^state:[^A-Z]+([A-Z]+)$/
|
||||||
File.read(state_file_path).to_sym
|
$1.downcase.to_sym
|
||||||
elsif @machine.id
|
elsif @machine.id
|
||||||
:unknown
|
:unknown
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def state_file_path
|
|
||||||
CONTAINER_STATE_FILE_PATH % {id: @machine.id}
|
|
||||||
end
|
|
||||||
|
|
||||||
# TODO: Review code below this line, it was pretty much a copy and paste from VirtualBox base driver
|
# TODO: Review code below this line, it was pretty much a copy and paste from VirtualBox base driver
|
||||||
def execute(*command, &block)
|
def execute(*command, &block)
|
||||||
# Get the options hash if it exists
|
# Get the options hash if it exists
|
||||||
|
|
|
@ -89,4 +89,25 @@ describe Vagrant::LXC::Container do
|
||||||
subject.should have_received(:wait_until).with(:running)
|
subject.should have_received(:wait_until).with(:running)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe 'state' do
|
||||||
|
let(:machine_id) { 'random-machine-id' }
|
||||||
|
let(:machine) { fire_double('Vagrant::Machine', id: machine_id) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
subject.stub(lxc: "state: STOPPED\npid: 2")
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'calls lxc-info with the right arguments' do
|
||||||
|
subject.state
|
||||||
|
subject.should have_received(:lxc).with(
|
||||||
|
:info,
|
||||||
|
'--name', machine_id
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
|
it 'maps the output of lxc-info status out to a symbol' do
|
||||||
|
subject.state.should == :stopped
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue