From 71c1a401cc2416dd5dc58da7e3d6bb54daa529e2 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sun, 3 Mar 2013 04:37:07 -0300 Subject: [PATCH] Add support for setting custom lxc-start arguments from Vagrantfile Useful for #10 --- example/Vagrantfile | 3 ++- lib/vagrant-lxc/action/boot.rb | 3 ++- lib/vagrant-lxc/config.rb | 8 ++++++++ lib/vagrant-lxc/container.rb | 6 ++++-- spec/unit/container_spec.rb | 9 ++++++--- 5 files changed, 22 insertions(+), 7 deletions(-) diff --git a/example/Vagrantfile b/example/Vagrantfile index 068de5c..4692b2b 100644 --- a/example/Vagrantfile +++ b/example/Vagrantfile @@ -8,6 +8,7 @@ Vagrant.configure("2") do |config| config.vm.hostname = 'ubuntu-cloud-box' config.vm.provider :lxc do |lxc| - # ... soon to come lxc configs... + lxc.start_opts << 'lxc.cgroup.memory.limit_in_bytes=400M' + lxc.start_opts << 'lxc.cgroup.memory.memsw.limit_in_bytes=500M' end end diff --git a/lib/vagrant-lxc/action/boot.rb b/lib/vagrant-lxc/action/boot.rb index 2572314..3509549 100644 --- a/lib/vagrant-lxc/action/boot.rb +++ b/lib/vagrant-lxc/action/boot.rb @@ -3,7 +3,8 @@ module Vagrant module Action class Boot < BaseAction def call(env) - env[:machine].provider.container.start + config = env[:machine].provider_config + env[:machine].provider.container.start(config) @app.call env end end diff --git a/lib/vagrant-lxc/config.rb b/lib/vagrant-lxc/config.rb index 732f9e2..2dc5e3a 100644 --- a/lib/vagrant-lxc/config.rb +++ b/lib/vagrant-lxc/config.rb @@ -1,6 +1,14 @@ module Vagrant module LXC class Config < Vagrant.plugin("2", :config) + # An array of options to be passed to lxc-start when booting the machine. + # + # @return [Array] + attr_reader :start_opts + + def initialize + @start_opts = [] + end end end end diff --git a/lib/vagrant-lxc/container.rb b/lib/vagrant-lxc/container.rb index b3daea1..fac3d49 100644 --- a/lib/vagrant-lxc/container.rb +++ b/lib/vagrant-lxc/container.rb @@ -66,8 +66,10 @@ module Vagrant end end - def start - lxc :start, '-d', '--name', @name + def start(config) + # @logger.info('Starting container...') + opts = config.start_opts.map { |opt| ["-s", opt] }.flatten + lxc :start, '-d', '--name', @name, *opts wait_until :running end diff --git a/spec/unit/container_spec.rb b/spec/unit/container_spec.rb index 72cf496..81aa2d0 100644 --- a/spec/unit/container_spec.rb +++ b/spec/unit/container_spec.rb @@ -139,18 +139,21 @@ describe Vagrant::LXC::Container do end describe 'start' do - let(:name) { 'container-name' } + let(:config) { mock(:config, start_opts: ['a=1', 'b=2']) } + let(:name) { 'container-name' } before do subject.stub(lxc: true, wait_until: true) - subject.start + subject.start(config) end it 'calls lxc-start with the right arguments' do subject.should have_received(:lxc).with( :start, '-d', - '--name', name + '--name', name, + '-s', 'a=1', + '-s', 'b=2' ) end