Config#start_opts -> Config#customize

This commit is contained in:
Fabio Rehm 2013-04-05 22:08:02 -03:00
parent 2147ec0ba5
commit ee9bfa4189
10 changed files with 39 additions and 21 deletions

View file

@ -65,9 +65,9 @@ Vagrant.configure("2") do |config|
config.vm.provider :lxc do |lxc|
# Same as 'customize ["modifyvm", :id, "--memory", "1024"]' for VirtualBox
lxc.start_opts << 'lxc.cgroup.memory.limit_in_bytes=400M'
lxc.customize 'cgroup.memory.limit_in_bytes', '400M'
# Limits swap size
lxc.start_opts << 'lxc.cgroup.memory.memsw.limit_in_bytes=500M'
lxc.customize 'cgroup.memory.memsw.limit_in_bytes', '500M'
end
# ... your puppet / chef / shell provisioner configs here ...

View file

@ -63,7 +63,7 @@ Vagrant.configure("2") do |config|
lxc_config.vm.provider :lxc do |lxc|
# Required to boot nested containers
lxc.start_opts << 'lxc.aa_profile=unconfined'
lxc.customize 'aa_profile', 'unconfined'
end
end

4
example/Vagrantfile vendored
View file

@ -27,8 +27,8 @@ Vagrant.configure("2") do |config|
config.vm.synced_folder cache_dir, "/var/cache/apt/archives"
config.vm.provider :lxc do |lxc|
lxc.start_opts << 'lxc.cgroup.memory.limit_in_bytes=400M'
lxc.start_opts << 'lxc.cgroup.memory.memsw.limit_in_bytes=500M'
lxc.customize 'cgroup.memory.limit_in_bytes', '400M'
lxc.customize 'cgroup.memory.memsw.limit_in_bytes', '500M'
end
config.vm.provision :shell, :inline => <<-SCRIPT

View file

@ -9,7 +9,7 @@ module Vagrant
def call(env)
@env = env
prepare_folders
add_start_opts
add_override_configs
@app.call env
end
@ -47,7 +47,7 @@ module Vagrant
end
end
def add_start_opts
def add_override_configs
@env[:ui].info I18n.t("vagrant.actions.lxc.share_folders.preparing")
folders = []

View file

@ -1,14 +1,31 @@
module Vagrant
module LXC
class Config < Vagrant.plugin("2", :config)
# An array of options to be passed to lxc-start when booting the machine.
# An array of container's configuration overrides to be provided to `lxc-start`.
#
# @return [Array]
attr_reader :start_opts
attr_reader :customizations
def initialize
@start_opts = []
@customizations = []
end
# Customize the container by calling `lxc-start` with the given
# configuration overrides.
#
# For example, if you want to set the memory limit, you can use it
# like: config.customize 'cgroup.memory.limit_in_bytes', '400M'
#
# When `lxc-start`ing the container, vagrant-lxc will pass in
# "-s lxc.cgroup.memory.limit_in_bytes=400M" to it.
#
# @param [String] key Configuration key to override
# @param [String] value Configuration value to override
def customize(key, value)
@customizations << [key, value]
end
# TODO: At some point in the future it would be nice to validate these options
end
end
end

View file

@ -55,19 +55,19 @@ module Vagrant
end
end
config.start_opts << "lxc.mount.entry=#{folder[:hostpath]} #{guestpath} none bind 0 0"
# TODO: Move outside
config.customize 'mount.entry', "#{folder[:hostpath]} #{guestpath} none bind 0 0"
end
end
def start(config)
@logger.info('Starting container...')
opts = config.start_opts.dup
if ENV['LXC_START_LOG_FILE']
extra = ['-o', ENV['LXC_START_LOG_FILE'], '-l', 'DEBUG']
end
@cli.transition_to(:running) { |c| c.start(opts, (extra || nil)) }
@cli.transition_to(:running) { |c| c.start(config.customizations, (extra || nil)) }
end
def halt

View file

@ -45,10 +45,10 @@ module Vagrant
run :destroy, '--name', @name
end
def start(configs = [], extra_opts = [])
configs = configs.map { |conf| ["-s", conf] }.flatten
configs += extra_opts if extra_opts
run :start, '-d', '--name', @name, *configs
def start(overrides = [], extra_opts = [])
options = overrides.map { |key, value| ["-s", "lxc.#{key}=#{value}"] }.flatten
options += extra_opts if extra_opts
run :start, '-d', '--name', @name, *options
end
def shutdown

View file

@ -17,6 +17,7 @@ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
if ENV['VERIFY_CONSTANT_NAMES']
require 'vagrant-lxc/plugin'
require 'vagrant-lxc/provider'
require 'vagrant-lxc/config'
end
require 'rspec/fire'

View file

@ -80,8 +80,8 @@ describe Vagrant::LXC::Driver::CLI do
)
end
it 'uses provided hash to configure the container' do
subject.start(['lxc.config=value', 'lxc.other=value'])
it 'uses provided array to override container configs' do
subject.start([['config', 'value'], ['other', 'value']])
subject.should have_received(:run).with(:start, '-d', '--name', name,
'-s', 'lxc.config=value',
'-s', 'lxc.other=value'

View file

@ -70,8 +70,8 @@ describe Vagrant::LXC::Driver do
end
describe 'start' do
let(:config) { mock(:config, start_opts: ['a=1', 'b=2']) }
let(:name) { 'container-name' }
let(:config) { fire_double('Vagrant::LXC::Config', customizations: [['a', '1'], ['b', '2']]) }
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', start: true) }
subject { described_class.new(name, cli) }
@ -81,7 +81,7 @@ describe Vagrant::LXC::Driver do
end
it 'starts container with configured lxc settings' do
cli.should_receive(:start).with(['a=1', 'b=2'], nil)
cli.should_receive(:start).with(config.customizations, nil)
subject.start(config)
end