diff --git a/Berksfile b/Berksfile index f795efe..f2488bb 100644 --- a/Berksfile +++ b/Berksfile @@ -2,6 +2,8 @@ site :opscode metadata +cookbook "chef-sugar" + group :integration do cookbook "minitest-handler" cookbook "apt" diff --git a/attributes/default.rb b/attributes/default.rb index e78d47b..8e8d117 100644 --- a/attributes/default.rb +++ b/attributes/default.rb @@ -43,3 +43,6 @@ default['python']['virtualenv_location'] = "#{node['python']['prefix_dir']}/bin/virtualenv" default['python']['setuptools_version'] = nil # defaults to latest default['python']['virtualenv_version'] = nil +default['python']['pip_version'] = nil +default['python']['yolk_version'] = nil +default['python']['yolk_location'] = "#{node['python']['prefix_dir']}/bin/yolk" diff --git a/metadata.rb b/metadata.rb index 4a86423..41a2a0c 100644 --- a/metadata.rb +++ b/metadata.rb @@ -7,6 +7,7 @@ depends "build-essential" depends "yum-epel" +depends "chef-sugar" recipe "python", "Installs python, pip, and virtualenv" recipe "python::package", "Installs python using packages." diff --git a/providers/pip.rb b/providers/pip.rb index bdd8142..10e9929 100644 --- a/providers/pip.rb +++ b/providers/pip.rb @@ -117,11 +117,30 @@ def current_installed_version end def candidate_version + yolk_path = which_yolk(new_resource) @candidate_version ||= begin - # `pip search` doesn't return versions yet - # `pip list` may be coming soon: - # https://bitbucket.org/ianb/pip/issue/197/option-to-show-what-version-would-be - new_resource.version||'latest' + # if yolk is installed, check if a newer version is available in PyPi + # then check if newer version is greater then the version specified by new_resource + # return as appropriate + if ::File.exists?(yolk_path) then + out = shell_out("#{yolk_path} -U #{new_resource.package_name}").stdout + if out.match(/not installed/) then + new_resource.version||'latest' + elsif out.match(/#{new_resource.package_name} [\d\.]+ \([\d\.]+\)/) then + available_version = out.split(' ').last.tr('()','') + if ! new_resource.version + available_version + elsif available_version.satisfies?(">= #{new_resource.version}") + new_resource.version + else + available_version.to_s + end + else + current_installed_version + end + else + new_resource.version||'latest' + end end end @@ -133,11 +152,12 @@ def install_package(version) else version = "==#{version}" end + new_resource.options "#{new_resource.options} --egg" pip_cmd('install', version) end def upgrade_package(version) - new_resource.options "#{new_resource.options} --upgrade" + new_resource.options "#{new_resource.options} --upgrade --egg" install_package(version) end @@ -166,3 +186,13 @@ def which_pip(nr) 'pip' end end + +def which_yolk(nr) + if (nr.respond_to?("virtualenv") && nr.virtualenv) + ::File.join(nr.virtualenv,'/bin/yolk') + elsif ::File.exists?(node['python']['yolk_location']) + node['python']['yolk_location'] + else + 'yolk' + end +end diff --git a/recipes/pip.rb b/recipes/pip.rb index 17110fa..e536188 100644 --- a/recipes/pip.rb +++ b/recipes/pip.rb @@ -23,6 +23,9 @@ # redhat/package: /usr/bin/pip (sha a8a3a3) # omnibus/source: /opt/local/bin/pip (sha 29ce9874) +include_recipe 'chef-sugar::default' +require 'chef/sugar/core_extensions' + if node['python']['install_method'] == 'source' pip_binary = "#{node['python']['prefix_dir']}/bin/pip" elsif platform_family?("rhel", "fedora") @@ -33,10 +36,11 @@ pip_binary = "/usr/local/bin/pip" end -cookbook_file "#{Chef::Config[:file_cache_path]}/get-pip.py" do - source 'get-pip.py' +remote_file "#{Chef::Config[:file_cache_path]}/get-pip.py" do + source 'https://bootstrap.pypa.io/get-pip.py' mode "0644" - not_if { ::File.exists?(pip_binary) } + action :create_if_missing + notifies :run, 'execute[install-pip]', :immediately end execute "install-pip" do @@ -44,10 +48,35 @@ command <<-EOF #{node['python']['binary']} get-pip.py EOF - not_if { ::File.exists?(pip_binary) } + action :nothing +end + +remote_file "#{Chef::Config[:file_cache_path]}/ez_setup.py" do + source 'https://bootstrap.pypa.io/ez_setup.py' + mode "0644" + action :create_if_missing + notifies :run, 'execute[install-setuptools]', :immediately +end + +execute "install-setuptools" do + cwd Chef::Config[:file_cache_path] + command <<-EOF + #{node['python']['binary']} ez_setup.py + EOF + action :nothing +end + +python_pip 'pip' do + action :upgrade + version node['python']['pip_version'] end python_pip 'setuptools' do action :upgrade version node['python']['setuptools_version'] end + +python_pip 'yolk' do + action :upgrade + version node['python']['yolk_version'] +end \ No newline at end of file