1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
|
#
# Author:: Adam Edwards (<adamed@getchef.com>)
#
# Copyright:: 2014, 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
require 'chef/util/powershell/cmdlet'
require 'chef/util/dsc/local_configuration_manager'
require 'chef/mixin/powershell_type_coercions'
require 'chef/util/dsc/resource_store'
class Chef
class Provider
class DscResource < Chef::Provider
include Chef::Mixin::PowershellTypeCoercions
provides :dsc_resource, os: "windows"
def initialize(new_resource, run_context)
super
@new_resource = new_resource
@resource_converged = false
@module_name = new_resource.module_name
end
def action_run
if ! @resource_converged
converge_by(generate_description) do
result = set_resource
end
end
end
def load_current_resource
@resource_converged = test_resource
end
def whyrun_supported?
true
end
def define_resource_requirements
requirements.assert(:run) do |a|
a.assertion { supports_dsc_invoke_resource? }
err = "Assuming a previous resource installs Powershell 5.0.10018.0 or higher."
a.failure_message Chef::Exceptions::NoProviderAvailable,
err
a.whyrun err
a.block_action!
end
requirements.assert(:run) do |a|
a.assertion {
meta_configuration['RefreshMode'] == 'Disabled'
}
err = ["The LCM must have its RefreshMode set to Disabled. "]
a.failure_message Chef::Exceptions::NoProviderAvailable, err.join(' ')
a.whyrun err + ["Assuming a previous resource sets the RefreshMode."]
a.block_action!
end
end
protected
def local_configuration_manager
@local_configuration_manager ||= Chef::Util::DSC::LocalConfigurationManager.new(
node,
nil
)
end
def resource_store
Chef::Util::DSC::ResourceStore.instance
end
def supports_dsc_invoke_resource?
run_context && Chef::Platform.supports_dsc_invoke_resource?(node)
end
def generate_description
"Converge dsc resource"
end
def module_name
@module_name ||= begin
r = resource_store.resource(@new_resource.resource.to_s)
if r['Module']
r['Module']['Name']
else
:none
end
end
end
def test_resource
result = invoke_resource(:test)
result.return_value[0]["InDesiredState"]
end
def set_resource
result = invoke_resource(:set)
result.return_value
end
def invoke_resource(method, output_format=:object)
properties = translate_type(@new_resource.properties)
switches = "-Method #{method.to_s} -Name #{@new_resource.resource} -Property #{properties}"
if module_name != :none
switches += " -Module #{module_name}"
end
cmdlet = Chef::Util::Powershell::Cmdlet.new(
node,
"Invoke-DscResource #{switches}",
output_format
)
cmdlet.run!
end
def meta_configuration
cmdlet = Chef::Util::Powershell::Cmdlet.new(node, "Get-DscLocalConfigurationManager", :object)
result = cmdlet.run!
result.return_value
end
end
end
end
|