Skip to content

fix: Do not hard code package name in puppet_agent_end_run.rb #761

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

fetzerms
Copy link

When using this module to install puppet-agent compatible packages with a different name (such as openvox-agent) calling puppet_agent_end_run with latest will fail, as the package name is hard coded.

This patch replaces the hard coded puppet-agent with a dynamic lookup for the package name. It also considers that (during migration) the latest_version lookup will not work.

How to reproduce the error:

    class { '::puppet_agent':
      package_version => 'latest',
      package_name    => 'openvox-agent',
      manage_repo     => false,
      require         => [
        Apt::Source['openvox']
      ],
    }

Results in:

Error: undefined method `parameters' for nil:NilClass
Info: Unknown failure using insync_values? on type: Puppet_agent_end_run[latest] / property: end_run to compare values [true] and false
Error: /Stage[main]/Puppet_agent::Install/Puppet_agent_end_run[latest]/end_run: change from false to true failed: undefined method `parameters' for nil:NilClass

@fetzerms fetzerms requested review from bastelfreak and a team as code owners February 28, 2025 16:40
@CLAassistant
Copy link

CLAassistant commented Feb 28, 2025

CLA assistant check
All committers have signed the CLA.

@fetzerms fetzerms force-pushed the openvox_compatibility branch from 2000bc5 to 4eb240d Compare February 28, 2025 16:45
@fetzerms
Copy link
Author

fetzerms commented Feb 28, 2025

Not sure how to handle the tests, as they do not seem to ensure that the puppet_agent class is in the catalog. Most likely adding to catalog in puppet_agent_end_run_spec? And also adding other "bogus" packages to ensure it works properly?

# 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.

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.

@joshcooper
Copy link
Contributor

joshcooper commented Mar 12, 2025

The tests are failing because they "manually" create a catalog and add the agent_latest_package to it. You could add the class too, it's a little gross. Note the differences with dashes and underscores.

       Puppet::Type.type(:package).new(name: 'puppet-agent', ensure: 'latest', provider: :yum)
     end
 
+    let(:puppet_agent_class) do
+      Puppet::Type.type(:component).new(name: 'puppet_agent', package_name: 'puppet-agent')
+    end
+
     before(:each) do
+      catalog.add_resource(puppet_agent_class)
       catalog.add_resource(agent_latest_package)
       resource.catalog = catalog
     end

@fetzerms
Copy link
Author

The tests are failing because they "manually" create a catalog and add the agent_latest_package to it. You could add the class too, it's a little gross. Note the differences with dashes and underscores.

       Puppet::Type.type(:package).new(name: 'puppet-agent', ensure: 'latest', provider: :yum)
     end
 
+    let(:puppet_agent_class) do
+      Puppet::Type.type(:component).new(name: 'puppet_agent', package_name: 'puppet-agent')
+    end
+
     before(:each) do
+      catalog.add_resource(puppet_agent_class)
       catalog.add_resource(agent_latest_package)
       resource.catalog = catalog
     end

Thanks a lot! I'll enhance/fix the tests and will include your previous review comments early next week.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants