diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d87d4be --- /dev/null +++ b/.gitignore @@ -0,0 +1,17 @@ +*.gem +*.rbc +.bundle +.config +.yardoc +Gemfile.lock +InstalledFiles +_yardoc +coverage +doc/ +lib/bundler/man +pkg +rdoc +spec/reports +test/tmp +test/version_tmp +tmp diff --git a/Gemfile b/Gemfile new file mode 100644 index 0000000..851fabc --- /dev/null +++ b/Gemfile @@ -0,0 +1,2 @@ +source 'https://rubygems.org' +gemspec diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..35d9d8a --- /dev/null +++ b/LICENSE @@ -0,0 +1,22 @@ +Copyright (c) 2012 Andrew Vit + +MIT License + +Permission is hereby granted, free of charge, to any person obtaining +a copy of this software and associated documentation files (the +"Software"), to deal in the Software without restriction, including +without limitation the rights to use, copy, modify, merge, publish, +distribute, sublicense, and/or sell copies of the Software, and to +permit persons to whom the Software is furnished to do so, subject to +the following conditions: + +The above copyright notice and this permission notice shall be +included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. \ No newline at end of file diff --git a/README.md b/README.md new file mode 100644 index 0000000..f62f6b4 --- /dev/null +++ b/README.md @@ -0,0 +1,30 @@ +# Vagrant apt-cache + +Share a common package cache among all VM instances. This gem will share a directory from the host to the `/var/cache/apt` on guest VMs to avoid downloading already-downloaded packages when building VMs. + + +## Installation + +Add this line to your application's Gemfile: + + gem 'vagrant-apt_cache' + +And then execute: + + $ bundle + +Or install it yourself as: + + $ gem install vagrant-apt_cache + +## Usage + +No configuration is currently provided. This gem is autoloaded by Vagrant. + +## Contributing + +1. Fork it +2. Create your feature branch (`git checkout -b my-new-feature`) +3. Commit your changes (`git commit -am 'Added some feature'`) +4. Push to the branch (`git push origin my-new-feature`) +5. Create new Pull Request diff --git a/Rakefile b/Rakefile new file mode 100644 index 0000000..f57ae68 --- /dev/null +++ b/Rakefile @@ -0,0 +1,2 @@ +#!/usr/bin/env rake +require "bundler/gem_tasks" diff --git a/lib/vagrant-apt_cache.rb b/lib/vagrant-apt_cache.rb new file mode 100644 index 0000000..8c21fb5 --- /dev/null +++ b/lib/vagrant-apt_cache.rb @@ -0,0 +1,7 @@ +require 'vagrant-apt_cache/version' +require 'vagrant-apt_cache/register_shared_folder' +require 'vagrant-apt_cache/middleware' + +Vagrant.actions[:start].insert_before( + Vagrant::Action::VM::ShareFolders, Vagrant::AptCache::Middleware +) diff --git a/lib/vagrant-apt_cache/middleware.rb b/lib/vagrant-apt_cache/middleware.rb new file mode 100644 index 0000000..9099ab1 --- /dev/null +++ b/lib/vagrant-apt_cache/middleware.rb @@ -0,0 +1,36 @@ +module Vagrant + module AptCache + + class Middleware + def initialize(app, env, options={}) + @app = app + @env = env + @options = options + end + + def call(env) + @env = env + + if is_apt_managed? + Vagrant::AptCache::RegisterSharedFolder.new(@env[:vm]).run + end + + @app.call(env) + end + + private + + def guest + @env[:vm].guest + end + + def is_apt_managed? + [ + Vagrant::Guest::Debian, + Vagrant::Guest::Ubuntu + ].any? { |deb_distro| guest.is_a? deb_distro } + end + end + + end +end diff --git a/lib/vagrant-apt_cache/register_shared_folder.rb b/lib/vagrant-apt_cache/register_shared_folder.rb new file mode 100644 index 0000000..0b9e3f5 --- /dev/null +++ b/lib/vagrant-apt_cache/register_shared_folder.rb @@ -0,0 +1,35 @@ +module Vagrant + module AptCache + + class RegisterSharedFolder + def initialize(vm, options={}) + @vm = vm + @options = options + end + + def run + config_vm.share_folder("v-apt-cache", + guest_apt_cache_dir, host_apt_cache_dir) + end + + private + + def config_vm + @vm.config.vm + end + + def guest_apt_cache_dir + "/var/cache/apt/archives" + end + + def host_apt_cache_dir + prefix = File.expand_path(Vagrant::Environment::DEFAULT_HOME) + cache_dir = File.join(prefix, 'cache', 'apt', @vm.box.name) + partial_dir = File.join(cache_dir, 'partial') + FileUtils.mkdir_p(partial_dir) unless File.exists? partial_dir + cache_dir + end + end + + end +end diff --git a/lib/vagrant-apt_cache/version.rb b/lib/vagrant-apt_cache/version.rb new file mode 100644 index 0000000..3a2c443 --- /dev/null +++ b/lib/vagrant-apt_cache/version.rb @@ -0,0 +1,5 @@ +module Vagrant + module AptCache + VERSION = "0.0.1" + end +end diff --git a/lib/vagrant_init.rb b/lib/vagrant_init.rb new file mode 100644 index 0000000..7c0f524 --- /dev/null +++ b/lib/vagrant_init.rb @@ -0,0 +1 @@ +require 'vagrant-apt_cache' diff --git a/vagrant-apt_cache.gemspec b/vagrant-apt_cache.gemspec new file mode 100644 index 0000000..3f97157 --- /dev/null +++ b/vagrant-apt_cache.gemspec @@ -0,0 +1,20 @@ +# -*- encoding: utf-8 -*- +require File.expand_path('../lib/vagrant-apt_cache/version', __FILE__) + +Gem::Specification.new do |gem| + gem.authors = ["Andrew Vit"] + gem.email = ["andrew@avit.ca"] + gem.description = <<-DESC + Shares the /var/cache/apt/ folder for Ubuntu/Debian instances by storing + already-downloaded packages on the host. + DESC + gem.summary = %q{Share apt-cache among vagrant VMs of the same box type} + gem.homepage = "http://github.com/avit/vagrant-apt_cache" + + gem.files = `git ls-files`.split($\) + gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) } + gem.test_files = gem.files.grep(%r{^(test|spec|features)/}) + gem.name = "vagrant-apt_cache" + gem.require_paths = ["lib"] + gem.version = Vagrant::AptCache::VERSION +end