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/pacman.rb,
lib/chef/provider/package/zypper.rb,
lib/chef/provider/package/freebsd.rb,
lib/chef/provider/package/portage.rb,
lib/chef/provider/package/smartos.rb,
lib/chef/provider/package/solaris.rb,
lib/chef/provider/package/macports.rb,
lib/chef/provider/package/rubygems.rb,
lib/chef/provider/package/easy_install.rb

Defined Under Namespace

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

Instance Attribute Summary collapse

Attributes inherited from Chef::Provider

#current_resource, #new_resource, #run_context

Instance Method Summary collapse

Methods included from Mixin::Command

#chdir_or_tmpdir, #handle_command_failures, #output_of_command, #run_command, #run_command_with_systems_locale

Methods included from Mixin::Command::Windows

#popen4

Methods included from Mixin::Command::Unix

#popen4

Methods inherited from Chef::Provider

#action_nothing, build_from_file, #cookbook_name, #load_current_resource, #node, #resource_collection

Methods included from Mixin::ConvertToClassName

#convert_to_class_name, #convert_to_snake_case, #filename_to_qualified_string, #snake_case_basename

Methods included from Mixin::EnforceOwnershipAndPermissions

#enforce_ownership_and_permissions

Methods included from Mixin::RecipeDefinitionDSLCore

#method_missing

Methods included from Mixin::Language

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

Constructor Details

#initialize(new_resource, run_context) ⇒ Package

Returns a new instance of Package.



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

def initialize(new_resource, run_context)
  super
  @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.



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

def candidate_version
  @candidate_version
end

Instance Method Details

#action_installObject



37
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 37

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

  unless install_version
    raise(Chef::Exceptions::Package, "No version specified, and no candidate version available for #{@new_resource.package_name}")
  end


  # 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_by_last_action(true)
  end
  Chef::Log.info("#{@new_resource} installed version #{install_version}")
end

#action_purgeObject



106
107
108
109
110
111
112
# File 'lib/chef/provider/package.rb', line 106

def action_purge
  if removing_package?
    purge_package(@current_resource.package_name, @new_resource.version)
    @new_resource.updated_by_last_action(true)
    Chef::Log.info("#{@new_resource} purged")
  end
end

#action_reconfigObject



114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/chef/provider/package.rb', line 114

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

  status = preseed_package(@new_resource.package_name, @current_resource.version)
  unless status then
    Chef::Log.debug("#{@new_resource} preseeding has not changed - nothing to do")
    return
  end

  status = reconfig_package(@new_resource.package_name, @current_resource.version)
  @new_resource.updated_by_last_action(true) if status
  Chef::Log.info("#{@new_resource} reconfigured")
end

#action_removeObject



84
85
86
87
88
89
90
91
92
# File 'lib/chef/provider/package.rb', line 84

def action_remove
  if removing_package?
    remove_package(@current_resource.package_name, @new_resource.version)
    @new_resource.updated_by_last_action(true)
    Chef::Log.info("#{@new_resource} removed")
  else
    Chef::Log.debug("#{@new_resource} package does not exist - nothing to do")
  end
end

#action_upgradeObject



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/chef/provider/package.rb', line 66

def action_upgrade
  # Can't upgrade what we don't have
  if @current_resource.version.nil? && candidate_version.nil?
    raise(Chef::Exceptions::Package, "No candidate version available for #{@new_resource.package_name}")
  elsif 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
    orig_version = @current_resource.version || "uninstalled"
    status = upgrade_package(@new_resource.package_name, candidate_version)
    if status
      @new_resource.updated_by_last_action(true)
    end
    Chef::Log.info("#{@new_resource} upgraded from #{orig_version} to #{candidate_version}")
  end
end

#expand_options(options) ⇒ Object



198
199
200
# File 'lib/chef/provider/package.rb', line 198

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

#get_preseed_file(name, version) ⇒ Object



160
161
162
163
164
165
166
167
168
169
170
# File 'lib/chef/provider/package.rb', line 160

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) ⇒ Object



136
137
138
# File 'lib/chef/provider/package.rb', line 136

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

#preseed_package(name, version) ⇒ Object



152
153
154
# File 'lib/chef/provider/package.rb', line 152

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

#preseed_resource(name, version) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
# File 'lib/chef/provider/package.rb', line 172

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}")

  begin
    remote_file = Chef::Resource::Template.new(cache_seed_to, run_context)
    remote_file.cookbook_name = @new_resource.cookbook_name
    remote_file.source(@new_resource.response_file)
    remote_file.backup(false)
    provider = Chef::Platform.provider_for_resource(remote_file)
    provider.template_location
  rescue
    Chef::Log.debug("#{@new_resource} fetching preseed file via Template resource failed, fallback to CookbookFile resource")
    remote_file = Chef::Resource::CookbookFile.new(cache_seed_to, run_context)
    remote_file.cookbook_name = @new_resource.cookbook_name
    remote_file.source(@new_resource.response_file)
    remote_file.backup(false)
  end

  remote_file
end

#purge_package(name, version) ⇒ Object



148
149
150
# File 'lib/chef/provider/package.rb', line 148

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

#reconfig_package(name, version) ⇒ Object



156
157
158
# File 'lib/chef/provider/package.rb', line 156

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

#remove_package(name, version) ⇒ Object



144
145
146
# File 'lib/chef/provider/package.rb', line 144

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

#removing_package?Boolean

Returns:

  • (Boolean)


94
95
96
97
98
99
100
101
102
103
104
# File 'lib/chef/provider/package.rb', line 94

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

#upgrade_package(name, version) ⇒ Object



140
141
142
# File 'lib/chef/provider/package.rb', line 140

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