summaryrefslogtreecommitdiff
path: root/lib/chef/provider/package
diff options
context:
space:
mode:
authorTim Smith <tsmith@chef.io>2017-11-02 17:43:14 -0700
committerTim Smith <tsmith@chef.io>2017-11-03 18:26:24 -0700
commit190e17dbd1e9ee2b21f9acd60844a5661f80a329 (patch)
tree2422e5e2027edf4e57ae62f7c7ec7325404c24ba /lib/chef/provider/package
parent2b60a984cf817b5a554a749e1cdfa16f4952eedf (diff)
downloadchef-190e17dbd1e9ee2b21f9acd60844a5661f80a329.tar.gz
Avoid the need to parse nil versions in the package provider
If we don't have a version installed yet we shouldn't go do a version comparison with nil. No matter what that's a waste of time and in dpkg/apt it's going to require shelling out now. Instead just do the install. This also adds the same version comparison method to the dpkg resource. At the moment it doesn't look like this is going to get called since our upgrade action there is a bit broken (doesn't actually compare versions), but that's going to get fixed next. This also cleans up the version comparison to make sure we're always dealing with strings Signed-off-by: Tim Smith <tsmith@chef.io>
Diffstat (limited to 'lib/chef/provider/package')
-rw-r--r--lib/chef/provider/package/apt.rb12
-rw-r--r--lib/chef/provider/package/dpkg.rb16
2 files changed, 24 insertions, 4 deletions
diff --git a/lib/chef/provider/package/apt.rb b/lib/chef/provider/package/apt.rb
index 605bbc70eb..3f32f9d380 100644
--- a/lib/chef/provider/package/apt.rb
+++ b/lib/chef/provider/package/apt.rb
@@ -127,12 +127,16 @@ class Chef
private
+ # compare 2 versions to each other to see which is newer.
+ # this differs from the standard package method because we
+ # need to be able to parse debian version strings which contain
+ # tildes which Gem cannot properly parse
+ #
+ # @return [Integer] 1 if v1 > v2. 0 if they're equal. -1 if v1 < v2
def version_compare(v1, v2)
- dpkg_v1 = v1 || '0'
- dpkg_v2 = v2 || '0'
- if shell_out_compact_timeout("dpkg", "--compare-versions", dpkg_v1, "gt", dpkg_v2).status.success?
+ if !shell_out_compact_timeout("dpkg", "--compare-versions", v1.to_s, "gt", v2.to_s).error?
1
- elsif shell_out_compact_timeout("dpkg", "--compare-versions", dpkg_v1, "eq", dpkg_v2).status.success?
+ elsif !shell_out_compact_timeout("dpkg", "--compare-versions", v1.to_s, "eq", v2.to_s).error?
0
else
-1
diff --git a/lib/chef/provider/package/dpkg.rb b/lib/chef/provider/package/dpkg.rb
index 89a57affac..cf92e6d3e7 100644
--- a/lib/chef/provider/package/dpkg.rb
+++ b/lib/chef/provider/package/dpkg.rb
@@ -106,6 +106,22 @@ class Chef
private
+ # compare 2 versions to each other to see which is newer.
+ # this differs from the standard package method because we
+ # need to be able to parse debian version strings which contain
+ # tildes which Gem cannot properly parse
+ #
+ # @return [Integer] 1 if v1 > v2. 0 if they're equal. -1 if v1 < v2
+ def version_compare(v1, v2)
+ if !shell_out_compact_timeout("dpkg", "--compare-versions", v1.to_s, "gt", v2.to_s).error?
+ 1
+ elsif !shell_out_compact_timeout("dpkg", "--compare-versions", v1.to_s, "eq", v2.to_s).error?
+ 0
+ else
+ -1
+ end
+ end
+
def read_current_version_of_package(package_name)
Chef::Log.debug("#{new_resource} checking install state of #{package_name}")
status = shell_out_compact_timeout!("dpkg", "-s", package_name, returns: [0, 1])