Extract container object

This commit is contained in:
Fabio Rehm 2013-03-01 00:34:51 -03:00
parent 1bdd3f87a6
commit 7168da249e
5 changed files with 76 additions and 106 deletions

View file

@ -91,7 +91,7 @@ module Vagrant
# TODO: b2.use Vagrant::Action::Builtin::GracefulHalt, :poweroff, :running
unless env[:machine].state.off?
puts 'TODO: Halt container using Vagrant::Action::Builtin::GracefulHalt'
env[:machine].state.update!(:poweroff)
env[:machine].provider.container.halt
end
else
b2.use VagrantPlugins::ProviderVirtualBox::Action::MessageNotCreated
@ -167,23 +167,22 @@ module Vagrant
def call(env)
puts "TODO: Create container"
env[:machine].id = 'TODO-set-a-proper-machine-id' unless env[:machine].id
env[:machine].provider.container.create
@app.call env
end
end
class Destroy < BaseAction
def call(env)
puts "TODO: Destroy container"
env[:machine].id = nil
env[:machine].state.update!(:not_created)
env[:machine].provider.container.destroy
@app.call env
end
end
class Boot < BaseAction
def call(env)
puts 'TODO: Start container'
env[:machine].state.update!(:running)
env[:machine].provider.container.start
@app.call env
end
end

View file

@ -0,0 +1,52 @@
module Vagrant
module LXC
class Container
CONTAINER_STATE_FILE_PATH = '/tmp/vagrant-lxc-container-state-%<id>s'
def initialize(machine)
@machine = machine
end
def create
puts 'TODO: Create container'
end
def start
puts 'TODO: Start container'
update!(:running)
end
def halt
update!(:poweroff)
end
def destroy
puts "TODO: Destroy container"
File.delete(state_file_path) if state_file_path
end
def state
# TODO: Grab the real machine state here
read_state_from_file
end
private
def update!(state)
File.open(state_file_path, 'w') { |f| f.print state }
end
def read_state_from_file
if File.exists?(state_file_path)
File.read(state_file_path).to_sym
elsif @machine.id
:unknown
end
end
def state_file_path
CONTAINER_STATE_FILE_PATH % {id: @machine.id}
end
end
end
end

View file

@ -1,31 +1,12 @@
module Vagrant
module LXC
class MachineState < Vagrant::MachineState
CONTAINER_STATE_FILE_PATH = '/tmp/vagrant-lxc-container-state-%<id>s'
CREATED_STATES = %w( running poweroff ).map!(&:to_sym)
CREATED_STATES = %w( running poweroff ).map!(&:to_sym)
def initialize(machine)
@machine = machine
end
def id
@id ||=
begin
state_id = nil
state_id = :not_created if !@machine.id
# TODO: Grab the real machine state here
state_id = read_state_from_file if !state_id
state_id = :unknown if !state_id
state_id
end
end
def short_description
@short ||= self.id.to_s.gsub("_", " ")
end
def long_description
@long ||= I18n.t("vagrant.commands.status.#{self.id}")
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?
@ -39,25 +20,6 @@ module Vagrant
def running?
self.id == :running
end
def update!(state)
return File.delete(state_file_path) if state.to_sym == :not_created
File.open(state_file_path, 'w') { |f| f.print state }
end
def read_state_from_file
if File.exists?(state_file_path)
File.read(state_file_path).to_sym
elsif @machine.id
:unknown
end
end
private :read_state_from_file
def state_file_path
@path ||= CONTAINER_STATE_FILE_PATH % {id: @machine.id}
end
private :state_file_path
end
end
end

View file

@ -1,4 +1,5 @@
require "vagrant-lxc/actions"
require "vagrant-lxc/container"
require "vagrant-lxc/machine_state"
require "log4r"
@ -7,9 +8,12 @@ module Vagrant
module LXC
# DISCUSS: VirtualBox provider has a #machine_id_changed, do we need to handle it as well?
class Provider < Vagrant.plugin("2", :provider)
attr_reader :container
def initialize(machine)
@logger = Log4r::Logger.new("vagrant::provider::lxc")
@machine = machine
@logger = Log4r::Logger.new("vagrant::provider::lxc")
@machine = machine
@container = Container.new(@machine)
end
# @see Vagrant::Plugin::V1::Provider#action
@ -18,12 +22,13 @@ module Vagrant
# exists, otherwise return nil to show that we don't support the
# given action.
action_method = "action_#{name}"
# TODO: Rename to singular
return LXC::Actions.send(action_method) if LXC::Actions.respond_to?(action_method)
nil
end
def state
LXC::MachineState.new(@machine)
LXC::MachineState.new(@container.state)
end
def to_s

View file

@ -1,48 +1,10 @@
require 'unit_helper'
require 'vagrant-lxc/machine_state'
describe Vagrant::LXC::MachineState do
let(:machine) { mocked_machine }
let(:state_file_path) { subject.send(:state_file_path) }
subject { described_class.new(machine) }
after { File.delete state_file_path if File.exists? state_file_path }
# Yeah, I know, this test is not really useful, but vagrant will complain
# if the state is not a Vagrant::MachineState:
# https://github.com/mitchellh/vagrant/blob/master/lib/vagrant/machine.rb#L300
it { should be_a Vagrant::MachineState }
describe 'state id' do
context 'when machine id is not present' do
let(:machine) { mocked_machine(id: nil) }
its(:id) { should == :not_created }
end
context 'when machine id is present' do
let(:machine) { mocked_machine(id: 'machine-id') }
context 'and state file exists' do
before { File.stub(read: 'running', exists?: true) }
after { File.unstub!(:exists?) }
it 'reads it from file' do
subject.id.should == :running
end
end
context 'and state file does not exist' do
it 'returns :unknown' do
subject.id.should == :unknown
end
end
end
end
describe 'short description' do
before { subject.stub(id: :not_created) }
subject { described_class.new(:not_created) }
it 'is a humanized version of state id' do
subject.short_description.should == 'not created'
@ -50,22 +12,17 @@ describe Vagrant::LXC::MachineState do
end
describe 'long description' do
before do
subject.stub(id: 'short')
I18n.stub(t: 'some really long description')
end
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'
end
it 'uses the status locale "namespace"' do
I18n.should have_received(:t).with('vagrant.commands.status.short')
I18n.should have_received(:t).with('vagrant.commands.status.short_name')
end
end
context 'when state id is :running' do
before { subject.stub(id: :running) }
subject { described_class.new(:running) }
it { should be_created }
it { should be_running }
@ -73,15 +30,10 @@ describe Vagrant::LXC::MachineState do
end
context 'when state id is :poweroff' do
before { subject.stub(id: :poweroff) }
subject { described_class.new(:poweroff) }
it { should be_created }
it { should be_off }
it { should_not be_running }
end
MACHINE_DEFAULTS = {id: nil}
def mocked_machine(stubbed_methods = {})
fire_double('Vagrant::Machine', MACHINE_DEFAULTS.merge(stubbed_methods))
end
end