Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,13 @@ def needs_upgrade?
return false if desired_version == 'present'

if desired_version == 'latest'
latest_version = @resource.catalog.resource('package', 'puppet-agent').parameters[:ensure].latest
desired_version = latest_version.match(%r{^(?:[0-9]:)?(\d+\.\d+(\.\d+)?(?:\.\d+))?}).captures.first
# Package name might be different to puppet-agent, hence we need to look it up.
package_name = @resource.catalog.resource('class', 'puppet_agent')[:package_name]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If package_name can be nil (not sure that's possible?), then it'd be good to set a default:

Suggested change
package_name = @resource.catalog.resource('class', 'puppet_agent')[:package_name]
package_name = @resource.catalog.resource('class', 'puppet_agent')[:package_name] || 'puppet-agent'

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it can (realistically) be nil - but having a sane default still seems like a good idea.


# Latest version might be undefined, e.G. if we're about to install a different named
# package than the currently running one. In that case, we'll leave desired_version empty.
latest_version = @resource.catalog.resource('package', package_name).parameters[:ensure].latest
desired_version = latest_version.match(%r{^(?:[0-9]:)?(\d+\.\d+(\.\d+)?(?:\.\d+))?}).captures.first unless latest_version.nil?
Copy link
Contributor

@joshcooper joshcooper Mar 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If latest_version is nil, then desired_version will be too, and the versioncmp function will raise:

❯ bundle exec ruby -rpuppet -e 'Puppet::Util::Package.versioncmp(nil, "1.2.3")' 
/home/josh/work/puppet-private/lib/puppet/util/package.rb:12:in `versioncmp': undefined method `scan' for nil:NilClass (NoMethodError)

Maybe use empty string instead?

desired_version = if latest_version.nil?
                    ""
                  else
                    latest_version.match(%r{^(?:[0-9]:)?(\d+\.\d+(\.\d+)?(?:\.\d+))?}).captures.first
                  end

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If latest_version is nil then desired_version will still be 'latest' - But I think it makes sense to be more explicit here.

end

Puppet::Util::Package.versioncmp(desired_version, current_version) != 0
Expand Down
Loading