Skip to content
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

(PUP-11655) Use run_mode for better platform independence #8938

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 5 additions & 23 deletions lib/puppet/defaults.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,29 +45,11 @@ def self.default_cadir
end

def self.default_basemodulepath
if Puppet::Util::Platform.windows?
path = ['$codedir/modules']
installdir = ENV["FACTER_env_windows_installdir"]
if installdir
path << "#{installdir}/puppet/modules"
end
path.join(File::PATH_SEPARATOR)
else
'$codedir/modules:/opt/puppetlabs/puppet/modules'
end
end

def self.default_vendormoduledir
if Puppet::Util::Platform.windows?
installdir = ENV["FACTER_env_windows_installdir"]
if installdir
"#{installdir}\\puppet\\vendor_modules"
else
nil
end
else
'/opt/puppetlabs/puppet/vendor_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

############################################################################################
Expand Down Expand Up @@ -1393,7 +1375,7 @@ def self.initialize_default_settings!(settings)
<https://puppet.com/docs/puppet/latest/environments_about.html>",
},
:vendormoduledir => {
:default => lambda { default_vendormoduledir },
:default => lambda { Puppet.run_mode.vendor_module_dir },
:type => :string,
:desc => "The directory containing **vendored** modules. These modules will
be used by _all_ environments like those in the `basemodulepath`. The only
Expand Down
19 changes: 4 additions & 15 deletions lib/puppet/provider/package/puppet_gem.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,7 @@

confine :true => Puppet.runtime[:facter].value(:aio_agent_version)

def self.windows_gemcmd
puppet_dir = Puppet::Util.get_env('PUPPET_DIR')
if puppet_dir
File.join(Puppet::Util.get_env('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
Expand All @@ -28,7 +15,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
41 changes: 41 additions & 0 deletions lib/puppet/util/run_mode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,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
Expand Down Expand Up @@ -112,8 +128,33 @@ 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 = Puppet::Util.get_env('PUPPET_DIR'))
File.join(puppet_dir.to_s, 'bin', 'gem.bat')
else
File.join(Gem.default_bindir, 'gem.bat')
end
end

def common_module_dir
# TODO: use File.join?
"#{installdir}/puppet/modules" if installdir
end

def vendor_module_dir
File.join(installdir, 'puppet', 'vendor_modules') if installdir
end

private

def installdir
ENV['FACTER_env_windows_installdir']
end

def windows_common_base(*extra)
[ENV['ALLUSERSPROFILE'], "PuppetLabs"] + extra
end
Expand Down
4 changes: 2 additions & 2 deletions spec/unit/defaults_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,14 +132,14 @@
allow(ENV).to receive(:[]).with("FACTER_env_windows_installdir").and_return(installdir)

expect(
Puppet.default_vendormoduledir
Puppet[:vendormoduledir]
).to eq('C:\Program Files\Puppet Labs\Puppet\puppet\vendor_modules')
end

it 'is nil if installdir fact is missing' do
allow(ENV).to receive(:[]).with("FACTER_env_windows_installdir").and_return(nil)

expect(Puppet.default_vendormoduledir).to be_nil
expect(Puppet[:vendormoduledir]).to be_nil
end
end
end
Expand Down
29 changes: 1 addition & 28 deletions spec/unit/provider/package/puppet_gem_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,38 +27,11 @@

before :each do
resource.provider = provider
allow(described_class).to receive(:command).with(:gemcmd).and_return(provider_gem_cmd)
allow(Puppet.run_mode).to receive(:gem_cmd).and_return(provider_gem_cmd)
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
end


describe '.windows_gemcmd' do
context 'when PUPPET_DIR is not set' do
before do
allow(Puppet::Util).to receive(:get_env).and_call_original
allow(Puppet::Util).to receive(:get_env).with('PUPPET_DIR').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(Puppet::Util).to receive(:get_env).and_call_original
allow(Puppet::Util).to receive(:get_env).with('PUPPET_DIR').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')
Expand Down
91 changes: 91 additions & 0 deletions spec/unit/util/run_mode_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
ekohl marked this conversation as resolved.
Show resolved Hide resolved

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
Expand Down Expand Up @@ -172,6 +188,81 @@
end
end

describe '#gem_cmd' do
context 'when PUPPET_DIR is not set' do
before do
allow(Puppet::Util).to receive(:get_env).and_call_original
allow(Puppet::Util).to receive(:get_env).with('PUPPET_DIR').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(@run_mode.gem_cmd).to eql(expected_path)
end
end

context 'when PUPPET_DIR is set' do
before do
allow(Puppet::Util).to receive(:get_env).and_call_original
allow(Puppet::Util).to receive(:get_env).with('PUPPET_DIR').and_return('puppet_dir')
end

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
context 'when installdir is not set' do
before do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with('FACTER_env_windows_installdir').and_return(nil)
end

it 'returns nil' do
expect(@run_mode.common_module_dir).to be(nil)
end
end

context 'with installdir' do
before do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with('FACTER_env_windows_installdir').and_return('C:\Program Files\Puppet Labs\Puppet')
end

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
context 'when installdir is not set' do
before do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with('FACTER_env_windows_installdir').and_return(nil)
end

it 'returns nil' do
expect(@run_mode.vendor_module_dir).to be(nil)
end
end

context 'with installdir' do
before do
allow(ENV).to receive(:[]).and_call_original
allow(ENV).to receive(:[]).with('FACTER_env_windows_installdir').and_return('C:\Program Files\Puppet Labs\Puppet')
end

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

describe "#without_env internal helper with UTF8 characters" do
let(:varname) { "\u16A0\u16C7\u16BB\u16EB\u16D2\u16E6\u16A6\u16EB\u16A0\u16B1\u16A9\u16A0\u16A2\u16B1\u16EB\u16A0\u16C1\u16B1\u16AA\u16EB\u16B7\u16D6\u16BB\u16B9\u16E6\u16DA\u16B3\u16A2\u16D7" }
let(:rune_utf8) { "\u16A0\u16C7\u16BB\u16EB\u16D2\u16E6\u16A6\u16EB\u16A0\u16B1\u16A9\u16A0\u16A2\u16B1\u16EB\u16A0\u16C1\u16B1\u16AA\u16EB\u16B7\u16D6\u16BB\u16B9\u16E6\u16DA\u16B3\u16A2\u16D7" }
Expand Down
Loading