Skip to content
This repository has been archived by the owner on Dec 2, 2020. It is now read-only.

Commit

Permalink
Merge pull request #56 from nanliu/rhel
Browse files Browse the repository at this point in the history
Allow provider to work beyond MacOS.
  • Loading branch information
jhaals committed Dec 23, 2014
2 parents b092b16 + 878d304 commit 533a7c6
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 66 deletions.
32 changes: 32 additions & 0 deletions lib/puppet/provider/vagrant.rb
Original file line number Diff line number Diff line change
@@ -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
28 changes: 2 additions & 26 deletions lib/puppet/provider/vagrant_box/vagrant_box.rb
Original file line number Diff line number Diff line change
@@ -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('/')
Expand All @@ -22,7 +21,6 @@ def create

def destroy
name, vprovider = @resource[:name].split('/')

vagrant "box", "remove", name, "--provider", vprovider
end

Expand All @@ -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
64 changes: 29 additions & 35 deletions lib/puppet/provider/vagrant_plugin/vagrant_plugin.rb
Original file line number Diff line number Diff line change
@@ -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
8 changes: 7 additions & 1 deletion lib/puppet/type/vagrant_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
14 changes: 10 additions & 4 deletions lib/puppet/type/vagrant_plugin.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'etc'

Puppet::Type.newtype(:vagrant_plugin) do
ensurable do
newvalue :present do
Expand All @@ -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

0 comments on commit 533a7c6

Please sign in to comment.