From 1eb7b52da94fb96bd0e2ebb295b65642f87accb5 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Mon, 8 Apr 2013 20:07:06 -0300 Subject: [PATCH] :bomb: custom machine state class --- lib/vagrant-lxc/action.rb | 2 +- lib/vagrant-lxc/action/boot.rb | 11 ++----- lib/vagrant-lxc/action/check_created.rb | 2 +- lib/vagrant-lxc/action/check_running.rb | 2 +- lib/vagrant-lxc/action/created.rb | 2 +- lib/vagrant-lxc/action/forced_halt.rb | 2 +- lib/vagrant-lxc/action/is_running.rb | 2 +- lib/vagrant-lxc/machine_state.rb | 25 ---------------- lib/vagrant-lxc/provider.rb | 7 +++-- spec/unit/machine_state_spec.rb | 39 ------------------------- 10 files changed, 14 insertions(+), 80 deletions(-) delete mode 100644 lib/vagrant-lxc/machine_state.rb delete mode 100644 spec/unit/machine_state_spec.rb diff --git a/lib/vagrant-lxc/action.rb b/lib/vagrant-lxc/action.rb index 64bed0d..217a7d8 100644 --- a/lib/vagrant-lxc/action.rb +++ b/lib/vagrant-lxc/action.rb @@ -124,7 +124,7 @@ module Vagrant b2.use Disconnect b2.use ClearForwardedPorts 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 end end diff --git a/lib/vagrant-lxc/action/boot.rb b/lib/vagrant-lxc/action/boot.rb index e1e8f71..90d700f 100644 --- a/lib/vagrant-lxc/action/boot.rb +++ b/lib/vagrant-lxc/action/boot.rb @@ -11,14 +11,9 @@ module Vagrant 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[:machine].provider.driver.start(config) - raise Vagrant::Errors::VMFailedToBoot if !wait_for_boot - end + env[:ui].info I18n.t("vagrant.actions.vm.boot.booting") + env[:machine].provider.driver.start(config) + raise Vagrant::Errors::VMFailedToBoot if !wait_for_boot @app.call env end diff --git a/lib/vagrant-lxc/action/check_created.rb b/lib/vagrant-lxc/action/check_created.rb index d7bde98..94f7cb8 100644 --- a/lib/vagrant-lxc/action/check_created.rb +++ b/lib/vagrant-lxc/action/check_created.rb @@ -7,7 +7,7 @@ module Vagrant end def call(env) - unless env[:machine].state.created? + if env[:machine].state.id == :not_created raise Vagrant::Errors::VMNotCreatedError end diff --git a/lib/vagrant-lxc/action/check_running.rb b/lib/vagrant-lxc/action/check_running.rb index 4d849ea..c4481a3 100644 --- a/lib/vagrant-lxc/action/check_running.rb +++ b/lib/vagrant-lxc/action/check_running.rb @@ -7,7 +7,7 @@ module Vagrant end def call(env) - unless env[:machine].state.running? + if env[:machine].state.id != :running raise Vagrant::Errors::VMNotRunningError end diff --git a/lib/vagrant-lxc/action/created.rb b/lib/vagrant-lxc/action/created.rb index 3c0420a..71897e9 100644 --- a/lib/vagrant-lxc/action/created.rb +++ b/lib/vagrant-lxc/action/created.rb @@ -8,7 +8,7 @@ module Vagrant def call(env) # 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 # middleware is built to run with the Call-type middlewares) diff --git a/lib/vagrant-lxc/action/forced_halt.rb b/lib/vagrant-lxc/action/forced_halt.rb index b92266f..cb9c670 100644 --- a/lib/vagrant-lxc/action/forced_halt.rb +++ b/lib/vagrant-lxc/action/forced_halt.rb @@ -7,7 +7,7 @@ module Vagrant end 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") # TODO: Driver#halt is kinda graceful as well, if it doesn't # work we can issue a lxc-stop. diff --git a/lib/vagrant-lxc/action/is_running.rb b/lib/vagrant-lxc/action/is_running.rb index 05c035c..0c14e2d 100644 --- a/lib/vagrant-lxc/action/is_running.rb +++ b/lib/vagrant-lxc/action/is_running.rb @@ -7,7 +7,7 @@ module Vagrant end 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 # middleware is built to run with the Call-type middlewares) diff --git a/lib/vagrant-lxc/machine_state.rb b/lib/vagrant-lxc/machine_state.rb deleted file mode 100644 index cbc8361..0000000 --- a/lib/vagrant-lxc/machine_state.rb +++ /dev/null @@ -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 diff --git a/lib/vagrant-lxc/provider.rb b/lib/vagrant-lxc/provider.rb index fdb0fc9..644079c 100644 --- a/lib/vagrant-lxc/provider.rb +++ b/lib/vagrant-lxc/provider.rb @@ -2,7 +2,6 @@ require "log4r" require "vagrant-lxc/action" require "vagrant-lxc/driver" -require "vagrant-lxc/machine_state" module Vagrant module LXC @@ -61,7 +60,11 @@ module Vagrant state_id = :not_created if !@driver.container_name state_id = @driver.state 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 def to_s diff --git a/spec/unit/machine_state_spec.rb b/spec/unit/machine_state_spec.rb deleted file mode 100644 index e1082b4..0000000 --- a/spec/unit/machine_state_spec.rb +++ /dev/null @@ -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