|
| 1 | +# |
| 2 | +# Author:: Seth Chisamore <[email protected]> |
| 3 | +# Cookbook Name:: python |
| 4 | +# Provider:: pip |
| 5 | +# |
| 6 | +# Copyright:: 2011, Opscode, Inc <[email protected]> |
| 7 | +# |
| 8 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 9 | +# you may not use this file except in compliance with the License. |
| 10 | +# You may obtain a copy of the License at |
| 11 | +# |
| 12 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 13 | +# |
| 14 | +# Unless required by applicable law or agreed to in writing, software |
| 15 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 16 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 17 | +# See the License for the specific language governing permissions and |
| 18 | +# limitations under the License. |
| 19 | +# |
| 20 | + |
| 21 | +require 'chef/mixin/shell_out' |
| 22 | +require 'chef/mixin/language' |
| 23 | +include Chef::Mixin::ShellOut |
| 24 | + |
| 25 | +# the logic in all action methods mirror that of |
| 26 | +# the Chef::Provider::Package which will make |
| 27 | +# refactoring into core chef easy |
| 28 | + |
| 29 | +action :install do |
| 30 | + # If we specified a version, and it's not the current version, move to the specified version |
| 31 | + if @new_resource.version != nil && @new_resource.version != @current_resource.version |
| 32 | + install_version = @new_resource.version |
| 33 | + # If it's not installed at all, install it |
| 34 | + elsif @current_resource.version == nil |
| 35 | + install_version = candidate_version |
| 36 | + end |
| 37 | + |
| 38 | + if install_version |
| 39 | + Chef::Log.info("Installing #{@new_resource} version #{install_version}") |
| 40 | + status = install_package(@new_resource.package_name, install_version) |
| 41 | + if status |
| 42 | + @new_resource.updated_by_last_action(true) |
| 43 | + end |
| 44 | + end |
| 45 | +end |
| 46 | + |
| 47 | +action :upgrade do |
| 48 | + if @current_resource.version != candidate_version |
| 49 | + orig_version = @current_resource.version || "uninstalled" |
| 50 | + Chef::Log.info("Upgrading #{@new_resource} version from #{orig_version} to #{candidate_version}") |
| 51 | + status = upgrade_package(@new_resource.package_name, candidate_version) |
| 52 | + if status |
| 53 | + @new_resource.updated_by_last_action(true) |
| 54 | + end |
| 55 | + end |
| 56 | +end |
| 57 | + |
| 58 | +action :remove do |
| 59 | + if removing_package? |
| 60 | + Chef::Log.info("Removing #{@new_resource}") |
| 61 | + remove_package(@current_resource.package_name, @new_resource.version) |
| 62 | + @new_resource.updated_by_last_action(true) |
| 63 | + else |
| 64 | + end |
| 65 | +end |
| 66 | + |
| 67 | +def removing_package? |
| 68 | + if @current_resource.version.nil? |
| 69 | + false # nothing to remove |
| 70 | + elsif @new_resource.version.nil? |
| 71 | + true # remove any version of a package |
| 72 | + elsif @new_resource.version == @current_resource.version |
| 73 | + true # remove the version we have |
| 74 | + else |
| 75 | + false # we don't have the version we want to remove |
| 76 | + end |
| 77 | +end |
| 78 | + |
| 79 | +def expand_options(options) |
| 80 | + options ? " #{options}" : "" |
| 81 | +end |
| 82 | + |
| 83 | +# these methods are the required overrides of |
| 84 | +# a provider that extends from Chef::Provider::Package |
| 85 | +# so refactoring into core Chef should be easy |
| 86 | + |
| 87 | +def load_current_resource |
| 88 | + @current_resource = Chef::Resource::PythonPip.new(@new_resource.name) |
| 89 | + @current_resource.package_name(@new_resource.package_name) |
| 90 | + @current_resource.version(nil) |
| 91 | + |
| 92 | + unless current_installed_version.nil? |
| 93 | + @current_resource.version(current_installed_version) |
| 94 | + end |
| 95 | + |
| 96 | + @current_resource |
| 97 | +end |
| 98 | + |
| 99 | +def current_installed_version |
| 100 | + @current_installed_version ||= begin |
| 101 | + delimeter = /==/ |
| 102 | + |
| 103 | + version_check_cmd = "pip freeze#{expand_virtualenv(can_haz_virtualenv(@new_resource))} | grep -i #{@new_resource.package_name}" |
| 104 | + # incase you upgrade pip with pip! |
| 105 | + if @new_resource.package_name.eql?('pip') |
| 106 | + delimeter = /\s/ |
| 107 | + version_check_cmd = "pip --version" |
| 108 | + end |
| 109 | + p = shell_out!(version_check_cmd) |
| 110 | + p.stdout.split(delimeter)[1].strip |
| 111 | + rescue Chef::Exceptions::ShellCommandFailed |
| 112 | + end |
| 113 | +end |
| 114 | + |
| 115 | +def candidate_version |
| 116 | + @candidate_version ||= begin |
| 117 | + # `pip search` doesn't return versions yet |
| 118 | + # `pip list` may be coming soon: |
| 119 | + # https://bitbucket.org/ianb/pip/issue/197/option-to-show-what-version-would-be |
| 120 | + @new_resource.version||'latest' |
| 121 | + end |
| 122 | +end |
| 123 | + |
| 124 | +def install_package(name,version) |
| 125 | + v = "==#{version}" unless version.eql?('latest') |
| 126 | + shell_out!("pip install#{expand_options(@new_resource.options)}#{expand_virtualenv(can_haz_virtualenv(@new_resource))} #{name}#{v}") |
| 127 | +end |
| 128 | + |
| 129 | +def upgrade_package(name, version) |
| 130 | + v = "==#{version}" unless version.eql?('latest') |
| 131 | + shell_out!("pip install --upgrade#{expand_options(@new_resource.options)}#{expand_virtualenv(can_haz_virtualenv(@new_resource))} #{@new_resource.name}#{v}") |
| 132 | +end |
| 133 | + |
| 134 | +def remove_package(name, version) |
| 135 | + shell_out!("pip uninstall -y#{expand_options(@new_resource.options)}#{expand_virtualenv(can_haz_virtualenv(@new_resource))} #{@new_resource.name}") |
| 136 | +end |
| 137 | + |
| 138 | +def expand_virtualenv(virtualenv) |
| 139 | + virtualenv && " --environment=#{virtualenv}" |
| 140 | +end |
| 141 | + |
| 142 | +# TODO remove when provider is moved into Chef core |
| 143 | +# this allows PythonPip to work with Chef::Resource::Package |
| 144 | +def can_haz_virtualenv(nr) |
| 145 | + nr.respond_to?("virtualenv") ? nr.virtualenv : nil |
| 146 | +end |
0 commit comments