a | b | |
---|
| 0 | + | # |
---|
| 0 | + | # Author:: Joe Williams (<joe@joetify.com>) |
---|
| 0 | + | # Copyright:: Copyright (c) 2009 Joe Williams |
---|
| 0 | + | # License:: Apache License, Version 2.0 |
---|
| 0 | + | # |
---|
| 0 | + | # Licensed under the Apache License, Version 2.0 (the "License"); |
---|
| 0 | + | # you may not use this file except in compliance with the License. |
---|
| 0 | + | # You may obtain a copy of the License at |
---|
| 0 | + | # |
---|
| 0 | + | # http://www.apache.org/licenses/LICENSE-2.0 |
---|
| 0 | + | # |
---|
| 0 | + | # Unless required by applicable law or agreed to in writing, software |
---|
| 0 | + | # distributed under the License is distributed on an "AS IS" BASIS, |
---|
| 0 | + | # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
---|
| 0 | + | # See the License for the specific language governing permissions and |
---|
| 0 | + | # limitations under the License. |
---|
| 0 | + | # |
---|
| 0 | + | |
---|
| 0 | + | require 'chef/provider/package' |
---|
| 0 | + | require 'chef/mixin/command' |
---|
| 0 | + | require 'chef/mixin/shell_out' |
---|
| 0 | + | require 'chef/resource/package' |
---|
| 0 | + | require 'chef/mixin/shell_out' |
---|
| 0 | + | |
---|
| 0 | + | class Chef |
---|
| 0 | + | class Provider |
---|
| 0 | + | class Package |
---|
| 0 | + | class EasyInstall < Chef::Provider::Package |
---|
| 0 | + | |
---|
| 0 | + | include Chef::Mixin::ShellOut |
---|
| 0 | + | |
---|
| 0 | + | def install_check(name) |
---|
| 0 | + | check = false |
---|
| 0 | + | |
---|
| 0 | + | begin |
---|
| 0 | + | # first check to see if we can import it |
---|
| 0 | + | output = shell_out!("python -c \"import #{name}\"", :returns=>[0,1]).stderr |
---|
| 0 | + | if output.include? "ImportError" |
---|
| 0 | + | # then check to see if its on the path |
---|
| 0 | + | output = shell_out!("python -c \"import sys; print sys.path\"", :returns=>[0,1]).stdout |
---|
| 0 | + | if output.downcase.include? "#{name.downcase}" |
---|
| 0 | + | check = true |
---|
| 0 | + | end |
---|
| 0 | + | else |
---|
| 0 | + | check = true |
---|
| 0 | + | end |
---|
| 0 | + | rescue |
---|
| 0 | + | # it's probably not installed |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | check |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | def easy_install_binary_path |
---|
| 0 | + | path = @new_resource.easy_install_binary |
---|
| 0 | + | path ? path : 'easy_install' |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | def load_current_resource |
---|
| 0 | + | @current_resource = Chef::Resource::Package.new(@new_resource.name) |
---|
| 0 | + | @current_resource.package_name(@new_resource.package_name) |
---|
| 0 | + | @current_resource.version(nil) |
---|
| 0 | + | |
---|
| 0 | + | # get the currently installed version if installed |
---|
| 0 | + | package_version = nil |
---|
| 0 | + | if install_check(@new_resource.package_name) |
---|
| 0 | + | begin |
---|
| 0 | + | output = shell_out!("python -c \"import #{@new_resource.package_name}; print #{@new_resource.package_name}.__version__\"").stdout |
---|
| 0 | + | package_version = output.strip |
---|
| 0 | + | rescue |
---|
| 0 | + | output = shell_out!("python -c \"import sys; print sys.path\"", :returns=>[0,1]).stdout |
---|
| 0 | + | |
---|
| 0 | + | output_array = output.gsub(/[\[\]]/,'').split(/\s*,\s*/) |
---|
| 0 | + | package_path = "" |
---|
| 0 | + | |
---|
| 0 | + | output_array.each do |entry| |
---|
| 0 | + | if entry.downcase.include?(@new_resource.package_name) |
---|
| 0 | + | package_path = entry |
---|
| 0 | + | end |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | package_path[/\S\S(.*)\/(.*)-(.*)-py(.*).egg\S/] |
---|
| 0 | + | package_version = $3 |
---|
| 0 | + | end |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | if package_version == @new_resource.version |
---|
| 0 | + | Chef::Log.debug("#{@new_resource.package_name} at version #{@new_resource.version}") |
---|
| 0 | + | @current_resource.version(@new_resource.version) |
---|
| 0 | + | else |
---|
| 0 | + | Chef::Log.debug("#{@new_resource.package_name} at version #{package_version}") |
---|
| 0 | + | @current_resource.version(package_version) |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | @current_resource |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | def candidate_version |
---|
| 0 | + | return @candidate_version if @candidate_version |
---|
| 0 | + | |
---|
| 0 | + | # do a dry run to get the latest version |
---|
| 0 | + | result = shell_out!("#{easy_install_binary_path} -n #{@new_resource.package_name}", :returns=>[0,1]) |
---|
| 0 | + | @candidate_version = result.stdout[/(.*)Best match: (.*) (.*)$/, 3] |
---|
| 0 | + | @candidate_version |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | def install_package(name, version) |
---|
| 0 | + | run_command(:command => "#{easy_install_binary_path} \"#{name}==#{version}\"") |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | def upgrade_package(name, version) |
---|
| 0 | + | install_package(name, version) |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | def remove_package(name, version) |
---|
| 0 | + | run_command(:command => "#{easy_install_binary_path} -m #{name}") |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | def purge_package(name, version) |
---|
| 0 | + | remove_package(name, version) |
---|
| 0 | + | end |
---|
| 0 | + | |
---|
| 0 | + | end |
---|
| 0 | + | end |
---|
| 0 | + | end |
---|
| 0 | + | end |
---|
... | |
---|