diff --git a/lib/puppet/provider/vagrant.rb b/lib/puppet/provider/vagrant.rb new file mode 100644 index 0000000..728e74c --- /dev/null +++ b/lib/puppet/provider/vagrant.rb @@ -0,0 +1,32 @@ +require 'etc' +require 'puppet/util/execution' + +class Puppet::Provider::Vagrant < Puppet::Provider + include Puppet::Util::Execution + + private + def home_dir + Etc.getpwnam(@resource[:user]).dir + end + + def custom_environment + { + "HOME" => home_dir, + "VAGRANT_HOME" => "#{home_dir}/.vagrant.d", + } + end + + def opts + { + :combine => true, + :custom_environment => custom_environment, + :failonfail => true, + :uid => @resource[:user], + } + end + + def vagrant(*args) + cmd = ["/usr/bin/vagrant"] + args + execute cmd, opts + end +end diff --git a/lib/puppet/provider/vagrant_box/vagrant_box.rb b/lib/puppet/provider/vagrant_box/vagrant_box.rb index 2f7d879..45c202c 100644 --- a/lib/puppet/provider/vagrant_box/vagrant_box.rb +++ b/lib/puppet/provider/vagrant_box/vagrant_box.rb @@ -1,7 +1,6 @@ -require 'puppet/util/execution' +require 'puppet/provider/vagrant' -Puppet::Type.type(:vagrant_box).provide :vagrant_box do - include Puppet::Util::Execution +Puppet::Type.type(:vagrant_box).provide(:vagrant_box, :parent => Puppet::Provider::Vagrant) do def create name, vprovider = @resource[:name].split('/') @@ -22,7 +21,6 @@ def create def destroy name, vprovider = @resource[:name].split('/') - vagrant "box", "remove", name, "--provider", vprovider end @@ -36,26 +34,4 @@ def exists? boxes =~ /^#{name}\s+\(#{vprovider}(, .+)?\)/ end end - - private - def custom_environment - { - "HOME" => "/Users/#{Facter[:boxen_user].value}", - "VAGRANT_HOME" => "/Users/#{Facter[:boxen_user].value}/.vagrant.d", - } - end - - def opts - { - :combine => true, - :custom_environment => custom_environment, - :failonfail => true, - :uid => Facter[:boxen_user].value, - } - end - - def vagrant(*args) - cmd = ["/usr/bin/vagrant"] + args - execute cmd, opts - end end diff --git a/lib/puppet/provider/vagrant_plugin/vagrant_plugin.rb b/lib/puppet/provider/vagrant_plugin/vagrant_plugin.rb index a0e9c0d..8a1e2e9 100644 --- a/lib/puppet/provider/vagrant_plugin/vagrant_plugin.rb +++ b/lib/puppet/provider/vagrant_plugin/vagrant_plugin.rb @@ -1,69 +1,63 @@ require 'json' -require 'puppet/util/execution' +require 'puppet/provider/vagrant' -Puppet::Type.type(:vagrant_plugin).provide :vagrant_plugin do - include Puppet::Util::Execution +Puppet::Type.type(:vagrant_plugin).provide(:vagrant_plugin, :parent => Puppet::Provider::Vagrant) do + def self.fetch_plugin_json(user_plugin_files) + result = Hash.new - def self.fetch_plugin_json - plugins = "/Users/#{Facter[:boxen_user].value}/.vagrant.d/plugins.json" + user_plugin_files.each do |user, plugin| + if File.exist? plugin + json = JSON.parse IO.read(plugin) - if File.exist? plugins - json = JSON.parse IO.read(plugins) - - if json.has_key? "installed" - return json["installed"] + if json.has_key? "installed" + result[user] = json["installed"] + end end end - [] + result end - def self.installed_plugins - @installed_plugins ||= fetch_plugin_json + def self.installed_plugins(user_plugin_json_files) + @installed_plugins ||= fetch_plugin_json(user_plugin_json_files) end - def create - cmd = [ - "/usr/bin/vagrant", + args = [ "plugin", "install", @resource[:name] ] - cmd << '--plugin-version' << @resource[:version] unless @resource[:version] == 'latest' + args << '--plugin-version' << @resource[:version] if @resource[:version] and resource[:version] != 'latest' - execute cmd, opts + vagrant(*args) end def destroy - cmd = [ - "/usr/bin/vagrant", + args = [ "plugin", "uninstall", @resource[:name] ] - execute cmd, opts + vagrant(*args) end def exists? - self.class.installed_plugins.member? @resource[:name] + installed=self.class.installed_plugins(user_plugins_json_files) + installed.member? @resource[:user] and installed[@resource[:user]].member? @resource[:name] end - private - def custom_environment - { - "HOME" => "/Users/#{Facter[:boxen_user].value}", - "VAGRANT_HOME" => "/Users/#{Facter[:boxen_user].value}/.vagrant.d", - } - end + # We do not have access to model.catalog in self.class + def user_plugins_json_files + users = model.catalog.resources. + find_all{|s| s.type==:vagrant_plugin}. + collect{|s| s[:user]} - def opts - { - :custom_environment => custom_environment, - :failonfail => true, - :uid => Facter[:boxen_user].value, - } + users.uniq.inject(Hash.new) do |result, user| + result[user] = "#{Etc.getpwnam(user).dir}/.vagrant.d/plugins.json" + result + end end end diff --git a/lib/puppet/type/vagrant_box.rb b/lib/puppet/type/vagrant_box.rb index 40d1160..309ce65 100755 --- a/lib/puppet/type/vagrant_box.rb +++ b/lib/puppet/type/vagrant_box.rb @@ -18,6 +18,10 @@ newparam :source do end + newparam :user do + defaultto(Facter.value(:boxen_user) || 'root') + end + newparam :force do validate do |value| unless value.is_a? Boolean @@ -27,7 +31,9 @@ end autorequire :package do - %w(Vagrant_1_4_2 vagrant) + catalog.resources. + find_all{|s| s.type == :package and s[:name] =~ /^[Vv]agrant/ }. + collect{|s| s[:name]} end autorequire :vagrant_plugin do diff --git a/lib/puppet/type/vagrant_plugin.rb b/lib/puppet/type/vagrant_plugin.rb index d2c1771..fa344bb 100755 --- a/lib/puppet/type/vagrant_plugin.rb +++ b/lib/puppet/type/vagrant_plugin.rb @@ -1,3 +1,5 @@ +require 'etc' + Puppet::Type.newtype(:vagrant_plugin) do ensurable do newvalue :present do @@ -24,15 +26,19 @@ end end + newparam :user do + defaultto(Facter.value(:boxen_user) || 'root') + end + newparam :version autorequire :package do - %w(Vagrant_1_4_2 vagrant) + catalog.resources. + find_all{|s| s.type == :package and s[:name] =~ /^[Vv]agrant/ }. + collect{|s| s[:name]} end autorequire :file do - %W( - /Users/#{Facter[:boxen_user].value}/.vagrant.d/license-#{self[:name]}.lic - ) + %W(#{Etc.getpwnam(self[:user]).dir}/.vagrant.d/license-#{self[:name]}.lic) end end