Config#start_opts -> Config#customize
This commit is contained in:
parent
2147ec0ba5
commit
ee9bfa4189
10 changed files with 39 additions and 21 deletions
|
@ -65,9 +65,9 @@ Vagrant.configure("2") do |config|
|
||||||
|
|
||||||
config.vm.provider :lxc do |lxc|
|
config.vm.provider :lxc do |lxc|
|
||||||
# Same as 'customize ["modifyvm", :id, "--memory", "1024"]' for VirtualBox
|
# 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
|
# Limits swap size
|
||||||
lxc.start_opts << 'lxc.cgroup.memory.memsw.limit_in_bytes=500M'
|
lxc.customize 'cgroup.memory.memsw.limit_in_bytes', '500M'
|
||||||
end
|
end
|
||||||
|
|
||||||
# ... your puppet / chef / shell provisioner configs here ...
|
# ... your puppet / chef / shell provisioner configs here ...
|
||||||
|
|
2
development/Vagrantfile
vendored
2
development/Vagrantfile
vendored
|
@ -63,7 +63,7 @@ Vagrant.configure("2") do |config|
|
||||||
|
|
||||||
lxc_config.vm.provider :lxc do |lxc|
|
lxc_config.vm.provider :lxc do |lxc|
|
||||||
# Required to boot nested containers
|
# Required to boot nested containers
|
||||||
lxc.start_opts << 'lxc.aa_profile=unconfined'
|
lxc.customize 'aa_profile', 'unconfined'
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
4
example/Vagrantfile
vendored
4
example/Vagrantfile
vendored
|
@ -27,8 +27,8 @@ Vagrant.configure("2") do |config|
|
||||||
config.vm.synced_folder cache_dir, "/var/cache/apt/archives"
|
config.vm.synced_folder cache_dir, "/var/cache/apt/archives"
|
||||||
|
|
||||||
config.vm.provider :lxc do |lxc|
|
config.vm.provider :lxc do |lxc|
|
||||||
lxc.start_opts << 'lxc.cgroup.memory.limit_in_bytes=400M'
|
lxc.customize 'cgroup.memory.limit_in_bytes', '400M'
|
||||||
lxc.start_opts << 'lxc.cgroup.memory.memsw.limit_in_bytes=500M'
|
lxc.customize 'cgroup.memory.memsw.limit_in_bytes', '500M'
|
||||||
end
|
end
|
||||||
|
|
||||||
config.vm.provision :shell, :inline => <<-SCRIPT
|
config.vm.provision :shell, :inline => <<-SCRIPT
|
||||||
|
|
|
@ -9,7 +9,7 @@ module Vagrant
|
||||||
def call(env)
|
def call(env)
|
||||||
@env = env
|
@env = env
|
||||||
prepare_folders
|
prepare_folders
|
||||||
add_start_opts
|
add_override_configs
|
||||||
@app.call env
|
@app.call env
|
||||||
end
|
end
|
||||||
|
|
||||||
|
@ -47,7 +47,7 @@ module Vagrant
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def add_start_opts
|
def add_override_configs
|
||||||
@env[:ui].info I18n.t("vagrant.actions.lxc.share_folders.preparing")
|
@env[:ui].info I18n.t("vagrant.actions.lxc.share_folders.preparing")
|
||||||
|
|
||||||
folders = []
|
folders = []
|
||||||
|
|
|
@ -1,14 +1,31 @@
|
||||||
module Vagrant
|
module Vagrant
|
||||||
module LXC
|
module LXC
|
||||||
class Config < Vagrant.plugin("2", :config)
|
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]
|
# @return [Array]
|
||||||
attr_reader :start_opts
|
attr_reader :customizations
|
||||||
|
|
||||||
def initialize
|
def initialize
|
||||||
@start_opts = []
|
@customizations = []
|
||||||
end
|
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
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -55,19 +55,19 @@ module Vagrant
|
||||||
end
|
end
|
||||||
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
|
||||||
end
|
end
|
||||||
|
|
||||||
def start(config)
|
def start(config)
|
||||||
@logger.info('Starting container...')
|
@logger.info('Starting container...')
|
||||||
|
|
||||||
opts = config.start_opts.dup
|
|
||||||
if ENV['LXC_START_LOG_FILE']
|
if ENV['LXC_START_LOG_FILE']
|
||||||
extra = ['-o', ENV['LXC_START_LOG_FILE'], '-l', 'DEBUG']
|
extra = ['-o', ENV['LXC_START_LOG_FILE'], '-l', 'DEBUG']
|
||||||
end
|
end
|
||||||
|
|
||||||
@cli.transition_to(:running) { |c| c.start(opts, (extra || nil)) }
|
@cli.transition_to(:running) { |c| c.start(config.customizations, (extra || nil)) }
|
||||||
end
|
end
|
||||||
|
|
||||||
def halt
|
def halt
|
||||||
|
|
|
@ -45,10 +45,10 @@ module Vagrant
|
||||||
run :destroy, '--name', @name
|
run :destroy, '--name', @name
|
||||||
end
|
end
|
||||||
|
|
||||||
def start(configs = [], extra_opts = [])
|
def start(overrides = [], extra_opts = [])
|
||||||
configs = configs.map { |conf| ["-s", conf] }.flatten
|
options = overrides.map { |key, value| ["-s", "lxc.#{key}=#{value}"] }.flatten
|
||||||
configs += extra_opts if extra_opts
|
options += extra_opts if extra_opts
|
||||||
run :start, '-d', '--name', @name, *configs
|
run :start, '-d', '--name', @name, *options
|
||||||
end
|
end
|
||||||
|
|
||||||
def shutdown
|
def shutdown
|
||||||
|
|
|
@ -17,6 +17,7 @@ Dir[File.dirname(__FILE__) + "/support/**/*.rb"].each { |f| require f }
|
||||||
if ENV['VERIFY_CONSTANT_NAMES']
|
if ENV['VERIFY_CONSTANT_NAMES']
|
||||||
require 'vagrant-lxc/plugin'
|
require 'vagrant-lxc/plugin'
|
||||||
require 'vagrant-lxc/provider'
|
require 'vagrant-lxc/provider'
|
||||||
|
require 'vagrant-lxc/config'
|
||||||
end
|
end
|
||||||
|
|
||||||
require 'rspec/fire'
|
require 'rspec/fire'
|
||||||
|
|
|
@ -80,8 +80,8 @@ describe Vagrant::LXC::Driver::CLI do
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'uses provided hash to configure the container' do
|
it 'uses provided array to override container configs' do
|
||||||
subject.start(['lxc.config=value', 'lxc.other=value'])
|
subject.start([['config', 'value'], ['other', 'value']])
|
||||||
subject.should have_received(:run).with(:start, '-d', '--name', name,
|
subject.should have_received(:run).with(:start, '-d', '--name', name,
|
||||||
'-s', 'lxc.config=value',
|
'-s', 'lxc.config=value',
|
||||||
'-s', 'lxc.other=value'
|
'-s', 'lxc.other=value'
|
||||||
|
|
|
@ -70,8 +70,8 @@ describe Vagrant::LXC::Driver do
|
||||||
end
|
end
|
||||||
|
|
||||||
describe 'start' do
|
describe 'start' do
|
||||||
let(:config) { mock(:config, start_opts: ['a=1', 'b=2']) }
|
|
||||||
let(:name) { 'container-name' }
|
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) }
|
let(:cli) { fire_double('Vagrant::LXC::Driver::CLI', start: true) }
|
||||||
|
|
||||||
subject { described_class.new(name, cli) }
|
subject { described_class.new(name, cli) }
|
||||||
|
@ -81,7 +81,7 @@ describe Vagrant::LXC::Driver do
|
||||||
end
|
end
|
||||||
|
|
||||||
it 'starts container with configured lxc settings' do
|
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)
|
subject.start(config)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue