Add support for starting processes inside a running container using lxc-attach

This commit is contained in:
Fabio Rehm 2013-03-19 01:25:27 -03:00
parent 555d7d61e2
commit e8388743ca
3 changed files with 33 additions and 1 deletions

View file

@ -1,4 +1,3 @@
# FIXME: Ruby 1.8 users dont have SecureRandom
require 'securerandom'
require "vagrant-lxc/errors"

View file

@ -56,6 +56,18 @@ module Vagrant
run :shutdown, '--name', @name
end
def attach(*cmd)
cmd = ['--'] + cmd
if cmd.last.is_a?(Hash)
opts = cmd.pop
namespaces = Array(opts[:namespaces]).map(&:upcase).join('|')
extra = ['--namespaces', namespaces] if namespaces
end
run :attach, '--name', @name, *((extra || []) + cmd)
end
def transition_to(state, &block)
raise TransitionBlockNotProvided unless block_given?

View file

@ -122,6 +122,27 @@ describe Vagrant::LXC::Container::CLI do
end
end
describe 'attach' do
let(:name) { 'a-running-container' }
let(:command) { ['ls', 'cat /tmp/file'] }
let(:command_output) { 'folders list' }
subject { described_class.new(name) }
before do
subject.stub(run: command_output)
end
it 'calls lxc-attach with specified command' do
subject.attach(*command)
subject.should have_received(:run).with(:attach, '--name', name, '--', *command)
end
it 'supports a "namespaces" parameter' do
subject.attach *(command + [{namespaces: ['network', 'mount']}])
subject.should have_received(:run).with(:attach, '--name', name, '--namespaces', 'NETWORK|MOUNT', '--', *command)
end
end
describe 'transition block' do
let(:name) { 'a-running-container' }
subject { described_class.new(name) }