Skip to content

Commit

Permalink
Merge branch 'COOK-1715'
Browse files Browse the repository at this point in the history
  • Loading branch information
jtimberman committed Nov 22, 2012
2 parents c6c891b + 7a7beb7 commit b8fffe2
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 41 deletions.
68 changes: 29 additions & 39 deletions providers/pip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,52 +39,40 @@ def whyrun_supported?
install_version = candidate_version
end

# Set the timeout (units in seconds)
timeout = 900
if @new_resource.timeout
timeout = @new_resource.timeout
end

if install_version
description = "install package #{@new_resource} version #{install_version}"
converge_by(description) do
Chef::Log.info("Installing #{@new_resource} version #{install_version}")
status = install_package(@new_resource.package_name, install_version, timeout)
Chef::Log.info("Installing #{@new_resource} version #{install_version}")
status = install_package(install_version)
if status
@new_resource.updated_by_last_action(true)
end
end
end
end

action :upgrade do
# Set the timeout (units in seconds)
timeout = 900
if @new_resource.timeout
timeout = @new_resource.timeout
end

if @current_resource.version != candidate_version
orig_version = @current_resource.version || "uninstalled"
description = "upgrade #{@current_resource} version from #{@current_resource.version} to #{candidate_version}"
converge_by(description) do
Chef::Log.info("Upgrading #{@new_resource} version from #{orig_version} to #{candidate_version}")
status = upgrade_package(@new_resource.package_name, candidate_version, timeout)
Chef::Log.info("Upgrading #{@new_resource} version from #{orig_version} to #{candidate_version}")
status = upgrade_package(candidate_version)
if status
@new_resource.updated_by_last_action(true)
end
end
end
end

action :remove do
# Set the timeout (units in seconds)
timeout = 900
if @new_resource.timeout
timeout = @new_resource.timeout
end

if removing_package?
description = "remove package #{@new_resource}"
converge_by(description) do
Chef::Log.info("Removing #{@new_resource}")
remove_package(@current_resource.package_name, @new_resource.version, timeout)
Chef::Log.info("Removing #{@new_resource}")
remove_package(@new_resource.version)
@new_resource.updated_by_last_action(true)
end
else
end
end

Expand All @@ -100,10 +88,6 @@ def removing_package?
end
end

def expand_options(options)
options ? " #{options}" : ""
end

# these methods are the required overrides of
# a provider that extends from Chef::Provider::Package
# so refactoring into core Chef should be easy
Expand All @@ -124,7 +108,7 @@ def current_installed_version
@current_installed_version ||= begin
delimeter = /==/

version_check_cmd = "#{pip_cmd(@new_resource)} freeze | grep -i '^#{@new_resource.package_name}=='"
version_check_cmd = "#{which_pip(@new_resource)} freeze | grep -i '^#{@new_resource.package_name}=='"
# incase you upgrade pip with pip!
if @new_resource.package_name.eql?('pip')
delimeter = /\s/
Expand All @@ -146,23 +130,29 @@ def candidate_version
end
end

def install_package(name, version, timeout)
v = "==#{version}" unless version.eql?('latest')
shell_out!("#{pip_cmd(@new_resource)} install#{expand_options(@new_resource.options)} #{name}#{v}", :timeout => timeout)
def install_package(version)
pip_cmd('install', version == 'latest' ? '' : "==#{version}")
end

def upgrade_package(version)
@new_resource.options "#{@new_resource.options} --upgrade"
install_package(version)
end

def upgrade_package(name, version, timeout)
v = "==#{version}" unless version.eql?('latest')
shell_out!("#{pip_cmd(@new_resource)} install --upgrade#{expand_options(@new_resource.options)} #{@new_resource.name}#{v}", :timeout => timeout)
def remove_package(version)
@new_resource.options "#{@new_resource.options} --yes"
pip_cmd('uninstall')
end

def remove_package(name, version, timeout)
shell_out!("#{pip_cmd(@new_resource)} uninstall -y#{expand_options(@new_resource.options)} #{@new_resource.name}", :timeout => timeout)
def pip_cmd(subcommand, version='')
options = { :timeout => @new_resource.timeout, :user => @new_resource.user, :group => @new_resource.group }
options[:environment] = { 'HOME' => ::File.expand_path("~#{@new_resource.user}") } if @new_resource.user
shell_out!("#{which_pip(@new_resource)} #{subcommand} #{@new_resource.options} #{@new_resource.name}#{version}", options)
end

# TODO remove when provider is moved into Chef core
# this allows PythonPip to work with Chef::Resource::Package
def pip_cmd(nr)
def which_pip(nr)
if (nr.respond_to?("virtualenv") && nr.virtualenv)
::File.join(nr.virtualenv,'/bin/pip')
elsif "#{node['python']['install_method']}".eql?("source")
Expand Down
6 changes: 4 additions & 2 deletions resources/pip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@

attribute :package_name, :kind_of => String, :name_attribute => true
attribute :version, :default => nil
attribute :timeout, :default => nil
attribute :timeout, :default => 900
attribute :virtualenv, :kind_of => String
attribute :options, :kind_of => String
attribute :user, :regex => Chef::Config[:user_valid_regex]
attribute :group, :regex => Chef::Config[:group_valid_regex]
attribute :options, :kind_of => String, :default => ''

0 comments on commit b8fffe2

Please sign in to comment.