Skip to content

Commit

Permalink
Merge pull request #176 from petems/fix_facts_to_not_use_rubygems
Browse files Browse the repository at this point in the history
Fixes facts not to use Rubygems
  • Loading branch information
stankevich committed Mar 18, 2015
2 parents f5f86d3 + 5fe03a0 commit c6ca80f
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 52 deletions.
21 changes: 7 additions & 14 deletions lib/facter/pip_version.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
# 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

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
33 changes: 11 additions & 22 deletions lib/facter/python_version.rb
Original file line number Diff line number Diff line change
@@ -1,52 +1,41 @@
# 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

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

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
25 changes: 9 additions & 16 deletions lib/facter/virtualenv_version.rb
Original file line number Diff line number Diff line change
@@ -1,40 +1,33 @@
# 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

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
43 changes: 43 additions & 0 deletions spec/acceptance/facts_test_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit c6ca80f

Please sign in to comment.