Skip to content

Commit ed30562

Browse files
committed
(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.
1 parent 2377fad commit ed30562

File tree

6 files changed

+144
-68
lines changed

6 files changed

+144
-68
lines changed

Diff for: lib/puppet/defaults.rb

+5-23
Original file line numberDiff line numberDiff line change
@@ -49,29 +49,11 @@ def self.default_cadir
4949
end
5050

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

7759
############################################################################################
@@ -1384,7 +1366,7 @@ def self.initialize_default_settings!(settings)
13841366
<https://puppet.com/docs/puppet/latest/environments_about.html>",
13851367
},
13861368
:vendormoduledir => {
1387-
:default => lambda { default_vendormoduledir },
1369+
:default => lambda { Puppet.run_mode.vendor_module_dir },
13881370
:type => :string,
13891371
:desc => "The directory containing **vendored** modules. These modules will
13901372
be used by _all_ environments like those in the `basemodulepath`. The only

Diff for: lib/puppet/provider/package/puppet_gem.rb

+4-15
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,7 @@
66

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

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

2411
def uninstall
2512
super
@@ -28,7 +15,9 @@ def uninstall
2815
end
2916

3017
def self.execute_gem_command(command, command_options, custom_environment = {})
31-
custom_environment['PKG_CONFIG_PATH'] = '/opt/puppetlabs/puppet/lib/pkgconfig' unless Puppet::Util::Platform.windows?
18+
if (pkg_config_path = Puppet.run_mode.pkg_config_path)
19+
custom_environment['PKG_CONFIG_PATH'] = pkg_config_path
20+
end
3221
super(command, command_options, custom_environment)
3322
end
3423
end

Diff for: lib/puppet/util/run_mode.rb

+41
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,22 @@ def run_dir
8585
def log_dir
8686
which_dir("/var/log/puppetlabs/puppet", "~/.puppetlabs/var/log")
8787
end
88+
89+
def pkg_config_path
90+
'/opt/puppetlabs/puppet/lib/pkgconfig'
91+
end
92+
93+
def gem_cmd
94+
'/opt/puppetlabs/puppet/bin/gem'
95+
end
96+
97+
def common_module_dir
98+
'/opt/puppetlabs/puppet/modules'
99+
end
100+
101+
def vendor_module_dir
102+
'/opt/puppetlabs/puppet/vendor_modules'
103+
end
88104
end
89105

90106
class WindowsRunMode < RunMode
@@ -112,8 +128,33 @@ def log_dir
112128
which_dir(File.join(windows_common_base("puppet/var/log")), "~/.puppetlabs/var/log")
113129
end
114130

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

154+
def installdir
155+
ENV['FACTER_env_windows_installdir']
156+
end
157+
117158
def windows_common_base(*extra)
118159
[ENV['ALLUSERSPROFILE'], "PuppetLabs"] + extra
119160
end

Diff for: spec/unit/defaults_spec.rb

+2-2
Original file line numberDiff line numberDiff line change
@@ -170,14 +170,14 @@
170170
allow(ENV).to receive(:[]).with("FACTER_env_windows_installdir").and_return(installdir)
171171

172172
expect(
173-
Puppet.default_vendormoduledir
173+
Puppet[:vendormoduledir]
174174
).to eq('C:\Program Files\Puppet Labs\Puppet\puppet\vendor_modules')
175175
end
176176

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

180-
expect(Puppet.default_vendormoduledir).to be_nil
180+
expect(Puppet[:vendormoduledir]).to be_nil
181181
end
182182
end
183183
end

Diff for: spec/unit/provider/package/puppet_gem_spec.rb

+1-28
Original file line numberDiff line numberDiff line change
@@ -27,38 +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(Puppet.run_mode).to receive(:gem_cmd).and_return(provider_gem_cmd)
3131
allow(Puppet::Util::Platform).to receive(:windows?).and_return(false)
3232
end
3333

3434

35-
describe '.windows_gemcmd' do
36-
context 'when PUPPET_DIR is not set' do
37-
before do
38-
allow(Puppet::Util).to receive(:get_env).and_call_original
39-
allow(Puppet::Util).to receive(:get_env).with('PUPPET_DIR').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(Puppet::Util).to receive(:get_env).and_call_original
52-
allow(Puppet::Util).to receive(:get_env).with('PUPPET_DIR').and_return('puppet_dir')
53-
end
54-
55-
it 'uses Gem.default_bindir' do
56-
expected_path = File.join('puppet_dir', 'bin', 'gem.bat')
57-
expect(described_class.windows_gemcmd).to eql(expected_path)
58-
end
59-
end
60-
end
61-
6235
context "when installing" do
6336
before :each do
6437
allow(provider).to receive(:rubygem_version).and_return('1.9.9')

Diff for: 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+
expect(@run_mode.pkg_config_path).to eq('/opt/puppetlabs/puppet/lib/pkgconfig')
103+
end
104+
105+
describe "#gem_cmd" do
106+
expect(@run_mode.gem_cmd).to eq('/opt/puppetlabs/puppet/bin/gem')
107+
end
108+
109+
describe "#common_module_dir" do
110+
expect(@run_mode.common_module_dir).to eq('/opt/puppetlabs/puppet/modules')
111+
end
112+
113+
describe "#vendor_module_dir" do
114+
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
@@ -172,6 +188,81 @@
172188
end
173189
end
174190

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
265+
175266
describe "#without_env internal helper with UTF8 characters" do
176267
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" }
177268
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" }

0 commit comments

Comments
 (0)