class Chef::Provider::Package::Homebrew

Public Instance Methods

brew(*args) click to toggle source
# File lib/chef/provider/package/homebrew.rb, line 76
def brew(*args)
  get_response_from_command("brew", *args)
end
brew_info() click to toggle source

We implement a querying method that returns the JSON-as-Hash data for a formula per the Homebrew documentation. Previous implementations of this provider in the homebrew cookbook performed a bit of magic with the load path to get this information, but that is not any more robust than using the command-line interface that returns the same thing.

github.com/Homebrew/homebrew/wiki/Querying-Brew

# File lib/chef/provider/package/homebrew.rb, line 88
def brew_info
  @brew_info ||= Chef::JSONCompat.from_json(brew("info", "--json=v1", new_resource.package_name)).first
end
candidate_version() click to toggle source

Packages (formula) available to install should have a “stable” version, per the Homebrew project's acceptable formula documentation, so we will rely on that being the case. Older implementations of this provider in the homebrew cookbook would fall back to +brew_info+, but the schema has changed, and homebrew is a constantly rolling forward project.

github.com/Homebrew/homebrew/wiki/Acceptable-Formulae#stable-versions

# File lib/chef/provider/package/homebrew.rb, line 119
def candidate_version
  brew_info["versions"]["stable"]
end
current_installed_version() click to toggle source

Some packages (formula) are “keg only” and aren't linked, because multiple versions installed can cause conflicts. We handle this by using the last installed version as the “current” (as in latest). Otherwise, we will use the version that brew thinks is linked as the current version.

# File lib/chef/provider/package/homebrew.rb, line 98
def current_installed_version
  if brew_info["keg_only"]
    if brew_info["installed"].empty?
      nil
    else
      brew_info["installed"].last["version"]
    end
  else
    brew_info["linked_keg"]
  end
end
install_package(name, version) click to toggle source
# File lib/chef/provider/package/homebrew.rb, line 47
def install_package(name, version)
  unless current_resource.version == version
    brew("install", options, name)
  end
end
load_current_resource() click to toggle source
# File lib/chef/provider/package/homebrew.rb, line 34
def load_current_resource
  self.current_resource = Chef::Resource::HomebrewPackage.new(new_resource.name)
  current_resource.package_name(new_resource.package_name)
  current_resource.version(current_installed_version)
  logger.trace("#{new_resource} current version is #{current_resource.version}") if current_resource.version

  @candidate_version = candidate_version

  logger.trace("#{new_resource} candidate version is #{@candidate_version}") if @candidate_version

  current_resource
end
purge_package(name, version) click to toggle source

Homebrew doesn't really have a notion of purging, do a “force remove”

# File lib/chef/provider/package/homebrew.rb, line 70
def purge_package(name, version)
  if current_resource.version
    brew("uninstall", "--force", options, name)
  end
end
remove_package(name, version) click to toggle source
# File lib/chef/provider/package/homebrew.rb, line 63
def remove_package(name, version)
  if current_resource.version
    brew("uninstall", options, name)
  end
end
upgrade_package(name, version) click to toggle source
# File lib/chef/provider/package/homebrew.rb, line 53
def upgrade_package(name, version)
  current_version = current_resource.version

  if current_version.nil? || current_version.empty?
    install_package(name, version)
  elsif current_version != version
    brew("upgrade", options, name)
  end
end

Private Instance Methods

get_response_from_command(*command) click to toggle source
# File lib/chef/provider/package/homebrew.rb, line 125
def get_response_from_command(*command)
  homebrew_uid = find_homebrew_uid(new_resource.respond_to?(:homebrew_user) && new_resource.homebrew_user)
  homebrew_user = Etc.getpwuid(homebrew_uid)

  logger.trace "Executing '#{command.join(' ')}' as user '#{homebrew_user.name}'"
  # FIXME: this 1800 second default timeout should be deprecated
  output = shell_out_compact_timeout!(*command, timeout: 1800, user: homebrew_uid, environment: { "HOME" => homebrew_user.dir, "RUBYOPT" => nil, "TMPDIR" => nil })
  output.stdout.chomp
end