summaryrefslogtreecommitdiff
path: root/lib
diff options
context:
space:
mode:
authortyler-ball <tyleraball@gmail.com>2014-09-19 15:25:46 -0700
committertyler-ball <tyleraball@gmail.com>2014-09-19 16:08:44 -0700
commit7e2c0b1518866ad046115ed804945937b6cd4f0a (patch)
treecac30646df462d3fde566a99c47e6a91d52ad8d4 /lib
parente7d3e2adf486131cfba78fc4eb7e31d1e36b7b0e (diff)
downloadchef-7e2c0b1518866ad046115ed804945937b6cd4f0a.tar.gz
Restoring https://github.com/opscode/chef/pull/1921 to master - somehow it got deleted from the git log
Diffstat (limited to 'lib')
-rw-r--r--lib/chef/exceptions.rb4
-rw-r--r--lib/chef/mixin/homebrew_owner.rb58
-rw-r--r--lib/chef/platform/provider_mapping.rb4
-rw-r--r--lib/chef/provider/package/homebrew.rb121
-rw-r--r--lib/chef/providers.rb1
-rw-r--r--lib/chef/resource/homebrew_package.rb34
-rw-r--r--lib/chef/resources.rb1
7 files changed, 219 insertions, 4 deletions
diff --git a/lib/chef/exceptions.rb b/lib/chef/exceptions.rb
index 7298f1f4d1..eb16abdaa6 100644
--- a/lib/chef/exceptions.rb
+++ b/lib/chef/exceptions.rb
@@ -120,6 +120,8 @@ class Chef
class PowershellCmdletException < RuntimeError; end
+ class CannotDetermineHomebrewOwner < Package; end
+
# A different version of a cookbook was added to a
# VersionedRecipeList than the one already there.
class CookbookVersionConflict < ArgumentError ; end
@@ -181,8 +183,6 @@ class Chef
class ChildConvergeError < RuntimeError; end
- class NoProviderAvailable < RuntimeError; end
-
class MissingRole < RuntimeError
NULL = Object.new
diff --git a/lib/chef/mixin/homebrew_owner.rb b/lib/chef/mixin/homebrew_owner.rb
new file mode 100644
index 0000000000..73bb22ddf5
--- /dev/null
+++ b/lib/chef/mixin/homebrew_owner.rb
@@ -0,0 +1,58 @@
+#
+# Author:: Joshua Timberman (<joshua@getchef.com>)
+# Author:: Graeme Mathieson (<mathie@woss.name>)
+#
+# Copyright 2011-2013, Opscode, Inc.
+# Copyright 2014, Chef Software, Inc <legal@getchef.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+# Ported from the homebrew cookbook's Homebrew::Mixin owner helpers
+#
+# This lives here in Chef::Mixin because Chef's namespacing makes it
+# awkward to use modules elsewhere (e.g., chef/provider/package/homebrew/owner)
+
+class Chef
+ module Mixin
+ module HomebrewOwner
+ def homebrew_owner(node)
+ @homebrew_owner ||= calculate_owner(node)
+ end
+
+ private
+
+ def calculate_owner(node)
+ owner = homebrew_owner_attr(node) || sudo_user || current_user
+ if owner == 'root'
+ raise Chef::Exceptions::CannotDetermineHomebrewOwner,
+ 'The homebrew owner is not specified and the current user is \"root\"' +
+ 'Homebrew does not support root installs, please specify the homebrew' +
+ 'owner by setting the attribute `node[\'homebrew\'][\'owner\']`.'
+ end
+ owner
+ end
+
+ def homebrew_owner_attr(node)
+ node['homebrew']['owner'] if node.attribute?('homebrew') && node['homebrew'].attribute?('owner')
+ end
+
+ def sudo_user
+ ENV['SUDO_USER']
+ end
+
+ def current_user
+ ENV['USER']
+ end
+ end
+ end
+end
diff --git a/lib/chef/platform/provider_mapping.rb b/lib/chef/platform/provider_mapping.rb
index 7f79c38a6a..b10fecc53e 100644
--- a/lib/chef/platform/provider_mapping.rb
+++ b/lib/chef/platform/provider_mapping.rb
@@ -40,7 +40,7 @@ class Chef
{
:mac_os_x => {
:default => {
- :package => Chef::Provider::Package::Macports,
+ :package => Chef::Provider::Package::Homebrew,
:service => Chef::Provider::Service::Macosx,
:user => Chef::Provider::User::Dscl,
:group => Chef::Provider::Group::Dscl
@@ -48,7 +48,7 @@ class Chef
},
:mac_os_x_server => {
:default => {
- :package => Chef::Provider::Package::Macports,
+ :package => Chef::Provider::Package::Homebrew,
:service => Chef::Provider::Service::Macosx,
:user => Chef::Provider::User::Dscl,
:group => Chef::Provider::Group::Dscl
diff --git a/lib/chef/provider/package/homebrew.rb b/lib/chef/provider/package/homebrew.rb
new file mode 100644
index 0000000000..d964703c87
--- /dev/null
+++ b/lib/chef/provider/package/homebrew.rb
@@ -0,0 +1,121 @@
+#
+# Author:: Joshua Timberman (<joshua@getchef.com>)
+# Author:: Graeme Mathieson (<mathie@woss.name>)
+#
+# Copyright 2011-2013, Opscode, Inc.
+# Copyright 2014, Chef Software, Inc <legal@getchef.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'etc'
+require 'chef/mixin/homebrew_owner'
+
+class Chef
+ class Provider
+ class Package
+ class Homebrew < Chef::Provider::Package
+ include Chef::Mixin::HomebrewOwner
+ def load_current_resource
+ self.current_resource = Chef::Resource::Package.new(new_resource.name)
+ current_resource.package_name(new_resource.package_name)
+ current_resource.version(current_installed_version)
+ Chef::Log.debug("#{new_resource} current version is #{current_resource.version}") if current_resource.version
+
+ @candidate_version = candidate_version
+
+ Chef::Log.debug("#{new_resource} candidate version is #{@candidate_version}") if @candidate_version
+
+ current_resource
+ end
+
+ def install_package(name, version)
+ unless current_resource.version == version
+ brew('install', new_resource.options, name)
+ end
+ end
+
+ def upgrade_package(name, version)
+ current_version = current_resource.version
+
+ if current_version.nil? or current_version.empty?
+ install_package(name, version)
+ elsif current_version != version
+ brew('upgrade', new_resource.options, name)
+ end
+ end
+
+ def remove_package(name, version)
+ if current_resource.version
+ brew('uninstall', new_resource.options, name)
+ end
+ end
+
+ # Homebrew doesn't really have a notion of purging, do a "force remove"
+ def purge_package(name, version)
+ new_resource.options((new_resource.options || '') << ' --force').strip
+ remove_package(name, version)
+ end
+
+ def brew(*args)
+ get_response_from_command("brew #{args.join(' ')}")
+ end
+
+ # 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.
+ #
+ # https://github.com/Homebrew/homebrew/wiki/Querying-Brew
+ def brew_info
+ @brew_info ||= Chef::JSONCompat.from_json(brew('info', '--json=v1', new_resource.package_name)).first
+ end
+
+ # 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.
+ #
+ def current_installed_version
+ brew_info['keg_only'] ? brew_info['installed'].last['version'] : brew_info['linked_keg']
+ end
+
+ # 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['version']+, but the
+ # schema has changed, and homebrew is a constantly rolling
+ # forward project.
+ #
+ # https://github.com/Homebrew/homebrew/wiki/Acceptable-Formulae#stable-versions
+ def candidate_version
+ brew_info['versions']['stable']
+ end
+
+ private
+
+ def get_response_from_command(command)
+ home_dir = Etc.getpwnam(homebrew_owner(node)).dir
+
+ Chef::Log.debug "Executing '#{command}' as user '#{homebrew_owner(node)}'"
+ output = shell_out!(command, :timeout => 1800, :user => homebrew_owner(node), :environment => { 'HOME' => home_dir, 'RUBYOPT' => nil })
+ output.stdout.chomp
+ end
+ end
+ end
+ end
+end
diff --git a/lib/chef/providers.rb b/lib/chef/providers.rb
index dd384c90a6..fec00d0e63 100644
--- a/lib/chef/providers.rb
+++ b/lib/chef/providers.rb
@@ -60,6 +60,7 @@ require 'chef/provider/package/easy_install'
require 'chef/provider/package/freebsd/port'
require 'chef/provider/package/freebsd/pkg'
require 'chef/provider/package/freebsd/pkgng'
+require 'chef/provider/package/homebrew'
require 'chef/provider/package/ips'
require 'chef/provider/package/macports'
require 'chef/provider/package/pacman'
diff --git a/lib/chef/resource/homebrew_package.rb b/lib/chef/resource/homebrew_package.rb
new file mode 100644
index 0000000000..c3fa9ddffc
--- /dev/null
+++ b/lib/chef/resource/homebrew_package.rb
@@ -0,0 +1,34 @@
+#
+# Author:: Joshua Timberman (<joshua@getchef.com>)
+# Author:: Graeme Mathieson (<mathie@woss.name>)
+#
+# Copyright 2011-2013, Opscode, Inc.
+# Copyright 2014, Chef Software, Inc <legal@getchef.com>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+# http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+require 'chef/provider/package'
+require 'chef/resource/package'
+
+class Chef
+ class Resource
+ class HomebrewPackage < Chef::Resource::Package
+ def initialize(name, run_context=nil)
+ super
+ @resource_name = :homebrew_package
+ @provider = Chef::Provider::Package::Homebrew
+ end
+ end
+ end
+end
diff --git a/lib/chef/resources.rb b/lib/chef/resources.rb
index fa5c82761d..5b938095c6 100644
--- a/lib/chef/resources.rb
+++ b/lib/chef/resources.rb
@@ -40,6 +40,7 @@ require 'chef/resource/gem_package'
require 'chef/resource/git'
require 'chef/resource/group'
require 'chef/resource/http_request'
+require 'chef/resource/homebrew_package'
require 'chef/resource/ifconfig'
require 'chef/resource/link'
require 'chef/resource/log'