Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

major rewrite on how to deal with installers.

- no longer using shell scripts to upload into the box.
- provide a config options to mount a custom Installer class into vagrant-vbguest
  • Loading branch information...
commit 43d343a7cd6379250edf2ae8f4a4c2147e7f84d6 1 parent 8c85649
Robert Schulze fnordfish authored
5 files/setup_debian.sh
View
@@ -1,5 +0,0 @@
-#!/bin/bash
-apt-get install -y linux-headers-`uname -r` dkms
-mount /tmp/VBoxGuestAdditions.iso -o loop /mnt
-/mnt/VBoxLinuxAdditions.run --nox11
-umount /mnt
4 files/setup_linux.sh
View
@@ -1,4 +0,0 @@
-#!/bin/bash
-mount /tmp/VBoxGuestAdditions.iso -o loop /mnt
-/mnt/VBoxLinuxAdditions.run --nox11
-umount /mnt
25 lib/vagrant-vbguest.rb
View
@@ -1,20 +1,25 @@
require 'vagrant'
-require "vagrant-vbguest/errors"
+require 'vagrant-vbguest/errors'
+
+require 'vagrant-vbguest/installer'
+require 'vagrant-vbguest/installers/base'
+require 'vagrant-vbguest/installers/linux'
+require 'vagrant-vbguest/installers/debian'
+require 'vagrant-vbguest/installers/ubuntu'
+
+require 'vagrant-vbguest/action'
+require 'vagrant-vbguest/config'
+require 'vagrant-vbguest/command'
+
+require 'vagrant-vbguest/detector'
+require 'vagrant-vbguest/download'
+
# Add our custom translations to the load path
I18n.load_path << File.expand_path("../../locales/en.yml", __FILE__)
module VagrantVbguest
- autoload :Action, 'vagrant-vbguest/action'
- autoload :Config, 'vagrant-vbguest/config'
- autoload :Command, 'vagrant-vbguest/command'
- autoload :Errors, 'vagrant-vbguest/errors'
-
- autoload :Detector, "vagrant-vbguest/detector"
- autoload :Download, "vagrant-vbguest/download"
- autoload :Installer, "vagrant-vbguest/installer"
-
class Plugin < Vagrant.plugin("1")
name "vbguest management"
description <<-DESC
12 lib/vagrant-vbguest/config.rb
View
@@ -1,10 +1,13 @@
module VagrantVbguest
class Config < Vagrant::Config::Base
- attr_accessor :iso_path
- attr_accessor :auto_update
- attr_accessor :no_install
- attr_accessor :no_remote
+ attr_accessor :iso_path, :auto_update, :installer, :no_install, :no_remote
+
+ def validate(env, errors)
+ if !installer.nil? && (!installer.is_a?(Class) || installer <= Installer::Base)
+ errors.add I18n.t("vagrant.plugins.vbguest.invalid_installer_class")
+ end
+ end
def auto_update; @auto_update.nil? ? (@auto_update = true) : @auto_update; end
def no_remote; @no_remote.nil? ? (@no_remote = false) : @no_remote; end
@@ -15,6 +18,7 @@ def to_hash
{
:iso_path => iso_path,
:auto_update => auto_update,
+ :installer => installer,
:no_install => no_install,
:no_remote => no_remote
}
4 lib/vagrant-vbguest/detector.rb
View
@@ -14,7 +14,7 @@ def iso_path
private
def autodetect_iso
- path = media_manager_iso || guess_iso || web_iso
+ path = media_manager_iso || guess_iso || (!@options[:no_remote] && web_iso)
raise VagrantVbguest::Errors::IsoPathAutodetectionError if !path || path.empty?
path
end
@@ -35,7 +35,7 @@ def guess_iso
end
def web_iso
- "http://download.virtualbox.org/virtualbox/$VBOX_VERSION/VBoxGuestAdditions_$VBOX_VERSION.iso" unless @options[:no_remote]
+ "http://download.virtualbox.org/virtualbox/$VBOX_VERSION/VBoxGuestAdditions_$VBOX_VERSION.iso"
end
end
59 lib/vagrant-vbguest/installer.rb
View
@@ -4,6 +4,22 @@ module VagrantVbguest
class Installer
+ class << self
+ def register(installer_class, prio = 5)
+ @installers ||= {}
+ @installers[prio] ||= []
+ @installers[prio] << installer_class
+ end
+
+ def detect(vm)
+ @installers.keys.sort.reverse.each do |prio|
+ klass = @installers[prio].detect { |k| k.match?(vm) }
+ return klass.new(vm) if klass
+ end
+ return nil
+ end
+ end
+
def initialize(vm, options = {})
@env = {
:ui => vm.ui,
@@ -32,26 +48,16 @@ def run
if @options[:force] || (!@options[:no_install] && needs_update?)
@vm.ui.warn(I18n.t("vagrant.plugins.vbguest.installing#{@options[:force] ? '_forced' : ''}", :host => vb_version, :guest => guest_version))
- # :TODO:
- # the whole installation process should be put into own classes
- # like the vagrant system loading
- if (i_script = installer_script)
+ if (installer = guest_installer)
@options[:iso_path] ||= VagrantVbguest::Detector.new(@vm, @options).iso_path
- @vm.ui.info(I18n.t("vagrant.plugins.vbguest.start_copy_iso", :from => iso_path, :to => iso_destination))
- @vm.channel.upload(iso_path, iso_destination)
-
- @vm.ui.info(I18n.t("vagrant.plugins.vbguest.start_copy_script", :from => File.basename(i_script), :to => installer_destination))
- @vm.channel.upload(i_script, installer_destination)
-
- @vm.channel.sudo("sh #{installer_destination}") do |type, data|
+ installer.install(iso_path) do |type, data|
@vm.ui.info(data, :prefix => false, :new_line => false)
end
-
- @vm.channel.execute("rm #{installer_destination} #{iso_destination}") do |type, data|
- @vm.ui.error(data.chomp, :prefix => false)
- end
+ else
+ @vm.ui.error(I18n.t("vagrant.plugins.vbguest.no_install_script_for_platform"))
end
+
end
end
ensure
@@ -71,28 +77,14 @@ def vb_version
@vm.driver.version
end
- def installer_script
- platform = @vm.guest.distro_dispatch
- case platform
- when :debian, :ubuntu
- File.expand_path("../../../files/setup_debian.sh", __FILE__)
- when :gentoo, :redhat, :suse, :arch, :linux
- @vm.ui.warn(I18n.t("vagrant.plugins.vbguest.generic_install_script_for_platform", :platform => platform.to_s))
- File.expand_path("../../../files/setup_linux.sh", __FILE__)
+ def guest_installer
+ if @options[:installer]
+ @options[:installer].new(@vm)
else
- @vm.ui.error(I18n.t("vagrant.plugins.vbguest.no_install_script_for_platform", :platform => platform.to_s))
- nil
+ Installer.detect(@vm)
end
end
- def installer_destination
- '/tmp/install_vbguest.sh'
- end
-
- def iso_destination
- '/tmp/VBoxGuestAdditions.iso'
- end
-
def iso_path
@iso_path ||= begin
@env[:iso_url] ||= @options[:iso_path].gsub '$VBOX_VERSION', vb_version
@@ -119,3 +111,4 @@ def cleanup
end
end
+
34 lib/vagrant-vbguest/installers/base.rb
View
@@ -0,0 +1,34 @@
+module VagrantVbguest
+ module Installers
+ class Base
+
+ def self.match?(vm)
+ nil
+ end
+
+ attr_reader :vm
+
+ def initialize(vm)
+ @vm = vm
+ end
+
+ def upload(file)
+ @vm.ui.info(I18n.t("vagrant.plugins.vbguest.start_copy_iso", :from => file, :to => tmp_path))
+ @vm.channel.upload(file, tmp_path)
+ end
+
+ def cleanup
+ @vm.channel.execute("rm #{tmp_path}") do |type, data|
+ @vm.ui.error(data.chomp, :prefix => false)
+ end
+ end
+
+ def tmp_path

Why is this method empty?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+
+ def install(iso_file, opts=nil, &block)

The same for this line.

Robert Schulze Owner

It's kind of an "abstract" method. It just declares the signature.

Okay :) - a small comment wouldn't be so wrong!

Robert Schulze Owner

full-ack. need to do some API doc all over the place.

Okay - take your time before moving on to create new features.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+
+ end
+ end
+end
21 lib/vagrant-vbguest/installers/debian.rb
View
@@ -0,0 +1,21 @@
+module VagrantVbguest
+ module Installers
+ class Debian < Linux
+
+ def self.match?(vm)
+ :debian == self.distro(vm)
+ end
+
+ def install(iso_file, opts=nil, &block)
+ upload(iso_file)
+ vm.channel.sudo('apt-get install -y linux-headers-`uname -r` dkms', opts, &block)
+ vm.channel.sudo("mount #{tmp_path} -o loop #{mount_point}", opts, &block)
+ vm.channel.sudo("#{mount_point}/VBoxLinuxAdditions.run --nox11", opts, &block)
+ vm.channel.sudo("umount #{mount_point}", opts, &block)
+ cleanup
+ end
+
+ end
+ end
+end
+VagrantVbguest::Installer.register(VagrantVbguest::Installers::Debian, 5)
34 lib/vagrant-vbguest/installers/linux.rb
View
@@ -0,0 +1,34 @@
+module VagrantVbguest
+ module Installers
+ class Linux < Base
+
+ def self.distro(vm)
+ @@ditro ||= {}
+ @@ditro[vm.uuid] ||= vm.guest.distro_dispatch
+ end
+
+ def self.match?(vm)
+ vm.channel.test("uname | grep 'Linux'")
+ end
+
+ def tmp_path
+ '/tmp/VBoxGuestAdditions.iso'
+ end
+
+ def mount_point
+ '/mnt'
+ end
+
+ def install(iso_file, opts=nil, &block)
+ @vm.ui.warn I18n.t("vagrant.plugins.vbguest.generic_linux_installer")
+ upload(iso_file)
+ vm.channel.sudo("mount #{tmp_path} -o loop #{mount_point}", opts, &block)
+ vm.channel.sudo("#{mount_point}/VBoxLinuxAdditions.run --nox11", opts, &block)
+ vm.channel.sudo("umount #{mount_point}", opts, &block)
+ cleanup
+ end
+
+ end
+ end
+end
+VagrantVbguest::Installer.register(VagrantVbguest::Installers::Linux, 2)
12 lib/vagrant-vbguest/installers/ubuntu.rb
View
@@ -0,0 +1,12 @@
+module VagrantVbguest
+ module Installers
+ class Ubuntu < Debian
+
+ def self.match?(vm)
+ :ubuntu == self.distro(vm)
+ end
+
+ end
+ end
+end
+VagrantVbguest::Installer.register(VagrantVbguest::Installers::Ubuntu, 5)
9 locales/en.yml
View
@@ -8,8 +8,8 @@ en:
installing_forced: "Forcing installation of Virtualbox Guest Additions %{host} - guest's version is %{guest}"
start_copy_iso: "Copy iso file %{from} into the box %{to}"
start_copy_script: "Copy installer file %{from} into the box %{to}"
- no_install_script_for_platform: "Sorry, don't know how to install on a %{platform} system. Stopping installation."
- generic_install_script_for_platform: "%{platform} is currently not supported, will try generic Linux method..."
+ no_install_script_for_platform: "Sorry, don't know how to install on a this platform. Stopping installation."
+ generic_linux_installer: "The guest's platform is currently not supported, will try generic Linux method..."
errors:
autodetect_iso_path: |-
@@ -26,6 +26,11 @@ en:
Please configure a local path to the iso using `config.vbguest.iso_path`
in your Vagrantfile or the `--iso` command line option.
+ config:
+ errors:
+ invalid_installer_class: |-
+ The specified installer does not inherit from `VagrantVbguest::Installer::Base`
+
download:
with: "Downloading VirtualBox Guest Additions ISO with %{class}..."
cleaning: "Cleaning up downloaded VirtualBox Guest Additions ISO..."

2 comments on commit 43d343a

Matthias Günther

Why is this method empty?

Matthias Günther

All in all, a great commit and huge benefit for the Vagrant Community

Robert Schulze

It's kind of an "abstract" method. It just declares the signature.

Matthias Günther

Okay :) - a small comment wouldn't be so wrong!

Robert Schulze

full-ack. need to do some API doc all over the place.

Matthias Günther

Okay - take your time before moving on to create new features.

Please sign in to comment.
Something went wrong with that request. Please try again.