Skip to content

Commit 842a949

Browse files
ekohljoshcooper
andcommitted
(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 <[email protected]>
1 parent a423af8 commit 842a949

File tree

6 files changed

+144
-68
lines changed

6 files changed

+144
-68
lines changed

lib/puppet/defaults.rb

+5-23
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,11 @@ def self.default_cadir
4747
end
4848

4949
def self.default_basemodulepath
50-
if Puppet::Util::Platform.windows?
51-
path = ['$codedir/modules']
52-
installdir = ENV.fetch("FACTER_env_windows_installdir", nil)
53-
if installdir
54-
path << "#{installdir}/puppet/modules"
55-
end
56-
path.join(File::PATH_SEPARATOR)
57-
else
58-
'$codedir/modules:/opt/puppetlabs/puppet/modules'
59-
end
60-
end
61-
62-
def self.default_vendormoduledir
63-
if Puppet::Util::Platform.windows?
64-
installdir = ENV.fetch("FACTER_env_windows_installdir", nil)
65-
if installdir
66-
"#{installdir}\\puppet\\vendor_modules"
67-
else
68-
nil
69-
end
70-
else
71-
'/opt/puppetlabs/puppet/vendor_modules'
50+
path = ['$codedir/modules']
51+
if (run_mode_dir = Puppet.run_mode.common_module_dir)
52+
path << run_mode_dir
7253
end
54+
path.join(File::PATH_SEPARATOR)
7355
end
7456

7557
############################################################################################
@@ -1437,7 +1419,7 @@ def self.initialize_default_settings!(settings)
14371419
<https://puppet.com/docs/puppet/latest/environments_about.html>",
14381420
},
14391421
:vendormoduledir => {
1440-
:default => -> { default_vendormoduledir },
1422+
:default => -> { Puppet.run_mode.vendor_module_dir },
14411423
:type => :string,
14421424
:desc => "The directory containing **vendored** modules. These modules will
14431425
be used by _all_ environments like those in the `basemodulepath`. The only

lib/puppet/provider/package/puppet_gem.rb

+4-15
Original file line numberDiff line numberDiff line change
@@ -8,20 +8,7 @@
88

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

11-
def self.windows_gemcmd
12-
puppet_dir = ENV.fetch('PUPPET_DIR', nil)
13-
if puppet_dir
14-
File.join(puppet_dir.to_s, 'bin', 'gem.bat')
15-
else
16-
File.join(Gem.default_bindir, 'gem.bat')
17-
end
18-
end
19-
20-
if Puppet::Util::Platform.windows?
21-
commands :gemcmd => windows_gemcmd
22-
else
23-
commands :gemcmd => "/opt/puppetlabs/puppet/bin/gem"
24-
end
11+
commands :gemcmd => Puppet.run_mode.gem_cmd
2512

2613
def uninstall
2714
super
@@ -30,7 +17,9 @@ def uninstall
3017
end
3118

3219
def self.execute_gem_command(command, command_options, custom_environment = {})
33-
custom_environment['PKG_CONFIG_PATH'] = '/opt/puppetlabs/puppet/lib/pkgconfig' unless Puppet::Util::Platform.windows?
20+
if (pkg_config_path = Puppet.run_mode.pkg_config_path)
21+
custom_environment['PKG_CONFIG_PATH'] = pkg_config_path
22+
end
3423
super(command, command_options, custom_environment)
3524
end
3625
end

lib/puppet/util/run_mode.rb

+40
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,22 @@ def run_dir
8787
def log_dir
8888
which_dir("/var/log/puppetlabs/puppet", "~/.puppetlabs/var/log")
8989
end
90+
91+
def pkg_config_path
92+
'/opt/puppetlabs/puppet/lib/pkgconfig'
93+
end
94+
95+
def gem_cmd
96+
'/opt/puppetlabs/puppet/bin/gem'
97+
end
98+
99+
def common_module_dir
100+
'/opt/puppetlabs/puppet/modules'
101+
end
102+
103+
def vendor_module_dir
104+
'/opt/puppetlabs/puppet/vendor_modules'
105+
end
90106
end
91107

92108
class WindowsRunMode < RunMode
@@ -114,8 +130,32 @@ def log_dir
114130
which_dir(File.join(windows_common_base("puppet/var/log")), "~/.puppetlabs/var/log")
115131
end
116132

133+
def pkg_config_path
134+
nil
135+
end
136+
137+
def gem_cmd
138+
if (puppet_dir = Puppet::Util.get_env('PUPPET_DIR'))
139+
File.join(puppet_dir.to_s, 'bin', 'gem.bat')
140+
else
141+
File.join(Gem.default_bindir, 'gem.bat')
142+
end
143+
end
144+
145+
def common_module_dir
146+
"#{installdir}/puppet/modules" if installdir
147+
end
148+
149+
def vendor_module_dir
150+
File.join(installdir, 'puppet', 'vendor_modules') if installdir
151+
end
152+
117153
private
118154

155+
def installdir
156+
ENV.fetch('FACTER_env_windows_installdir', nil)
157+
end
158+
119159
def windows_common_base(*extra)
120160
[ENV.fetch('ALLUSERSPROFILE', nil), "PuppetLabs"] + extra
121161
end

spec/unit/defaults_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -132,14 +132,14 @@
132132
allow(ENV).to receive(:fetch).with("FACTER_env_windows_installdir", anything).and_return(installdir)
133133

134134
expect(
135-
Puppet.default_vendormoduledir
135+
Puppet[:vendormoduledir]
136136
).to eq('C:\Program Files\Puppet Labs\Puppet\puppet\vendor_modules')
137137
end
138138

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

142-
expect(Puppet.default_vendormoduledir).to be_nil
142+
expect(Puppet[:vendormoduledir]).to be_nil
143143
end
144144
end
145145
end

spec/unit/provider/package/puppet_gem_spec.rb

+2-28
Original file line numberDiff line numberDiff line change
@@ -27,37 +27,11 @@
2727

2828
before :each do
2929
resource.provider = provider
30-
allow(described_class).to receive(:command).with(:gemcmd).and_return(provider_gem_cmd)
30+
allow(provider.class).to receive(:which).with(provider_gem_cmd).and_return(provider_gem_cmd)
31+
allow(Puppet.run_mode).to receive(:gem_cmd).and_return(provider_gem_cmd)
3132
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
3233
end
3334

34-
35-
describe '.windows_gemcmd' do
36-
context 'when PUPPET_DIR is not set' do
37-
before do
38-
# allow(ENV).to receive(:fetch, anything).and_call_original
39-
allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return(nil)
40-
allow(Gem).to receive(:default_bindir).and_return('default_gem_bin')
41-
end
42-
43-
it 'uses Gem.default_bindir' do
44-
expected_path = File.join('default_gem_bin', 'gem.bat')
45-
expect(described_class.windows_gemcmd).to eql(expected_path)
46-
end
47-
end
48-
49-
context 'when PUPPET_DIR is set' do
50-
before do
51-
allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return('puppet_dir')
52-
end
53-
54-
it 'uses Gem.default_bindir' do
55-
expected_path = File.join('puppet_dir', 'bin', 'gem.bat')
56-
expect(described_class.windows_gemcmd).to eql(expected_path)
57-
end
58-
end
59-
end
60-
6135
context "when installing" do
6236
before :each do
6337
allow(provider).to receive(:rubygem_version).and_return('1.9.9')

spec/unit/util/run_mode_spec.rb

+91
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,22 @@
9797
end
9898
end
9999
end
100+
101+
describe "#pkg_config_path" do
102+
it { expect(@run_mode.pkg_config_path).to eq('/opt/puppetlabs/puppet/lib/pkgconfig') }
103+
end
104+
105+
describe "#gem_cmd" do
106+
it { expect(@run_mode.gem_cmd).to eq('/opt/puppetlabs/puppet/bin/gem') }
107+
end
108+
109+
describe "#common_module_dir" do
110+
it { expect(@run_mode.common_module_dir).to eq('/opt/puppetlabs/puppet/modules') }
111+
end
112+
113+
describe "#vendor_module_dir" do
114+
it { expect(@run_mode.vendor_module_dir).to eq('/opt/puppetlabs/puppet/vendor_modules') }
115+
end
100116
end
101117

102118
describe Puppet::Util::WindowsRunMode, :if => Puppet::Util::Platform.windows? do
@@ -171,6 +187,81 @@
171187
end
172188
end
173189
end
190+
191+
describe '#gem_cmd' do
192+
context 'when PUPPET_DIR is not set' do
193+
before do
194+
allow(Puppet::Util).to receive(:get_env).and_call_original
195+
allow(Puppet::Util).to receive(:get_env).with('PUPPET_DIR').and_return(nil)
196+
allow(Gem).to receive(:default_bindir).and_return('default_gem_bin')
197+
end
198+
199+
it 'uses Gem.default_bindir' do
200+
expected_path = File.join('default_gem_bin', 'gem.bat')
201+
expect(@run_mode.gem_cmd).to eql(expected_path)
202+
end
203+
end
204+
205+
context 'when PUPPET_DIR is set' do
206+
before do
207+
allow(Puppet::Util).to receive(:get_env).and_call_original
208+
allow(Puppet::Util).to receive(:get_env).with('PUPPET_DIR').and_return('puppet_dir')
209+
end
210+
211+
it 'uses Gem.default_bindir' do
212+
expected_path = File.join('puppet_dir', 'bin', 'gem.bat')
213+
expect(@run_mode.gem_cmd).to eql(expected_path)
214+
end
215+
end
216+
end
217+
218+
describe '#common_module_dir' do
219+
context 'when installdir is not set' do
220+
before do
221+
allow(ENV).to receive(:[]).and_call_original
222+
allow(ENV).to receive(:[]).with('FACTER_env_windows_installdir').and_return(nil)
223+
end
224+
225+
it 'returns nil' do
226+
expect(@run_mode.common_module_dir).to be(nil)
227+
end
228+
end
229+
230+
context 'with installdir' do
231+
before do
232+
allow(ENV).to receive(:[]).and_call_original
233+
allow(ENV).to receive(:[]).with('FACTER_env_windows_installdir').and_return('C:\Program Files\Puppet Labs\Puppet')
234+
end
235+
236+
it 'returns INSTALLDIR/puppet/modules' do
237+
expect(@run_mode.common_module_dir).to eq('C:\Program Files\Puppet Labs\Puppet/puppet/modules')
238+
end
239+
end
240+
end
241+
242+
describe '#vendor_module_dir' do
243+
context 'when installdir is not set' do
244+
before do
245+
allow(ENV).to receive(:[]).and_call_original
246+
allow(ENV).to receive(:[]).with('FACTER_env_windows_installdir').and_return(nil)
247+
end
248+
249+
it 'returns nil' do
250+
expect(@run_mode.vendor_module_dir).to be(nil)
251+
end
252+
end
253+
254+
context 'with installdir' do
255+
before do
256+
allow(ENV).to receive(:[]).and_call_original
257+
allow(ENV).to receive(:[]).with('FACTER_env_windows_installdir').and_return('C:\Program Files\Puppet Labs\Puppet')
258+
end
259+
260+
it 'returns INSTALLDIR\puppet\vendor_modules' do
261+
expect(@run_mode.vendor_module_dir).to eq('C:\Program Files\Puppet Labs\Puppet\puppet\vendor_modules')
262+
end
263+
end
264+
end
174265
end
175266

176267
def as_root

0 commit comments

Comments
 (0)