Class: Puppet::Provider::IIS_PowerShell

Inherits:
Puppet::Provider
  • Object
show all
Defined in:
lib/puppet/provider/iis_powershell.rb

Overview

This is the base class on which other providers are based.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = {}) ⇒ IIS_PowerShell

rubocop:disable all



8
9
10
11
12
13
14
15
# File 'lib/puppet/provider/iis_powershell.rb', line 8

def initialize(value = {})
  super(value)
  @original_values = if value.is_a? Hash
                       value.clone
                     else
                       {}
                     end
end

Class Method Details

.parse_json_result(raw) ⇒ Object

parse json result



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# File 'lib/puppet/provider/iis_powershell.rb', line 82

def self.parse_json_result(raw)
  return nil if raw.nil?
  # Unfortunately PowerShell tends to automatically insert CRLF characters mid-string (Console Width)
  # However as we're using JSON which does not use Line Endings for termination, we can safely strip them
  raw = raw.delete("\n").delete("\r")

  result = JSON.parse(raw)
  return nil if result.nil?

  # The JSON conversion for PowerShell 2.0 always creates a root HashTable with a single key of 'Objects'
  # whereas under PowerShell 3.0+ this is not the case.  Detect the PowerShell 2.0 style and render it back
  # into a PowerShell 3.0+ format.
  if result.is_a?(Hash) && result.keys[0] == 'Objects'
    return nil if result['Objects'].nil?

    # Due to Convert-XML in PowerShell 2.0 converting elements with empty elements (<something />) into nulls,
    # need to be careful how things are processed e.g.
    # - An empty array comes in as nil
    # - A blank string comes in as nil
    # Only the provider will be able to determine what a nil value really means

    # If only a single object is returned then the result is Hash with a single 'Object' key
    # if multiple objects are returned then the result is an array of Hashes
    if result['Objects'].is_a?(Hash) && result['Objects'].keys[0] == 'Object'
      return [result['Objects']['Object']]
    elsif result['Objects'].is_a?(Array)
      return result['Objects']
    else
      raise 'Unable to determine the JSON encoding from PowerShell 2.0'
    end
  end

  # Always return an Array type
  result.is_a?(Array) ? result : [result]
end

.prefetch(resources) ⇒ Object

Match resources with existing providers



18
19
20
21
22
23
24
25
# File 'lib/puppet/provider/iis_powershell.rb', line 18

def self.prefetch(resources)
  nodes = instances
  resources.each_key do |name|
    if provider = nodes.find { |node| node.name == name }
      resources[name].provider = provider
    end
  end
end

.ps_major_version(do_not_use_cached_value = false) ⇒ Object

do_not_use_cached_value is typically only used for testing. In normal usage the PowerShell version does not suddenly change during a Puppet run.



73
74
75
76
77
78
79
# File 'lib/puppet/provider/iis_powershell.rb', line 73

def self.ps_major_version(do_not_use_cached_value = false)
  if @powershell_major_version.nil? || do_not_use_cached_value
    version = Pwsh::WindowsPowerShell.version
    @powershell_major_version = version.nil? ? nil : version.split('.').first.to_i
  end
  @powershell_major_version
end

.ps_managerObject

PowerShellManager - Responsible for managing PowerShell



67
68
69
# File 'lib/puppet/provider/iis_powershell.rb', line 67

def self.ps_manager
  Pwsh::Manager.instance(command(:powershell), Pwsh::Manager.powershell_args)
end

.ps_script_content(template, resource) ⇒ Object

powershell script content



119
120
121
122
123
124
125
# File 'lib/puppet/provider/iis_powershell.rb', line 119

def self.ps_script_content(template, resource)
  @param_hash = resource
  template_path = File.expand_path('../templates', __FILE__)
  template_file = File.new(template_path + "/webadministration/#{template}.ps1.erb").read
  template      = ERB.new(template_file, nil, '-')
  template.result(binding)
end

.run(command, _check = false) ⇒ Object

run command



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/puppet/provider/iis_powershell.rb', line 39

def self.run(command, _check = false)
  Puppet.debug("COMMAND: #{command}")

  if ps_major_version == 2
    # - PowerShell 2.0 does not support autoload of modules therefore we must explicitly add the WebAdministration module
    # - Must change the current location to the be the IIS: provider
    # - Add the ConvertTo-JSON command support
    command = "Import-Module WebAdministration -ErrorAction Stop\n" \
              "cd iis:\n" +
              ps_script_content('json_1.7', @resource) + "\n" \
              "$ConfirmPreference = 'high'" + "\n" +
              command
  end

  result = ps_manager.execute(command)
  stderr = result[:stderr]

  stderr&.each do |er|
    er.each { |e| Puppet.debug "STDERR: #{e.chop}" } unless er.empty?
  end

  Puppet.debug "STDOUT: #{result[:stdout]}" unless result[:stdout].nil?
  Puppet.debug "ERRMSG: #{result[:errormessage]}" unless result[:errormessage].nil?

  result
end

Instance Method Details

#exists?Boolean

Returns:

  • (Boolean)


27
28
29
# File 'lib/puppet/provider/iis_powershell.rb', line 27

def exists?
  @property_hash[:ensure] == :present
end

#flushObject

update if exists



32
33
34
35
36
# File 'lib/puppet/provider/iis_powershell.rb', line 32

def flush
  if exists?
    update
  end
end