Skip to content

Commit 0017a95

Browse files
committed
(PUP-1353) abstract away the priority setting
1 parent 5b17ec6 commit 0017a95

File tree

12 files changed

+88
-121
lines changed

12 files changed

+88
-121
lines changed

lib/puppet/provider/service/service.rb

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,13 +24,12 @@ def texecute(type, command, fof = true, squelch = false, combine = true)
2424
# Set the process priority to 0 (or normal in Windows) so that services
2525
# which are started as children of puppet will start with normal priority,
2626
# rather than the priority of the puppet process itself.
27-
priority = Puppet::Util::Platform.windows? ? Process::NORMAL_PRIORITY_CLASS : 0
2827
begin
2928
opts = {
3029
:combine => combine,
3130
:failonfail => fof,
3231
:override_locale => false,
33-
:priority => priority,
32+
:priority => :normal,
3433
:squelch => squelch,
3534
}
3635
execute(command, opts)

lib/puppet/util/execution.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ def self.execfail(command, exception)
134134
# Passing in a value of false for this option will allow the command to be executed using the user/system locale.
135135
# @option options [Hash<{String => String}>] :custom_environment ({}) a hash of key/value pairs to set as environment variables for the duration
136136
# of the command.
137-
# @option options [Integer] :priority (nil) The priority of child process, aka the nice value in Posix operatingsystems.
137+
# @option options [Symbol] :priority (nil) The priority of child process, see Puppet::Settings::PrioritySetting::PRIORITY_MAP for values.
138138
# @return [Puppet::Util::Execution::ProcessOutput] output as specified by options
139139
# @raise [Puppet::ExecutionFailure] if the executed chiled process did not exit with status == 0 and `failonfail` is
140140
# `true`.
@@ -341,7 +341,7 @@ def self.execute_posix(command, options, stdin, stdout, stderr)
341341
Process.setsid
342342
if options[:priority]
343343
if Puppet.features.root?
344-
setpriority(options[:priority])
344+
setpriority(Puppet::Settings::PrioritySetting::PRIORITY_MAP[options[:priority]])
345345
end
346346
end
347347
begin

lib/puppet/util/windows/process.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,13 @@ def execute(command, arguments, stdin, stdout, stderr)
2424
:stderr => stderr
2525
},
2626
:close_handles => false,
27+
:creation_flags => 0,
2728
}
2829
if arguments[:suppress_window]
29-
create_args[:creation_flags] = CREATE_NO_WINDOW
30+
create_args[:creation_flags] |= CREATE_NO_WINDOW
31+
end
32+
if arguments[:priority]
33+
create_args[:creation_flags] |= Puppet::Settings::PrioritySetting::PRIORITY_MAP[arguments[:priority]]
3034
end
3135
if arguments[:cwd]
3236
create_args[:cwd] = arguments[:cwd]

spec/unit/provider/service/bsd_spec.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,13 +94,13 @@
9494

9595
it "should use the supplied start command if specified" do
9696
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :start => '/bin/foo'))
97-
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
97+
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
9898
provider.start
9999
end
100100

101101
it "should start the serviced directly otherwise" do
102102
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
103-
expect(provider).to receive(:execute).with(['/etc/rc.d/sshd', :onestart], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
103+
expect(provider).to receive(:execute).with(['/etc/rc.d/sshd', :onestart], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
104104
expect(provider).to receive(:search).with('sshd').and_return('/etc/rc.d/sshd')
105105
provider.start
106106
end
@@ -113,13 +113,13 @@
113113

114114
it "should use the supplied stop command if specified" do
115115
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :stop => '/bin/foo'))
116-
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
116+
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
117117
provider.stop
118118
end
119119

120120
it "should stop the serviced directly otherwise" do
121121
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
122-
expect(provider).to receive(:execute).with(['/etc/rc.d/sshd', :onestop], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
122+
expect(provider).to receive(:execute).with(['/etc/rc.d/sshd', :onestop], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
123123
expect(provider).to receive(:search).with('sshd').and_return('/etc/rc.d/sshd')
124124
provider.stop
125125
end

spec/unit/provider/service/gentoo_spec.rb

Lines changed: 20 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@
7979
describe "#start" do
8080
it "should use the supplied start command if specified" do
8181
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :start => '/bin/foo'))
82-
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
82+
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
8383
provider.start
8484
end
8585

8686
it "should start the service with <initscript> start otherwise" do
8787
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
88-
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:start], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
88+
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:start], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
8989
expect(provider).to receive(:search).with('sshd').and_return('/etc/init.d/sshd')
9090
provider.start
9191
end
@@ -94,13 +94,13 @@
9494
describe "#stop" do
9595
it "should use the supplied stop command if specified" do
9696
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :stop => '/bin/foo'))
97-
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
97+
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
9898
provider.stop
9999
end
100100

101101
it "should stop the service with <initscript> stop otherwise" do
102102
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd'))
103-
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:stop], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
103+
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:stop], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
104104
expect(provider).to receive(:search).with('sshd').and_return('/etc/init.d/sshd')
105105
provider.stop
106106
end
@@ -160,24 +160,24 @@
160160
describe "when a special status command is specified" do
161161
it "should use the status command from the resource" do
162162
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :status => '/bin/foo'))
163-
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => 0, :squelch => false)
164-
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => false, :override_locale => false, :priority => 0, :squelch => false)
163+
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => :normal, :squelch => false)
164+
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => false, :override_locale => false, :priority => :normal, :squelch => false)
165165
allow($CHILD_STATUS).to receive(:exitstatus).and_return(0)
166166
provider.status
167167
end
168168

169169
it "should return :stopped when the status command returns with a non-zero exitcode" do
170170
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :status => '/bin/foo'))
171-
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => 0, :squelch => false)
172-
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => false, :override_locale => false, :priority => 0, :squelch => false)
171+
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => :normal, :squelch => false)
172+
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => false, :override_locale => false, :priority => :normal, :squelch => false)
173173
allow($CHILD_STATUS).to receive(:exitstatus).and_return(3)
174174
expect(provider.status).to eq(:stopped)
175175
end
176176

177177
it "should return :running when the status command returns with a zero exitcode" do
178178
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :status => '/bin/foo'))
179-
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => 0, :squelch => false)
180-
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => false, :override_locale => false, :priority => 0, :squelch => false)
179+
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => :normal, :squelch => false)
180+
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => false, :override_locale => false, :priority => :normal, :squelch => false)
181181
allow($CHILD_STATUS).to receive(:exitstatus).and_return(0)
182182
expect(provider.status).to eq(:running)
183183
end
@@ -186,14 +186,14 @@
186186
describe "when hasstatus is false" do
187187
it "should return running if a pid can be found" do
188188
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :hasstatus => false))
189-
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => 0, :squelch => false)
189+
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => :normal, :squelch => false)
190190
expect(provider).to receive(:getpid).and_return(1000)
191191
expect(provider.status).to eq(:running)
192192
end
193193

194194
it "should return stopped if no pid can be found" do
195195
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :hasstatus => false))
196-
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => 0, :squelch => false)
196+
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => :normal, :squelch => false)
197197
expect(provider).to receive(:getpid).and_return(nil)
198198
expect(provider.status).to eq(:stopped)
199199
end
@@ -203,15 +203,15 @@
203203
it "should return running if <initscript> status exits with a zero exitcode" do
204204
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :hasstatus => true))
205205
expect(provider).to receive(:search).with('sshd').and_return('/etc/init.d/sshd')
206-
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => 0, :squelch => false)
206+
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => :normal, :squelch => false)
207207
allow($CHILD_STATUS).to receive(:exitstatus).and_return(0)
208208
expect(provider.status).to eq(:running)
209209
end
210210

211211
it "should return stopped if <initscript> status exits with a non-zero exitcode" do
212212
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :hasstatus => true))
213213
expect(provider).to receive(:search).with('sshd').and_return('/etc/init.d/sshd')
214-
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => 0, :squelch => false)
214+
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:status], :combine => true, :failonfail => false, :override_locale => false, :priority => :normal, :squelch => false)
215215
allow($CHILD_STATUS).to receive(:exitstatus).and_return(3)
216216
expect(provider.status).to eq(:stopped)
217217
end
@@ -221,24 +221,24 @@
221221
describe "#restart" do
222222
it "should use the supplied restart command if specified" do
223223
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :restart => '/bin/foo'))
224-
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:restart], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
225-
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
224+
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:restart], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
225+
expect(provider).to receive(:execute).with(['/bin/foo'], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
226226
provider.restart
227227
end
228228

229229
it "should restart the service with <initscript> restart if hasrestart is true" do
230230
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :hasrestart => true))
231231
expect(provider).to receive(:search).with('sshd').and_return('/etc/init.d/sshd')
232-
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:restart], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
232+
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:restart], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
233233
provider.restart
234234
end
235235

236236
it "should restart the service with <initscript> stop/start if hasrestart is false" do
237237
provider = provider_class.new(Puppet::Type.type(:service).new(:name => 'sshd', :hasrestart => false))
238238
expect(provider).to receive(:search).with('sshd').and_return('/etc/init.d/sshd')
239-
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:restart], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
240-
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:stop], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
241-
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:start], :combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false)
239+
expect(provider).not_to receive(:execute).with(['/etc/init.d/sshd',:restart], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
240+
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:stop], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
241+
expect(provider).to receive(:execute).with(['/etc/init.d/sshd',:start], :combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false)
242242
provider.restart
243243
end
244244
end

spec/unit/provider/service/init_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@
242242
describe "when starting a service on Solaris" do
243243
it "should use ctrun" do
244244
allow(Facter).to receive(:value).with(:osfamily).and_return('Solaris')
245-
expect(provider).to receive(:execute).with('/usr/bin/ctrun -l child /service/path/myservice start', {:combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false}).and_return("")
245+
expect(provider).to receive(:execute).with('/usr/bin/ctrun -l child /service/path/myservice start', {:combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false}).and_return("")
246246
allow($CHILD_STATUS).to receive(:exitstatus).and_return(0)
247247
provider.start
248248
end
@@ -251,7 +251,7 @@
251251
describe "when starting a service on RedHat" do
252252
it "should not use ctrun" do
253253
allow(Facter).to receive(:value).with(:osfamily).and_return('RedHat')
254-
expect(provider).to receive(:execute).with(['/service/path/myservice', :start], {:combine => true, :failonfail => true, :override_locale => false, :priority => 0, :squelch => false}).and_return("")
254+
expect(provider).to receive(:execute).with(['/service/path/myservice', :start], {:combine => true, :failonfail => true, :override_locale => false, :priority => :normal, :squelch => false}).and_return("")
255255
allow($CHILD_STATUS).to receive(:exitstatus).and_return(0)
256256
provider.start
257257
end

0 commit comments

Comments
 (0)