From 0d9d80846e35820449401628d2970394b5e44b93 Mon Sep 17 00:00:00 2001 From: Fabio Rehm Date: Sat, 30 Mar 2013 19:18:52 -0300 Subject: [PATCH] Add action to setup box package files --- lib/vagrant-lxc/action/setup_package_files.rb | 52 +++++++++++++++++++ spec/unit/action/setup_package_files_spec.rb | 41 +++++++++++++++ 2 files changed, 93 insertions(+) create mode 100644 lib/vagrant-lxc/action/setup_package_files.rb create mode 100644 spec/unit/action/setup_package_files_spec.rb diff --git a/lib/vagrant-lxc/action/setup_package_files.rb b/lib/vagrant-lxc/action/setup_package_files.rb new file mode 100644 index 0000000..a1a8a73 --- /dev/null +++ b/lib/vagrant-lxc/action/setup_package_files.rb @@ -0,0 +1,52 @@ +require 'fileutils' + +module Vagrant + module LXC + module Action + class SetupPackageFiles + def initialize(app, env) + @app = app + + env["package.include"] ||= [] + env["package.vagrantfile"] ||= nil + end + + def call(env) + @env = env + + create_package_temp_dir + move_rootfs_to_pkg_dir + copy_box_files_to_pkg_dir + + @app.call env + + recover # called to cleanup temp directory + end + + def recover(*) + if @temp_dir && File.exist?(@temp_dir) + FileUtils.rm_rf(@temp_dir) + end + end + + private + + def create_package_temp_dir + @env[:ui].info I18n.t("vagrant.actions.vm.export.create_dir") + @temp_dir = @env["package.directory"] = @env[:tmp_path].join("container-export-#{Time.now.to_i.to_s}") + FileUtils.mkpath(@temp_dir) + end + + def move_rootfs_to_pkg_dir + FileUtils.mv @env['package.rootfs'].to_s, @env['package.directory'].to_s + end + + def copy_box_files_to_pkg_dir + box_dir = @env[:machine].box.directory + FileUtils.cp box_dir.join('lxc-template').to_s, @env['package.directory'].to_s + FileUtils.cp box_dir.join('metadata.json').to_s, @env['package.directory'].to_s + end + end + end + end +end diff --git a/spec/unit/action/setup_package_files_spec.rb b/spec/unit/action/setup_package_files_spec.rb new file mode 100644 index 0000000..4e50189 --- /dev/null +++ b/spec/unit/action/setup_package_files_spec.rb @@ -0,0 +1,41 @@ +require 'unit_helper' + +require 'vagrant-lxc/action/setup_package_files' + +describe Vagrant::LXC::Action::SetupPackageFiles do + let(:app) { mock(:app, call: true) } + let(:env) { {machine: machine, tmp_path: tmp_path, ui: stub(info: true), 'package.rootfs' => rootfs_path} } + let(:machine) { fire_double('Vagrant::Machine', box: box) } + let!(:tmp_path) { Pathname.new(Dir.mktmpdir) } + let(:box) { fire_double('Vagrant::Box', directory: tmp_path.join('box')) } + let(:rootfs_path) { tmp_path.join('rootfs-amd64.tar.gz') } + + subject { described_class.new(app, env) } + + before do + box.directory.mkdir + [box.directory.join('lxc-template'), box.directory.join('metadata.json'), rootfs_path].each do |file| + file.open('w') { |f| f.puts file.to_s } + end + + subject.stub(recover: true) # Prevents files from being removed on specs + subject.call(env) + end + + after do + FileUtils.rm_rf(tmp_path.to_s) + end + + it 'copies box lxc-template to package directory' do + env['package.directory'].join('lxc-template').should be_file + end + + it 'copies metadata.json to package directory' do + env['package.directory'].join('metadata.json').should be_file + end + + it 'moves the compressed rootfs to package directory' do + env['package.directory'].join(rootfs_path.basename).should be_file + env['package.rootfs'].should_not be_file + end +end