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

Commit 9963e0f

Browse files
committed
Merge pull request #42 from boxen/manage-homebrew-revision
Manage homebrew revision
2 parents 824b986 + d26fb1e commit 9963e0f

File tree

6 files changed

+142
-15
lines changed

6 files changed

+142
-15
lines changed
Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,94 @@
1+
require 'puppet/util/errors'
2+
require 'puppet/util/execution'
3+
4+
Puppet::Type.type(:homebrew_repo).provide :homebrew do
5+
include Puppet::Util::Execution
6+
include Puppet::Util::Errors
7+
8+
optional_commands :git => 'git'
9+
10+
confine :operatingsystem => :darwin
11+
12+
def self.home
13+
if boxen_home = Facter.value(:boxen_home)
14+
"#{boxen_home}/homebrew"
15+
else
16+
raise "The puppet-homebrew module depends on Boxen"
17+
end
18+
end
19+
20+
def check_min_revision
21+
result = Dir.chdir @resource[:path] do
22+
execute([
23+
"/bin/sh", "-c",
24+
"#{command(:git)} rev-list #{current_revision} | fgrep #{min_revision}"
25+
], command_opts.merge(:failonfail => false))
26+
end
27+
result.exitstatus == 0 ? @resource[:min_revision] : current_revision
28+
end
29+
30+
def brew_update
31+
if Puppet.features.bundled_environment?
32+
Bundler.with_clean_env do
33+
execute(["brew", "update"], brew_command_opts)
34+
end
35+
else
36+
execute(["brew", "update"], brew_command_opts)
37+
end
38+
end
39+
40+
private
41+
42+
def current_revision
43+
@current_revision ||= Dir.chdir @resource[:path] do
44+
execute([
45+
command(:git), "rev-parse", "HEAD"
46+
], command_opts).chomp
47+
end
48+
end
49+
50+
def min_revision
51+
@min_revision ||= Dir.chdir @resource[:path] do
52+
execute([
53+
command(:git), "rev-list", "--max-count=1", @resource[:min_revision]
54+
], command_opts).chomp
55+
end
56+
end
57+
58+
def command_opts
59+
@command_opts ||= build_command_opts
60+
end
61+
62+
def build_command_opts
63+
default_command_opts.tap do |h|
64+
if uid = (@resource[:user] || self.class.default_user)
65+
h[:uid] = uid
66+
end
67+
end
68+
end
69+
70+
def default_command_opts
71+
{
72+
:combine => true,
73+
:failonfail => true
74+
}
75+
end
76+
77+
def homedir_prefix
78+
case Facter[:osfamily].value
79+
when "Darwin" then "Users"
80+
when "Linux" then "home"
81+
else
82+
raise "unsupported"
83+
end
84+
end
85+
86+
def brew_command_opts
87+
default_command_opts.merge({
88+
:custom_environment => {
89+
"HOME" => "/#{homedir_prefix}/#{@resource[:user]}",
90+
"PATH" => "#{self.class.home}/bin:/usr/bin:/usr/sbin:/bin:/sbin",
91+
}
92+
})
93+
end
94+
end

lib/puppet/type/homebrew_repo.rb

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Puppet::Type.newtype(:homebrew_repo) do
2+
newparam(:path) do
3+
isnamevar
4+
5+
validate do |v|
6+
if v.nil?
7+
raise Puppet::ParseError, "Homebrew_repo requires a path parameter!"
8+
end
9+
end
10+
end
11+
12+
newproperty(:min_revision) do
13+
newvalue(/.+/) do
14+
provider.brew_update
15+
end
16+
17+
def retrieve
18+
provider.check_min_revision
19+
end
20+
end
21+
22+
newparam :user do
23+
desc "User to run this operation as."
24+
25+
defaultto do
26+
Facter.value(:boxen_user) || Facter.value(:id) || "root"
27+
end
28+
end
29+
end

manifests/config.pp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,6 @@
1515
$tapsdir = "${installdir}/Library/Taps"
1616

1717
$brewsdir = "${tapsdir}/boxen-brews"
18+
19+
$min_revision = 'e567009'
1820
}

manifests/init.pp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,15 @@
55
# include homebrew
66

77
class homebrew(
8-
$cachedir = $homebrew::config::cachedir,
9-
$installdir = $homebrew::config::installdir,
10-
$libdir = $homebrew::config::libdir,
11-
$cmddir = $homebrew::config::cmddir,
12-
$tapsdir = $homebrew::config::tapsdir,
13-
$brewsdir = $homebrew::config::brewsdir,
14-
$set_cflags = true,
15-
$set_ldflags = true,
8+
$cachedir = $homebrew::config::cachedir,
9+
$installdir = $homebrew::config::installdir,
10+
$libdir = $homebrew::config::libdir,
11+
$cmddir = $homebrew::config::cmddir,
12+
$tapsdir = $homebrew::config::tapsdir,
13+
$brewsdir = $homebrew::config::brewsdir,
14+
$min_revision = $homebrew::config::min_revision,
15+
$set_cflags = true,
16+
$set_ldflags = true,
1617
) inherits homebrew::config {
1718
include boxen::config
1819
include homebrew::repo

manifests/repo.pp

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
# Examples
44
#
55
# include homebrew::repo
6-
class homebrew::repo {
7-
include homebrew
6+
class homebrew::repo (
7+
$installdir = $homebrew::config::installdir,
8+
$min_revision = $homebrew::config::min_revision,
9+
) {
10+
require homebrew
811

912
if $::osfamily == 'Darwin' {
10-
exec { 'brew update':
11-
refreshonly => true,
12-
require => Class['homebrew']
13-
}
13+
homebrew_repo { $installdir:
14+
min_revision => $min_revision,
15+
} -> Package <| |>
1416
}
1517
}

spec/classes/homebrew__repo_spec.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,5 @@
55

66
it do
77
should include_class("homebrew")
8-
should contain_exec("brew update").with_refreshonly(true)
98
end
109
end

0 commit comments

Comments
 (0)