summaryrefslogtreecommitdiff
path: root/lib/chef/resource
diff options
context:
space:
mode:
authorGary Ewan Park <gep13@gep13.co.uk>2019-05-23 12:22:59 -0700
committerGary Ewan Park <gep13@gep13.co.uk>2019-05-27 21:13:32 +0100
commit71fe79cae89a57b37f6750466e96f49eedc9097b (patch)
tree124abcd346170cd49b38b5303045288fe466b8ad /lib/chef/resource
parent819307636de80266c6eb7e2072839e47f2dd4dbe (diff)
downloadchef-71fe79cae89a57b37f6750466e96f49eedc9097b.tar.gz
(GH-8580) Add ability to toggle Chocolatey feature
Add the ability to enable and disable a named Chocolatey feature by creating a new Chef resource named chocolatey_feature. This accepts a single property either from feature_name or directly from name attribute then, based on the defined action, will either enable or disable that feature. Resource inspects the current state of the chocolatey.config in order to decide whether a change is required or not. Signed-off-by: Gary Ewan Park <gep13@gep13.co.uk>
Diffstat (limited to 'lib/chef/resource')
-rw-r--r--lib/chef/resource/chocolatey_feature.rb80
1 files changed, 80 insertions, 0 deletions
diff --git a/lib/chef/resource/chocolatey_feature.rb b/lib/chef/resource/chocolatey_feature.rb
new file mode 100644
index 0000000000..1c190905f3
--- /dev/null
+++ b/lib/chef/resource/chocolatey_feature.rb
@@ -0,0 +1,80 @@
+#
+# Copyright:: 2019, 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.
+#
+
+class Chef
+ class Resource
+ class ChocolateyFeature < Chef::Resource
+ resource_name :chocolatey_feature
+
+ description "Use the chocolatey_feature resource to enable and disable Chocolatey features."
+ introduced "15.1"
+
+ property :feature_name, String, name_property: true,
+ description: "The name of the Chocolatey feature to enable or disable."
+
+ property :feature_state, [TrueClass, FalseClass], default: false
+
+ load_current_value do
+ current_state = fetch_feature_element(feature_name)
+ current_value_does_not_exist! if current_state.nil?
+
+ feature_name feature_name
+ feature_state current_state == "true"
+ end
+
+ # @param [String] id the feature name
+ # @return [String] the element's value field
+ def fetch_feature_element(name)
+ require "rexml/document" unless defined?(REXML::Document)
+ config_file = "#{ENV['ALLUSERSPROFILE']}\\chocolatey\\config\\chocolatey.config"
+ raise "Could not find the Chocolatey config at #{config_file}!" unless ::File.exist?(config_file)
+
+ contents = REXML::Document.new(::File.read(config_file))
+ data = REXML::XPath.first(contents, "//features/feature[@name=\"#{name}\"]")
+ data ? data.attribute("enabled").to_s : nil # REXML just returns nil if it can't find anything so avoid an undefined method error
+ end
+
+ action :enable do
+ description "Enables a named Chocolatey feature."
+
+ if current_resource.feature_state != true
+ converge_by("enable Chocolatey feature '#{new_resource.feature_name}'") do
+ shell_out!(choco_cmd("enable"))
+ end
+ end
+ end
+
+ action :disable do
+ description "Disables a named Chocolatey feature."
+
+ if current_resource.feature_state == true
+ converge_by("disable Chocolatey feature '#{new_resource.feature_name}'") do
+ shell_out!(choco_cmd("disable"))
+ end
+ end
+ end
+
+ action_class do
+ # @param [String] action the name of the action to perform
+ # @return [String] the choco feature command string
+ def choco_cmd(action)
+ cmd = "#{ENV['ALLUSERSPROFILE']}\\chocolatey\\bin\\choco feature #{action} --name #{new_resource.feature_name}"
+ cmd
+ end
+ end
+ end
+ end
+end