Skip to content

Commit 9133064

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 e4e2b3d commit 9133064

File tree

5 files changed

+140
-61
lines changed

5 files changed

+140
-61
lines changed

lib/puppet/defaults.rb

+5-19
Original file line numberDiff line numberDiff line change
@@ -47,29 +47,15 @@ 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'
50+
path = ['$codedir/modules']
51+
if (run_mode_dir = Puppet.run_mode.common_module_dir)
52+
path << run_mode_dir
5953
end
54+
path.join(File::PATH_SEPARATOR)
6055
end
6156

6257
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'
72-
end
58+
Puppet.run_mode.vendor_module_dir
7359
end
7460

7561
############################################################################################

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 = ENV.fetch('PUPPET_DIR', nil))
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+
"#{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/provider/package/puppet_gem_spec.rb

-27
Original file line numberDiff line numberDiff line change
@@ -42,32 +42,6 @@
4242
allow(File).to receive(:file?).with(provider_gem_cmd).and_return(true)
4343
end
4444

45-
describe '.windows_gemcmd' do
46-
context 'when PUPPET_DIR is not set' do
47-
before do
48-
# allow(ENV).to receive(:fetch, anything).and_call_original
49-
allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return(nil)
50-
allow(Gem).to receive(:default_bindir).and_return('default_gem_bin')
51-
end
52-
53-
it 'uses Gem.default_bindir' do
54-
expected_path = File.join('default_gem_bin', 'gem.bat')
55-
expect(described_class.windows_gemcmd).to eql(expected_path)
56-
end
57-
end
58-
59-
context 'when PUPPET_DIR is set' do
60-
before do
61-
allow(ENV).to receive(:fetch).with('PUPPET_DIR', anything).and_return('puppet_dir')
62-
end
63-
64-
it 'uses Gem.default_bindir' do
65-
expected_path = File.join('puppet_dir', 'bin', 'gem.bat')
66-
expect(described_class.windows_gemcmd).to eql(expected_path)
67-
end
68-
end
69-
end
70-
7145
context "when installing" do
7246
before :each do
7347
allow(provider).to receive(:rubygem_version).and_return('1.9.9')
@@ -133,5 +107,4 @@
133107
it { is_expected.to be > 100 }
134108
end
135109
end
136-
137110
end

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+
before do
193+
allow(ENV).to receive(:fetch).and_call_original
194+
allow(ENV).to receive(:fetch).with('PUPPET_DIR').and_return(puppetdir)
195+
end
196+
197+
context 'when PUPPET_DIR is not set' do
198+
let(:puppetdir) { nil }
199+
200+
before do
201+
allow(Gem).to receive(:default_bindir).and_return('default_gem_bin')
202+
end
203+
204+
it 'uses Gem.default_bindir' do
205+
expected_path = File.join('default_gem_bin', 'gem.bat')
206+
expect(@run_mode.gem_cmd).to eql(expected_path)
207+
end
208+
end
209+
210+
context 'when PUPPET_DIR is set' do
211+
let(:puppetdir) { 'puppet_dir' }
212+
213+
it 'uses Gem.default_bindir' do
214+
expected_path = File.join('puppet_dir', 'bin', 'gem.bat')
215+
expect(@run_mode.gem_cmd).to eql(expected_path)
216+
end
217+
end
218+
end
219+
220+
describe '#common_module_dir' do
221+
before do
222+
allow(ENV).to receive(:fetch).and_call_original
223+
allow(ENV).to receive(:fetch).with('FACTER_env_windows_installdir', nil).and_return(installdir)
224+
end
225+
226+
context 'when installdir is not set' do
227+
let(:installdir) { nil }
228+
229+
it 'returns nil' do
230+
expect(@run_mode.common_module_dir).to be(nil)
231+
end
232+
end
233+
234+
context 'with installdir' do
235+
let(:installdir) { 'C:\Program Files\Puppet Labs\Puppet' }
236+
237+
it 'returns INSTALLDIR/puppet/modules' do
238+
expect(@run_mode.common_module_dir).to eq('C:\Program Files\Puppet Labs\Puppet/puppet/modules')
239+
end
240+
end
241+
end
242+
243+
describe '#vendor_module_dir' do
244+
before do
245+
allow(ENV).to receive(:fetch).and_call_original
246+
allow(ENV).to receive(:fetch).with('FACTER_env_windows_installdir', nil).and_return(installdir)
247+
end
248+
249+
context 'when installdir is not set' do
250+
let(:installdir) { nil }
251+
252+
it 'returns nil' do
253+
expect(@run_mode.vendor_module_dir).to be(nil)
254+
end
255+
end
256+
257+
context 'with installdir' do
258+
let(:installdir) { 'C:\Program Files\Puppet Labs\Puppet' }
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)