# class Chef::Provider::Package

### Attributes

candidate_version[RW]

### Public Class Methods

new(new_resource, run_context) click to toggle source
Calls superclass method Chef::Provider.new

    # File lib/chef/provider/package.rb, line 31
    def initialize(new_resource, run_context)
      super
      @candidate_version = nil
    end

### Public Instance Methods

action_install() click to toggle source

    # File lib/chef/provider/package.rb, line 59
    def action_install
      # If we specified a version, and it's not the current version, move to the specified version
      if !@new_resource.version.nil? && !(target_version_already_installed?)
        install_version = @new_resource.version
      # If it's not installed at all, install it
      elsif @current_resource.version.nil?
        install_version = candidate_version
      else
        Chef::Log.debug("#{@new_resource} is already installed - nothing to do")
        return
      end

      # We need to make sure we handle the preseed file
      if @new_resource.response_file
        if preseed_file = get_preseed_file(@new_resource.package_name, install_version)
          converge_by("preseed package #{@new_resource.package_name}") do
            preseed_package(preseed_file)
          end
        end
      end
      description = install_version ? "version #{install_version} of" : ""
      converge_by("install #{description} package #{@new_resource.package_name}") do
        @new_resource.version(install_version)
        install_package(@new_resource.package_name, install_version)
      end
    end

action_purge() click to toggle source

    # File lib/chef/provider/package.rb, line 125
    def action_purge
      if removing_package?
        description = @new_resource.version ? "version #{@new_resource.version} of" : ""
        converge_by("purge #{description} package #{@current_resource.package_name}") do
          purge_package(@current_resource.package_name, @new_resource.version)
          Chef::Log.info("#{@new_resource} purged")
        end
      end
    end

action_reconfig() click to toggle source

    # File lib/chef/provider/package.rb, line 135
    def action_reconfig
      if @current_resource.version == nil then
        Chef::Log.debug("#{@new_resource} is NOT installed - nothing to do")
        return
      end

      unless @new_resource.response_file then
        Chef::Log.debug("#{@new_resource} no response_file provided - nothing to do")
        return
      end

      if preseed_file = get_preseed_file(@new_resource.package_name, @current_resource.version)
        converge_by("reconfigure package #{@new_resource.package_name}") do
          preseed_package(preseed_file)
          reconfig_package(@new_resource.package_name, @current_resource.version)
          Chef::Log.info("#{@new_resource} reconfigured")
        end
      else
        Chef::Log.debug("#{@new_resource} preseeding has not changed - nothing to do")
      end
    end

action_remove() click to toggle source

    # File lib/chef/provider/package.rb, line 101
    def action_remove
      if removing_package?
        description = @new_resource.version ? "version #{@new_resource.version} of " :  ""
        converge_by("remove #{description} package #{@current_resource.package_name}") do
          remove_package(@current_resource.package_name, @new_resource.version)
          Chef::Log.info("#{@new_resource} removed")
        end
      else
        Chef::Log.debug("#{@new_resource} package does not exist - nothing to do")
      end
    end

action_upgrade() click to toggle source

    # File lib/chef/provider/package.rb, line 86
    def action_upgrade
      if candidate_version.nil?
        Chef::Log.debug("#{@new_resource} no candidate version - nothing to do")
      elsif @current_resource.version == candidate_version
        Chef::Log.debug("#{@new_resource} is at the latest version - nothing to do")
      else
        @new_resource.version(candidate_version)
        orig_version = @current_resource.version || "uninstalled"
        converge_by("upgrade package #{@new_resource.package_name} from #{orig_version} to #{candidate_version}") do
          upgrade_package(@new_resource.package_name, candidate_version)
          Chef::Log.info("#{@new_resource} upgraded from #{orig_version} to #{candidate_version}")
        end
      end
    end

define_resource_requirements() click to toggle source

    # File lib/chef/provider/package.rb, line 43
    def define_resource_requirements
      requirements.assert(:install) do |a|
        a.assertion { ((@new_resource.version != nil) && !(target_version_already_installed?))              || !(@current_resource.version.nil? && candidate_version.nil?)  }
        a.failure_message(Chef::Exceptions::Package, "No version specified, and no candidate version available for #{@new_resource.package_name}")
        a.whyrun("Assuming a repository that offers #{@new_resource.package_name} would have been configured")
      end

      requirements.assert(:upgrade) do |a|
        # Can't upgrade what we don't have
        a.assertion  { !(@current_resource.version.nil? && candidate_version.nil?) }
        a.failure_message(Chef::Exceptions::Package, "No candidate version available for #{@new_resource.package_name}")
        a.whyrun("Assuming a repository that offers #{@new_resource.package_name} would have been configured")
      end
    end

expand_options(options) click to toggle source

    # File lib/chef/provider/package.rb, line 220
    def expand_options(options)
      options ? " #{options}" : ""
    end

get_preseed_file(name, version) click to toggle source

    # File lib/chef/provider/package.rb, line 181
    def get_preseed_file(name, version)
      resource = preseed_resource(name, version)
      resource.run_action(:create)
      Chef::Log.debug("#{@new_resource} fetched preseed file to #{resource.path}")

      if resource.updated_by_last_action?
        resource.path
      else
        false
      end
    end

install_package(name, version) click to toggle source

    # File lib/chef/provider/package.rb, line 157
    def install_package(name, version)
      raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :install"
    end

load_current_resource() click to toggle source

    # File lib/chef/provider/package.rb, line 40
    def load_current_resource
    end

preseed_package(file) click to toggle source

    # File lib/chef/provider/package.rb, line 173
    def preseed_package(file)
      raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support pre-seeding package install/upgrade instructions"
    end

preseed_resource(name, version) click to toggle source

    # File lib/chef/provider/package.rb, line 193
    def preseed_resource(name, version)
      # A directory in our cache to store this cookbook's preseed files in
      file_cache_dir = Chef::FileCache.create_cache_path("preseed/#{@new_resource.cookbook_name}")
      # The full path where the preseed file will be stored
      cache_seed_to = "#{file_cache_dir}/#{name}-#{version}.seed"

      Chef::Log.debug("#{@new_resource} fetching preseed file to #{cache_seed_to}")

      if template_available?(@new_resource.response_file)
        Chef::Log.debug("#{@new_resource} fetching preseed file via Template")
        remote_file = Chef::Resource::Template.new(cache_seed_to, run_context)
        remote_file.variables(@new_resource.response_file_variables)
      elsif cookbook_file_available?(@new_resource.response_file)
        Chef::Log.debug("#{@new_resource} fetching preseed file via cookbook_file")
        remote_file = Chef::Resource::CookbookFile.new(cache_seed_to, run_context)
      else
        message = "No template or cookbook file found for response file #{@new_resource.response_file}"
        raise Chef::Exceptions::FileNotFound, message
      end

      remote_file.cookbook_name = @new_resource.cookbook_name
      remote_file.source(@new_resource.response_file)
      remote_file.backup(false)
      remote_file
    end

purge_package(name, version) click to toggle source

    # File lib/chef/provider/package.rb, line 169
    def purge_package(name, version)
      raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :purge"
    end

reconfig_package(name, version) click to toggle source

    # File lib/chef/provider/package.rb, line 177
    def reconfig_package(name, version)
      raise( Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :reconfig" )
    end

remove_package(name, version) click to toggle source

    # File lib/chef/provider/package.rb, line 165
    def remove_package(name, version)
      raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :remove"
    end

removing_package?() click to toggle source

    # File lib/chef/provider/package.rb, line 113
    def removing_package?
      if @current_resource.version.nil?
        false # nothing to remove
      elsif @new_resource.version.nil?
        true # remove any version of a package
      elsif @new_resource.version == @current_resource.version
        true # remove the version we have
      else
        false # we don't have the version we want to remove
      end
    end

target_version_already_installed?() click to toggle source

    # File lib/chef/provider/package.rb, line 224
    def target_version_already_installed?
      @new_resource.version == @current_resource.version
    end

upgrade_package(name, version) click to toggle source

    # File lib/chef/provider/package.rb, line 161
    def upgrade_package(name, version)
      raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :upgrade"
    end

whyrun_supported?() click to toggle source

    # File lib/chef/provider/package.rb, line 36
    def whyrun_supported?
      true
    end