Extract container object
This commit is contained in:
parent
1bdd3f87a6
commit
7168da249e
5 changed files with 76 additions and 106 deletions
|
@ -91,7 +91,7 @@ module Vagrant
|
||||||
# TODO: b2.use Vagrant::Action::Builtin::GracefulHalt, :poweroff, :running
|
# TODO: b2.use Vagrant::Action::Builtin::GracefulHalt, :poweroff, :running
|
||||||
unless env[:machine].state.off?
|
unless env[:machine].state.off?
|
||||||
puts 'TODO: Halt container using Vagrant::Action::Builtin::GracefulHalt'
|
puts 'TODO: Halt container using Vagrant::Action::Builtin::GracefulHalt'
|
||||||
env[:machine].state.update!(:poweroff)
|
env[:machine].provider.container.halt
|
||||||
end
|
end
|
||||||
else
|
else
|
||||||
b2.use VagrantPlugins::ProviderVirtualBox::Action::MessageNotCreated
|
b2.use VagrantPlugins::ProviderVirtualBox::Action::MessageNotCreated
|
||||||
|
@ -167,23 +167,22 @@ module Vagrant
|
||||||
def call(env)
|
def call(env)
|
||||||
puts "TODO: Create container"
|
puts "TODO: Create container"
|
||||||
env[:machine].id = 'TODO-set-a-proper-machine-id' unless env[:machine].id
|
env[:machine].id = 'TODO-set-a-proper-machine-id' unless env[:machine].id
|
||||||
|
env[:machine].provider.container.create
|
||||||
@app.call env
|
@app.call env
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Destroy < BaseAction
|
class Destroy < BaseAction
|
||||||
def call(env)
|
def call(env)
|
||||||
puts "TODO: Destroy container"
|
|
||||||
env[:machine].id = nil
|
env[:machine].id = nil
|
||||||
env[:machine].state.update!(:not_created)
|
env[:machine].provider.container.destroy
|
||||||
@app.call env
|
@app.call env
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
class Boot < BaseAction
|
class Boot < BaseAction
|
||||||
def call(env)
|
def call(env)
|
||||||
puts 'TODO: Start container'
|
env[:machine].provider.container.start
|
||||||
env[:machine].state.update!(:running)
|
|
||||||
@app.call env
|
@app.call env
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
52
lib/vagrant-lxc/container.rb
Normal file
52
lib/vagrant-lxc/container.rb
Normal 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
|
|
@ -1,31 +1,12 @@
|
||||||
module Vagrant
|
module Vagrant
|
||||||
module LXC
|
module LXC
|
||||||
class MachineState < Vagrant::MachineState
|
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)
|
def initialize(state_id)
|
||||||
@machine = machine
|
short = state_id.to_s.gsub("_", " ")
|
||||||
end
|
long = I18n.t("vagrant.commands.status.#{state_id}")
|
||||||
|
super(state_id, short, long)
|
||||||
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}")
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def created?
|
def created?
|
||||||
|
@ -39,25 +20,6 @@ module Vagrant
|
||||||
def running?
|
def running?
|
||||||
self.id == :running
|
self.id == :running
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
require "vagrant-lxc/actions"
|
require "vagrant-lxc/actions"
|
||||||
|
require "vagrant-lxc/container"
|
||||||
require "vagrant-lxc/machine_state"
|
require "vagrant-lxc/machine_state"
|
||||||
|
|
||||||
require "log4r"
|
require "log4r"
|
||||||
|
@ -7,9 +8,12 @@ module Vagrant
|
||||||
module LXC
|
module LXC
|
||||||
# DISCUSS: VirtualBox provider has a #machine_id_changed, do we need to handle it as well?
|
# DISCUSS: VirtualBox provider has a #machine_id_changed, do we need to handle it as well?
|
||||||
class Provider < Vagrant.plugin("2", :provider)
|
class Provider < Vagrant.plugin("2", :provider)
|
||||||
|
attr_reader :container
|
||||||
|
|
||||||
def initialize(machine)
|
def initialize(machine)
|
||||||
@logger = Log4r::Logger.new("vagrant::provider::lxc")
|
@logger = Log4r::Logger.new("vagrant::provider::lxc")
|
||||||
@machine = machine
|
@machine = machine
|
||||||
|
@container = Container.new(@machine)
|
||||||
end
|
end
|
||||||
|
|
||||||
# @see Vagrant::Plugin::V1::Provider#action
|
# @see Vagrant::Plugin::V1::Provider#action
|
||||||
|
@ -18,12 +22,13 @@ module Vagrant
|
||||||
# exists, otherwise return nil to show that we don't support the
|
# exists, otherwise return nil to show that we don't support the
|
||||||
# given action.
|
# given action.
|
||||||
action_method = "action_#{name}"
|
action_method = "action_#{name}"
|
||||||
|
# TODO: Rename to singular
|
||||||
return LXC::Actions.send(action_method) if LXC::Actions.respond_to?(action_method)
|
return LXC::Actions.send(action_method) if LXC::Actions.respond_to?(action_method)
|
||||||
nil
|
nil
|
||||||
end
|
end
|
||||||
|
|
||||||
def state
|
def state
|
||||||
LXC::MachineState.new(@machine)
|
LXC::MachineState.new(@container.state)
|
||||||
end
|
end
|
||||||
|
|
||||||
def to_s
|
def to_s
|
||||||
|
|
|
@ -1,48 +1,10 @@
|
||||||
require 'unit_helper'
|
require 'unit_helper'
|
||||||
|
|
||||||
require 'vagrant-lxc/machine_state'
|
require 'vagrant-lxc/machine_state'
|
||||||
|
|
||||||
describe Vagrant::LXC::MachineState do
|
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
|
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
|
it 'is a humanized version of state id' do
|
||||||
subject.short_description.should == 'not created'
|
subject.short_description.should == 'not created'
|
||||||
|
@ -50,22 +12,17 @@ describe Vagrant::LXC::MachineState do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'long description' do
|
describe 'long description' do
|
||||||
before do
|
subject { described_class.new(:short_name) }
|
||||||
subject.stub(id: 'short')
|
before { I18n.stub(t: 'some really long description') }
|
||||||
I18n.stub(t: 'some really long description')
|
|
||||||
end
|
|
||||||
|
|
||||||
it 'is a localized version of the state id' do
|
it 'is a localized version of the state id' do
|
||||||
subject.long_description.should == 'some really long description'
|
subject.long_description.should == 'some really long description'
|
||||||
end
|
I18n.should have_received(:t).with('vagrant.commands.status.short_name')
|
||||||
|
|
||||||
it 'uses the status locale "namespace"' do
|
|
||||||
I18n.should have_received(:t).with('vagrant.commands.status.short')
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when state id is :running' do
|
context 'when state id is :running' do
|
||||||
before { subject.stub(id: :running) }
|
subject { described_class.new(:running) }
|
||||||
|
|
||||||
it { should be_created }
|
it { should be_created }
|
||||||
it { should be_running }
|
it { should be_running }
|
||||||
|
@ -73,15 +30,10 @@ describe Vagrant::LXC::MachineState do
|
||||||
end
|
end
|
||||||
|
|
||||||
context 'when state id is :poweroff' do
|
context 'when state id is :poweroff' do
|
||||||
before { subject.stub(id: :poweroff) }
|
subject { described_class.new(:poweroff) }
|
||||||
|
|
||||||
it { should be_created }
|
it { should be_created }
|
||||||
it { should be_off }
|
it { should be_off }
|
||||||
it { should_not be_running }
|
it { should_not be_running }
|
||||||
end
|
end
|
||||||
|
|
||||||
MACHINE_DEFAULTS = {id: nil}
|
|
||||||
def mocked_machine(stubbed_methods = {})
|
|
||||||
fire_double('Vagrant::Machine', MACHINE_DEFAULTS.merge(stubbed_methods))
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in a new issue