Skip to content

Commit 4e46a38

Browse files
authored
Fix multiple issues with ~FC023 (#341)
Signed-off-by: Justin Schuhmann <jmschu02@gmail.com>
1 parent 6c53af0 commit 4e46a38

File tree

6 files changed

+87
-45
lines changed

6 files changed

+87
-45
lines changed

.foodcritic

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
~FC059
2+
~FC023

resources/pool.rb

Lines changed: 49 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
doc = Document.new(xml)
112112

113113
# root items
114-
runtime_version value doc.root, 'APPPOOL/@RuntimeVersion'
114+
runtime_version value(doc.root, 'APPPOOL/@RuntimeVersion').gsub(/^v/, '')
115115
pipeline_mode value(doc.root, 'APPPOOL/@PipelineMode').to_sym
116116

117117
# add items
@@ -125,6 +125,9 @@
125125
load_user_profile bool(value(doc.root, 'APPPOOL/add/processModel/@loadUserProfile'))
126126
identity_type value(doc.root, 'APPPOOL/add/processModel/@identityType').to_sym if iis_version > 7.0
127127
username value doc.root, 'APPPOOL/add/processModel/@userName'
128+
unless username.nil? || desired.username.nil?
129+
Chef::Log.info('username: ' + username + ' -> ' + desired.username)
130+
end
128131
password value doc.root, 'APPPOOL/add/processModel/@password'
129132
logon_type value(doc.root, 'APPPOOL/add/processModel/@logonType').to_sym if iis_version > 7.0
130133
manual_group_membership bool(value(doc.root, 'APPPOOL/add/processModel/@manualGroupMembership'))
@@ -189,15 +192,13 @@
189192
end
190193

191194
action :config do
192-
converge_by "Configured Application Pool \"#{new_resource}\"" do
193-
configure
194-
end
195+
configure
195196
end
196197

197198
action :delete do
198199
if current_resource.runtime_version
199200
converge_by "Deleted Application Pool \"#{new_resource}\"" do
200-
shell_out!("#{appcmd(node)} delete apppool \"#{site_identifier}\"")
201+
shell_out!("#{appcmd(node)} delete apppool \"#{new_resource.name}\"")
201202
end
202203
else
203204
Chef::Log.debug("#{new_resource} pool does not exist - nothing to do")
@@ -207,7 +208,7 @@
207208
action :start do
208209
if !current_resource.runtime_version
209210
converge_by "Started Application Pool \"#{new_resource}\"" do
210-
shell_out!("#{appcmd(node)} start apppool \"#{site_identifier}\"") unless new_resource.running
211+
shell_out!("#{appcmd(node)} start apppool \"#{new_resource.name}\"") unless new_resource.running
211212
end
212213
else
213214
Chef::Log.debug("#{new_resource} already running - nothing to do")
@@ -217,45 +218,47 @@
217218
action :stop do
218219
if current_resource.runtime_version
219220
converge_by "Stopped Application Pool \"#{new_resource}\"" do
220-
shell_out!("#{appcmd(node)} stop apppool \"#{site_identifier}\"")
221+
shell_out!("#{appcmd(node)} stop apppool \"#{new_resource.name}\"")
221222
end
222223
else
223224
Chef::Log.debug("#{new_resource} already stopped - nothing to do")
224225
end
225226
end
226227

227228
action :restart do
228-
converge_by "Restarted Application Pool \"#{new_resource}\"" do
229-
shell_out!("#{appcmd(node)} stop APPPOOL \"#{site_identifier}\"") if running
230-
sleep 2
231-
shell_out!("#{appcmd(node)} start APPPOOL \"#{site_identifier}\"")
229+
if current_resource.runtime_version
230+
converge_by "Restarted Application Pool \"#{new_resource}\"" do
231+
shell_out!("#{appcmd(node)} stop APPPOOL \"#{new_resource.name}\"") if running
232+
sleep 2
233+
shell_out!("#{appcmd(node)} start APPPOOL \"#{new_resource.name}\"")
234+
end
232235
end
233236
end
234237

235238
action :recycle do
236-
converge_by "Recycled Application Pool \"#{new_resource}\"" do
237-
shell_out!("#{appcmd(node)} recycle APPPOOL \"#{site_identifier}\"")
239+
if current_resource.runtime_version
240+
converge_by "Recycled Application Pool \"#{new_resource}\"" do
241+
shell_out!("#{appcmd(node)} recycle APPPOOL \"#{new_resource.name}\"")
242+
end
238243
end
239244
end
240245

241246
action_class.class_eval do
242-
def site_identifier
243-
new_resource.name
244-
end
245-
246247
def configure
247248
# Application Pool Config
248249
cmd = "#{appcmd(node)} set config /section:applicationPools"
249250

250251
# root items
251-
converge_if_changed :auto_start do
252-
cmd << configure_application_pool("autoStart:#{new_resource.auto_start}")
253-
only_if { iis_version >= 7.0 }
252+
if iis_version >= 7.0
253+
converge_if_changed :auto_start do
254+
cmd << configure_application_pool("autoStart:#{new_resource.auto_start}")
255+
end
254256
end
255257

256-
converge_if_changed :start_mode do
257-
cmd << configure_application_pool("startMode:#{new_resource.start_mode}")
258-
only_if { iis_version >= 7.5 }
258+
if iis_version >= 7.5
259+
converge_if_changed :start_mode do
260+
cmd << configure_application_pool("startMode:#{new_resource.start_mode}")
261+
end
259262
end
260263

261264
if new_resource.no_managed_code
@@ -294,9 +297,10 @@ def configure
294297
converge_if_changed :idle_timeout do
295298
cmd << configure_application_pool("processModel.idleTimeout:#{new_resource.idle_timeout}")
296299
end
297-
converge_if_changed :idle_timeout_action do
298-
cmd << configure_application_pool("processModel.idleTimeoutAction:#{new_resource.idle_timeout_action}")
299-
only_if { iis_version >= 8.5 }
300+
if iis_version >= 8.5
301+
converge_if_changed :idle_timeout_action do
302+
cmd << configure_application_pool("processModel.idleTimeoutAction:#{new_resource.idle_timeout_action}")
303+
end
300304
end
301305
converge_if_changed :shutdown_time_limit do
302306
cmd << configure_application_pool("processModel.shutdownTimeLimit:#{new_resource.shutdown_time_limit}")
@@ -314,17 +318,18 @@ def configure
314318
cmd << configure_application_pool("processModel.pingResponseTime:#{new_resource.ping_response_time}")
315319
end
316320

317-
should_clear_apppool_schedules = ((new_resource.recycle_at_time != current_resource.recycle_at_time) && !@node_array.empty?) || (new_resource.recycle_schedule_clear && !@node_array.empty?)
321+
should_clear_apppool_schedules = ((new_resource.recycle_at_time != current_resource.recycle_at_time) && !@node_array.nil? && !@node_array.empty?) || (new_resource.recycle_schedule_clear && !@node_array.nil? && !@node_array.empty?)
318322

319323
# recycling items
320324
## Special case this collection removal for now.
321325
# TODO: test if this is needed
322326
# is_new_recycle_at_time = true
323-
converge_by "Cleared Periodic Restart Schedule #{new_resource}" do
324-
clear_pool_schedule_cmd = "#{appcmd(node)} set config /section:applicationPools \"/-[name='#{new_resource.name}'].recycling.periodicRestart.schedule\""
325-
Chef::Log.debug(clear_pool_schedule_cmd)
326-
shell_out!(clear_pool_schedule_cmd)
327-
only_if { should_clear_apppool_schedules }
327+
if should_clear_apppool_schedules
328+
converge_by "Cleared Periodic Restart Schedule #{new_resource} - #{should_clear_apppool_schedules}" do
329+
clear_pool_schedule_cmd = "#{appcmd(node)} set config /section:applicationPools \"/-[name='#{new_resource.name}'].recycling.periodicRestart.schedule\""
330+
Chef::Log.debug(clear_pool_schedule_cmd)
331+
shell_out!(clear_pool_schedule_cmd)
332+
end
328333
end
329334

330335
converge_if_changed :recycle_after_time do
@@ -398,25 +403,23 @@ def configure
398403
cmd << configure_application_pool("cpu.smpProcessorAffinityMask2:#{new_resource.smp_processor_affinity_mask_2.floor}")
399404
end
400405

401-
converge_by "Configured Application Pool settings \"#{new_resource}\"" do
402-
Chef::Log.debug(cmd)
403-
shell_out!(cmd)
404-
not_if { cmd == "#{appcmd(node)} set config /section:applicationPools" }
406+
unless cmd == "#{appcmd(node)} set config /section:applicationPools"
407+
converge_by "Configured Application Pool \"#{new_resource}\"" do
408+
Chef::Log.debug(cmd)
409+
shell_out!(cmd)
410+
end
405411
end
406412

407413
# Application Pool Identity Settings
408414
if new_resource.username && new_resource.username != ''
409-
cmd_default = "#{appcmd(node)} set config /section:applicationPools"
410-
cmd_default << " \"/[name='#{new_resource.name}'].processModel.identityType:SpecificUser\""
411-
412-
cmd = cmd_default
415+
cmd = default_app_pool_user
413416
converge_if_changed :username do
414417
cmd << " \"/[name='#{new_resource.name}'].processModel.userName:#{new_resource.username}\""
415418
end
416419
converge_if_changed :password do
417420
cmd << " \"/[name='#{new_resource.name}'].processModel.password:#{new_resource.password}\""
418421
end
419-
if cmd != cmd_default
422+
if cmd != default_app_pool_user
420423
Chef::Log.debug(cmd)
421424
shell_out!(cmd)
422425
end
@@ -432,6 +435,11 @@ def configure
432435
end
433436
end
434437

438+
def default_app_pool_user
439+
cmd_default = "#{appcmd(node)} set config /section:applicationPools"
440+
cmd_default << " \"/[name='#{new_resource.name}'].processModel.identityType:SpecificUser\""
441+
end
442+
435443
def configure_application_pool(config, add_remove = '')
436444
" \"/#{add_remove}[name='#{new_resource.name}'].#{config}\""
437445
end

test/cookbooks/test/recipes/pool.rb

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,5 +26,16 @@
2626
iis_pool 'myAppPool_v1_1' do
2727
runtime_version '2.0'
2828
pipeline_mode :Classic
29-
action :add
29+
action [:add, :config]
30+
end
31+
32+
iis_pool 'testapppool' do
33+
thirty_two_bit false
34+
runtime_version '4.0'
35+
pipeline_mode :Integrated
36+
start_mode :OnDemand
37+
identity_type :SpecificUser
38+
username "#{node['hostname']}\\vagrant"
39+
password 'vagrant'
40+
action [:add, :config]
3041
end

test/integration/pool/controls/pool_spec.rb

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,3 +17,15 @@
1717
it { should have_name('myAppPool_v1_1') }
1818
it { should have_queue_length(1000) }
1919
end
20+
21+
describe iis_pool('testapppool') do
22+
it { should exist }
23+
it { should be_running }
24+
its('managed_runtime_version') { should eq 'v4.0' }
25+
its('managed_pipeline_mode') { should eq 'Integrated' }
26+
it { should have_name('testapppool') }
27+
its('start_mode') { should eq 'OnDemand' }
28+
its('identity_type') { should eq 'SpecificUser' }
29+
its('username') { should contain '\\vagrant' }
30+
its('password') { should eq 'vagrant' }
31+
end

test/integration/pool/libraries/iis_pool.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,18 @@ def worker_processes
8585
iis_pool[:worker_processes]
8686
end
8787

88+
def identity_type
89+
iis_pool[:process_model][:identity_type]
90+
end
91+
92+
def username
93+
iis_pool[:process_model][:username]
94+
end
95+
96+
def password
97+
iis_pool[:process_model][:password]
98+
end
99+
88100
def exists?
89101
!iis_pool.nil? && !iis_pool[:name].nil?
90102
end
@@ -121,7 +133,7 @@ def initialize(inspec)
121133

122134
# want to populate everything using one powershell command here and spit it out as json
123135
def iis_pool(pool_name)
124-
command = "Import-Module WebAdministration; Get-Item IIS:\\AppPools\\#{pool_name} | Select-Object name, queueLength, autoStart, enable32BitAppOnWin64, managedRuntimeVersion, managedRuntimeLoader, enableConfigurationOverride,managedPipelineMode, passAnonymousToken, startMode, state, ItemXPath | ConvertTo-Json"
136+
command = "Import-Module WebAdministration; Get-Item IIS:\\AppPools\\#{pool_name} | Select-Object name, queueLength, autoStart, enable32BitAppOnWin64, managedRuntimeVersion, managedRuntimeLoader, enableConfigurationOverride, managedPipelineMode, passAnonymousToken, startMode, state, ItemXPath | ConvertTo-Json"
125137
cmd = @inspec.command(command)
126138
command_process_model = "(Get-Item IIS:\\AppPools\\#{pool_name}).processModel | Select-Object identityType, userName, password, loadUserProfile, setProfileEnvironment, logonType, manualGroupMembership, idleTimeout, idleTimeoutAction, maxProcesses, shutdownTimeLimit, startupTimeLimit, pingingEnabled, pingInterval, pingResponseTime, logEventOnProcessModel | ConvertTo-Json"
127139
cmd_process_model = @inspec.command(command_process_model)

test/integration/vdir/libraries/iis_vdir.rb

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,9 +112,7 @@ def initialize(inspec)
112112
# want to populate everything using one powershell command here and spit it out as json
113113
def iis_vdir(name, path)
114114
command = "Import-Module WebAdministration; Get-WebVirtualDirectory -Site \"#{name}\" -Name \"#{path}\" | Select-Object path, physicalPath, userName, password, logonMethod, allowSubDirConfig, PSPath, ItemXPath | ConvertTo-Json"
115-
Log.info(command)
116115
cmd = @inspec.command(command)
117-
Log.info(cmd.stdout)
118116

119117
begin
120118
vdir = JSON.parse(cmd.stdout)

0 commit comments

Comments
 (0)