diff --git a/lib/facter/pip_version.rb b/lib/facter/pip_version.rb index 9011da4b..8f2e328d 100644 --- a/lib/facter/pip_version.rb +++ b/lib/facter/pip_version.rb @@ -1,27 +1,24 @@ # Make pip version available as a fact # Works with pip loaded and without, pip installed using pip and package installed -require 'puppet' -require 'rubygems' facter_puppet_version = Facter.value(:puppetversion) facter_is_pe = Facter.value(:is_pe) if facter_is_pe - facter_puppet_version = facter_puppet_version.to_s.split(' ')[0] + facter_puppet_version = facter_puppet_version.to_s.split(' ')[0] end -if Gem::Version.new(facter_puppet_version) >= Gem::Version.new('3.6') +if (Puppet::Util::Package.versioncmp(facter_puppet_version, '3.6') >= 0) pkg = Puppet::Type.type(:package).new(:name => 'python-pip', :allow_virtual => 'false') else pkg = Puppet::Type.type(:package).new(:name => 'python-pip') end + Facter.add("pip_version") do has_weight 100 setcode do - begin - /^pip (\d+\.\d+\.?\d*).*$/.match(Facter::Util::Resolution.exec('pip --version 2>/dev/null'))[1] - rescue - false + if Facter::Util::Resolution.which('pip') + Facter::Util::Resolution.exec('pip --version 2>/dev/null').match(/^pip (\d+\.\d+\.?\d*).*$/)[1] end end end @@ -29,12 +26,8 @@ Facter.add("pip_version") do has_weight 50 setcode do - begin - unless [:absent,'purged'].include?(pkg.retrieve[pkg.property(:ensure)]) - /^.*(\d+\.\d+\.\d+).*$/.match(pkg.retrieve[pkg.property(:ensure)])[1] - end - rescue - false + unless [:absent,:purged].include?(pkg.retrieve[pkg.property(:ensure)]) + pkg.retrieve[pkg.property(:ensure)] end end end diff --git a/lib/facter/python_version.rb b/lib/facter/python_version.rb index 08eb9e06..febcd81e 100644 --- a/lib/facter/python_version.rb +++ b/lib/facter/python_version.rb @@ -1,28 +1,23 @@ # Make python versions available as facts # In lists default python and system python versions -require 'puppet' -require 'rubygems' facter_puppet_version = Facter.value(:puppetversion) facter_is_pe = Facter.value(:is_pe) if facter_is_pe - facter_puppet_version = facter_puppet_version.to_s.split(' ')[0] + facter_puppet_version = facter_puppet_version.to_s.split(' ')[0] end -if Gem::Version.new(facter_puppet_version) >= Gem::Version.new('3.6') - pkg = Puppet::Type.type(:package).new(:name => 'python', :allow_virtual => 'false') +if (Puppet::Util::Package.versioncmp(facter_puppet_version, '3.6') >= 0) + pkg = Puppet::Type.type(:package).new(:name => 'python-pip', :allow_virtual => 'false') else - pkg = Puppet::Type.type(:package).new(:name => 'python') + pkg = Puppet::Type.type(:package).new(:name => 'python-pip') end + Facter.add("system_python_version") do setcode do - begin - unless [:absent,'purged'].include?(pkg.retrieve[pkg.property(:ensure)]) - /^(\d+\.\d+\.\d+).*$/.match(pkg.retrieve[pkg.property(:ensure)])[1] - end - rescue - false + unless [:absent,:purged].include?(pkg.retrieve[pkg.property(:ensure)]) + pkg.retrieve[pkg.property(:ensure)] end end end @@ -30,10 +25,8 @@ Facter.add("python_version") do has_weight 100 setcode do - begin - /^.*(\d+\.\d+\.\d+)$/.match(Facter::Util::Resolution.exec('python -V 2>&1'))[1] - rescue - false + if Facter::Util::Resolution.which('python') + Facter::Util::Resolution.exec('python -V 2>&1').match(/^.*(\d+\.\d+\.\d+)$/)[1] end end end @@ -41,12 +34,8 @@ Facter.add("python_version") do has_weight 50 setcode do - begin - unless [:absent,'purged'].include?(pkg.retrieve[pkg.property(:ensure)]) - /^.*(\d+\.\d+\.\d+).*$/.match(pkg.retrieve[pkg.property(:ensure)])[1] - end - rescue - false + unless [:absent,:purged].include?(pkg.retrieve[pkg.property(:ensure)]) + pkg.retrieve[pkg.property(:ensure)] end end end diff --git a/lib/facter/virtualenv_version.rb b/lib/facter/virtualenv_version.rb index 127a002a..0c505a0e 100644 --- a/lib/facter/virtualenv_version.rb +++ b/lib/facter/virtualenv_version.rb @@ -1,27 +1,24 @@ # Make virtualenv version available as a fact # Works with virualenv loaded and without, pip installed and package installed -require 'puppet' -require 'rubygems' facter_puppet_version = Facter.value(:puppetversion) facter_is_pe = Facter.value(:is_pe) if facter_is_pe - facter_puppet_version = facter_puppet_version.to_s.split(' ')[0] + facter_puppet_version = facter_puppet_version.to_s.split(' ')[0] end -if Gem::Version.new(facter_puppet_version) >= Gem::Version.new('3.6') - pkg = Puppet::Type.type(:package).new(:name => 'virtualenv', :allow_virtual => 'false') +if (Puppet::Util::Package.versioncmp(facter_puppet_version, '3.6') >= 0) + pkg = Puppet::Type.type(:package).new(:name => 'python-pip', :allow_virtual => 'false') else - pkg = Puppet::Type.type(:package).new(:name => 'virtualenv') + pkg = Puppet::Type.type(:package).new(:name => 'python-pip') end + Facter.add("virtualenv_version") do has_weight 100 setcode do - begin - Facter::Util::Resolution.exec('virtualenv --version') || "absent" - rescue - false + if Facter::Util::Resolution.which('virtualenv') + Facter::Util::Resolution.exec('virtualenv --version 2>&1') end end end @@ -29,12 +26,8 @@ Facter.add("virtualenv_version") do has_weight 50 setcode do - begin - unless [:absent,'purged'].include?(pkg.retrieve[pkg.property(:ensure)]) - /^.*(\d+\.\d+\.\d+).*$/.match(pkg.retrieve[pkg.property(:ensure)])[1] - end - rescue - false + unless [:absent,:purged].include?(pkg.retrieve[pkg.property(:ensure)]) + pkg.retrieve[pkg.property(:ensure)] end end end diff --git a/spec/acceptance/facts_test_spec.rb b/spec/acceptance/facts_test_spec.rb new file mode 100644 index 00000000..bdfd6009 --- /dev/null +++ b/spec/acceptance/facts_test_spec.rb @@ -0,0 +1,43 @@ +require 'spec_helper_acceptance' + +describe 'python class' do + + context 'facts' do + install_python = <<-EOS + class { 'python' : + version => 'system', + pip => true, + virtualenv => true, + } + EOS + + fact_notices = <<-EOS + notify{"pip_version: ${::pip_version}":} + notify{"system_python_version: ${::system_python_version}":} + notify{"python_version: ${::python_version}":} + notify{"virtualenv_version: ${::virtualenv_version}":} + EOS + + it 'should output python facts when not installed' do + apply_manifest(fact_notices, :catch_failures => true) do |r| + expect(r.stdout).to match(/python_version: \S+/) + expect(r.stdout).to match(/pip_version: \S+/) + expect(r.stdout).to match(/virtualenv_version: \S+/) + expect(r.stdout).to match(/system_python_version: \S+/) + end + end + + it 'sets up python' do + apply_manifest(install_python, :catch_failures => true) + end + + it 'should output python facts when installed' do + apply_manifest(fact_notices, :catch_failures => true) do |r| + expect(r.stdout).to match(/python_version: \S+/) + expect(r.stdout).to match(/pip_version: \S+/) + expect(r.stdout).to match(/virtualenv_version: \S+/) + expect(r.stdout).to match(/system_python_version: \S+/) + end + end + end +end