💣 custom machine state class

This commit is contained in:
Fabio Rehm 2013-04-08 20:07:06 -03:00
parent d4edab4979
commit 1eb7b52da9
10 changed files with 14 additions and 80 deletions

View file

@ -124,7 +124,7 @@ module Vagrant
b2.use Disconnect b2.use Disconnect
b2.use ClearForwardedPorts b2.use ClearForwardedPorts
b2.use Vagrant::Action::Builtin::Call, Vagrant::Action::Builtin::GracefulHalt, :stopped, :running do |env2, b3| b2.use Vagrant::Action::Builtin::Call, Vagrant::Action::Builtin::GracefulHalt, :stopped, :running do |env2, b3|
if !env2[:result] && env2[:machine].provider.state.running? if !env2[:result]
b3.use ForcedHalt b3.use ForcedHalt
end end
end end

View file

@ -11,14 +11,9 @@ module Vagrant
config = env[:machine].provider_config config = env[:machine].provider_config
# Allows this middleware to be called multiple times. We need to
# support this as base boxes might have after create scripts which
# require SSH access
unless env[:machine].state.running?
env[:ui].info I18n.t("vagrant.actions.vm.boot.booting") env[:ui].info I18n.t("vagrant.actions.vm.boot.booting")
env[:machine].provider.driver.start(config) env[:machine].provider.driver.start(config)
raise Vagrant::Errors::VMFailedToBoot if !wait_for_boot raise Vagrant::Errors::VMFailedToBoot if !wait_for_boot
end
@app.call env @app.call env
end end

View file

@ -7,7 +7,7 @@ module Vagrant
end end
def call(env) def call(env)
unless env[:machine].state.created? if env[:machine].state.id == :not_created
raise Vagrant::Errors::VMNotCreatedError raise Vagrant::Errors::VMNotCreatedError
end end

View file

@ -7,7 +7,7 @@ module Vagrant
end end
def call(env) def call(env)
unless env[:machine].state.running? if env[:machine].state.id != :running
raise Vagrant::Errors::VMNotRunningError raise Vagrant::Errors::VMNotRunningError
end end

View file

@ -8,7 +8,7 @@ module Vagrant
def call(env) def call(env)
# Set the result to be true if the machine is created. # Set the result to be true if the machine is created.
env[:result] = env[:machine].state.created? env[:result] = env[:machine].state.id != :not_created
# Call the next if we have one (but we shouldn't, since this # Call the next if we have one (but we shouldn't, since this
# middleware is built to run with the Call-type middlewares) # middleware is built to run with the Call-type middlewares)

View file

@ -7,7 +7,7 @@ module Vagrant
end end
def call(env) def call(env)
if env[:machine].provider.state.running? if env[:machine].provider.state.id == :running
env[:ui].info I18n.t("vagrant.actions.vm.halt.force") env[:ui].info I18n.t("vagrant.actions.vm.halt.force")
# TODO: Driver#halt is kinda graceful as well, if it doesn't # TODO: Driver#halt is kinda graceful as well, if it doesn't
# work we can issue a lxc-stop. # work we can issue a lxc-stop.

View file

@ -7,7 +7,7 @@ module Vagrant
end end
def call(env) def call(env)
env[:result] = env[:machine].state.running? env[:result] = env[:machine].state.id == :running
# Call the next if we have one (but we shouldn't, since this # Call the next if we have one (but we shouldn't, since this
# middleware is built to run with the Call-type middlewares) # middleware is built to run with the Call-type middlewares)

View file

@ -1,25 +0,0 @@
module Vagrant
module LXC
class MachineState < Vagrant::MachineState
CREATED_STATES = %w( running stopped ).map!(&:to_sym)
def initialize(state_id)
short = state_id.to_s.gsub("_", " ")
long = I18n.t("vagrant.commands.status.#{state_id}")
super(state_id, short, long)
end
def created?
CREATED_STATES.include?(self.id)
end
def off?
self.id == :stopped
end
def running?
self.id == :running
end
end
end
end

View file

@ -2,7 +2,6 @@ require "log4r"
require "vagrant-lxc/action" require "vagrant-lxc/action"
require "vagrant-lxc/driver" require "vagrant-lxc/driver"
require "vagrant-lxc/machine_state"
module Vagrant module Vagrant
module LXC module LXC
@ -61,7 +60,11 @@ module Vagrant
state_id = :not_created if !@driver.container_name state_id = :not_created if !@driver.container_name
state_id = @driver.state if !state_id state_id = @driver.state if !state_id
state_id = :unknown if !state_id state_id = :unknown if !state_id
LXC::MachineState.new(state_id)
short = state_id.to_s.gsub("_", " ")
long = I18n.t("vagrant.commands.status.#{state_id}")
Vagrant::MachineState.new(state_id, short, long)
end end
def to_s def to_s

View file

@ -1,39 +0,0 @@
require 'unit_helper'
require 'vagrant-lxc/machine_state'
describe Vagrant::LXC::MachineState do
describe 'short description' do
subject { described_class.new(:not_created) }
it 'is a humanized version of state id' do
subject.short_description.should == 'not created'
end
end
describe 'long description' do
subject { described_class.new(:short_name) }
before { I18n.stub(t: 'some really long description') }
it 'is a localized version of the state id' do
subject.long_description.should == 'some really long description'
I18n.should have_received(:t).with('vagrant.commands.status.short_name')
end
end
context 'when state id is :running' do
subject { described_class.new(:running) }
it { should be_created }
it { should be_running }
it { should_not be_off }
end
context 'when state id is :stopped' do
subject { described_class.new(:stopped) }
it { should be_created }
it { should be_off }
it { should_not be_running }
end
end