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

Commit

Permalink
Add homebrew_tap native type
Browse files Browse the repository at this point in the history
  • Loading branch information
wfarr committed Jul 22, 2013
1 parent 7042755 commit 894bc3e
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 19 deletions.
92 changes: 92 additions & 0 deletions lib/puppet/provider/homebrew_tap/default.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
require "fileutils"
require "pathname"
require "puppet/util/execution"

Puppet::Type.type(:homebrew_tap).provide :default do
include Puppet::Util::Execution

def self.home
@home ||= if boxen_home = Facter.value(:boxen_home)
"#{boxen_home}/homebrew"
else
"/usr/local/homebrew"
end
end

def self.taps_dir
@taps_dir ||= "#{home}/Library/Taps"
end

def self.instances
Dir.entries(taps_dir).map { |t| t.gsub('-', '/') }
end

def exists?
File.directory? install_dir
end

def create
execute [ "brew", "tap", @resource[:source] ], command_opts
end

def destroy
FileUtils.rm_rf install_dir
end

private

def install_dir
@install_dir ||= "#{self.class.taps_dir}/#{hyphenated_source}"
end

def hyphenate(s)
s.gsub('/', '-')
end

def hyphenated_source
@hyphenated_source ||= hyphenate(@resource[:source])
end

# Override default `execute` to run super method in a clean
# environment without Bundler, if Bundler is present
def execute(*args)
if Puppet.features.bundled_environment?
Bundler.with_clean_env do
super
end
else
super
end
end

# Override default `execute` to run super method in a clean
# environment without Bundler, if Bundler is present
def self.execute(*args)
if Puppet.features.bundled_environment?
Bundler.with_clean_env do
super
end
else
super
end
end

def default_user
Facter.value(:boxen_user) || Facter.value(:id) || "root"
end

def command_opts
@command_opts ||= {
:combine => true,
:custom_environment => {
"HOME" => "/#{homedir_prefix}/#{default_user}",
"PATH" => "#{self.class.home}/bin:/usr/bin:/usr/sbin:/bin:/sbin",
"CFLAGS" => "-O2",
"CPPFLAGS" => "-O2",
"CXXFLAGS" => "-O2"
},
:failonfail => true,
:uid => default_user
}
end
end
23 changes: 23 additions & 0 deletions lib/puppet/type/homebrew_tap.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
Puppet::Type.newtype(:homebrew_tap) do
ensurable do
newvalue :present do
provider.create
end

newvalue :absent do
provider.destroy
end

defaultto :present
end

newparam(:source) do
isnamevar

validate do |v|
if v.nil?
raise Puppet::ParseError, "Homebrew_tap requires a source parameter!"
end
end
end
end
20 changes: 1 addition & 19 deletions manifests/tap.pp
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,5 @@
) {
require homebrew

$source_with_hyphen = regsubst($source, '\/', '-')

case $ensure {
present: {
exec { "brew tap ${source}":
creates => "${homebrew::tapsdir}/${source_with_hyphen}"
}
}

absent: {
exec { "rm -rf ${homebrew::tapsdir}/${source_with_hyphen}":
onlyif => "test -d ${homebrew::tapsdir}/${source_with_hyphen}"
}
}

default: {
fail('Ensure must be present or absent!')
}
}
ensure_resource('homebrew_tap', $source, { 'ensure' => $ensure })
}

1 comment on commit 894bc3e

@createdbypete
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems this new ensure_resource caused the specs to fail as they are expecting exec resources. Not sure how to update specs to test for ensure_resource however as it is part of stdlib

Please sign in to comment.