Class: Chef::Provider::Package

Inherits:
Chef::Provider show all
Includes:
Mixin::Command
Defined in:
lib/chef/provider/package.rb,
lib/chef/provider/package/apt.rb,
lib/chef/provider/package/rpm.rb,
lib/chef/provider/package/yum.rb,
lib/chef/provider/package/dpkg.rb,
lib/chef/provider/package/zypper.rb,
lib/chef/provider/package/freebsd.rb,
lib/chef/provider/package/portage.rb,
lib/chef/provider/package/macports.rb,
lib/chef/provider/package/rubygems.rb,
lib/chef/provider/package/easy_install.rb

Direct Known Subclasses

Apt, EasyInstall, Freebsd, Macports, Portage, Rpm, Rubygems, Yum, Zypper

Defined Under Namespace

Classes: Apt, Dpkg, EasyInstall, Freebsd, Macports, Portage, Rpm, Rubygems, Yum, Zypper

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #node

Instance Method Summary collapse

Methods included from Mixin::Command

handle_command_failures, not_if, only_if, output_of_command, popen4, run_command, run_command_with_systems_locale

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #load_current_resource

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

#data_bag, #data_bag_item, #platform?, #search, #value_for_platform

Constructor Details

#initialize(node, new_resource, collection = nil, definitions = nil, cookbook_loader = nil) ⇒ Package

Returns a new instance of Package.



33
34
35
36
# File 'lib/chef/provider/package.rb', line 33

def initialize(node, new_resource, collection=nil, definitions=nil, cookbook_loader=nil)
  super(node, new_resource, collection, definitions, cookbook_loader)
  @candidate_version = nil
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Chef::Mixin::RecipeDefinitionDSLCore

Instance Attribute Details

#candidate_versionObject

Returns the value of attribute candidate_version.



31
32
33
# File 'lib/chef/provider/package.rb', line 31

def candidate_version
  @candidate_version
end

Instance Method Details

#action_installObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/chef/provider/package.rb', line 38

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 && @new_resource.version != @current_resource.version
    install_version = @new_resource.version
  # If it's not installed at all, install it
  elsif @current_resource.version == nil
    install_version = candidate_version
  else
    return
  end

  unless install_version
    raise(Chef::Exceptions::Package, "No version specified, and no candidate version available!")
  end

  Chef::Log.info("Installing #{@new_resource} version #{install_version}")
    
  # We need to make sure we handle the preseed file
  if @new_resource.response_file
    preseed_package(@new_resource.package_name, install_version)
  end
    
  status = install_package(@new_resource.package_name, install_version)
  if status
    @new_resource.updated = true
  end
end

#action_purgeObject



99
100
101
102
103
104
105
# File 'lib/chef/provider/package.rb', line 99

def action_purge
  if should_remove_package(@current_resource.version, @new_resource.version)
    Chef::Log.info("Purging #{@new_resource}")
    purge_package(@current_resource.package_name, @new_resource.version)
    @new_resource.updated = true
  end
end

#action_removeObject



77
78
79
80
81
82
83
# File 'lib/chef/provider/package.rb', line 77

def action_remove        
  if should_remove_package(@current_resource.version, @new_resource.version)
    Chef::Log.info("Removing #{@new_resource}")
    remove_package(@current_resource.package_name, @new_resource.version)
    @new_resource.updated = true
  end
end

#action_upgradeObject



66
67
68
69
70
71
72
73
74
75
# File 'lib/chef/provider/package.rb', line 66

def action_upgrade
  if @current_resource.version != candidate_version
    orig_version = @current_resource.version || "uninstalled"
    Chef::Log.info("Upgrading #{@new_resource} version from #{orig_version} to #{candidate_version}")
    status = upgrade_package(@new_resource.package_name, candidate_version)
    if status
      @new_resource.updated = true
    end
  end
end

#expand_options(options) ⇒ Object



154
155
156
# File 'lib/chef/provider/package.rb', line 154

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

#get_preseed_file(name, version) ⇒ Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/chef/provider/package.rb', line 127

def get_preseed_file(name, version)        
  full_cache_dir = Chef::FileCache.create_cache_path("preseed/#{@new_resource.cookbook_name}")
  full_cache_file = "#{full_cache_dir}/#{name}-#{version}.seed"
  cache_path = "preseed/#{@new_resource.cookbook_name}/#{name}-#{version}.seed"
        
  Chef::Log.debug("Fetching preseed file to #{cache_path}")
  
  remote_file = Chef::Resource::RemoteFile.new(
    full_cache_file,
    nil,
    @node
  )
  remote_file.cookbook_name = @new_resource.cookbook_name
  remote_file.source(@new_resource.response_file)
  remote_file.backup(false)
  
  rf_provider = Chef::Platform.provider_for_node(@node, remote_file)
  rf_provider.load_current_resource
  rf_provider.action_create
  
  if remote_file.updated
    Chef::FileCache.load(cache_path, false)
  else
    false
  end
end

#install_package(name, version) ⇒ Object



107
108
109
# File 'lib/chef/provider/package.rb', line 107

def install_package(name, version)
  raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :install"
end

#preseed_package(name, version, preseed) ⇒ Object



123
124
125
# File 'lib/chef/provider/package.rb', line 123

def preseed_package(name, version, preseed)
  raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support pre-seeding package install/upgrade instructions - don't ask it to!" 
end

#purge_package(name, version) ⇒ Object



119
120
121
# File 'lib/chef/provider/package.rb', line 119

def purge_package(name, version)
  raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :purge" 
end

#remove_package(name, version) ⇒ Object



115
116
117
# File 'lib/chef/provider/package.rb', line 115

def remove_package(name, version)
  raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :remove" 
end

#should_remove_package(current_version, new_version) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/chef/provider/package.rb', line 85

def should_remove_package(current_version, new_version)
  to_remove_package = false
  if current_version != nil
    if new_version != nil 
      if new_version == current_version
        to_remove_package = true
      end
    else
      to_remove_package = true
    end
  end
  to_remove_package
end

#upgrade_package(name, version) ⇒ Object



111
112
113
# File 'lib/chef/provider/package.rb', line 111

def upgrade_package(name, version)
  raise Chef::Exceptions::UnsupportedAction, "#{self.to_s} does not support :upgrade" 
end