|
| 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 |
0 commit comments