diff --git a/lib/vagrant-lxc/action/compress_rootfs.rb b/lib/vagrant-lxc/action/compress_rootfs.rb new file mode 100644 index 0000000..63b7186 --- /dev/null +++ b/lib/vagrant-lxc/action/compress_rootfs.rb @@ -0,0 +1,30 @@ +require "fileutils" + +module Vagrant + module LXC + module Action + class CompressRootFS + def initialize(app, env) + @app = app + end + + def call(env) + raise Vagrant::Errors::VMPowerOffToPackage if env[:machine].provider.state.id != :stopped + + env[:ui].info I18n.t("vagrant.actions.lxc.compressing_rootfs") + @rootfs = env['package.rootfs'] = env[:machine].provider.container.compress_rootfs + + @app.call env + + recover # called to remove the rootfs tarball + end + + def recover(*) + if @rootfs && File.exist?(@rootfs) + FileUtils.rm_rf(File.dirname @rootfs) + end + end + end + end + end +end diff --git a/spec/unit/action/compress_rootfs_spec.rb b/spec/unit/action/compress_rootfs_spec.rb new file mode 100644 index 0000000..17d854e --- /dev/null +++ b/spec/unit/action/compress_rootfs_spec.rb @@ -0,0 +1,27 @@ +require 'unit_helper' + +require 'vagrant-lxc/action/compress_rootfs' + +describe Vagrant::LXC::Action::CompressRootFS do + let(:app) { mock(:app, call: true) } + let(:env) { {machine: machine, ui: stub(info: true)} } + let(:machine) { fire_double('Vagrant::Machine', provider: provider) } + let(:provider) { fire_double('Vagrant::LXC::Provider', container: container) } + let(:container) { fire_double('Vagrant::LXC::Container', compress_rootfs: compressed_rootfs_path) } + let(:compressed_rootfs_path) { '/path/to/rootfs.tar.gz' } + + subject { described_class.new(app, env) } + + before do + provider.stub_chain(:state, :id).and_return(:stopped) + subject.call(env) + end + + it 'asks the container to compress its rootfs' do + container.should have_received(:compress_rootfs) + end + + it 'sets export.temp_dir on action env' do + env['package.rootfs'].should == compressed_rootfs_path + end +end