forked from voxpupuli/puppet-windowsfeature
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request voxpupuli#47 from petems/type_provider_with_specs
Type and provider with specs
- Loading branch information
Showing
17 changed files
with
11,199 additions
and
355 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,4 @@ | ||
fixtures: | ||
repositories: | ||
stdlib: https://github.com/puppetlabs/puppetlabs-stdlib | ||
powershell: https://github.com/joshcooper/puppetlabs-powershell | ||
symlinks: | ||
windowsfeature: "#{source_dir}" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
require 'json' | ||
Puppet::Type.type(:windowsfeature).provide(:default) do | ||
# We don't support 1.8.7 officially, but lets be nice and not cause errors | ||
# rubocop:disable Style/HashSyntax | ||
|
||
commands :ps => | ||
if File.exist?("#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe") | ||
"#{ENV['SYSTEMROOT']}\\sysnative\\WindowsPowershell\\v1.0\\powershell.exe" | ||
elsif File.exist?("#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe") | ||
"#{ENV['SYSTEMROOT']}\\system32\\WindowsPowershell\\v1.0\\powershell.exe" | ||
else | ||
'powershell.exe' | ||
end | ||
|
||
confine :kernel => :windows | ||
|
||
def self.instances | ||
features = JSON.parse(ps('Get-WindowsFeature | ConvertTo-JSON')) | ||
features.collect do |feature| | ||
name = feature['Name'].downcase | ||
installed = feature['InstallState'] | ||
if installed == 1 | ||
currentstate = :present | ||
elsif installed == 0 | ||
currentstate = :absent | ||
end | ||
new(:name => name, :ensure => currentstate) | ||
end | ||
end | ||
|
||
def self.prefetch(resources) | ||
features = instances | ||
resources.keys.each do |name| | ||
if provider = features.find { |feature| feature.name == name.downcase } # rubocop:disable Lint/AssignmentInCondition | ||
resources[name].provider = provider | ||
end | ||
end | ||
end | ||
|
||
def exists? | ||
@property_hash[:ensure] == :present | ||
end | ||
|
||
def create | ||
install_cmd = case Facter.value(:kernelmajversion) | ||
when /6.1/ | ||
'Import-Module ServerManager; Add-WindowsFeature' | ||
else | ||
'Install-WindowsFeature' | ||
end | ||
|
||
# Options currently not used, timeout should be added in the future | ||
options = [] | ||
|
||
if resource['installmanagementtools'] == true | ||
case Facter.value(:kernelmajversion) | ||
when /6.1/ | ||
raise Puppet::Error, 'installmanagementtools can only be used with Windows 2012 and above' | ||
when /6.2|6.3|10/ | ||
options.push('-IncludeManagementTools') | ||
end | ||
end | ||
if resource['installsubfeatures'] == true | ||
options.push('-IncludeAllSubFeature') | ||
end | ||
options.push('-Restart') if resource['restart'] == true | ||
options.push("-Source #{resource['source']}") unless resource['source'].nil? | ||
|
||
if options.empty? | ||
ps(install_cmd, resource['name']) | ||
else | ||
psopts = options.join(' ') | ||
ps(install_cmd, resource['name'], psopts) | ||
end | ||
end | ||
|
||
def destroy | ||
uninstall_cmd = case Facter.value(:kernelmajversion) | ||
when /6.1/ | ||
'Import-Module ServerManager; Remove-WindowsFeature' | ||
else | ||
'Remove-WindowsFeature' | ||
end | ||
|
||
uninstall_options = [] | ||
|
||
uninstall_options.push('-Restart') if resource['restart'] == true | ||
|
||
if uninstall_options.empty? | ||
ps(uninstall_cmd, resource['name']) | ||
else | ||
psopts = uninstall_options.join(' ') | ||
ps(uninstall_cmd, resource['name'], psopts) | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
Puppet::Type.newtype(:windowsfeature) do | ||
ensurable | ||
|
||
newparam(:name) do | ||
isnamevar | ||
end | ||
|
||
newparam(:installmanagementtools) do | ||
validate do |value| | ||
unless value == true || value == false | ||
raise Puppet::Error, 'Parameter installmanagementtools must be true or false' | ||
end | ||
end | ||
end | ||
|
||
newparam(:installsubfeatures) do | ||
# validate is bool | ||
validate do |value| | ||
unless value == true || value == false | ||
raise Puppet::Error, 'Parameter installmanagementtools must be true or false' | ||
end | ||
end | ||
end | ||
|
||
newparam(:restart) do | ||
validate do |value| | ||
unless value == true || value == false | ||
raise Puppet::Error, 'Parameter installmanagementtools must be true or false' | ||
end | ||
end | ||
end | ||
|
||
newparam(:source) do | ||
# validate is tring or false | ||
validate do |value| | ||
unless value.is_a?(String) | ||
raise Puppet::Error, 'Parameter source is not a string.' | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.