From d3d73f1642dc36c3a35a4b5a8c4694e803861556 Mon Sep 17 00:00:00 2001 From: Ewoud Kohl van Wijngaarden Date: Tue, 15 Jun 2021 13:04:08 +0200 Subject: [PATCH] (PUP-11655) Use run_mode for better platform independence The run mode already determines the platform. Moving all platform specific paths into RunMode makes it easier to get a complete overview and change things where needed. Co-authored-by: Josh Cooper --- lib/puppet/defaults.rb | 24 +---- lib/puppet/provider/package/puppet_gem.rb | 19 +--- lib/puppet/util/run_mode.rb | 40 ++++++++ spec/unit/provider/package/puppet_gem_spec.rb | 27 ------ spec/unit/util/run_mode_spec.rb | 91 +++++++++++++++++++ 5 files changed, 140 insertions(+), 61 deletions(-) diff --git a/lib/puppet/defaults.rb b/lib/puppet/defaults.rb index 97fed251066..9d1757863b7 100644 --- a/lib/puppet/defaults.rb +++ b/lib/puppet/defaults.rb @@ -47,29 +47,15 @@ def self.default_cadir end def self.default_basemodulepath - if Puppet::Util::Platform.windows? - path = ['$codedir/modules'] - installdir = ENV.fetch("FACTER_env_windows_installdir", nil) - if installdir - path << "#{installdir}/puppet/modules" - end - path.join(File::PATH_SEPARATOR) - else - '$codedir/modules:/opt/puppetlabs/puppet/modules' + path = ['$codedir/modules'] + if (run_mode_dir = Puppet.run_mode.common_module_dir) + path << run_mode_dir end + path.join(File::PATH_SEPARATOR) end def self.default_vendormoduledir - if Puppet::Util::Platform.windows? - installdir = ENV.fetch("FACTER_env_windows_installdir", nil) - if installdir - "#{installdir}\\puppet\\vendor_modules" - else - nil - end - else - '/opt/puppetlabs/puppet/vendor_modules' - end + Puppet.run_mode.vendor_module_dir end ############################################################################################ diff --git a/lib/puppet/provider/package/puppet_gem.rb b/lib/puppet/provider/package/puppet_gem.rb index 3adde08a2d9..3578a05c197 100644 --- a/lib/puppet/provider/package/puppet_gem.rb +++ b/lib/puppet/provider/package/puppet_gem.rb @@ -8,20 +8,7 @@ confine :true => Puppet.runtime[:facter].value(:aio_agent_version) - def self.windows_gemcmd - puppet_dir = ENV.fetch('PUPPET_DIR', nil) - if puppet_dir - File.join(puppet_dir.to_s, 'bin', 'gem.bat') - else - File.join(Gem.default_bindir, 'gem.bat') - end - end - - if Puppet::Util::Platform.windows? - commands :gemcmd => windows_gemcmd - else - commands :gemcmd => "/opt/puppetlabs/puppet/bin/gem" - end + commands :gemcmd => Puppet.run_mode.gem_cmd def uninstall super @@ -30,7 +17,9 @@ def uninstall end def self.execute_gem_command(command, command_options, custom_environment = {}) - custom_environment['PKG_CONFIG_PATH'] = '/opt/puppetlabs/puppet/lib/pkgconfig' unless Puppet::Util::Platform.windows? + if (pkg_config_path = Puppet.run_mode.pkg_config_path) + custom_environment['PKG_CONFIG_PATH'] = pkg_config_path + end super(command, command_options, custom_environment) end end diff --git a/lib/puppet/util/run_mode.rb b/lib/puppet/util/run_mode.rb index 06af3eea006..a64198150f4 100644 --- a/lib/puppet/util/run_mode.rb +++ b/lib/puppet/util/run_mode.rb @@ -87,6 +87,22 @@ def run_dir def log_dir which_dir("/var/log/puppetlabs/puppet", "~/.puppetlabs/var/log") end + + def pkg_config_path + '/opt/puppetlabs/puppet/lib/pkgconfig' + end + + def gem_cmd + '/opt/puppetlabs/puppet/bin/gem' + end + + def common_module_dir + '/opt/puppetlabs/puppet/modules' + end + + def vendor_module_dir + '/opt/puppetlabs/puppet/vendor_modules' + end end class WindowsRunMode < RunMode @@ -114,8 +130,32 @@ def log_dir which_dir(File.join(windows_common_base("puppet/var/log")), "~/.puppetlabs/var/log") end + def pkg_config_path + nil + end + + def gem_cmd + if (puppet_dir = ENV.fetch('PUPPET_DIR', nil)) + File.join(puppet_dir.to_s, 'bin', 'gem.bat') + else + File.join(Gem.default_bindir, 'gem.bat') + end + end + + def common_module_dir + "#{installdir}/puppet/modules" if installdir + end + + def vendor_module_dir + "#{installdir}\\puppet\\vendor_modules" if installdir + end + private + def installdir + ENV.fetch('FACTER_env_windows_installdir', nil) + end + def windows_common_base(*extra) [ENV.fetch('ALLUSERSPROFILE', nil), "PuppetLabs"] + extra end diff --git a/spec/unit/provider/package/puppet_gem_spec.rb b/spec/unit/provider/package/puppet_gem_spec.rb index 52105da4565..d2098ee3061 100644 --- a/spec/unit/provider/package/puppet_gem_spec.rb +++ b/spec/unit/provider/package/puppet_gem_spec.rb @@ -42,32 +42,6 @@ allow(File).to receive(:file?).with(provider_gem_cmd).and_return(true) end - describe '.windows_gemcmd' do - context 'when PUPPET_DIR is not set' do - before do - # allow(ENV).to receive(:fetch, anything).and_call_original - allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return(nil) - allow(Gem).to receive(:default_bindir).and_return('default_gem_bin') - end - - it 'uses Gem.default_bindir' do - expected_path = File.join('default_gem_bin', 'gem.bat') - expect(described_class.windows_gemcmd).to eql(expected_path) - end - end - - context 'when PUPPET_DIR is set' do - before do - allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return('puppet_dir') - end - - it 'uses Gem.default_bindir' do - expected_path = File.join('puppet_dir', 'bin', 'gem.bat') - expect(described_class.windows_gemcmd).to eql(expected_path) - end - end - end - context "when installing" do before :each do allow(provider).to receive(:rubygem_version).and_return('1.9.9') @@ -133,5 +107,4 @@ it { is_expected.to be > 100 } end end - end diff --git a/spec/unit/util/run_mode_spec.rb b/spec/unit/util/run_mode_spec.rb index 46a0a55008e..cd7d3f72d6a 100644 --- a/spec/unit/util/run_mode_spec.rb +++ b/spec/unit/util/run_mode_spec.rb @@ -97,6 +97,22 @@ end end end + + describe "#pkg_config_path" do + it { expect(@run_mode.pkg_config_path).to eq('/opt/puppetlabs/puppet/lib/pkgconfig') } + end + + describe "#gem_cmd" do + it { expect(@run_mode.gem_cmd).to eq('/opt/puppetlabs/puppet/bin/gem') } + end + + describe "#common_module_dir" do + it { expect(@run_mode.common_module_dir).to eq('/opt/puppetlabs/puppet/modules') } + end + + describe "#vendor_module_dir" do + it { expect(@run_mode.vendor_module_dir).to eq('/opt/puppetlabs/puppet/vendor_modules') } + end end describe Puppet::Util::WindowsRunMode, :if => Puppet::Util::Platform.windows? do @@ -171,6 +187,81 @@ end end end + + describe '#gem_cmd' do + before do + allow(ENV).to receive(:fetch).and_call_original + allow(ENV).to receive(:fetch).with('PUPPET_DIR', nil).and_return(puppetdir) + end + + context 'when PUPPET_DIR is not set' do + let(:puppetdir) { nil } + + before do + allow(Gem).to receive(:default_bindir).and_return('default_gem_bin') + end + + it 'uses Gem.default_bindir' do + expected_path = File.join('default_gem_bin', 'gem.bat') + expect(@run_mode.gem_cmd).to eql(expected_path) + end + end + + context 'when PUPPET_DIR is set' do + let(:puppetdir) { 'puppet_dir' } + + it 'uses Gem.default_bindir' do + expected_path = File.join('puppet_dir', 'bin', 'gem.bat') + expect(@run_mode.gem_cmd).to eql(expected_path) + end + end + end + + describe '#common_module_dir' do + before do + allow(ENV).to receive(:fetch).and_call_original + allow(ENV).to receive(:fetch).with('FACTER_env_windows_installdir', nil).and_return(installdir) + end + + context 'when installdir is not set' do + let(:installdir) { nil } + + it 'returns nil' do + expect(@run_mode.common_module_dir).to be(nil) + end + end + + context 'with installdir' do + let(:installdir) { 'C:\Program Files\Puppet Labs\Puppet' } + + it 'returns INSTALLDIR/puppet/modules' do + expect(@run_mode.common_module_dir).to eq('C:\Program Files\Puppet Labs\Puppet/puppet/modules') + end + end + end + + describe '#vendor_module_dir' do + before do + allow(ENV).to receive(:fetch).and_call_original + allow(ENV).to receive(:fetch).with('FACTER_env_windows_installdir', nil).and_return(installdir) + end + + context 'when installdir is not set' do + let(:installdir) { nil } + + it 'returns nil' do + expect(@run_mode.vendor_module_dir).to be(nil) + end + end + + context 'with installdir' do + let(:installdir) { 'C:\Program Files\Puppet Labs\Puppet' } + + it 'returns INSTALLDIR\puppet\vendor_modules' do + expect(@run_mode.vendor_module_dir).to eq('C:\Program Files\Puppet Labs\Puppet\puppet\vendor_modules') + end + end + end end def as_root