Add action to compress container's rootfs

This commit is contained in:
Fabio Rehm 2013-03-30 19:18:05 -03:00
parent 08a949ab82
commit 9705483b21
2 changed files with 57 additions and 0 deletions

View file

@ -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

View file

@ -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