Skip to content

Commit

Permalink
Fix multiple issues with ~FC023
Browse files Browse the repository at this point in the history
Signed-off-by: Justin Schuhmann <jmschu02@gmail.com>
  • Loading branch information
EasyAsABC123 committed Apr 25, 2017
1 parent 6c53af0 commit 0a6443a
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 45 deletions.
1 change: 1 addition & 0 deletions .foodcritic
@@ -1 +1,2 @@
~FC059
~FC023
90 changes: 49 additions & 41 deletions resources/pool.rb
Expand Up @@ -111,7 +111,7 @@
doc = Document.new(xml)

# root items
runtime_version value doc.root, 'APPPOOL/@RuntimeVersion'
runtime_version value(doc.root, 'APPPOOL/@RuntimeVersion').gsub(/^v/, '')
pipeline_mode value(doc.root, 'APPPOOL/@PipelineMode').to_sym

# add items
Expand All @@ -125,6 +125,9 @@
load_user_profile bool(value(doc.root, 'APPPOOL/add/processModel/@loadUserProfile'))
identity_type value(doc.root, 'APPPOOL/add/processModel/@identityType').to_sym if iis_version > 7.0
username value doc.root, 'APPPOOL/add/processModel/@userName'
unless username.nil? || desired.username.nil?
Chef::Log.info('username: ' + username + ' -> ' + desired.username)
end
password value doc.root, 'APPPOOL/add/processModel/@password'
logon_type value(doc.root, 'APPPOOL/add/processModel/@logonType').to_sym if iis_version > 7.0
manual_group_membership bool(value(doc.root, 'APPPOOL/add/processModel/@manualGroupMembership'))
Expand Down Expand Up @@ -189,15 +192,13 @@
end

action :config do
converge_by "Configured Application Pool \"#{new_resource}\"" do
configure
end
configure
end

action :delete do
if current_resource.runtime_version
converge_by "Deleted Application Pool \"#{new_resource}\"" do
shell_out!("#{appcmd(node)} delete apppool \"#{site_identifier}\"")
shell_out!("#{appcmd(node)} delete apppool \"#{new_resource.name}\"")
end
else
Chef::Log.debug("#{new_resource} pool does not exist - nothing to do")
Expand All @@ -207,7 +208,7 @@
action :start do
if !current_resource.runtime_version
converge_by "Started Application Pool \"#{new_resource}\"" do
shell_out!("#{appcmd(node)} start apppool \"#{site_identifier}\"") unless new_resource.running
shell_out!("#{appcmd(node)} start apppool \"#{new_resource.name}\"") unless new_resource.running
end
else
Chef::Log.debug("#{new_resource} already running - nothing to do")
Expand All @@ -217,45 +218,47 @@
action :stop do
if current_resource.runtime_version
converge_by "Stopped Application Pool \"#{new_resource}\"" do
shell_out!("#{appcmd(node)} stop apppool \"#{site_identifier}\"")
shell_out!("#{appcmd(node)} stop apppool \"#{new_resource.name}\"")
end
else
Chef::Log.debug("#{new_resource} already stopped - nothing to do")
end
end

action :restart do
converge_by "Restarted Application Pool \"#{new_resource}\"" do
shell_out!("#{appcmd(node)} stop APPPOOL \"#{site_identifier}\"") if running
sleep 2
shell_out!("#{appcmd(node)} start APPPOOL \"#{site_identifier}\"")
if current_resource.runtime_version
converge_by "Restarted Application Pool \"#{new_resource}\"" do
shell_out!("#{appcmd(node)} stop APPPOOL \"#{new_resource.name}\"") if running
sleep 2
shell_out!("#{appcmd(node)} start APPPOOL \"#{new_resource.name}\"")
end
end
end

action :recycle do
converge_by "Recycled Application Pool \"#{new_resource}\"" do
shell_out!("#{appcmd(node)} recycle APPPOOL \"#{site_identifier}\"")
if current_resource.runtime_version
converge_by "Recycled Application Pool \"#{new_resource}\"" do
shell_out!("#{appcmd(node)} recycle APPPOOL \"#{new_resource.name}\"")
end
end
end

action_class.class_eval do
def site_identifier
new_resource.name
end

def configure
# Application Pool Config
cmd = "#{appcmd(node)} set config /section:applicationPools"

# root items
converge_if_changed :auto_start do
cmd << configure_application_pool("autoStart:#{new_resource.auto_start}")
only_if { iis_version >= 7.0 }
if iis_version >= 7.0
converge_if_changed :auto_start do
cmd << configure_application_pool("autoStart:#{new_resource.auto_start}")
end
end

converge_if_changed :start_mode do
cmd << configure_application_pool("startMode:#{new_resource.start_mode}")
only_if { iis_version >= 7.5 }
if iis_version >= 7.5
converge_if_changed :start_mode do
cmd << configure_application_pool("startMode:#{new_resource.start_mode}")
end
end

if new_resource.no_managed_code
Expand Down Expand Up @@ -294,9 +297,10 @@ def configure
converge_if_changed :idle_timeout do
cmd << configure_application_pool("processModel.idleTimeout:#{new_resource.idle_timeout}")
end
converge_if_changed :idle_timeout_action do
cmd << configure_application_pool("processModel.idleTimeoutAction:#{new_resource.idle_timeout_action}")
only_if { iis_version >= 8.5 }
if iis_version >= 8.5
converge_if_changed :idle_timeout_action do
cmd << configure_application_pool("processModel.idleTimeoutAction:#{new_resource.idle_timeout_action}")
end
end
converge_if_changed :shutdown_time_limit do
cmd << configure_application_pool("processModel.shutdownTimeLimit:#{new_resource.shutdown_time_limit}")
Expand All @@ -314,17 +318,18 @@ def configure
cmd << configure_application_pool("processModel.pingResponseTime:#{new_resource.ping_response_time}")
end

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?)
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?)

# recycling items
## Special case this collection removal for now.
# TODO: test if this is needed
# is_new_recycle_at_time = true
converge_by "Cleared Periodic Restart Schedule #{new_resource}" do
clear_pool_schedule_cmd = "#{appcmd(node)} set config /section:applicationPools \"/-[name='#{new_resource.name}'].recycling.periodicRestart.schedule\""
Chef::Log.debug(clear_pool_schedule_cmd)
shell_out!(clear_pool_schedule_cmd)
only_if { should_clear_apppool_schedules }
if should_clear_apppool_schedules
converge_by "Cleared Periodic Restart Schedule #{new_resource} - #{should_clear_apppool_schedules}" do
clear_pool_schedule_cmd = "#{appcmd(node)} set config /section:applicationPools \"/-[name='#{new_resource.name}'].recycling.periodicRestart.schedule\""
Chef::Log.debug(clear_pool_schedule_cmd)
shell_out!(clear_pool_schedule_cmd)
end
end

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

converge_by "Configured Application Pool settings \"#{new_resource}\"" do
Chef::Log.debug(cmd)
shell_out!(cmd)
not_if { cmd == "#{appcmd(node)} set config /section:applicationPools" }
unless cmd == "#{appcmd(node)} set config /section:applicationPools"
converge_by "Configured Application Pool \"#{new_resource}\"" do
Chef::Log.debug(cmd)
shell_out!(cmd)
end
end

# Application Pool Identity Settings
if new_resource.username && new_resource.username != ''
cmd_default = "#{appcmd(node)} set config /section:applicationPools"
cmd_default << " \"/[name='#{new_resource.name}'].processModel.identityType:SpecificUser\""

cmd = cmd_default
cmd = default_app_pool_user
converge_if_changed :username do
cmd << " \"/[name='#{new_resource.name}'].processModel.userName:#{new_resource.username}\""
end
converge_if_changed :password do
cmd << " \"/[name='#{new_resource.name}'].processModel.password:#{new_resource.password}\""
end
if cmd != cmd_default
if cmd != default_app_pool_user
Chef::Log.debug(cmd)
shell_out!(cmd)
end
Expand All @@ -432,6 +435,11 @@ def configure
end
end

def default_app_pool_user
cmd_default = "#{appcmd(node)} set config /section:applicationPools"
cmd_default << " \"/[name='#{new_resource.name}'].processModel.identityType:SpecificUser\""
end

def configure_application_pool(config, add_remove = '')
" \"/#{add_remove}[name='#{new_resource.name}'].#{config}\""
end
Expand Down
13 changes: 12 additions & 1 deletion test/cookbooks/test/recipes/pool.rb
Expand Up @@ -26,5 +26,16 @@
iis_pool 'myAppPool_v1_1' do
runtime_version '2.0'
pipeline_mode :Classic
action :add
action [:add, :config]
end

iis_pool 'testapppool' do
thirty_two_bit false
runtime_version '4.0'
pipeline_mode :Integrated
start_mode :OnDemand
identity_type :SpecificUser
username "#{node['hostname']}\\vagrant"
password 'vagrant'
action [:add, :config]
end
12 changes: 12 additions & 0 deletions test/integration/pool/controls/pool_spec.rb
Expand Up @@ -17,3 +17,15 @@
it { should have_name('myAppPool_v1_1') }
it { should have_queue_length(1000) }
end

describe iis_pool('testapppool') do
it { should exist }
it { should be_running }
its('managed_runtime_version') { should eq 'v4.0' }
its('managed_pipeline_mode') { should eq 'Integrated' }
it { should have_name('testapppool') }
its('start_mode') { should eq 'OnDemand' }
its('identity_type') { should eq 'SpecificUser' }
its('username') { should contain '\\vagrant' }
its('password') { should eq 'vagrant' }
end
14 changes: 13 additions & 1 deletion test/integration/pool/libraries/iis_pool.rb
Expand Up @@ -85,6 +85,18 @@ def worker_processes
iis_pool[:worker_processes]
end

def identity_type
iis_pool[:process_model][:identity_type]
end

def username
iis_pool[:process_model][:username]
end

def password
iis_pool[:process_model][:password]
end

def exists?
!iis_pool.nil? && !iis_pool[:name].nil?
end
Expand Down Expand Up @@ -121,7 +133,7 @@ def initialize(inspec)

# want to populate everything using one powershell command here and spit it out as json
def iis_pool(pool_name)
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"
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"
cmd = @inspec.command(command)
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"
cmd_process_model = @inspec.command(command_process_model)
Expand Down
2 changes: 0 additions & 2 deletions test/integration/vdir/libraries/iis_vdir.rb
Expand Up @@ -112,9 +112,7 @@ def initialize(inspec)
# want to populate everything using one powershell command here and spit it out as json
def iis_vdir(name, path)
command = "Import-Module WebAdministration; Get-WebVirtualDirectory -Site \"#{name}\" -Name \"#{path}\" | Select-Object path, physicalPath, userName, password, logonMethod, allowSubDirConfig, PSPath, ItemXPath | ConvertTo-Json"
Log.info(command)
cmd = @inspec.command(command)
Log.info(cmd.stdout)

begin
vdir = JSON.parse(cmd.stdout)
Expand Down

0 comments on commit 0a6443a

Please sign in to comment.