puppet-nginx icon indicating copy to clipboard operation
puppet-nginx copied to clipboard

Override nginx_version facts for own nginx builds

Open wilful opened this issue 3 years ago • 2 comments

Affected Puppet, Ruby, OS and module versions/distributions

  • Puppet: 6.24
  • Ruby: jruby 9.2.14.0 (2.5.7) 2020-12-08 ebe64bafb9 OpenJDK 64-Bit Server VM 11.0.11+9-LTS on 11.0.11+9-LTS +jit [linux-x86_64]
  • Distribution: CentOS/Ubuntu
  • Module version: 96089f3b54c25625ad929eb62e59df3b616e15dc

How to reproduce (e.g Puppet code you use)

We have different nginx versions and builds. and in one example, the build specifies a non-standard version of nginx, like these: nginx version: team_slug/1.17.10

What are you seeing

# facter -p nginx_version
2022-03-01 16:14:38.476534 ERROR puppetlabs.facter - error while resolving custom fact "nginx_version": undefined method `[]' for nil:NilClass

Any additional information you'd like to impart

I try to override these fact by custom, but it is not working and i don't know why.

What i do

Create new fact like: modules/my_global_module/lib/facter/nginx_version.rb

Facter.add(:nginx_version) do
  has_weight 1001
  confine { Facter.value(:kernel) != 'windows' }
  confine { Facter.value(:operatingsystem) != 'nexus' }
  setcode do
    if Facter::Util::Resolution.which('nginx') || Facter::Util::Resolution.which('openresty')
      nginx_version_command = Facter::Util::Resolution.which('nginx') ? 'nginx -v 2>&1' : 'openresty -v 2>&1'
      nginx_version = Facter::Util::Resolution.exec(nginx_version_command)
      %r{nginx version: (nginx|openresty|team_slug)\/([\w\.]+)}.match(nginx_version)[2]
    end
  end
end

But I still see the version from this module. Thanks

wilful avatar Mar 01 '22 16:03 wilful

I renamed the file like nginx_version_some.rb, and then the desired fact was overrided

wilful avatar Mar 03 '22 17:03 wilful

files named the same will cause issues with pluginsync. Presumably, your fact overrides the one from the module due to alphabetical ordering? It'd probably be ok to submit a PR with a relaxed regex.

eg

%r{nginx version: (\w+)\/([\w\.]+$)}

and an additional test in https://github.com/voxpupuli/puppet-nginx/blob/da56143a291fc73879b3d65eee4c2174600e1877/spec/unit/facter/util/fact_nginx_version_spec.rb

alexjfisher avatar Mar 03 '22 17:03 alexjfisher