From 5f102d3e10bb0d0cafcc7083ca1eec07c1b8a35a Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Mon, 29 Jul 2013 12:12:41 -0300 Subject: [PATCH] First stab at persisting lxc customizations on config file instead of using lxc-start "-s" parameter --- lib/vagrant-lxc/driver.rb | 25 +++++++++++++++++++++++-- lib/vagrant-lxc/driver/cli.rb | 2 +- lib/vagrant-lxc/sudo_wrapper.rb | 10 ++++++++++ 3 files changed, 34 insertions(+), 3 deletions(-) diff --git a/lib/vagrant-lxc/driver.rb b/lib/vagrant-lxc/driver.rb index 84da6e5..9289d88 100644 --- a/lib/vagrant-lxc/driver.rb +++ b/lib/vagrant-lxc/driver.rb @@ -72,9 +72,11 @@ module Vagrant if ENV['LXC_START_LOG_FILE'] extra = ['-o', ENV['LXC_START_LOG_FILE'], '-l', 'DEBUG'] end - customizations = customizations + @customizations - @cli.transition_to(:running) { |c| c.start(customizations, (extra || nil)) } + prune_customizations + write_customizations(customizations + @customizations) + + @cli.transition_to(:running) { |c| c.start([], (extra || nil)) } end def forced_halt @@ -121,8 +123,27 @@ module Vagrant end end + def prune_customizations + # Use sed to just strip out the block of code which was inserted + # by Vagrant + @logger.debug 'Prunning vagrant-lxc customizations' + @sudo_wrapper.su_c("sed -e '/^# VAGRANT-BEGIN/,/^# VAGRANT-END/ d' -ibak #{base_path.join('config')}") + end + protected + def write_customizations(customizations) + customizations = customizations.map do |key, value| + "lxc.#{key}=#{value}" + end + customizations.unshift '# VAGRANT-BEGIN' + customizations << '# VAGRANT-END' + + config_file = base_path.join('config').to_s + customizations.each do |line| + @sudo_wrapper.su_c("echo '#{line}' >> #{config_file}") + end + end def import_template(path) template_name = "vagrant-tmp-#{@container_name}" diff --git a/lib/vagrant-lxc/driver/cli.rb b/lib/vagrant-lxc/driver/cli.rb index ee9445c..0bc1829 100644 --- a/lib/vagrant-lxc/driver/cli.rb +++ b/lib/vagrant-lxc/driver/cli.rb @@ -64,7 +64,7 @@ module Vagrant end def start(overrides = [], extra_opts = []) - options = overrides.map { |key, value| ["-s", "lxc.#{key}='#{value}'"] }.flatten + options = [] options += extra_opts if extra_opts run :start, '-d', '--name', @name, *options end diff --git a/lib/vagrant-lxc/sudo_wrapper.rb b/lib/vagrant-lxc/sudo_wrapper.rb index 4bdb54a..6966496 100644 --- a/lib/vagrant-lxc/sudo_wrapper.rb +++ b/lib/vagrant-lxc/sudo_wrapper.rb @@ -14,6 +14,16 @@ module Vagrant execute *(['sudo'] + command) end + def su_c(command) + su_command = if @wrapper_path + "#{@wrapper_path} \"#{command}\"" + else + "su root -c \"#{command}\"" + end + @logger.debug "Running 'sudo #{su_command}'" + system "sudo #{su_command}" + end + private # TODO: Review code below this line, it was pretty much a copy and