Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enhancement module custom resource #372

Merged
merged 2 commits into from
May 31, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions .kitchen.appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ suites:
- name: app
run_list:
- recipe[test::app]
- name: module
run_list:
- recipe[test::module]
- name: pool
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for alphabetizing these!

run_list:
- recipe[test::pool]
- name: root
run_list:
- recipe[test::root]
Expand All @@ -30,9 +36,6 @@ suites:
- name: site
run_list:
- recipe[test::site]
- name: pool
run_list:
- recipe[test::pool]
- name: vdir
run_list:
- recipe[test::vdir]
9 changes: 6 additions & 3 deletions .kitchen.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ suites:
- name: app
run_list:
- recipe[test::app]
- name: module
run_list:
- recipe[test::module]
- name: pool
run_list:
- recipe[test::pool]
- name: root
run_list:
- recipe[test::root]
Expand All @@ -35,9 +41,6 @@ suites:
- name: site
run_list:
- recipe[test::site]
- name: pool
run_list:
- recipe[test::pool]
- name: vdir
run_list:
- recipe[test::vdir]
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ Lint/ParenthesesAsGroupedExpression:
Style/PredicateName:
Exclude:
- 'test/integration/app/libraries/*'
- 'test/integration/module/libraries/*'
- 'test/integration/pool/libraries/*'
- 'test/integration/root/libraries/*'
- 'test/integration/section/libraries/*'
Expand Down
41 changes: 26 additions & 15 deletions libraries/section_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,27 @@ def unlock(node, section, location = '', returns = [0])
cmd_list_section node, :unlock, section, location, returns
end

def override_mode(node, action, section, location = '', returns = [0])
cmd_list_section(node, action, section, location, returns)
end

def get_current_lock(node, section, location = '')
command_path = 'MACHINE/WEBROOT/APPHOST'
command_path << "/#{location}" if location
cmd = "#{appcmd(node)} list config \"#{command_path}}\""
cmd << " -section:#{section} -commit:apphost /config:* /xml"
result = shell_out cmd
if result.stderr.empty?
xml = result.stdout
doc = Document.new xml
value(doc.root, 'CONFIG/@overrideMode')
else
Chef::Log.info(result.stderr)
end

nil
end

def cmd_section(node, check, section, location, returns)
cmd = "#{appcmd(node)} set config \"MACHINE/WEBROOT/APPHOST/#{location}\""
cmd << " -section:\"#{section}\" -overrideMode:#{check}"
Expand All @@ -47,21 +68,11 @@ def cmd_section(node, check, section, location, returns)
end

def cmd_list_section(node, action, section, location, returns)
command_path = 'MACHINE/WEBROOT/APPHOST'
command_path << "/#{location}" if location
cmd = "#{appcmd(node)} list config \"#{command_path}}\""
cmd << " -section:#{section} -commit:apphost /config:* /xml"
result = shell_out cmd
if result.stderr.empty?
xml = result.stdout
doc = Document.new xml
check = action == :lock ? 'Deny' : 'Allow'
unless value(doc.root, 'CONFIG/@overrideMode') == check
cmd_section node, check, section, location, returns
end
else
Chef::Log.info(result.stderr)
end
current_lock = get_current_lock(node, section, location)
check = action if action == 'Inherit'
check = (action == :lock ? 'Deny' : 'Allow') if action != 'Inherit'

cmd_section node, check, section, location, returns unless current_lock == check
end
end
end
Expand Down
123 changes: 0 additions & 123 deletions providers/module.rb

This file was deleted.

2 changes: 1 addition & 1 deletion resources/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

load_current_value do |desired|
site_name desired.site_name
cmd = shell_out("#{appcmd(node)} list app")
cmd = shell_out("#{appcmd(node)} list app \"#{desired.site_name}#{desired.path}\"")
Chef::Log.debug("#{appcmd(node)} list app command output: #{cmd.stdout}")
if cmd.stderr.empty?
Chef::Log.debug('Running regex')
Expand Down
116 changes: 106 additions & 10 deletions resources/module.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
#
# Author:: Jon DeCamp (<jon.decamp@nordstrom.com>)
# Cookbook:: iis
# Resource:: module
#
# Copyright:: 2012-2016, Nordstrom, Inc.
# Copyright:: 2017, Chef Software, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
Expand All @@ -18,14 +17,111 @@
# limitations under the License.
#

actions :add, :delete, :install, :uninstall
include Opscode::IIS::Helper
include Opscode::IIS::Processors
include Opscode::IIS::SectionHelper

property :module_name, String, name_property: true
property :type, String
property :add, [true, false], default: false
property :image, String
property :precondition, String
property :application, String
property :previous_lock, String

default_action :add

attribute :module_name, kind_of: String, name_attribute: true
attribute :type, kind_of: String, default: nil
attribute :add, kind_of: [FalseClass, TrueClass], default: nil
attribute :image, kind_of: String, default: nil
attribute :precondition, kind_of: String, default: nil
attribute :application, kind_of: String, default: nil
load_current_value do |desired|
module_name desired.module_name
application desired.application if desired.application
cmd = "#{appcmd(node)} list module /module.name:\"#{desired.module_name}\""
cmd << " /app.name:\"#{desired.application}\"" if desired.application

cmd_result = shell_out cmd
# 'MODULE "Module Name" ( type:module.type, preCondition:condition )'
# 'MODULE "Module Name" ( native, preCondition:condition )'

Chef::Log.debug("#{desired.name} list module command output: #{cmd_result.stdout}")
unless cmd_result.stdout.empty?
previous_lock get_current_lock(node, 'system.webServer/modules', desired.application)
cmd = "#{appcmd(node)} list module /module.name:\"#{desired.module_name}\""
cmd << " /app.name:\"#{desired.application}\"" if desired.application
cmd << ' /config:* /xml'
cmd_result = shell_out cmd
if cmd_result.stderr.empty?
xml = cmd_result.stdout
doc = Document.new(xml)
type value doc.root, 'MODULE/@type'
precondition value doc.root, 'MODULE/@preCondition'
end
end
end

# appcmd syntax for adding modules
# appcmd add module /name:string /type:string /preCondition:string
action :add do
if !current_resource.type
converge_by("add IIS module #{new_resource.module_name}") do
unlock(node, 'system.webServer/modules', new_resource.application)
cmd = "#{appcmd(node)} add module /module.name:\"#{new_resource.module_name}\""
cmd << " /app.name:\"#{new_resource.application}\"" if new_resource.application
cmd << " /type:\"#{new_resource.type}\"" if new_resource.type
cmd << " /preCondition:\"#{new_resource.precondition}\"" if new_resource.precondition

shell_out!(cmd, returns: [0, 42])
override_mode(node, current_resource.previous_lock, 'system.webServer/modules', new_resource.application)
end
else
Chef::Log.debug("#{new_resource} module already exists - nothing to do")
end
end

action :delete do
if current_resource.type
converge_by("delete IIS module #{new_resource.module_name}") do
unlock(node, 'system.webServer/modules', new_resource.application)
cmd = "#{appcmd(node)} delete module /module.name:\"#{new_resource.module_name}\""
cmd << " /app.name:\"#{new_resource.application}\"" if new_resource.application

shell_out!(cmd, returns: [0, 42])
override_mode(node, current_resource.previous_lock, 'system.webServer/modules', new_resource.application)
end
else
Chef::Log.debug("#{new_resource} module does not exist - nothing to do")
end
end

# appcmd syntax for installing native modules
# appcmd install module /name:string /add:string(true|false) /image:string
action :install do
if !current_resource.type
converge_by("install IIS module #{new_resource.module_name}") do
unlock(node, 'system.webServer/modules', new_resource.application)
cmd = "#{appcmd(node)} install module /name:\"#{new_resource.module_name}\""
cmd << " /add:\"#{new_resource.add}\"" unless new_resource.add.nil?
cmd << " /image:\"#{new_resource.image}\"" if new_resource.image
cmd << " /preCondition:\"#{new_resource.precondition}\"" if new_resource.precondition

shell_out!(cmd, returns: [0, 42])
override_mode(node, current_resource.previous_lock, 'system.webServer/modules', new_resource.application)
end
else
Chef::Log.debug("#{new_resource} module already exists - nothing to do")
end
end

# appcmd syntax for uninstalling native modules
# appcmd uninstall module <name>
action :uninstall do
if current_resource.type
converge_by("uninstall IIS module #{new_resource.module_name}") do
unlock(node, 'system.webServer/modules', new_resource.application)
cmd = "#{appcmd(node)} uninstall module \"#{new_resource.module_name}\""

attr_accessor :exists
shell_out!(cmd, returns: [0, 42])
override_mode(node, current_resource.previous_lock, 'system.webServer/modules', new_resource.application)
end
else
Chef::Log.debug("#{new_resource} module does not exists - nothing to do")
end
end
Loading