From 8b6fccee92d29316d8b545fd19280ff53725ceb9 Mon Sep 17 00:00:00 2001 From: Christoph Hartmann Date: Sun, 20 Sep 2015 17:42:09 +0200 Subject: [PATCH] implement windows_feature resource --- lib/resources/windows_feature.rb | 75 ++++++++++++++++++++++++++++++++ lib/vulcano/resource.rb | 1 + 2 files changed, 76 insertions(+) create mode 100644 lib/resources/windows_feature.rb diff --git a/lib/resources/windows_feature.rb b/lib/resources/windows_feature.rb new file mode 100644 index 000000000..a96fa3b84 --- /dev/null +++ b/lib/resources/windows_feature.rb @@ -0,0 +1,75 @@ +# encoding: utf-8 + +# check for a Windows feature +# Usage: +# describe windows_feature('DHCP Server') do +# it{ should be_installed } +# end +# +# deprecated serverspec syntax: +# describe windows_feature('IIS-Webserver') do +# it{ should be_installed.by("dism") } +# end +# +# describe windows_feature('Web-Webserver') do +# it{ should be_installed.by("powershell") } +# end +# +# This implementation uses the Get-WindowsFeature commandlet: +# Get-WindowsFeature | Where-Object {$_.Name -eq 'XPS Viewer' -or $_.DisplayName -eq 'XPS Viewe +# r'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json +# { +# "Name": "XPS-Viewer", +# "DisplayName": "XPS Viewer", +# "Description": "The XPS Viewer is used to read, set permissions for, and digitally sign XPS documents.", +# "Installed": false, +# "InstallState": 0 +# } +class WindowsFeature < Vulcano.resource(1) + name 'windows_feature' + + def initialize(feature) + @feature = feature + @cache = nil + + # verify that this resource is only supported on Windows + return skip_resource 'The `windows_feature` resource is not supported on your OS.' if vulcano.os[:family] != 'windows' + end + + # returns true if the package is installed + def installed?(_provider = nil, _version = nil) + info[:installed] == true + end + + # returns the package description + def info + return @cache if !@cache.nil? + features_cmd = "Get-WindowsFeature | Where-Object {$_.Name -eq '#{@feature}' -or $_.DisplayName -eq '#{@feature}'} | Select-Object -Property Name,DisplayName,Description,Installed,InstallState | ConvertTo-Json" + cmd = vulcano.run_command(features_cmd) + + @cache = { + name: @feature, + type: 'windows-feature', + } + + # cannot rely on exit code for now, successful command returns exit code 1 + # return nil if cmd.exit_status != 0 + # try to parse json + begin + params = JSON.parse(cmd.stdout) + rescue JSON::ParserError => _e + return @cache + end + + @cache = { + name: params['Name'], + description: params['Description'], + installed: params['Installed'], + type: 'windows-feature', + } + end + + def to_s + "Windows feature '#{@feature}'" + end +end diff --git a/lib/vulcano/resource.rb b/lib/vulcano/resource.rb index 10626929a..634b0616c 100644 --- a/lib/vulcano/resource.rb +++ b/lib/vulcano/resource.rb @@ -52,4 +52,5 @@ require 'resources/registry_key' require 'resources/security_policy' require 'resources/service' require 'resources/ssh_conf' +require 'resources/windows_feature' require 'resources/yum'