From 4ab3089e8f8d9607195764e4ab1cb617604d2004 Mon Sep 17 00:00:00 2001 From: Lightning Date: Wed, 30 Aug 2023 18:09:20 +0200 Subject: [PATCH 01/18] rework shell/powershell commands and codes --- manifests/devices/override.pp | 2 +- manifests/devices/wake.pp | 2 +- manifests/global/battery.pp | 2 +- manifests/global/flags.pp | 2 +- manifests/global/hibernation.pp | 2 +- manifests/params.pp | 4 +--- manifests/schemes/scheme.pp | 18 ++++++++---------- manifests/schemes/settings.pp | 4 ++-- 8 files changed, 16 insertions(+), 20 deletions(-) diff --git a/manifests/devices/override.pp b/manifests/devices/override.pp index 9a4de2f..ad7a2a5 100644 --- a/manifests/devices/override.pp +++ b/manifests/devices/override.pp @@ -33,7 +33,7 @@ } default: { exec { "request override for ${name}": - command => "${windows_power::params::powercfg} /requestsoverride ${type} ${name} ${request}", + command => "powercfg /requestsoverride ${type} ${name} ${request}", provider => windows, } } diff --git a/manifests/devices/wake.pp b/manifests/devices/wake.pp index eeb9c3f..716fb50 100644 --- a/manifests/devices/wake.pp +++ b/manifests/devices/wake.pp @@ -28,7 +28,7 @@ include windows_power::params exec { "device ${device} ${ensure} wake": - command => "${windows_power::params::powercfg} /device${ensure}wake \"${device}\"", + command => "powercfg /device${ensure}wake \"${device}\"", provider => windows, } } diff --git a/manifests/global/battery.pp b/manifests/global/battery.pp index 3ee9ed9..f819a30 100644 --- a/manifests/global/battery.pp +++ b/manifests/global/battery.pp @@ -42,7 +42,7 @@ case $facts['operatingsystemversion'] { 'Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2': { exec { "set batteryalarm ${setting}": - command => "${windows_power::params::powercfg} /batteryalarm ${criticality} /${setting} ${status}", + command => "powercfg /batteryalarm ${criticality} /${setting} ${status}", provider => windows, } } diff --git a/manifests/global/flags.pp b/manifests/global/flags.pp index 4ecf5f4..6509451 100644 --- a/manifests/global/flags.pp +++ b/manifests/global/flags.pp @@ -34,7 +34,7 @@ case $facts['operatingsystemversion'] { 'Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2': { exec { "set globalpowerflag ${setting}": - command => "${windows_power::params::powercfg} /globalpowerflag /option:${setting} ${status}", + command => "powercfg /globalpowerflag /option:${setting} ${status}", provider => windows, } } diff --git a/manifests/global/hibernation.pp b/manifests/global/hibernation.pp index 2cf56ec..e6ce72e 100644 --- a/manifests/global/hibernation.pp +++ b/manifests/global/hibernation.pp @@ -23,7 +23,7 @@ include windows_power::params exec { 'update hibernate status': - command => "${windows_power::params::powercfg} -hibernate ${status}", + command => "powercfg /hibernate ${status}", provider => windows, } } diff --git a/manifests/params.pp b/manifests/params.pp index 7863adc..e89a039 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -39,7 +39,5 @@ $globalpower_flags = ['BatteryIcon','MultiBattery','ResumePassword','WakeOnRing','VideoDim'] - $powercfg = 'C:\Windows\System32\powercfg.exe' - - $nasty_ps = '$items = [System.Collections.ArrayList]@(powercfg -l | %{ $a = [System.Collections.ArrayList]$_.Split(\'(\'); $a.RemoveAt(0); if( (![string]::IsNullOrEmpty($a[0])) -and (!$a[0].contains(\'* Active\')) ) { $b = $a[0].Split(\')\')[0]; $b } });' + $nasty_ps = '$items = [System.Collections.ArrayList]@(powercfg /l | % { if ($_ -match "^.*GUID.*\((.*)\).*$") {$matches[1]} });' } diff --git a/manifests/schemes/scheme.pp b/manifests/schemes/scheme.pp index 2ed46d0..e646efd 100644 --- a/manifests/schemes/scheme.pp +++ b/manifests/schemes/scheme.pp @@ -42,28 +42,26 @@ ) { include windows_power::params - $scheme_check = "${windows_power::params::nasty_ps} \$items.contains('${scheme_name}')" - if $ensure == 'present' { case $facts['operatingsystemversion'] { 'Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2': { exec { "create power scheme ${scheme_name}": - command => "& ${windows_power::params::powercfg} /create '${scheme_name}'", + command => "& powercfg /create '${scheme_name}'", provider => powershell, logoutput => true, - onlyif => $scheme_check, + onlyif => "${windows_power::params::nasty_ps} \$items.contains('${scheme_name}')", } } default: { exec { "create power scheme ${scheme_name}": - command => "& ${windows_power::params::powercfg} -duplicatescheme ${template_scheme} ${scheme_guid}", + command => "& powercfg /duplicatescheme ${template_scheme} ${scheme_guid}", provider => powershell, logoutput => true, - unless => "${windows_power::params::powercfg} /query ${scheme_guid}", + unless => "powercfg /query ${scheme_guid}", } exec { "rename scheme to ${scheme_name}": - command => "& ${windows_power::params::powercfg} -changename ${scheme_guid} '${scheme_name}'", + command => "& powercfg /changename ${scheme_guid} '${scheme_name}'", provider => powershell, logoutput => true, onlyif => "if (Get-CimInstance -Namespace root\\cimv2\\power -ClassName win32_powerplan -Filter \"ElementName = '${scheme_name}'\" | Where -property InstanceID -eq 'Microsoft:PowerPlan\\{${scheme_guid}}') { exit 1 } else { exit 0 }", @@ -74,16 +72,16 @@ } elsif $ensure == 'absent' { exec { "delete power scheme ${scheme_name}": - command => "& ${windows_power::params::powercfg} -delete ${scheme_guid}", + command => "& powercfg /delete ${scheme_guid}", provider => powershell, logoutput => true, - onlyif => "${windows_power::params::powercfg} /query ${scheme_guid}", + onlyif => "powercfg /query ${scheme_guid}", } } if $activation == 'active' { exec { "set ${scheme_name} scheme as active": - command => "& ${windows_power::params::powercfg} -setactive ${scheme_guid}", + command => "& powercfg /setactive ${scheme_guid}", provider => powershell, logoutput => true, onlyif => "if (Get-CimInstance -Namespace root\\cimv2\\power -ClassName win32_powerplan -Filter \"IsActive = True\" | Where -property InstanceID -eq 'Microsoft:PowerPlan\\{${scheme_guid}}') { exit 1 }", diff --git a/manifests/schemes/settings.pp b/manifests/schemes/settings.pp index 4664bbd..c4dc4b7 100644 --- a/manifests/schemes/settings.pp +++ b/manifests/schemes/settings.pp @@ -43,9 +43,9 @@ } exec { "modify ${setting} setting for ${scheme_name}": - command => "& ${windows_power::params::powercfg} /change ${setting} ${value}", + command => "& powercfg /change ${setting} ${value}", provider => powershell, logoutput => true, - unless => "${windows_power::params::nasty_ps} \$items.contains(${scheme_name})", + unless => "${windows_power::params::nasty_ps} \$items.contains('${scheme_name}')", } } From 0327b647b5f60a384ed205fc24f8f6a418bf6fd6 Mon Sep 17 00:00:00 2001 From: Lightning Date: Wed, 30 Aug 2023 18:36:01 +0200 Subject: [PATCH 02/18] drop individual license information from manifest files --- manifests/devices/override.pp | 4 ---- manifests/devices/wake.pp | 4 ---- manifests/global/battery.pp | 6 +----- manifests/global/flags.pp | 4 ---- manifests/global/hibernation.pp | 4 ---- manifests/init.pp | 4 ---- manifests/params.pp | 4 ---- manifests/schemes/scheme.pp | 4 ---- manifests/schemes/settings.pp | 4 ---- 9 files changed, 1 insertion(+), 37 deletions(-) diff --git a/manifests/devices/override.pp b/manifests/devices/override.pp index ad7a2a5..abfd421 100644 --- a/manifests/devices/override.pp +++ b/manifests/devices/override.pp @@ -1,7 +1,3 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.0 - # == Define: windows_power::devices::override # # This definition manages a Power Request override for a particular Process, Service, or Driver. diff --git a/manifests/devices/wake.pp b/manifests/devices/wake.pp index 716fb50..966a7b2 100644 --- a/manifests/devices/wake.pp +++ b/manifests/devices/wake.pp @@ -1,7 +1,3 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.0 - # == Define: windows_power::devices::wake # # This definition enables/disables the device to wake the computer from a sleep state diff --git a/manifests/global/battery.pp b/manifests/global/battery.pp index f819a30..7629d57 100644 --- a/manifests/global/battery.pp +++ b/manifests/global/battery.pp @@ -1,8 +1,4 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.0 - -# Define windows_power::global::battery +# == Define windows_power::global::battery # # This definition configured the battery alarm # diff --git a/manifests/global/flags.pp b/manifests/global/flags.pp index 6509451..236c09a 100644 --- a/manifests/global/flags.pp +++ b/manifests/global/flags.pp @@ -1,7 +1,3 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.0 - # == Define: windows_power::global::flags # # This definition configured the battery alarm diff --git a/manifests/global/hibernation.pp b/manifests/global/hibernation.pp index e6ce72e..4821f27 100644 --- a/manifests/global/hibernation.pp +++ b/manifests/global/hibernation.pp @@ -1,7 +1,3 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.0 - # == Define: windows_power::global::hibernation # # This definition configures hibernation on a box diff --git a/manifests/init.pp b/manifests/init.pp index 4ff1989..b097f0c 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,7 +1,3 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.0 - # == Class: windows_power # # Module to mananage the configuration of a machines autoupdate settings diff --git a/manifests/params.pp b/manifests/params.pp index e89a039..abf8757 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -1,7 +1,3 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.0 - # == Class windows_power::params # # This private class is meant to be called from `windows_power` diff --git a/manifests/schemes/scheme.pp b/manifests/schemes/scheme.pp index e646efd..0bf4005 100644 --- a/manifests/schemes/scheme.pp +++ b/manifests/schemes/scheme.pp @@ -1,7 +1,3 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.0 - # == Define: windows_power::schemes::scheme # # This definition configures a specific power scheme diff --git a/manifests/schemes/settings.pp b/manifests/schemes/settings.pp index c4dc4b7..a3e7000 100644 --- a/manifests/schemes/settings.pp +++ b/manifests/schemes/settings.pp @@ -1,7 +1,3 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.0 - # == Define: windows_power::schemes::settings # # This definition configures settings for a specific scheme From e917871f4078c28691cfbb692b59ee081ac916d4 Mon Sep 17 00:00:00 2001 From: Lightning Date: Fri, 1 Sep 2023 14:26:10 +0200 Subject: [PATCH 03/18] rework operating system support this: - drops support for legacy windows systems - removes functionality targeting those legacy systems - simplifies module structure accordingly - corrects datatypes and values to match up-to-date systems - reworks tests adequately - drops dependency to liamjbennett/puppet-win_facts as it's not needed anymore - updates dependencies/requirements --- .fixtures.yml | 1 - manifests/devices/override.pp | 13 +- manifests/devices/wake.pp | 2 +- manifests/global/battery.pp | 47 ------- manifests/global/flags.pp | 39 ------ manifests/global/hibernation.pp | 2 +- manifests/params.pp | 31 ----- manifests/schemes/scheme.pp | 58 ++++---- manifests/schemes/settings.pp | 25 ++-- metadata.json | 19 +-- spec/defines/devices/override_spec.rb | 46 ++----- spec/defines/devices/wake_spec.rb | 23 ++-- spec/defines/global/battery_spec.rb | 63 --------- spec/defines/global/flags_spec.rb | 50 ------- spec/defines/global/hibernation_spec.rb | 10 +- spec/defines/schemes/scheme_spec.rb | 176 +++++++++--------------- spec/defines/schemes/settings_spec.rb | 55 ++------ 17 files changed, 157 insertions(+), 503 deletions(-) delete mode 100644 manifests/global/battery.pp delete mode 100644 manifests/global/flags.pp delete mode 100644 spec/defines/global/battery_spec.rb delete mode 100644 spec/defines/global/flags_spec.rb diff --git a/.fixtures.yml b/.fixtures.yml index ba113cc..6b4479d 100644 --- a/.fixtures.yml +++ b/.fixtures.yml @@ -2,4 +2,3 @@ fixtures: repositories: powershell: 'https://github.com/puppetlabs/puppetlabs-powershell.git' pwshlib: 'https://github.com/puppetlabs/ruby-pwsh.git' - win_facts: 'https://github.com/liamjbennett/puppet-win_facts.git' diff --git a/manifests/devices/override.pp b/manifests/devices/override.pp index abfd421..2d7c358 100644 --- a/manifests/devices/override.pp +++ b/manifests/devices/override.pp @@ -23,15 +23,8 @@ ) { include windows_power::params - case $facts['operatingsystemversion'] { - 'Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2': { - err("${facts['operatingsystemversion']} does not support requestsoverride") - } - default: { - exec { "request override for ${name}": - command => "powercfg /requestsoverride ${type} ${name} ${request}", - provider => windows, - } - } + exec { "request override for ${name}": + provider => windows, + command => "powercfg /requestsoverride ${type} ${name} ${request}", } } diff --git a/manifests/devices/wake.pp b/manifests/devices/wake.pp index 966a7b2..e5244e6 100644 --- a/manifests/devices/wake.pp +++ b/manifests/devices/wake.pp @@ -24,7 +24,7 @@ include windows_power::params exec { "device ${device} ${ensure} wake": - command => "powercfg /device${ensure}wake \"${device}\"", provider => windows, + command => "powercfg /device${ensure}wake \"${device}\"", } } diff --git a/manifests/global/battery.pp b/manifests/global/battery.pp deleted file mode 100644 index 7629d57..0000000 --- a/manifests/global/battery.pp +++ /dev/null @@ -1,47 +0,0 @@ -# == Define windows_power::global::battery -# -# This definition configured the battery alarm -# -# === Parameters -# -# [*setting*] -# Battery alarm setting to configure -# -# [*status*] -# Setting configuration -# -# [*criticality*] -# The level of battery criticality at which to provide an alarm. LOW or HIGH. -# -# === Examples -# -# windows_power::global::battery { 'activate battery alarm': -# setting => 'activate', -# status => 'on', -# } -# -define windows_power::global::battery ( - String[1] $setting, - String[1] $status, - Enum['LOW', 'HIGH'] $criticality = 'LOW', -) { - include windows_power::params - - if ! ($setting in $windows_power::params::batteryalarm_settings) { - fail('The setting argument does not match a valid batteryalarm setting') - } - - if $status !~ $windows_power::params::batteryalarm_settings[$setting] { - fail("The status argument is not valid for ${setting}") - } - - case $facts['operatingsystemversion'] { - 'Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2': { - exec { "set batteryalarm ${setting}": - command => "powercfg /batteryalarm ${criticality} /${setting} ${status}", - provider => windows, - } - } - default: {} - } -} diff --git a/manifests/global/flags.pp b/manifests/global/flags.pp deleted file mode 100644 index 236c09a..0000000 --- a/manifests/global/flags.pp +++ /dev/null @@ -1,39 +0,0 @@ -# == Define: windows_power::global::flags -# -# This definition configured the battery alarm -# -# === Parameters -# -# [*setting*] -# The global power flag to configure -# -# [*status*] -# Setting configuration (on/off) -# -# === Examples -# -# windows_power::global::flags { 'show battery icon': -# setting => 'BatteryIcon', -# status => 'on', -# } -# -define windows_power::global::flags ( - String[1] $setting, - Enum['on', 'off'] $status, -) { - include windows_power::params - - if ! ($setting in $windows_power::params::globalpower_flags) { - fail('The setting argument does not match a valid globalpower flag') - } - - case $facts['operatingsystemversion'] { - 'Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2': { - exec { "set globalpowerflag ${setting}": - command => "powercfg /globalpowerflag /option:${setting} ${status}", - provider => windows, - } - } - default: {} - } -} diff --git a/manifests/global/hibernation.pp b/manifests/global/hibernation.pp index 4821f27..797039b 100644 --- a/manifests/global/hibernation.pp +++ b/manifests/global/hibernation.pp @@ -19,7 +19,7 @@ include windows_power::params exec { 'update hibernate status': - command => "powercfg /hibernate ${status}", provider => windows, + command => "powercfg /hibernate ${status}", } } diff --git a/manifests/params.pp b/manifests/params.pp index abf8757..7f7de81 100644 --- a/manifests/params.pp +++ b/manifests/params.pp @@ -4,36 +4,5 @@ # It sets variables according to platform # class windows_power::params { - $template_schemes = { - 'Balanced' => '381b4222-f694-41f0-9685-ff5bb260df2e', - 'High Performance' => '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', - 'Power Saver' => 'a1841308-3541-4fab-bc81-f71556f20b4a', - } - - $batteryalarm_settings = { - 'activate' => '^(on|off)$', - 'level' => '^(0?[0-9]{1,2}0?0?)$', - 'text' => '^(on|off)$', - 'sound' => '^(on|off)$', - 'action' => '^(none|shutdown|hibernate|standby)$', - 'forceaction' => '^(on|off)$', - 'program' => '^(on|off)$', - } - - $scheme_settings = { - 'monitor-timeout-ac' => '^(0?[0-9]{1,4}0?0?)$', - 'monitor-timeout-dc' => '^(0?[0-9]{1,4}0?0?)$', - 'disk-timeout-ac' => '^(0?[0-9]{1,4}0?0?)$', - 'disk-timeout-dc' => '^(0?[0-9]{1,4}0?0?)$', - 'standby-timeout-ac' => '^(0?[0-9]{1,4}0?0?)$', - 'standby-timeout-dc' => '^(0?[0-9]{1,4}0?0?)$', - 'hibernate-timeout-ac' => '^(0?[0-9]{1,4}0?0?)$', - 'hibernate-timeout-dc' => '^(0?[0-9]{1,4}0?0?)$', - 'processor-throttle-ac' => '^(NONE|CONSTANT|DEGRADE|ADAPTIVE)$', - 'processor-throttle-dc' => '^(NONE|CONSTANT|DEGRADE|ADAPTIVE)$', - } - - $globalpower_flags = ['BatteryIcon','MultiBattery','ResumePassword','WakeOnRing','VideoDim'] - $nasty_ps = '$items = [System.Collections.ArrayList]@(powercfg /l | % { if ($_ -match "^.*GUID.*\((.*)\).*$") {$matches[1]} });' } diff --git a/manifests/schemes/scheme.pp b/manifests/schemes/scheme.pp index 0bf4005..86cbfa4 100644 --- a/manifests/schemes/scheme.pp +++ b/manifests/schemes/scheme.pp @@ -31,56 +31,44 @@ # define windows_power::schemes::scheme ( String[1] $scheme_name, - Pattern[/\A[A-Za-z0-9]{8}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{4}-[A-Za-z0-9]{12}\z/] $scheme_guid, - String[1] $template_scheme, + Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/] $scheme_guid, + Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/] $template_scheme, Enum['active', 'inactive'] $activation, Enum['present', 'absent'] $ensure = 'present', ) { include windows_power::params if $ensure == 'present' { - case $facts['operatingsystemversion'] { - 'Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2': { - exec { "create power scheme ${scheme_name}": - command => "& powercfg /create '${scheme_name}'", - provider => powershell, - logoutput => true, - onlyif => "${windows_power::params::nasty_ps} \$items.contains('${scheme_name}')", - } - } - default: { - exec { "create power scheme ${scheme_name}": - command => "& powercfg /duplicatescheme ${template_scheme} ${scheme_guid}", - provider => powershell, - logoutput => true, - unless => "powercfg /query ${scheme_guid}", - } + exec { "create power scheme ${scheme_name}": + provider => powershell, + command => "& powercfg /duplicatescheme ${template_scheme} ${scheme_guid}", + logoutput => true, + unless => "powercfg /query ${scheme_guid}", + } + + exec { "rename scheme to ${scheme_name}": + provider => powershell, + command => "& powercfg /changename ${scheme_guid} '${scheme_name}'", + logoutput => true, + onlyif => "if (Get-CimInstance -Namespace root\\cimv2\\power -ClassName win32_powerplan -Filter \"ElementName = '${scheme_name}'\" | Where -property InstanceID -eq 'Microsoft:PowerPlan\\{${scheme_guid}}') { exit 1 } else { exit 0 }", + require => Exec["create power scheme ${scheme_name}"], + } - exec { "rename scheme to ${scheme_name}": - command => "& powercfg /changename ${scheme_guid} '${scheme_name}'", - provider => powershell, - logoutput => true, - onlyif => "if (Get-CimInstance -Namespace root\\cimv2\\power -ClassName win32_powerplan -Filter \"ElementName = '${scheme_name}'\" | Where -property InstanceID -eq 'Microsoft:PowerPlan\\{${scheme_guid}}') { exit 1 } else { exit 0 }", - require => Exec["create power scheme ${scheme_name}"], - } + if $activation == 'active' { + exec { "set ${scheme_name} scheme as active": + provider => powershell, + command => "& powercfg /setactive ${scheme_guid}", + logoutput => true, + onlyif => "if (Get-CimInstance -Namespace root\\cimv2\\power -ClassName win32_powerplan -Filter \"IsActive = True\" | Where -property InstanceID -eq 'Microsoft:PowerPlan\\{${scheme_guid}}') { exit 1 }", } } } elsif $ensure == 'absent' { exec { "delete power scheme ${scheme_name}": - command => "& powercfg /delete ${scheme_guid}", provider => powershell, + command => "& powercfg /delete ${scheme_guid}", logoutput => true, onlyif => "powercfg /query ${scheme_guid}", } } - - if $activation == 'active' { - exec { "set ${scheme_name} scheme as active": - command => "& powercfg /setactive ${scheme_guid}", - provider => powershell, - logoutput => true, - onlyif => "if (Get-CimInstance -Namespace root\\cimv2\\power -ClassName win32_powerplan -Filter \"IsActive = True\" | Where -property InstanceID -eq 'Microsoft:PowerPlan\\{${scheme_guid}}') { exit 1 }", - } - } } diff --git a/manifests/schemes/settings.pp b/manifests/schemes/settings.pp index a3e7000..0b5a222 100644 --- a/manifests/schemes/settings.pp +++ b/manifests/schemes/settings.pp @@ -23,24 +23,23 @@ # define windows_power::schemes::settings ( String[1] $scheme_name, - String[1] $setting, - String[1] $value, + Enum[ + 'monitor-timeout-ac', + 'monitor-timeout-dc', + 'disk-timeout-ac', + 'disk-timeout-dc', + 'standby-timeout-ac', + 'standby-timeout-dc', + 'hibernate-timeout-ac', + 'hibernate-timeout-dc' + ] $setting, + Integer[0] $value, ) { include windows_power::params - $settings_regex = join(keys($windows_power::params::scheme_settings), '|') - - if $setting !~ "^${settings_regex}$" { - fail('The setting argument does not match a valid scheme setting') - } - - if $value !~ $windows_power::params::scheme_settings[$setting] { - fail("The value provided is not appropriate for the ${setting} setting") - } - exec { "modify ${setting} setting for ${scheme_name}": - command => "& powercfg /change ${setting} ${value}", provider => powershell, + command => "& powercfg /change ${setting} ${value}", logoutput => true, unless => "${windows_power::params::nasty_ps} \$items.contains('${scheme_name}')", } diff --git a/metadata.json b/metadata.json index ed92498..600345e 100644 --- a/metadata.json +++ b/metadata.json @@ -1,6 +1,6 @@ { "name": "puppet-windows_power", - "version": "3.0.3-rc0", + "version": "4.0.0-rc0", "author": "puppet", "license": "Apache-2.0", "summary": "Module to manage the power settings on Windows", @@ -11,27 +11,30 @@ { "operatingsystem": "windows", "operatingsystemrelease": [ - "2008", - "2008 R2", + "10", + "11", "2012", - "2012 R2" + "2012 R2", + "2016", + "2019", + "2022" ] } ], "dependencies": [ { "name": "puppetlabs/powershell", - "version_requirement": ">= 1.0.5 < 3.0.0" + "version_requirement": ">= 1.0.5 < 7.0.0" }, { - "name": "liamjbennett/win_facts", - "version_requirement": ">= 0.0.2 < 2.0.0" + "name": "puppetlabs/pwshlib", + "version_requirement": ">= 0.4.0 < 2.0.0" } ], "requirements": [ { "name": "puppet", - "version_requirement": ">= 7.0.0 < 8.0.0" + "version_requirement": ">= 7.0.0 < 9.0.0" } ] } diff --git a/spec/defines/devices/override_spec.rb b/spec/defines/devices/override_spec.rb index 8794fb8..23a73aa 100644 --- a/spec/defines/devices/override_spec.rb +++ b/spec/defines/devices/override_spec.rb @@ -3,51 +3,35 @@ require 'spec_helper' describe 'windows_power::devices::override', type: :define do - ['Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2'].each do |os| - describe "requestsoverride not supported on #{os}" do - let(:title) { 'wmplayer.exe' } - let(:facts) do - { operatingsystemversion: os } - end - let(:params) do - { type: 'PROCESS', request: 'Display' } - end - - it { is_expected.not_to contain_exec('request override for wmplayer.exe') } + describe 'requestsoverride of PROCESS' do + let(:title) { 'wmplayer.exe' } + let(:params) do + { type: 'PROCESS', request: 'Display' } end - end - ['Windows Vista', 'Windows 7', 'Windows 8', 'Windows Server 2008', 'Windows Server 2008 R2', 'Windows Server 2012'].each do |os| - describe "requestsoverride supported on #{os}" do - let(:title) { 'wmplayer.exe' } - let(:facts) do - { operatingsystemversion: os } - end - let(:params) do - { type: 'PROCESS', request: 'Display' } - end - - it do - is_expected.to contain_exec('request override for wmplayer.exe').with( - 'command' => 'C:\Windows\System32\powercfg.exe /requestsoverride PROCESS wmplayer.exe Display' - ) - end + it do + is_expected.to contain_exec('request override for wmplayer.exe').with( + 'provider' => 'windows', + 'command' => 'powercfg /requestsoverride PROCESS wmplayer.exe Display' + ) end + + it { is_expected.to compile } end describe 'requestsoverride of SERVICE' do let(:title) { 'MpsSvc' } - let(:facts) do - { operatingsystemversion: 'Windows 8' } - end let(:params) do { type: 'SERVICE', request: 'System' } end it do is_expected.to contain_exec('request override for MpsSvc').with( - 'command' => 'C:\Windows\System32\powercfg.exe /requestsoverride SERVICE MpsSvc System' + 'provider' => 'windows', + 'command' => 'powercfg /requestsoverride SERVICE MpsSvc System' ) end + + it { is_expected.to compile } end end diff --git a/spec/defines/devices/wake_spec.rb b/spec/defines/devices/wake_spec.rb index aa65581..634bda5 100644 --- a/spec/defines/devices/wake_spec.rb +++ b/spec/defines/devices/wake_spec.rb @@ -3,19 +3,6 @@ require 'spec_helper' describe 'windows_power::devices::wake', type: :define do - describe 'enabling wake with invalid device' do - let(:title) { 'network-device' } - let(:params) do - { device: true, ensure: 'enable' } - end - - it do - expect do - is_expected.to contain_exec('device network-device enable wake') - end.to raise_error(Puppet::Error) - end - end - describe 'enabling wake' do let(:title) { 'VMBus Enumerator (001)' } let(:params) do @@ -24,9 +11,12 @@ it do is_expected.to contain_exec('device VMBus Enumerator (001) enable wake').with( - 'command' => 'C:\Windows\System32\powercfg.exe /deviceenablewake "VMBus Enumerator (001)"' + 'provider' => 'windows', + 'command' => 'powercfg /deviceenablewake "VMBus Enumerator (001)"' ) end + + it { is_expected.to compile } end describe 'disabling wake' do @@ -37,8 +27,11 @@ it do is_expected.to contain_exec('device VMBus Enumerator (001) disable wake').with( - 'command' => 'C:\Windows\System32\powercfg.exe /devicedisablewake "VMBus Enumerator (001)"' + 'provider' => 'windows', + 'command' => 'powercfg /devicedisablewake "VMBus Enumerator (001)"' ) end + + it { is_expected.to compile } end end diff --git a/spec/defines/global/battery_spec.rb b/spec/defines/global/battery_spec.rb deleted file mode 100644 index 96c29c4..0000000 --- a/spec/defines/global/battery_spec.rb +++ /dev/null @@ -1,63 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'windows_power::global::battery', type: :define do - describe 'installing with invalid setting' do - let(:title) { 'sound off' } - let(:params) do - { setting: 'xxx', status: 'on' } - end - - it do - expect do - is_expected.to contain_exec('set batteryalarm xxx') - end.to raise_error(Puppet::Error, %r{The setting argument does not match a valid batteryalarm setting}) - end - end - - describe 'installing with invalid status' do - let(:title) { 'sound off' } - let(:params) do - { setting: 'sound', status: 'xxx' } - end - - it do - expect do - is_expected.to contain_exec('set batteryalarm sound') - end.to raise_error(Puppet::Error, %r{The status argument is not valid for sound}) - end - end - - ['Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2'].each do |os| - describe 'installing setting sound' do - let(:title) { 'sound' } - let(:facts) do - { operatingsystemversion: os } - end - let(:params) do - { setting: 'sound', status: 'on' } - end - - it do - is_expected.to contain_exec('set batteryalarm sound').with( - 'command' => 'C:\Windows\System32\powercfg.exe /batteryalarm LOW /sound on' - ) - end - end - end - - ['Windows Vista', 'Windows 7', 'Windows 8', 'Windows Server 2008', 'Windows Server 2008 R2', 'Windows Server 2012'].each do |os| - describe 'installing setting sound' do - let(:title) { 'sound' } - let(:facts) do - { operatingsystemversion: os } - end - let(:params) do - { setting: 'sound', status: 'on' } - end - - it { is_expected.not_to contain_exec('set batteryalarm sound') } - end - end -end diff --git a/spec/defines/global/flags_spec.rb b/spec/defines/global/flags_spec.rb deleted file mode 100644 index 2aad7f6..0000000 --- a/spec/defines/global/flags_spec.rb +++ /dev/null @@ -1,50 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'windows_power::global::flags', type: :define do - describe 'installing with invalid setting' do - let(:title) { 'BatteryIcon' } - let(:params) do - { setting: 'xxx', status: 'on' } - end - - it do - expect do - is_expected.to contain_exec('set globalpowerflag xxx') - end.to raise_error(Puppet::Error, %r{The setting argument does not match a valid globalpower flag}) - end - end - - ['Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2'].each do |os| - describe 'installing setting BatteryIcon' do - let(:title) { 'BatteryIcon' } - let(:facts) do - { operatingsystemversion: os } - end - let(:params) do - { setting: 'BatteryIcon', status: 'on' } - end - - it do - is_expected.to contain_exec('set globalpowerflag BatteryIcon').with( - 'command' => 'C:\Windows\System32\powercfg.exe /globalpowerflag /option:BatteryIcon on' - ) - end - end - end - - ['Windows Vista', 'Windows 7', 'Windows 8', 'Windows Server 2008', 'Windows Server 2008 R2', 'Windows Server 2012'].each do |os| - describe 'installing setting BatteryIcon' do - let(:title) { 'BatteryIcon' } - let(:facts) do - { operatingsystemversion: os } - end - let(:params) do - { setting: 'BatteryIcon', status: 'on' } - end - - it { is_expected.not_to contain_exec('set globalpowerflag BatteryIcon') } - end - end -end diff --git a/spec/defines/global/hibernation_spec.rb b/spec/defines/global/hibernation_spec.rb index 7c2c13f..89e8ef4 100644 --- a/spec/defines/global/hibernation_spec.rb +++ b/spec/defines/global/hibernation_spec.rb @@ -11,9 +11,12 @@ it do is_expected.to contain_exec('update hibernate status').with( - 'command' => 'C:\Windows\System32\powercfg.exe -hibernate on' + 'provider' => 'windows', + 'command' => 'powercfg /hibernate on' ) end + + it { is_expected.to compile } end describe 'updating hibernate off' do @@ -24,8 +27,11 @@ it do is_expected.to contain_exec('update hibernate status').with( - 'command' => 'C:\Windows\System32\powercfg.exe -hibernate off' + 'provider' => 'windows', + 'command' => 'powercfg /hibernate off' ) end + + it { is_expected.to compile } end end diff --git a/spec/defines/schemes/scheme_spec.rb b/spec/defines/schemes/scheme_spec.rb index 4b5d345..00167ab 100644 --- a/spec/defines/schemes/scheme_spec.rb +++ b/spec/defines/schemes/scheme_spec.rb @@ -3,138 +3,94 @@ require 'spec_helper' describe 'windows_power::schemes::scheme', type: :define do - describe 'installing with invalid scheme name' do - let(:title) { 'create new scheme test' } - let(:params) do - { scheme_name: true, scheme_guid: '381b4222-f694-41f0-9685-ff5bb260df2e' } - end - - it do - expect do - is_expected.to contain_exec('create power scheme test') - end.to raise_error(Puppet::Error) - end - end - - ['Windows Vista', 'Windows 7', 'Windows 8', 'Windows Server 2008', 'Windows Server 2008 R2', 'Windows Server 2012'].each do |os| - describe 'installing with invalid template scheme name' do - let(:title) { 'create new scheme test' } - let(:facts) do - { operatingsystemversion: os } - end + describe 'creating inactive scheme' do + let(:title) { 'inactive_scheme' } let(:params) do - { scheme_name: true, scheme_guid: '381b4222-f694-41f0-9685-ff5bb260df2e', template_scheme: true } + { + scheme_name: 'inactive_scheme', + scheme_guid: '381b4222-f694-41f0-9685-ff5bbxx65ddx', + template_scheme: '381b4222-f694-41f0-9685-ff5bb260df2e', + activation: 'inactive', + ensure: 'present' + } end - it do - expect do - is_expected.to contain_exec('create power scheme test') - end.to raise_error(Puppet::Error) - end + it do + is_expected.to contain_exec('create power scheme inactive_scheme').with( + 'provider' => 'powershell', + 'command' => '& powercfg /duplicatescheme 381b4222-f694-41f0-9685-ff5bb260df2e 381b4222-f694-41f0-9685-ff5bbxx65ddx' + ) end - end - ['Windows XP', 'Windows Server 2003', 'Windows Server 2003 R2'].each do |os| - describe "create scheme on #{os}" do - let(:title) { 'create new scheme test' } - let(:facts) do - { operatingsystemversion: os } - end - let(:params) do - { scheme_name: 'test', scheme_guid: '381b4222-f694-41f0-9685-ff5bb260df2f', ensure: 'present' } - end - - it do - is_expected.to contain_exec('create power scheme test').with( - 'provider' => 'powershell', - 'command' => '& C:\Windows\System32\powercfg.exe /create \'test\'' - ) - end - - it { is_expected.to compile } + it do + is_expected.to contain_exec('rename scheme to inactive_scheme').with( + 'provider' => 'powershell', + 'command' => '& powercfg /changename 381b4222-f694-41f0-9685-ff5bbxx65ddx \'inactive_scheme\'' + ) end + + it { is_expected.not_to contain_exec('set inactive_scheme scheme as active') } + it { is_expected.not_to contain_exec('delete power scheme inactive_scheme') } + it { is_expected.to compile } end - ['Windows Vista', 'Windows 7', 'Windows 8', 'Windows Server 2008', 'Windows Server 2008 R2', 'Windows Server 2012'].each do |os| - describe "create and activate scheme on #{os}" do - let(:title) { 'create new scheme test' } - let(:facts) do - { operatingsystemversion: os } - end + describe 'creating and activating scheme' do + let(:title) { 'active_scheme' } let(:params) do - { scheme_name: 'test', scheme_guid: '381b4222-f694-41f0-9685-ff5bb260df2f', - template_scheme: '381b4222-f694-41f0-9685-ff5bb260df2e', ensure: 'present', activation: 'active' } - end - - it do - is_expected.to contain_exec('create power scheme test').with( - 'provider' => 'powershell', - 'command' => '& C:\Windows\System32\powercfg.exe -duplicatescheme 381b4222-f694-41f0-9685-ff5bb260df2e 381b4222-f694-41f0-9685-ff5bb260df2f' - ) - end + { + scheme_name: 'active_scheme', + scheme_guid: '381b4222-f694-41f0-9685-ff5bbxx65ddy', + template_scheme: '381b4222-f694-41f0-9685-ff5bb260df2e', + activation: 'active', + ensure: 'present' + } - it do - is_expected.to contain_exec('rename scheme to test').with( - 'provider' => 'powershell', - 'command' => '& C:\Windows\System32\powercfg.exe -changename 381b4222-f694-41f0-9685-ff5bb260df2f \'test\'' - ) - end - - it do - is_expected.to contain_exec('set test scheme as active').with( - 'provider' => 'powershell', - 'command' => '& C:\Windows\System32\powercfg.exe -setactive 381b4222-f694-41f0-9685-ff5bb260df2f' - ) - end - - it { is_expected.to compile } + it do + is_expected.to contain_exec('create power scheme active_scheme').with( + 'provider' => 'powershell', + 'command' => '& powercfg /duplicatescheme 381b4222-f694-41f0-9685-ff5bb260df2e 381b4222-f694-41f0-9685-ff5bbxx65ddy' + ) end - describe "create and inactive scheme on #{os}" do - let(:title) { 'create new scheme test' } - let(:facts) do - { operatingsystemversion: os } - end - let(:params) do - { scheme_name: 'test', scheme_guid: '381b4222-f694-41f0-9685-ff5bb260df2f', - template_scheme: '381b4222-f694-41f0-9685-ff5bb260df2e', ensure: 'present', activation: 'inactive' } - end - - it do - is_expected.to contain_exec('create power scheme test').with( - 'provider' => 'powershell', - 'command' => '& C:\Windows\System32\powercfg.exe -duplicatescheme 381b4222-f694-41f0-9685-ff5bb260df2e 381b4222-f694-41f0-9685-ff5bb260df2f' - ) - end - - it do - is_expected.to contain_exec('rename scheme to test').with( - 'provider' => 'powershell', - 'command' => '& C:\Windows\System32\powercfg.exe -changename 381b4222-f694-41f0-9685-ff5bb260df2f \'test\'' - ) - end + it do + is_expected.to contain_exec('rename scheme to active_scheme').with( + 'provider' => 'powershell', + 'command' => '& powercfg /changename 381b4222-f694-41f0-9685-ff5bbxx65ddy \'active_scheme\'' + ) + end - it { is_expected.not_to contain_exec('set test scheme as active') } - it { is_expected.to compile } + it do + is_expected.to contain_exec('set active_scheme scheme as active').with( + 'provider' => 'powershell', + 'command' => '& powercfg /setactive 381b4222-f694-41f0-9685-ff5bbxx65ddy' + ) end + + it { is_expected.not_to contain_exec('delete power scheme active_scheme') } + it { is_expected.to compile } end - describe 'delete scheme' do - let(:title) { 'delete scheme scheme test' } - let(:facts) do - { operatingsystemversion: 'Windows 2008 R2' } - end - let(:params) do - { scheme_name: 'test', scheme_guid: '381b4222-f694-41f0-9685-ff5bb260df2f', ensure: 'absent' } - end + describe 'deleting scheme' do + let(:title) { 'delete_scheme' } + let(:params) do + { + scheme_name: 'delete_scheme', + scheme_guid: '381b4222-f694-41f0-9685-ff5bbxx65ddz', + template_scheme: '381b4222-f694-41f0-9685-ff5bb260df2e', + activation: 'inactive', + ensure: 'absent' + } it do - is_expected.to contain_exec('delete power scheme test').with( + is_expected.to contain_exec('delete power scheme delete_scheme').with( 'provider' => 'powershell', - 'command' => '& C:\Windows\System32\powercfg.exe -delete 381b4222-f694-41f0-9685-ff5bb260df2f' + 'command' => '& powercfg /delete 381b4222-f694-41f0-9685-ff5bbxx65ddz' ) end + it { is_expected.not_to contain_exec('create power scheme delete_scheme') } + it { is_expected.not_to contain_exec('rename scheme to delete_scheme') } + it { is_expected.not_to contain_exec('set delete_scheme scheme as active') } it { is_expected.to compile } end end diff --git a/spec/defines/schemes/settings_spec.rb b/spec/defines/schemes/settings_spec.rb index 4fe3021..c9383c7 100644 --- a/spec/defines/schemes/settings_spec.rb +++ b/spec/defines/schemes/settings_spec.rb @@ -3,56 +3,19 @@ require 'spec_helper' describe 'windows_power::schemes::settings', type: :define do - describe 'installing with invalid scheme name' do - let(:title) { 'set disk timeout' } - let(:params) do - { setting: 'disk-timeout-ac', scheme_name: true, value: '0' } - end - - it do - expect do - is_expected.to contain_exec('modify xxx setting for test') - end.to raise_error(Puppet::Error) - end - end - - describe 'installing with invalid setting' do - let(:title) { 'set disk timeout' } - let(:params) do - { setting: 'xxx', scheme_name: 'test', value: '0' } - end - - it do - expect do - is_expected.to contain_exec('modify xxx setting for test') - end.to raise_error(Puppet::Error, %r{The setting argument does not match a valid scheme setting}) - end - end - - describe 'installing with invalid value' do - let(:title) { 'set disk timeout' } - let(:params) do - { setting: 'disk-timeout-ac', scheme_name: 'test', value: 'xx' } - end - - it do - expect do - is_expected.to contain_exec('modify disk-timeout-ac setting for test') - end.to raise_error(Puppet::Error, %r{The value provided is not appropriate for the disk-timeout-ac setting}) - end - end - - describe 'installing with disk-timeout-ac to 10 mins' do - let(:title) { 'set disk timeout' } - let(:params) do - { setting: 'disk-timeout-ac', scheme_name: 'test', value: '10' } - end + describe 'modifying setting disk-timeout-ac' do + let(:title) { 'setting disk timeout (ac)' } + let(:params) do + { scheme_name: 'current_scheme', setting: 'disk-timeout-ac', value: 10 } + end it do - is_expected.to contain_exec('modify disk-timeout-ac setting for test').with( + is_expected.to contain_exec('modify disk-timeout-ac setting for current_scheme').with( 'provider' => 'powershell', - 'command' => '& C:\Windows\System32\powercfg.exe /change disk-timeout-ac 10' + 'command' => '& powercfg /change disk-timeout-ac 10' ) end + + it { is_expected.to compile } end end From dd503c806017e4bd327d4fe1229260768e4de60e Mon Sep 17 00:00:00 2001 From: Lightning Date: Mon, 4 Sep 2023 12:58:05 +0200 Subject: [PATCH 04/18] drop params.pp --- manifests/params.pp | 8 -------- manifests/schemes/settings.pp | 2 +- 2 files changed, 1 insertion(+), 9 deletions(-) delete mode 100644 manifests/params.pp diff --git a/manifests/params.pp b/manifests/params.pp deleted file mode 100644 index 7f7de81..0000000 --- a/manifests/params.pp +++ /dev/null @@ -1,8 +0,0 @@ -# == Class windows_power::params -# -# This private class is meant to be called from `windows_power` -# It sets variables according to platform -# -class windows_power::params { - $nasty_ps = '$items = [System.Collections.ArrayList]@(powercfg /l | % { if ($_ -match "^.*GUID.*\((.*)\).*$") {$matches[1]} });' -} diff --git a/manifests/schemes/settings.pp b/manifests/schemes/settings.pp index 0b5a222..fc37c22 100644 --- a/manifests/schemes/settings.pp +++ b/manifests/schemes/settings.pp @@ -41,6 +41,6 @@ provider => powershell, command => "& powercfg /change ${setting} ${value}", logoutput => true, - unless => "${windows_power::params::nasty_ps} \$items.contains('${scheme_name}')", + unless => "([System.Collections.ArrayList]@(powercfg /l | % { if ($_ -match '^.*GUID.*\((.*)\).*$') {\$matches[1]} })).contains('${scheme_name}')", } } From 3d112804c76fb0fe05192725a94adb6077b60253 Mon Sep 17 00:00:00 2001 From: Lightning Date: Fri, 8 Sep 2023 12:33:06 +0200 Subject: [PATCH 05/18] add fact "power_schemes" --- lib/facter/power_schemes.rb | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 lib/facter/power_schemes.rb diff --git a/lib/facter/power_schemes.rb b/lib/facter/power_schemes.rb new file mode 100644 index 0000000..a245787 --- /dev/null +++ b/lib/facter/power_schemes.rb @@ -0,0 +1,20 @@ +Facter.add(:power_schemes) do + confine kernel: 'windows' + + setcode do + schemes = Facter::Core::Execution.execute('powercfg /l') + + power_schemes = {} + + schemes.strip.each_line(chomp: true) do |line| + line.match(/^.*?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}).*\((.*)\).*$/) do |match| + power_schemes[match[1].to_sym] = { + name: match[2], + active: line.end_with?('*') + } + end + end + + power_schemes + end +end From 58cc9871440511f9bd28ce9e23d3e27da704e2fc Mon Sep 17 00:00:00 2001 From: Lightning Date: Wed, 8 Nov 2023 18:35:20 +0100 Subject: [PATCH 06/18] add fact "power_devices" --- lib/facter/power_devices.rb | 65 +++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 lib/facter/power_devices.rb diff --git a/lib/facter/power_devices.rb b/lib/facter/power_devices.rb new file mode 100644 index 0000000..f564e16 --- /dev/null +++ b/lib/facter/power_devices.rb @@ -0,0 +1,65 @@ +Facter.add(:power_devices, :type => :aggregate) do + confine kernel: 'windows' + + chunk(:devices) do + all_devices = Facter::Core::Execution.execute('powercfg /devicequery all_devices').split(/[\n\r]+/).reject{|x| x.eql?('')} + wake_programmable = Facter::Core::Execution.execute('powercfg /devicequery wake_programmable').split(/[\n\r]+/).reject{|x| x.eql?('')} + wake_armed = Facter::Core::Execution.execute('powercfg /devicequery wake_armed').split(/[\n\r]+/).reject{|x| x.eql?('')} + wake_from_any = Facter::Core::Execution.execute('powercfg /devicequery wake_from_any').split(/[\n\r]+/).reject{|x| x.eql?('')} + wake_from_s1_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s1_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} + wake_from_s2_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s2_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} + wake_from_s3_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s3_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} + s1_supported = Facter::Core::Execution.execute('powercfg /devicequery s1_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} + s2_supported = Facter::Core::Execution.execute('powercfg /devicequery s2_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} + s3_supported = Facter::Core::Execution.execute('powercfg /devicequery s3_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} + s4_supported = Facter::Core::Execution.execute('powercfg /devicequery s4_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} + + devices = {} + + all_devices.each do |device| + devices[device.to_sym] = {} unless devices.has_key?(device.to_sym) + devices[device.to_sym].store(:wake_programmable, true) if wake_programmable.include?(device) + devices[device.to_sym].store(:wake_armed, true) if wake_armed.include?(device) + devices[device.to_sym].store(:wake_from_any, true) if wake_from_any.include?(device) + devices[device.to_sym].store(:wake_from_s1_supported, true) if wake_from_s1_supported.include?(device) + devices[device.to_sym].store(:wake_from_s2_supported, true) if wake_from_s2_supported.include?(device) + devices[device.to_sym].store(:wake_from_s3_supported, true) if wake_from_s3_supported.include?(device) + devices[device.to_sym].store(:s1_supported, true) if s1_supported.include?(device) + devices[device.to_sym].store(:s2_supported, true) if s2_supported.include?(device) + devices[device.to_sym].store(:s3_supported, true) if s3_supported.include?(device) + devices[device.to_sym].store(:s4_supported, true) if s4_supported.include?(device) + end + + devices.delete_if{|key, value| value.empty?} + devices + end + + chunk(:overrides) do + overrides = Facter::Core::Execution.execute('powercfg /requestsoverride') + + devices = {} + + overrides.strip.each_line('') do |paragraph| + caller_type = :unknown + paragraph.strip.each_line(chomp: true) do |line| + line.match(/^\[(.*)\]$/) do |match| + caller_type = match[1].downcase.to_sym + next + end + if caller_type != :unknown + line.match(/^(.*?)((?:\s(DISPLAY|SYSTEM|AWAYMODE)(?!.*\b\3\b))+)$/) do |match| + request = match[2].strip.split + devices[match[1].to_sym] = {} unless devices.has_key?(match[1].to_sym) + devices[match[1].to_sym][:power_request_overrides] = {} unless devices[match[1].to_sym].has_key?(:power_request_overrides) + devices[match[1].to_sym][:power_request_overrides][caller_type] = {} unless devices[match[1].to_sym][:power_request_overrides].has_key?(caller_type) + devices[match[1].to_sym][:power_request_overrides][caller_type].store(:display, true) if request.include?('DISPLAY') + devices[match[1].to_sym][:power_request_overrides][caller_type].store(:system, true) if request.include?('SYSTEM') + devices[match[1].to_sym][:power_request_overrides][caller_type].store(:awaymode, true) if request.include?('AWAYMODE') + end + end + end + end + + devices + end +end From 24effa94f5a17b2829d4525ddeb4281a231323d3 Mon Sep 17 00:00:00 2001 From: Lightning Date: Wed, 8 Nov 2023 18:35:50 +0100 Subject: [PATCH 07/18] great rework --- manifests/device.pp | 77 +++++++++++++++++++++++++++++++++ manifests/devices/override.pp | 30 ------------- manifests/devices/wake.pp | 30 ------------- manifests/global/hibernation.pp | 25 ----------- manifests/hibernate.pp | 36 +++++++++++++++ manifests/init.pp | 21 +++++---- manifests/scheme.pp | 72 ++++++++++++++++++++++++++++++ manifests/schemes/scheme.pp | 74 ------------------------------- manifests/schemes/settings.pp | 46 -------------------- 9 files changed, 195 insertions(+), 216 deletions(-) create mode 100644 manifests/device.pp delete mode 100644 manifests/devices/override.pp delete mode 100644 manifests/devices/wake.pp delete mode 100644 manifests/global/hibernation.pp create mode 100644 manifests/hibernate.pp create mode 100644 manifests/scheme.pp delete mode 100644 manifests/schemes/scheme.pp delete mode 100644 manifests/schemes/settings.pp diff --git a/manifests/device.pp b/manifests/device.pp new file mode 100644 index 0000000..9a94c11 --- /dev/null +++ b/manifests/device.pp @@ -0,0 +1,77 @@ +define windows_power::device ( + String[1] $device = $title, + Optional[Boolean] $enable_wake = undef, + Optional[Hash[Enum['service', 'process', 'driver'], Hash[Enum['display', 'system', 'awaymode'], Boolean, 1, 3], 1, 3]] $power_request_overrides = undef, +) { + if $enable_wake !~ Undef { + if ($device in $facts['power_devices']) and ('wake_programmable' in $facts['power_devices'][$device]) and ($facts['power_devices'][$device]['wake_programmable']) { + if ('wake_armed' in $facts['power_devices'][$device]) and $facts['power_devices'][$device]['wake_armed'] { + if !($enable_wake) { + exec { "disable_device_wake_${device}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /devicedisablewake \"${device}\"", + } + } + } + else { + if $enable_wake { + exec { "enable_device_wake_${device}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /deviceenablewake \"${device}\"", + } + } + } + } + } + + if $power_request_overrides !~ Undef { + $overrides = reduce($power_request_overrides, {}) |$prev, $now| { + $filtered = filter($now[1]) |$key, $value| { $value } + + if empty($filtered) { + $prev + } + else { + $prev + { $now[0] => $filtered } + } + } + + if !($device in $facts['power_devices']) or !('power_request_overrides' in $facts['power_devices'][$device]) { + if !empty($overrides) { + each($overrides) |$key, $value| { + $requests = join(keys($value), ' ') + + exec { "set_power_request_override_${key}_${device}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /requestsoverride ${key} \"${device}\" ${requests}", + } + } + } + } + else { + if $facts['power_devices'][$device]['power_request_overrides'] != $overrides { + each($power_request_overrides) |$key, $value| { + if $key in $overrides { + $requests = join(keys($overrides[$key]), ' ') + + exec { "set_power_request_override_${key}_${device}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /requestsoverride ${key} \"${device}\" ${requests}", + } + } + else { + exec { "remove_power_request_override_${key}_${device}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /requestsoverride ${key} \"${device}\"", + } + } + } + } + } + } +} diff --git a/manifests/devices/override.pp b/manifests/devices/override.pp deleted file mode 100644 index 2d7c358..0000000 --- a/manifests/devices/override.pp +++ /dev/null @@ -1,30 +0,0 @@ -# == Define: windows_power::devices::override -# -# This definition manages a Power Request override for a particular Process, Service, or Driver. -# -# === Parameters -# -# [*type*] -# Specifies one of the following caller types: PROCESS, SERVICE, DRIVER -# -# [*request*] -# Specifies one or more of the following Power Request Types: Display, System, Awaymode -# -# === Examples -# -# windows_power::devices::override { 'wmplayer.exe': -# type => 'PROCESS', -# request => 'Display', -# } -# -define windows_power::devices::override ( - Enum['PROCESS', 'SERVICE', 'DRIVER'] $type, - Enum['Display', 'System', 'Awaymode'] $request, -) { - include windows_power::params - - exec { "request override for ${name}": - provider => windows, - command => "powercfg /requestsoverride ${type} ${name} ${request}", - } -} diff --git a/manifests/devices/wake.pp b/manifests/devices/wake.pp deleted file mode 100644 index e5244e6..0000000 --- a/manifests/devices/wake.pp +++ /dev/null @@ -1,30 +0,0 @@ -# == Define: windows_power::devices::wake -# -# This definition enables/disables the device to wake the computer from a sleep state -# -# === Parameters -# -# [*device*] -# Specifies the device name -# -# [*ensure*] -# Enable or disable the device for waking -# -# === Examples -# -# windows_power::devices::wake { 'VMBus Enumerator (001)': -# device => 'VMBus Enumerator (001)', -# ensure => 'enable', -# } -# -define windows_power::devices::wake ( - String[1] $device, - Enum['enable', 'disable'] $ensure = 'enable', -) { - include windows_power::params - - exec { "device ${device} ${ensure} wake": - provider => windows, - command => "powercfg /device${ensure}wake \"${device}\"", - } -} diff --git a/manifests/global/hibernation.pp b/manifests/global/hibernation.pp deleted file mode 100644 index 797039b..0000000 --- a/manifests/global/hibernation.pp +++ /dev/null @@ -1,25 +0,0 @@ -# == Define: windows_power::global::hibernation -# -# This definition configures hibernation on a box -# -# === Parameters -# -# [*status*] -# Setting configuration (on/off) -# -# === Examples -# -# windows_power::global::hibernation { 'enable hibernation': -# status => 'on', -# } -# -define windows_power::global::hibernation ( - Enum['on', 'off'] $status, -) { - include windows_power::params - - exec { 'update hibernate status': - provider => windows, - command => "powercfg /hibernate ${status}", - } -} diff --git a/manifests/hibernate.pp b/manifests/hibernate.pp new file mode 100644 index 0000000..cb9203d --- /dev/null +++ b/manifests/hibernate.pp @@ -0,0 +1,36 @@ +class windows_power::hibernate ( + Boolean $enable, + Optional[Integer[40, 100]] $hiberfile_size, + Optional[Enum['reduced', 'full']] $hiberfile_type, +) { + if $enable { + exec { 'enable_hibernate': + provider => windows, + path => $facts['os']['windows']['system32'], + command => 'powercfg /hibernate on', + } + + if $hiberfile_size !~ Undef { + exec { 'set_hiberfile_size': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /hibernate /size ${hiberfile_size}", + } + } + + if $hiberfile_type !~ Undef { + exec { 'set_hiberfile_type': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /hibernate /type ${hiberfile_type}", + } + } + } + else { + exec { 'disable_hibernate': + provider => windows, + path => $facts['os']['windows']['system32'], + command => 'powercfg /hibernate off', + } + } +} diff --git a/manifests/init.pp b/manifests/init.pp index b097f0c..7a80c1d 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,12 +1,11 @@ -# == Class: windows_power -# -# Module to mananage the configuration of a machines autoupdate settings -# -# === Parameters -# -# === Examples -# -# -class windows_power { - include windows_power::params +class windows_power ( + Optional[Hash[String[1], Hash[Pattern[/^[a-z][a-z0-9_]*$/], Data, 1], 1]] $devices, +) { + if $devices !~ Undef { + each($devices) |$key, $value| { + windows_power::device { $key: + * => $value, + } + } + } } diff --git a/manifests/scheme.pp b/manifests/scheme.pp new file mode 100644 index 0000000..e4c4c65 --- /dev/null +++ b/manifests/scheme.pp @@ -0,0 +1,72 @@ +class windows_power::scheme ( + Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/] $guid, + Optional[String[1]] $label, + Optional[Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/]] $template, + Optional[String[1]] $description, + Optional[Hash[Enum[ + 'monitor-timeout-ac', + 'monitor-timeout-dc', + 'disk-timeout-ac', + 'disk-timeout-dc', + 'standby-timeout-ac', + 'standby-timeout-dc', + 'hibernate-timeout-ac', + 'hibernate-timeout-dc' + ], Integer[0], 1, 8]] $settings, +) { + if $template !~ Undef { + if !($guid in $facts['power_schemes']) and ($template in $facts['power_schemes']) { + exec { 'duplicate_existing_power_scheme': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /duplicatescheme ${template} ${guid}", + } + } + } + + if ($guid in $facts['power_schemes']) and !($facts['power_schemes'][$guid]['active']) { + exec { 'activate_power_scheme': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /setactive ${guid}", + } + } + elsif !($guid in $facts['power_schemes']) { + exec { 'activate_power_scheme': + provider => powershell, + command => "& powercfg /setactive ${guid}", + onlyif => "([System.Collections.ArrayList]@(powercfg /l | % { if ($_ -match '^.*?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}).*$') {\$matches[1]} })).contains('${guid}')", + } + } + + if $label !~ Undef { + if ($guid in $facts['power_schemes']) and ($facts['power_schemes'][$guid]['name'] != $label) { + if $description !~ Undef { + exec { 'rename_power_scheme': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /changename ${guid} \"${label}\" \"${description}\"", + } + } + else { + exec { 'rename_power_scheme': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /changename ${guid} \"${label}\"", + } + } + } + } + + if ($guid in $facts['power_schemes']) and $facts['power_schemes'][$guid]['active'] { + if $settings !~ Undef { + each($settings) |$key, $value| { + exec { "set_power_scheme_setting_${key}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /change ${key} ${value}", + } + } + } + } +} diff --git a/manifests/schemes/scheme.pp b/manifests/schemes/scheme.pp deleted file mode 100644 index 86cbfa4..0000000 --- a/manifests/schemes/scheme.pp +++ /dev/null @@ -1,74 +0,0 @@ -# == Define: windows_power::schemes::scheme -# -# This definition configures a specific power scheme -# -# === Parameters -# -# [*scheme_name*] -# The name of the scheme to configure -# -# [*scheme_guid*] -# The windows guid used to uniquely identify the power scheme -# -# [*template_scheme*] -# The windows guid of an existing scheme to be used as a template for the current scheme -# -# [*activation*] -# Set the current scheme as the active scheme -# -# [*ensure*] -# Configure if the scheme is present or absent -# -# === Examples -# -# windows_power::schemes::scheme { 'test scheme': -# scheme_name => 'test', -# scheme_guid => '381b4222-f694-41f0-9685-ff5bbxx65ddx', -# template_scheme => '381b4222-f694-41f0-9685-ff5bb260df2e', -# activation => 'active', -# ensure => 'present', -# } -# -define windows_power::schemes::scheme ( - String[1] $scheme_name, - Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/] $scheme_guid, - Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/] $template_scheme, - Enum['active', 'inactive'] $activation, - Enum['present', 'absent'] $ensure = 'present', -) { - include windows_power::params - - if $ensure == 'present' { - exec { "create power scheme ${scheme_name}": - provider => powershell, - command => "& powercfg /duplicatescheme ${template_scheme} ${scheme_guid}", - logoutput => true, - unless => "powercfg /query ${scheme_guid}", - } - - exec { "rename scheme to ${scheme_name}": - provider => powershell, - command => "& powercfg /changename ${scheme_guid} '${scheme_name}'", - logoutput => true, - onlyif => "if (Get-CimInstance -Namespace root\\cimv2\\power -ClassName win32_powerplan -Filter \"ElementName = '${scheme_name}'\" | Where -property InstanceID -eq 'Microsoft:PowerPlan\\{${scheme_guid}}') { exit 1 } else { exit 0 }", - require => Exec["create power scheme ${scheme_name}"], - } - - if $activation == 'active' { - exec { "set ${scheme_name} scheme as active": - provider => powershell, - command => "& powercfg /setactive ${scheme_guid}", - logoutput => true, - onlyif => "if (Get-CimInstance -Namespace root\\cimv2\\power -ClassName win32_powerplan -Filter \"IsActive = True\" | Where -property InstanceID -eq 'Microsoft:PowerPlan\\{${scheme_guid}}') { exit 1 }", - } - } - } - elsif $ensure == 'absent' { - exec { "delete power scheme ${scheme_name}": - provider => powershell, - command => "& powercfg /delete ${scheme_guid}", - logoutput => true, - onlyif => "powercfg /query ${scheme_guid}", - } - } -} diff --git a/manifests/schemes/settings.pp b/manifests/schemes/settings.pp deleted file mode 100644 index fc37c22..0000000 --- a/manifests/schemes/settings.pp +++ /dev/null @@ -1,46 +0,0 @@ -# == Define: windows_power::schemes::settings -# -# This definition configures settings for a specific scheme -# -# === Parameters -# -# [*scheme_name*] -# The name of the scheme to configure -# -# [*setting*] -# The setting to configure -# -# [*value*] -# The value set the setting to - minutes or throttle -# -# === Examples -# -# windows_power::schemes::settings { 'set monitor timeout': -# scheme_name => 'test', -# setting => 'monitor-timeout-ac', -# value => '10', -# } -# -define windows_power::schemes::settings ( - String[1] $scheme_name, - Enum[ - 'monitor-timeout-ac', - 'monitor-timeout-dc', - 'disk-timeout-ac', - 'disk-timeout-dc', - 'standby-timeout-ac', - 'standby-timeout-dc', - 'hibernate-timeout-ac', - 'hibernate-timeout-dc' - ] $setting, - Integer[0] $value, -) { - include windows_power::params - - exec { "modify ${setting} setting for ${scheme_name}": - provider => powershell, - command => "& powercfg /change ${setting} ${value}", - logoutput => true, - unless => "([System.Collections.ArrayList]@(powercfg /l | % { if ($_ -match '^.*GUID.*\((.*)\).*$') {\$matches[1]} })).contains('${scheme_name}')", - } -} From 44fea7d14263f39e5c166a0c696a390fd1d14133 Mon Sep 17 00:00:00 2001 From: Lightning Date: Fri, 20 Oct 2023 11:32:39 +0200 Subject: [PATCH 08/18] add module defaults --- manifests/hibernate.pp | 4 ++-- manifests/init.pp | 2 +- manifests/scheme.pp | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/manifests/hibernate.pp b/manifests/hibernate.pp index cb9203d..897adfc 100644 --- a/manifests/hibernate.pp +++ b/manifests/hibernate.pp @@ -1,7 +1,7 @@ class windows_power::hibernate ( Boolean $enable, - Optional[Integer[40, 100]] $hiberfile_size, - Optional[Enum['reduced', 'full']] $hiberfile_type, + Optional[Integer[40, 100]] $hiberfile_size = undef, + Optional[Enum['reduced', 'full']] $hiberfile_type = undef, ) { if $enable { exec { 'enable_hibernate': diff --git a/manifests/init.pp b/manifests/init.pp index 7a80c1d..86b34f1 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,5 +1,5 @@ class windows_power ( - Optional[Hash[String[1], Hash[Pattern[/^[a-z][a-z0-9_]*$/], Data, 1], 1]] $devices, + Optional[Hash[String[1], Hash[Pattern[/^[a-z][a-z0-9_]*$/], Data, 1], 1]] $devices = undef, ) { if $devices !~ Undef { each($devices) |$key, $value| { diff --git a/manifests/scheme.pp b/manifests/scheme.pp index e4c4c65..ada14b6 100644 --- a/manifests/scheme.pp +++ b/manifests/scheme.pp @@ -1,8 +1,8 @@ class windows_power::scheme ( Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/] $guid, - Optional[String[1]] $label, - Optional[Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/]] $template, - Optional[String[1]] $description, + Optional[String[1]] $label = undef, + Optional[Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/]] $template = undef, + Optional[String[1]] $description = undef, Optional[Hash[Enum[ 'monitor-timeout-ac', 'monitor-timeout-dc', @@ -12,7 +12,7 @@ 'standby-timeout-dc', 'hibernate-timeout-ac', 'hibernate-timeout-dc' - ], Integer[0], 1, 8]] $settings, + ], Integer[0], 1, 8]] $settings = undef, ) { if $template !~ Undef { if !($guid in $facts['power_schemes']) and ($template in $facts['power_schemes']) { From 2f1c73f4e0f51deb4d4730d0992d2f263fd6e2e6 Mon Sep 17 00:00:00 2001 From: Lightning Date: Wed, 8 Nov 2023 18:21:24 +0100 Subject: [PATCH 09/18] add puppet strings documentation --- manifests/device.pp | 59 ++++++++++++++++++++++++++++++++++++++++++ manifests/hibernate.pp | 27 +++++++++++++++++++ manifests/init.pp | 32 +++++++++++++++++++++++ manifests/scheme.pp | 50 +++++++++++++++++++++++++++++++++++ 4 files changed, 168 insertions(+) diff --git a/manifests/device.pp b/manifests/device.pp index 9a94c11..88a7c23 100644 --- a/manifests/device.pp +++ b/manifests/device.pp @@ -1,3 +1,62 @@ +# @summary defined type to manage a Windows power device (physical, logical and virtual) +# +# @example let your mouse wake the system +# windows_power::device { 'HID-compliant mouse (001)': +# enable_wake => true +# } +# +# @example don't allow Windows media player to keep system from turning off the display or from going to away mode +# windows_power::device { 'wmplayer.exe': +# power_request_overrides => { +# process => { +# display => true, +# awaymode => true +# } +# } +# } +# +# @example don't allow your network card to wake the system, keep your display turned on or keep the system active +# windows_power::device { 'Realtek PCIe GbE Family Controller': +# enable_wake => false, +# power_request_overrides => { +# driver => { +# display => true, +# system => true +# } +# } +# } +# +# @example delete all power request overrides for/from vpn service +# windows_power::device { 'VPN Service': +# power_request_overrides => { +# service => { +# system => false +# } +# } +# } +# +# @param device +# name of the device/driver/service/process to handle, defaulting to resource's title (no need to set this manually); +# also see the shipped fact `power_devices`! +# note that the term "device" covers several things (due to the nature of Windows' power management): +# - physical devices build in or connected to the machine (such as a network card or a mouse) +# - logical devices or device groups (such as "HID-compliant system controller" or even "Volume (005)") +# - drivers or driver groups (such as "High Definition Audio Device") +# - services (e.g. your remote management software's service name) +# - processes (e.g. "your_media_player.exe") +# +# @param enable_wake +# allow (or prohibit) the device to wake the system from a sleep state; +# devices capable of waking the system do have `$facts['power_devices'][$device]['wake_programmable'] == true`; +# devices currently allowed waking the system do have `$facts['power_devices'][$device]['wake_armed'] == true`; +# it's safe to set `enable_wake => true` even if the device is not able to do; +# defined type only activates wake-up functionality if the device is reported to be capable of doing so! +# +# @param power_request_overrides +# set (or delete) one or more power request overrides for the device (see Microsoft documentation about power requests and overrides); +# note that not defining a request type is similar to setting it to `false` but not defining any request type means "don't touch the +# current state"; so explicitly setting (only) `false` values makes it possible to delete request overrides set outside of Puppet! +# see examples for clarity. define windows_power::device ( String[1] $device = $title, Optional[Boolean] $enable_wake = undef, diff --git a/manifests/hibernate.pp b/manifests/hibernate.pp index 897adfc..5c19b2c 100644 --- a/manifests/hibernate.pp +++ b/manifests/hibernate.pp @@ -1,3 +1,30 @@ +# @summary class to manage Windows hibernate settings +# +# @example disable hibernation system wide +# class { 'windows_power::hibernate': +# enable => false +# } +# +# @example enable hibernation with default settings +# class { 'windows_power::hibernate': +# enable => true +# } +# +# @example enable and configure hibernation +# class { 'windows_power::hibernate': +# enable => true, +# hiberfile_size => 100, +# hiberfile_type => 'full' +# } +# +# @param enable +# enable/disable the hibernate feature +# +# @param hiberfile_size +# set desired hiberfile size (percentage of total memory, 40-100) +# +# @param hiberfile_type +# set desired hiberfile type (`reduced`/`full`) class windows_power::hibernate ( Boolean $enable, Optional[Integer[40, 100]] $hiberfile_size = undef, diff --git a/manifests/init.pp b/manifests/init.pp index 86b34f1..685a6ab 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,3 +1,35 @@ +# @summary class to manage Windows power devices (physical, logical and virtual) +# +# @see windows_power::device +# +# @example basic usage +# class { 'windows_power': +# devices => { +# 'HID-compliant mouse (001)' => { +# enable_wake => true +# }, +# 'wmplayer.exe' => { +# power_request_overrides => { +# process => { +# display => true, +# awaymode => true +# } +# } +# }, +# 'Realtek PCIe GbE Family Controller' => { +# enable_wake => false, +# power_request_overrides => { +# driver => { +# display => true, +# system => true +# } +# } +# } +# } +# } +# +# @param devices +# hash of devices/drivers/services/tools to manage and what/how class windows_power ( Optional[Hash[String[1], Hash[Pattern[/^[a-z][a-z0-9_]*$/], Data, 1], 1]] $devices = undef, ) { diff --git a/manifests/scheme.pp b/manifests/scheme.pp index ada14b6..f6e0dba 100644 --- a/manifests/scheme.pp +++ b/manifests/scheme.pp @@ -1,3 +1,53 @@ +# @summary class to manage Windows power scheme +# +# @example activate the "High performance" system scheme +# class { 'windows_power::scheme': +# guid => '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' +# } +# +# @example activate the "Balanced" system scheme and set some monitor timeouts +# class { 'windows_power::scheme': +# guid => '381b4222-f694-41f0-9685-ff5bb260df2e', +# settings => { +# monitor-timeout-ac => 30, +# monitor-timeout-dc => 10 +# } +# } +# +# @example create a custom template inheriting "High performance" and tweak on that +# class { 'windows_power::scheme': +# guid => 'a1582e9e-9c9d-46fd-afdf-4d989292a073', +# label => 'really full power', +# template => '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', +# settings => { +# disk-timeout-ac => 0, +# disk-timeout-dc => 0, +# standby-timeout-ac => 0, +# standby-timeout-dc => 0, +# hibernate-timeout-ac => 0, +# hibernate-timeout-dc => 0 +# } +# } +# +# @param guid +# GUID of the scheme to create/activate; +# to activate/handle an existing scheme (e.g. the ones provided by the system) don't define a template; +# to create (and activate and handle) a new scheme (derived from an existing one) define the template. +# +# @param label +# desired label/name/title of the scheme (optional, recommended for custom schemes) +# +# @param template +# GUID of the template scheme to use for creating a new custom scheme (optional); +# if the template does not exist no action is performed; so it's safe to define a template that will appear later somehow. +# +# @param description +# desired descriptive text of the scheme (optional) +# +# @param settings +# settings to change (optional); +# settings are applied to the active scheme and only if this matches the declared GUID; this way accidential configuration of the wrong +# scheme is avoided but it might need more than one puppet run to complete all tasks. class windows_power::scheme ( Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/] $guid, Optional[String[1]] $label = undef, From 3cafb71a4993c6ffc3e67f0c1f91b13c18586b1f Mon Sep 17 00:00:00 2001 From: Lightning Date: Fri, 10 Nov 2023 11:25:02 +0100 Subject: [PATCH 10/18] align code to style guide rules --- manifests/device.pp | 138 ++++++++++++++++++++--------------------- manifests/hibernate.pp | 62 +++++++++--------- manifests/init.pp | 16 ++--- manifests/scheme.pp | 132 +++++++++++++++++++-------------------- 4 files changed, 174 insertions(+), 174 deletions(-) diff --git a/manifests/device.pp b/manifests/device.pp index 88a7c23..1f76a8e 100644 --- a/manifests/device.pp +++ b/manifests/device.pp @@ -58,79 +58,79 @@ # current state"; so explicitly setting (only) `false` values makes it possible to delete request overrides set outside of Puppet! # see examples for clarity. define windows_power::device ( - String[1] $device = $title, - Optional[Boolean] $enable_wake = undef, - Optional[Hash[Enum['service', 'process', 'driver'], Hash[Enum['display', 'system', 'awaymode'], Boolean, 1, 3], 1, 3]] $power_request_overrides = undef, + String[1] $device = $title, + Optional[Boolean] $enable_wake = undef, + Optional[Hash[Enum['service', 'process', 'driver'], Hash[Enum['display', 'system', 'awaymode'], Boolean, 1, 3], 1, 3]] $power_request_overrides = undef, ) { - if $enable_wake !~ Undef { - if ($device in $facts['power_devices']) and ('wake_programmable' in $facts['power_devices'][$device]) and ($facts['power_devices'][$device]['wake_programmable']) { - if ('wake_armed' in $facts['power_devices'][$device]) and $facts['power_devices'][$device]['wake_armed'] { - if !($enable_wake) { - exec { "disable_device_wake_${device}": - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /devicedisablewake \"${device}\"", - } - } - } - else { - if $enable_wake { - exec { "enable_device_wake_${device}": - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /deviceenablewake \"${device}\"", - } - } - } - } - } + if $enable_wake !~ Undef { + if ($device in $facts['power_devices']) and ('wake_programmable' in $facts['power_devices'][$device]) and ($facts['power_devices'][$device]['wake_programmable']) { + if ('wake_armed' in $facts['power_devices'][$device]) and $facts['power_devices'][$device]['wake_armed'] { + if !($enable_wake) { + exec { "disable_device_wake_${device}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /devicedisablewake \"${device}\"", + } + } + } + else { + if $enable_wake { + exec { "enable_device_wake_${device}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /deviceenablewake \"${device}\"", + } + } + } + } + } - if $power_request_overrides !~ Undef { - $overrides = reduce($power_request_overrides, {}) |$prev, $now| { - $filtered = filter($now[1]) |$key, $value| { $value } + if $power_request_overrides !~ Undef { + $overrides = reduce($power_request_overrides, {}) |$prev, $now| { + $filtered = filter($now[1]) |$key, $value| { $value } - if empty($filtered) { - $prev - } - else { - $prev + { $now[0] => $filtered } - } - } + if empty($filtered) { + $prev + } + else { + $prev + { $now[0] => $filtered } + } + } - if !($device in $facts['power_devices']) or !('power_request_overrides' in $facts['power_devices'][$device]) { - if !empty($overrides) { - each($overrides) |$key, $value| { - $requests = join(keys($value), ' ') + if !($device in $facts['power_devices']) or !('power_request_overrides' in $facts['power_devices'][$device]) { + if !empty($overrides) { + each($overrides) |$key, $value| { + $requests = join(keys($value), ' ') - exec { "set_power_request_override_${key}_${device}": - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /requestsoverride ${key} \"${device}\" ${requests}", - } - } - } - } - else { - if $facts['power_devices'][$device]['power_request_overrides'] != $overrides { - each($power_request_overrides) |$key, $value| { - if $key in $overrides { - $requests = join(keys($overrides[$key]), ' ') + exec { "set_power_request_override_${key}_${device}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /requestsoverride ${key} \"${device}\" ${requests}", + } + } + } + } + else { + if $facts['power_devices'][$device]['power_request_overrides'] != $overrides { + each($power_request_overrides) |$key, $value| { + if $key in $overrides { + $requests = join(keys($overrides[$key]), ' ') - exec { "set_power_request_override_${key}_${device}": - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /requestsoverride ${key} \"${device}\" ${requests}", - } - } - else { - exec { "remove_power_request_override_${key}_${device}": - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /requestsoverride ${key} \"${device}\"", - } - } - } - } - } - } + exec { "set_power_request_override_${key}_${device}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /requestsoverride ${key} \"${device}\" ${requests}", + } + } + else { + exec { "remove_power_request_override_${key}_${device}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /requestsoverride ${key} \"${device}\"", + } + } + } + } + } + } } diff --git a/manifests/hibernate.pp b/manifests/hibernate.pp index 5c19b2c..9e071ab 100644 --- a/manifests/hibernate.pp +++ b/manifests/hibernate.pp @@ -26,38 +26,38 @@ # @param hiberfile_type # set desired hiberfile type (`reduced`/`full`) class windows_power::hibernate ( - Boolean $enable, - Optional[Integer[40, 100]] $hiberfile_size = undef, - Optional[Enum['reduced', 'full']] $hiberfile_type = undef, + Boolean $enable, + Optional[Integer[40, 100]] $hiberfile_size = undef, + Optional[Enum['reduced', 'full']] $hiberfile_type = undef, ) { - if $enable { - exec { 'enable_hibernate': - provider => windows, - path => $facts['os']['windows']['system32'], - command => 'powercfg /hibernate on', - } + if $enable { + exec { 'enable_hibernate': + provider => windows, + path => $facts['os']['windows']['system32'], + command => 'powercfg /hibernate on', + } - if $hiberfile_size !~ Undef { - exec { 'set_hiberfile_size': - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /hibernate /size ${hiberfile_size}", - } - } + if $hiberfile_size !~ Undef { + exec { 'set_hiberfile_size': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /hibernate /size ${hiberfile_size}", + } + } - if $hiberfile_type !~ Undef { - exec { 'set_hiberfile_type': - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /hibernate /type ${hiberfile_type}", - } - } - } - else { - exec { 'disable_hibernate': - provider => windows, - path => $facts['os']['windows']['system32'], - command => 'powercfg /hibernate off', - } - } + if $hiberfile_type !~ Undef { + exec { 'set_hiberfile_type': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /hibernate /type ${hiberfile_type}", + } + } + } + else { + exec { 'disable_hibernate': + provider => windows, + path => $facts['os']['windows']['system32'], + command => 'powercfg /hibernate off', + } + } } diff --git a/manifests/init.pp b/manifests/init.pp index 685a6ab..2da9cf8 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -31,13 +31,13 @@ # @param devices # hash of devices/drivers/services/tools to manage and what/how class windows_power ( - Optional[Hash[String[1], Hash[Pattern[/^[a-z][a-z0-9_]*$/], Data, 1], 1]] $devices = undef, + Optional[Hash[String[1], Hash[Pattern[/^[a-z][a-z0-9_]*$/], Data, 1], 1]] $devices = undef, ) { - if $devices !~ Undef { - each($devices) |$key, $value| { - windows_power::device { $key: - * => $value, - } - } - } + if $devices !~ Undef { + each($devices) |$key, $value| { + windows_power::device { $key: + * => $value, + } + } + } } diff --git a/manifests/scheme.pp b/manifests/scheme.pp index f6e0dba..08cc2e6 100644 --- a/manifests/scheme.pp +++ b/manifests/scheme.pp @@ -49,74 +49,74 @@ # settings are applied to the active scheme and only if this matches the declared GUID; this way accidential configuration of the wrong # scheme is avoided but it might need more than one puppet run to complete all tasks. class windows_power::scheme ( - Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/] $guid, - Optional[String[1]] $label = undef, - Optional[Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/]] $template = undef, - Optional[String[1]] $description = undef, - Optional[Hash[Enum[ - 'monitor-timeout-ac', - 'monitor-timeout-dc', - 'disk-timeout-ac', - 'disk-timeout-dc', - 'standby-timeout-ac', - 'standby-timeout-dc', - 'hibernate-timeout-ac', - 'hibernate-timeout-dc' - ], Integer[0], 1, 8]] $settings = undef, + Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/] $guid, + Optional[String[1]] $label = undef, + Optional[Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/]] $template = undef, + Optional[String[1]] $description = undef, + Optional[Hash[Enum[ + 'monitor-timeout-ac', + 'monitor-timeout-dc', + 'disk-timeout-ac', + 'disk-timeout-dc', + 'standby-timeout-ac', + 'standby-timeout-dc', + 'hibernate-timeout-ac', + 'hibernate-timeout-dc' + ], Integer[0], 1, 8]] $settings = undef, ) { - if $template !~ Undef { - if !($guid in $facts['power_schemes']) and ($template in $facts['power_schemes']) { - exec { 'duplicate_existing_power_scheme': - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /duplicatescheme ${template} ${guid}", - } - } - } + if $template !~ Undef { + if !($guid in $facts['power_schemes']) and ($template in $facts['power_schemes']) { + exec { 'duplicate_existing_power_scheme': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /duplicatescheme ${template} ${guid}", + } + } + } - if ($guid in $facts['power_schemes']) and !($facts['power_schemes'][$guid]['active']) { - exec { 'activate_power_scheme': - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /setactive ${guid}", - } - } - elsif !($guid in $facts['power_schemes']) { - exec { 'activate_power_scheme': - provider => powershell, - command => "& powercfg /setactive ${guid}", - onlyif => "([System.Collections.ArrayList]@(powercfg /l | % { if ($_ -match '^.*?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}).*$') {\$matches[1]} })).contains('${guid}')", - } - } + if ($guid in $facts['power_schemes']) and !($facts['power_schemes'][$guid]['active']) { + exec { 'activate_power_scheme': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /setactive ${guid}", + } + } + elsif !($guid in $facts['power_schemes']) { + exec { 'activate_power_scheme': + provider => powershell, + command => "& powercfg /setactive ${guid}", + onlyif => "([System.Collections.ArrayList]@(powercfg /l | % { if ($_ -match '^.*?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}).*$') {\$matches[1]} })).contains('${guid}')", + } + } - if $label !~ Undef { - if ($guid in $facts['power_schemes']) and ($facts['power_schemes'][$guid]['name'] != $label) { - if $description !~ Undef { - exec { 'rename_power_scheme': - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /changename ${guid} \"${label}\" \"${description}\"", - } - } - else { - exec { 'rename_power_scheme': - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /changename ${guid} \"${label}\"", - } - } - } - } + if $label !~ Undef { + if ($guid in $facts['power_schemes']) and ($facts['power_schemes'][$guid]['name'] != $label) { + if $description !~ Undef { + exec { 'rename_power_scheme': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /changename ${guid} \"${label}\" \"${description}\"", + } + } + else { + exec { 'rename_power_scheme': + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /changename ${guid} \"${label}\"", + } + } + } + } - if ($guid in $facts['power_schemes']) and $facts['power_schemes'][$guid]['active'] { - if $settings !~ Undef { - each($settings) |$key, $value| { - exec { "set_power_scheme_setting_${key}": - provider => windows, - path => $facts['os']['windows']['system32'], - command => "powercfg /change ${key} ${value}", - } - } - } - } + if ($guid in $facts['power_schemes']) and $facts['power_schemes'][$guid]['active'] { + if $settings !~ Undef { + each($settings) |$key, $value| { + exec { "set_power_scheme_setting_${key}": + provider => windows, + path => $facts['os']['windows']['system32'], + command => "powercfg /change ${key} ${value}", + } + } + } + } } From 11db887689e59078b0ccbf98fac4c0935ae9e4aa Mon Sep 17 00:00:00 2001 From: Lightning Date: Fri, 10 Nov 2023 13:40:18 +0100 Subject: [PATCH 11/18] update license/owner/author information --- LICENSE | 1 + metadata.json | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/LICENSE b/LICENSE index 0651539..623d51e 100644 --- a/LICENSE +++ b/LICENSE @@ -188,6 +188,7 @@ identification within third-party archives. Copyright 2014 Liam Bennett (liamjbennett@gmail.com) + Copyright 2023 markt.de GmbH & Co. KG Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. diff --git a/metadata.json b/metadata.json index 600345e..8a95ed3 100644 --- a/metadata.json +++ b/metadata.json @@ -1,7 +1,7 @@ { "name": "puppet-windows_power", "version": "4.0.0-rc0", - "author": "puppet", + "author": "markt.de GmbH & Co. KG", "license": "Apache-2.0", "summary": "Module to manage the power settings on Windows", "source": "https://github.com/voxpupuli/puppet-windows_power", From c7d7bf73ef320bece11c68103d7e34f7fe5fff86 Mon Sep 17 00:00:00 2001 From: Lightning Date: Tue, 30 Apr 2024 14:32:31 +0200 Subject: [PATCH 12/18] fix powershell command --- manifests/scheme.pp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/manifests/scheme.pp b/manifests/scheme.pp index 08cc2e6..1186345 100644 --- a/manifests/scheme.pp +++ b/manifests/scheme.pp @@ -85,7 +85,7 @@ exec { 'activate_power_scheme': provider => powershell, command => "& powercfg /setactive ${guid}", - onlyif => "([System.Collections.ArrayList]@(powercfg /l | % { if ($_ -match '^.*?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}).*$') {\$matches[1]} })).contains('${guid}')", + onlyif => "([System.Collections.ArrayList]@(powercfg /l | % { if (\$_ -match '^.*?([0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}).*\$') {\$matches[1]} })).contains('${guid}')", } } From b173e04b0c18565affd5d219a2f4817c84cd8864 Mon Sep 17 00:00:00 2001 From: Lightning Date: Tue, 30 Apr 2024 11:49:57 +0200 Subject: [PATCH 13/18] reworks unit tests --- spec/classes/hibernate_spec.rb | 58 +++++ spec/classes/scheme_spec.rb | 325 ++++++++++++++++++++++++ spec/classes/windows_power_spec.rb | 54 ++++ spec/defines/device_spec.rb | 315 +++++++++++++++++++++++ spec/defines/devices/override_spec.rb | 37 --- spec/defines/devices/wake_spec.rb | 37 --- spec/defines/global/hibernation_spec.rb | 37 --- spec/defines/schemes/scheme_spec.rb | 96 ------- spec/defines/schemes/settings_spec.rb | 21 -- 9 files changed, 752 insertions(+), 228 deletions(-) create mode 100644 spec/classes/hibernate_spec.rb create mode 100644 spec/classes/scheme_spec.rb create mode 100644 spec/classes/windows_power_spec.rb create mode 100644 spec/defines/device_spec.rb delete mode 100644 spec/defines/devices/override_spec.rb delete mode 100644 spec/defines/devices/wake_spec.rb delete mode 100644 spec/defines/global/hibernation_spec.rb delete mode 100644 spec/defines/schemes/scheme_spec.rb delete mode 100644 spec/defines/schemes/settings_spec.rb diff --git a/spec/classes/hibernate_spec.rb b/spec/classes/hibernate_spec.rb new file mode 100644 index 0000000..e1e734e --- /dev/null +++ b/spec/classes/hibernate_spec.rb @@ -0,0 +1,58 @@ +require 'spec_helper' + +describe 'windows_power::hibernate' do + let(:facts) do + { + os: { + windows: { + system32: 'C:\WINDOWS\system32' + } + } + } + end + + context 'disable hibernation system wide' do + let(:params) do + { + enable: false + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::hibernate') } + + it { is_expected.to contain_exec('disable_hibernate') } + end + + context 'enable hibernation with default settings' do + let(:params) do + { + enable: true + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::hibernate') } + + it { is_expected.to contain_exec('enable_hibernate') } + it { is_expected.not_to contain_exec('set_hiberfile_size') } + it { is_expected.not_to contain_exec('set_hiberfile_type') } + end + + context 'enable and configure hibernation' do + let(:params) do + { + enable: true, + hiberfile_size: 100, + hiberfile_type: 'full' + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::hibernate') } + + it { is_expected.to contain_exec('enable_hibernate') } + it { is_expected.to contain_exec('set_hiberfile_size').with_command('powercfg /hibernate /size 100') } + it { is_expected.to contain_exec('set_hiberfile_type').with_command('powercfg /hibernate /type full') } + end +end diff --git a/spec/classes/scheme_spec.rb b/spec/classes/scheme_spec.rb new file mode 100644 index 0000000..04ed039 --- /dev/null +++ b/spec/classes/scheme_spec.rb @@ -0,0 +1,325 @@ +require 'spec_helper' + +describe 'windows_power::scheme' do + let(:facts) do + { + os: { + windows: { + system32: 'C:\WINDOWS\system32' + } + } + } + end + + context 'activate existing power scheme' do + context 'which is not active yet' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: false + } + } + }) + end + + let(:params) do + { + guid: '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.to contain_exec('activate_power_scheme').with_provider('windows') } + + it { is_expected.not_to contain_exec('duplicate_existing_power_scheme') } + it { is_expected.not_to contain_exec('rename_power_scheme') } + end + + context 'which is active already' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: true + } + } + }) + end + + let(:params) do + { + guid: '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.not_to contain_exec('activate_power_scheme') } + + it { is_expected.not_to contain_exec('duplicate_existing_power_scheme') } + it { is_expected.not_to contain_exec('rename_power_scheme') } + end + end + + context 'activate non-existing power scheme' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + } + }) + end + + let(:params) do + { + guid: '3c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.to contain_exec('activate_power_scheme').with_provider('powershell') } + + it { is_expected.not_to contain_exec('duplicate_existing_power_scheme') } + it { is_expected.not_to contain_exec('rename_power_scheme') } + end + + context 'rename existing power scheme' do + context 'to new name' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: true, + name: 'High performance' + } + } + }) + end + + let(:params) do + { + guid: '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', + label: 'super power' + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.to contain_exec('rename_power_scheme') } + + it { is_expected.not_to contain_exec('activate_power_scheme') } + it { is_expected.not_to contain_exec('duplicate_existing_power_scheme') } + end + + context 'to matching name' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: true, + name: 'super power' + } + } + }) + end + + let(:params) do + { + guid: '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', + label: 'super power' + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.not_to contain_exec('rename_power_scheme') } + + it { is_expected.not_to contain_exec('activate_power_scheme') } + it { is_expected.not_to contain_exec('duplicate_existing_power_scheme') } + end + end + + context 'duplicate existing power scheme' do + context 'as new scheme' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + } + }) + end + + let(:params) do + { + guid: '3c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', + template: '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.to contain_exec('duplicate_existing_power_scheme') } + + it { is_expected.to contain_exec('activate_power_scheme').with_provider('powershell') } + it { is_expected.not_to contain_exec('rename_power_scheme') } + end + + context 'as existing scheme' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}, + '3c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: true + } + } + }) + end + + let(:params) do + { + guid: '3c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', + template: '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.not_to contain_exec('duplicate_existing_power_scheme') } + + it { is_expected.not_to contain_exec('activate_power_scheme') } + it { is_expected.not_to contain_exec('rename_power_scheme') } + end + end + + context 'duplicate non-existing power scheme' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + } + }) + end + + let(:params) do + { + guid: '3c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', + template: '7c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.not_to contain_exec('duplicate_existing_power_scheme') } + + it { is_expected.to contain_exec('activate_power_scheme').with_provider('powershell') } + it { is_expected.not_to contain_exec('rename_power_scheme') } + end + + context 'configure existing power scheme' do + context 'which is active already' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: true + } + } + }) + end + + let(:params) do + { + guid: '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', + settings: { + 'monitor-timeout-ac': 30, + 'monitor-timeout-dc': 10 + } + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.to contain_exec('set_power_scheme_setting_monitor-timeout-ac') } + it { is_expected.to contain_exec('set_power_scheme_setting_monitor-timeout-dc') } + + it { is_expected.not_to contain_exec('activate_power_scheme') } + it { is_expected.not_to contain_exec('duplicate_existing_power_scheme') } + it { is_expected.not_to contain_exec('rename_power_scheme') } + end + + context 'which is not active yet' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: false + } + } + }) + end + + let(:params) do + { + guid: '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', + settings: { + 'monitor-timeout-ac': 30, + 'monitor-timeout-dc': 10 + } + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.not_to contain_exec('set_power_scheme_setting_monitor-timeout-ac') } + it { is_expected.not_to contain_exec('set_power_scheme_setting_monitor-timeout-dc') } + + it { is_expected.to contain_exec('activate_power_scheme').with_provider('windows') } + it { is_expected.not_to contain_exec('duplicate_existing_power_scheme') } + it { is_expected.not_to contain_exec('rename_power_scheme') } + end + end + + context 'configure non-existing power scheme' do + let(:facts) do + super().merge({ + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + } + }) + end + + let(:params) do + { + guid: '3c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', + settings: { + 'monitor-timeout-ac': 30, + 'monitor-timeout-dc': 10 + } + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power::scheme') } + + it { is_expected.not_to contain_exec('set_power_scheme_setting_monitor-timeout-ac') } + it { is_expected.not_to contain_exec('set_power_scheme_setting_monitor-timeout-dc') } + + it { is_expected.to contain_exec('activate_power_scheme').with_provider('powershell') } + it { is_expected.not_to contain_exec('duplicate_existing_power_scheme') } + it { is_expected.not_to contain_exec('rename_power_scheme') } + end +end diff --git a/spec/classes/windows_power_spec.rb b/spec/classes/windows_power_spec.rb new file mode 100644 index 0000000..cc81a4c --- /dev/null +++ b/spec/classes/windows_power_spec.rb @@ -0,0 +1,54 @@ +require 'spec_helper' + +describe 'windows_power' do + let(:facts) do + { + os: { + windows: { + system32: 'C:\WINDOWS\system32' + } + } + } + end + + context 'with default configuration' do + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power') } + end + + context 'with example configuration' do + let(:params) do + { + devices: { + 'HID-compliant mouse (001)': { + enable_wake: true + }, + 'wmplayer.exe': { + power_request_overrides: { + process: { + display: true, + awaymode: true + } + } + }, + 'Realtek PCIe GbE Family Controller': { + enable_wake: false, + power_request_overrides: { + driver: { + display: true, + system: true + } + } + } + } + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_class('windows_power') } + + it { is_expected.to contain_windows_power__device('HID-compliant mouse (001)') } + it { is_expected.to contain_windows_power__device('wmplayer.exe') } + it { is_expected.to contain_windows_power__device('Realtek PCIe GbE Family Controller') } + end +end diff --git a/spec/defines/device_spec.rb b/spec/defines/device_spec.rb new file mode 100644 index 0000000..1d87bb0 --- /dev/null +++ b/spec/defines/device_spec.rb @@ -0,0 +1,315 @@ +require 'spec_helper' + +describe 'windows_power::device' do + let(:facts) do + { + os: { + windows: { + system32: 'C:\WINDOWS\system32' + } + } + } + end + + context 'let your mouse wake the system' do + let(:title) { 'HID-compliant mouse (001)' } + + let(:params) do + { + enable_wake: true + } + end + + context 'if it doesn\'t already' do + let(:facts) do + super().merge({ + power_devices: { + 'HID-compliant mouse (001)': { + wake_programmable: true, + wake_armed: false + } + } + }) + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('HID-compliant mouse (001)') } + + it { is_expected.to contain_exec('enable_device_wake_HID-compliant mouse (001)') } + it { is_expected.not_to contain_exec('disable_device_wake_HID-compliant mouse (001)') } + end + + context 'if it does already' do + let(:facts) do + super().merge({ + power_devices: { + 'HID-compliant mouse (001)': { + wake_programmable: true, + wake_armed: true + } + } + }) + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('HID-compliant mouse (001)') } + + it { is_expected.not_to contain_exec('enable_device_wake_HID-compliant mouse (001)') } + it { is_expected.not_to contain_exec('disable_device_wake_HID-compliant mouse (001)') } + end + + context 'if it doesn\'t exist' do + let(:facts) do + super().merge({ + power_devices: {} + }) + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('HID-compliant mouse (001)') } + + it { is_expected.not_to contain_exec('enable_device_wake_HID-compliant mouse (001)') } + it { is_expected.not_to contain_exec('disable_device_wake_HID-compliant mouse (001)') } + end + + context 'if it doesn\'t allow to' do + let(:facts) do + super().merge({ + power_devices: { + 'HID-compliant mouse (001)': { + wake_programmable: false + } + } + }) + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('HID-compliant mouse (001)') } + + it { is_expected.not_to contain_exec('enable_device_wake_HID-compliant mouse (001)') } + it { is_expected.not_to contain_exec('disable_device_wake_HID-compliant mouse (001)') } + end + end + + context 'don\'t let your mouse wake the system' do + let(:title) { 'HID-compliant mouse (001)' } + + let(:params) do + { + enable_wake: false + } + end + + context 'if it does' do + let(:facts) do + super().merge({ + power_devices: { + 'HID-compliant mouse (001)': { + wake_programmable: true, + wake_armed: true + } + } + }) + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('HID-compliant mouse (001)') } + + it { is_expected.to contain_exec('disable_device_wake_HID-compliant mouse (001)') } + it { is_expected.not_to contain_exec('enable_device_wake_HID-compliant mouse (001)') } + end + + context 'if it doesn\'t' do + let(:facts) do + super().merge({ + power_devices: { + 'HID-compliant mouse (001)': { + wake_programmable: true, + wake_armed: false + } + } + }) + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('HID-compliant mouse (001)') } + + it { is_expected.not_to contain_exec('disable_device_wake_HID-compliant mouse (001)') } + it { is_expected.not_to contain_exec('enable_device_wake_HID-compliant mouse (001)') } + end + end + + context 'if device is not in facts' do + let(:facts) do + super().merge({ + power_devices: {} + }) + end + + context 'don\'t allow Windows media player to keep system from turning off the display or from going to away mode' do + let(:title) { 'wmplayer.exe' } + + let(:params) do + { + power_request_overrides: { + process: { + display: true, + awaymode: true + } + } + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('wmplayer.exe') } + + it { is_expected.to contain_exec('set_power_request_override_process_wmplayer.exe') } + it { is_expected.not_to contain_exec('set_power_request_override_service_wmplayer.exe') } + it { is_expected.not_to contain_exec('set_power_request_override_driver_wmplayer.exe') } + end + + context 'don\'t allow your network card to keep your display turned on or keep the system active' do + let(:title) { 'Realtek PCIe GbE Family Controller' } + + let(:params) do + { + power_request_overrides: { + driver: { + display: true, + system: true + } + } + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('Realtek PCIe GbE Family Controller') } + + it { is_expected.to contain_exec('set_power_request_override_driver_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('set_power_request_override_service_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('set_power_request_override_process_Realtek PCIe GbE Family Controller') } + end + end + + context 'if device is in facts' do + context 'if current state is desired state' do + let(:title) { 'Realtek PCIe GbE Family Controller' } + + let(:facts) do + super().merge({ + power_devices: { + 'Realtek PCIe GbE Family Controller': { + power_request_overrides: { + driver: { + display: true, + system: true + } + } + } + } + }) + end + + let(:params) do + { + power_request_overrides: { + driver: { + display: true, + system: true + } + } + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('Realtek PCIe GbE Family Controller') } + + it { is_expected.not_to contain_exec('set_power_request_override_service_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('set_power_request_override_process_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('set_power_request_override_driver_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('remove_power_request_override_service_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('remove_power_request_override_process_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('remove_power_request_override_driver_Realtek PCIe GbE Family Controller') } + end + + context 'if current state is not desired state' do + context 'overwrite existing overrides' do + let(:title) { 'Realtek PCIe GbE Family Controller' } + + let(:facts) do + super().merge({ + power_devices: { + 'Realtek PCIe GbE Family Controller': { + power_request_overrides: { + driver: { + system: true, + awaymode: true + } + } + } + } + }) + end + + let(:params) do + { + power_request_overrides: { + driver: { + display: true, + system: true + } + } + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('Realtek PCIe GbE Family Controller') } + + it { is_expected.to contain_exec('set_power_request_override_driver_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('set_power_request_override_service_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('set_power_request_override_process_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('remove_power_request_override_service_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('remove_power_request_override_process_Realtek PCIe GbE Family Controller') } + it { is_expected.not_to contain_exec('remove_power_request_override_driver_Realtek PCIe GbE Family Controller') } + end + + context 'remove existing overrides' do + let(:title) { 'VPN Service' } + + let(:facts) do + super().merge({ + power_devices: { + 'VPN Service': { + power_request_overrides: { + service: { + system: true + } + } + } + } + }) + end + + let(:params) do + { + power_request_overrides: { + service: { + system: false + } + } + } + end + + it { is_expected.to compile.with_all_deps } + it { is_expected.to contain_windows_power__device('VPN Service') } + + it { is_expected.to contain_exec('remove_power_request_override_service_VPN Service') } + it { is_expected.not_to contain_exec('remove_power_request_override_process_VPN Service') } + it { is_expected.not_to contain_exec('remove_power_request_override_driver_VPN Service') } + it { is_expected.not_to contain_exec('set_power_request_override_service_VPN Service') } + it { is_expected.not_to contain_exec('set_power_request_override_process_VPN Service') } + it { is_expected.not_to contain_exec('set_power_request_override_driver_VPN Service') } + end + end + end +end diff --git a/spec/defines/devices/override_spec.rb b/spec/defines/devices/override_spec.rb deleted file mode 100644 index 23a73aa..0000000 --- a/spec/defines/devices/override_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'windows_power::devices::override', type: :define do - describe 'requestsoverride of PROCESS' do - let(:title) { 'wmplayer.exe' } - let(:params) do - { type: 'PROCESS', request: 'Display' } - end - - it do - is_expected.to contain_exec('request override for wmplayer.exe').with( - 'provider' => 'windows', - 'command' => 'powercfg /requestsoverride PROCESS wmplayer.exe Display' - ) - end - - it { is_expected.to compile } - end - - describe 'requestsoverride of SERVICE' do - let(:title) { 'MpsSvc' } - let(:params) do - { type: 'SERVICE', request: 'System' } - end - - it do - is_expected.to contain_exec('request override for MpsSvc').with( - 'provider' => 'windows', - 'command' => 'powercfg /requestsoverride SERVICE MpsSvc System' - ) - end - - it { is_expected.to compile } - end -end diff --git a/spec/defines/devices/wake_spec.rb b/spec/defines/devices/wake_spec.rb deleted file mode 100644 index 634bda5..0000000 --- a/spec/defines/devices/wake_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'windows_power::devices::wake', type: :define do - describe 'enabling wake' do - let(:title) { 'VMBus Enumerator (001)' } - let(:params) do - { device: 'VMBus Enumerator (001)', ensure: 'enable' } - end - - it do - is_expected.to contain_exec('device VMBus Enumerator (001) enable wake').with( - 'provider' => 'windows', - 'command' => 'powercfg /deviceenablewake "VMBus Enumerator (001)"' - ) - end - - it { is_expected.to compile } - end - - describe 'disabling wake' do - let(:title) { 'VMBus Enumerator (001)' } - let(:params) do - { device: 'VMBus Enumerator (001)', ensure: 'disable' } - end - - it do - is_expected.to contain_exec('device VMBus Enumerator (001) disable wake').with( - 'provider' => 'windows', - 'command' => 'powercfg /devicedisablewake "VMBus Enumerator (001)"' - ) - end - - it { is_expected.to compile } - end -end diff --git a/spec/defines/global/hibernation_spec.rb b/spec/defines/global/hibernation_spec.rb deleted file mode 100644 index 89e8ef4..0000000 --- a/spec/defines/global/hibernation_spec.rb +++ /dev/null @@ -1,37 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'windows_power::global::hibernation', type: :define do - describe 'updating hibernate on' do - let(:title) { 'hibernate on' } - let(:params) do - { status: 'on' } - end - - it do - is_expected.to contain_exec('update hibernate status').with( - 'provider' => 'windows', - 'command' => 'powercfg /hibernate on' - ) - end - - it { is_expected.to compile } - end - - describe 'updating hibernate off' do - let(:title) { 'hibernate off' } - let(:params) do - { status: 'off' } - end - - it do - is_expected.to contain_exec('update hibernate status').with( - 'provider' => 'windows', - 'command' => 'powercfg /hibernate off' - ) - end - - it { is_expected.to compile } - end -end diff --git a/spec/defines/schemes/scheme_spec.rb b/spec/defines/schemes/scheme_spec.rb deleted file mode 100644 index 00167ab..0000000 --- a/spec/defines/schemes/scheme_spec.rb +++ /dev/null @@ -1,96 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'windows_power::schemes::scheme', type: :define do - describe 'creating inactive scheme' do - let(:title) { 'inactive_scheme' } - let(:params) do - { - scheme_name: 'inactive_scheme', - scheme_guid: '381b4222-f694-41f0-9685-ff5bbxx65ddx', - template_scheme: '381b4222-f694-41f0-9685-ff5bb260df2e', - activation: 'inactive', - ensure: 'present' - } - end - - it do - is_expected.to contain_exec('create power scheme inactive_scheme').with( - 'provider' => 'powershell', - 'command' => '& powercfg /duplicatescheme 381b4222-f694-41f0-9685-ff5bb260df2e 381b4222-f694-41f0-9685-ff5bbxx65ddx' - ) - end - - it do - is_expected.to contain_exec('rename scheme to inactive_scheme').with( - 'provider' => 'powershell', - 'command' => '& powercfg /changename 381b4222-f694-41f0-9685-ff5bbxx65ddx \'inactive_scheme\'' - ) - end - - it { is_expected.not_to contain_exec('set inactive_scheme scheme as active') } - it { is_expected.not_to contain_exec('delete power scheme inactive_scheme') } - it { is_expected.to compile } - end - - describe 'creating and activating scheme' do - let(:title) { 'active_scheme' } - let(:params) do - { - scheme_name: 'active_scheme', - scheme_guid: '381b4222-f694-41f0-9685-ff5bbxx65ddy', - template_scheme: '381b4222-f694-41f0-9685-ff5bb260df2e', - activation: 'active', - ensure: 'present' - } - - it do - is_expected.to contain_exec('create power scheme active_scheme').with( - 'provider' => 'powershell', - 'command' => '& powercfg /duplicatescheme 381b4222-f694-41f0-9685-ff5bb260df2e 381b4222-f694-41f0-9685-ff5bbxx65ddy' - ) - end - - it do - is_expected.to contain_exec('rename scheme to active_scheme').with( - 'provider' => 'powershell', - 'command' => '& powercfg /changename 381b4222-f694-41f0-9685-ff5bbxx65ddy \'active_scheme\'' - ) - end - - it do - is_expected.to contain_exec('set active_scheme scheme as active').with( - 'provider' => 'powershell', - 'command' => '& powercfg /setactive 381b4222-f694-41f0-9685-ff5bbxx65ddy' - ) - end - - it { is_expected.not_to contain_exec('delete power scheme active_scheme') } - it { is_expected.to compile } - end - - describe 'deleting scheme' do - let(:title) { 'delete_scheme' } - let(:params) do - { - scheme_name: 'delete_scheme', - scheme_guid: '381b4222-f694-41f0-9685-ff5bbxx65ddz', - template_scheme: '381b4222-f694-41f0-9685-ff5bb260df2e', - activation: 'inactive', - ensure: 'absent' - } - - it do - is_expected.to contain_exec('delete power scheme delete_scheme').with( - 'provider' => 'powershell', - 'command' => '& powercfg /delete 381b4222-f694-41f0-9685-ff5bbxx65ddz' - ) - end - - it { is_expected.not_to contain_exec('create power scheme delete_scheme') } - it { is_expected.not_to contain_exec('rename scheme to delete_scheme') } - it { is_expected.not_to contain_exec('set delete_scheme scheme as active') } - it { is_expected.to compile } - end -end diff --git a/spec/defines/schemes/settings_spec.rb b/spec/defines/schemes/settings_spec.rb deleted file mode 100644 index c9383c7..0000000 --- a/spec/defines/schemes/settings_spec.rb +++ /dev/null @@ -1,21 +0,0 @@ -# frozen_string_literal: true - -require 'spec_helper' - -describe 'windows_power::schemes::settings', type: :define do - describe 'modifying setting disk-timeout-ac' do - let(:title) { 'setting disk timeout (ac)' } - let(:params) do - { scheme_name: 'current_scheme', setting: 'disk-timeout-ac', value: 10 } - end - - it do - is_expected.to contain_exec('modify disk-timeout-ac setting for current_scheme').with( - 'provider' => 'powershell', - 'command' => '& powercfg /change disk-timeout-ac 10' - ) - end - - it { is_expected.to compile } - end -end From 4211a1f8fd52d8081bbaaf77e429a5700914d02c Mon Sep 17 00:00:00 2001 From: Lightning Date: Tue, 30 Apr 2024 15:58:39 +0200 Subject: [PATCH 14/18] add REFERENCE.md --- REFERENCE.md | 343 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 343 insertions(+) create mode 100644 REFERENCE.md diff --git a/REFERENCE.md b/REFERENCE.md new file mode 100644 index 0000000..e39fa92 --- /dev/null +++ b/REFERENCE.md @@ -0,0 +1,343 @@ +# Reference + + + +## Table of Contents + +### Classes + +* [`windows_power`](#windows_power): class to manage Windows power devices (physical, logical and virtual) +* [`windows_power::hibernate`](#windows_power--hibernate): class to manage Windows hibernate settings +* [`windows_power::scheme`](#windows_power--scheme): class to manage Windows power scheme + +### Defined types + +* [`windows_power::device`](#windows_power--device): defined type to manage a Windows power device (physical, logical and virtual) + +## Classes + +### `windows_power` + +class to manage Windows power devices (physical, logical and virtual) + +* **See also** + * windows_power::device + +#### Examples + +##### basic usage + +```puppet +class { 'windows_power': + devices => { + 'HID-compliant mouse (001)' => { + enable_wake => true + }, + 'wmplayer.exe' => { + power_request_overrides => { + process => { + display => true, + awaymode => true + } + } + }, + 'Realtek PCIe GbE Family Controller' => { + enable_wake => false, + power_request_overrides => { + driver => { + display => true, + system => true + } + } + } + } +} +``` + +#### Parameters + +The following parameters are available in the `windows_power` class: + +* [`devices`](#-windows_power--devices) + +##### `devices` + +Data type: `Optional[Hash[String[1], Hash[Pattern[/^[a-z][a-z0-9_]*$/], Data, 1], 1]]` + +hash of devices/drivers/services/tools to manage and what/how + +Default value: `undef` + +### `windows_power::hibernate` + +class to manage Windows hibernate settings + +#### Examples + +##### disable hibernation system wide + +```puppet +class { 'windows_power::hibernate': + enable => false +} +``` + +##### enable hibernation with default settings + +```puppet +class { 'windows_power::hibernate': + enable => true +} +``` + +##### enable and configure hibernation + +```puppet +class { 'windows_power::hibernate': + enable => true, + hiberfile_size => 100, + hiberfile_type => 'full' +} +``` + +#### Parameters + +The following parameters are available in the `windows_power::hibernate` class: + +* [`enable`](#-windows_power--hibernate--enable) +* [`hiberfile_size`](#-windows_power--hibernate--hiberfile_size) +* [`hiberfile_type`](#-windows_power--hibernate--hiberfile_type) + +##### `enable` + +Data type: `Boolean` + +enable/disable the hibernate feature + +##### `hiberfile_size` + +Data type: `Optional[Integer[40, 100]]` + +set desired hiberfile size (percentage of total memory, 40-100) + +Default value: `undef` + +##### `hiberfile_type` + +Data type: `Optional[Enum['reduced', 'full']]` + +set desired hiberfile type (`reduced`/`full`) + +Default value: `undef` + +### `windows_power::scheme` + +class to manage Windows power scheme + +#### Examples + +##### activate the "High performance" system scheme + +```puppet +class { 'windows_power::scheme': + guid => '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c' +} +``` + +##### activate the "Balanced" system scheme and set some monitor timeouts + +```puppet +class { 'windows_power::scheme': + guid => '381b4222-f694-41f0-9685-ff5bb260df2e', + settings => { + monitor-timeout-ac => 30, + monitor-timeout-dc => 10 + } +} +``` + +##### create a custom template inheriting "High performance" and tweak on that + +```puppet +class { 'windows_power::scheme': + guid => 'a1582e9e-9c9d-46fd-afdf-4d989292a073', + label => 'really full power', + template => '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c', + settings => { + disk-timeout-ac => 0, + disk-timeout-dc => 0, + standby-timeout-ac => 0, + standby-timeout-dc => 0, + hibernate-timeout-ac => 0, + hibernate-timeout-dc => 0 + } +} +``` + +#### Parameters + +The following parameters are available in the `windows_power::scheme` class: + +* [`guid`](#-windows_power--scheme--guid) +* [`label`](#-windows_power--scheme--label) +* [`template`](#-windows_power--scheme--template) +* [`description`](#-windows_power--scheme--description) +* [`settings`](#-windows_power--scheme--settings) + +##### `guid` + +Data type: `Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/]` + +GUID of the scheme to create/activate; +to activate/handle an existing scheme (e.g. the ones provided by the system) don't define a template; +to create (and activate and handle) a new scheme (derived from an existing one) define the template. + +##### `label` + +Data type: `Optional[String[1]]` + +desired label/name/title of the scheme (optional, recommended for custom schemes) + +Default value: `undef` + +##### `template` + +Data type: `Optional[Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/]]` + +GUID of the template scheme to use for creating a new custom scheme (optional); +if the template does not exist no action is performed; so it's safe to define a template that will appear later somehow. + +Default value: `undef` + +##### `description` + +Data type: `Optional[String[1]]` + +desired descriptive text of the scheme (optional) + +Default value: `undef` + +##### `settings` + +Data type: + +```puppet +Optional[Hash[Enum[ + 'monitor-timeout-ac', + 'monitor-timeout-dc', + 'disk-timeout-ac', + 'disk-timeout-dc', + 'standby-timeout-ac', + 'standby-timeout-dc', + 'hibernate-timeout-ac', + 'hibernate-timeout-dc' + ], Integer[0], 1, 8]] +``` + +settings to change (optional); +settings are applied to the active scheme and only if this matches the declared GUID; this way accidential configuration of the wrong +scheme is avoided but it might need more than one puppet run to complete all tasks. + +Default value: `undef` + +## Defined types + +### `windows_power::device` + +defined type to manage a Windows power device (physical, logical and virtual) + +#### Examples + +##### let your mouse wake the system + +```puppet +windows_power::device { 'HID-compliant mouse (001)': + enable_wake => true +} +``` + +##### don't allow Windows media player to keep system from turning off the display or from going to away mode + +```puppet +windows_power::device { 'wmplayer.exe': + power_request_overrides => { + process => { + display => true, + awaymode => true + } + } +} +``` + +##### don't allow your network card to wake the system, keep your display turned on or keep the system active + +```puppet +windows_power::device { 'Realtek PCIe GbE Family Controller': + enable_wake => false, + power_request_overrides => { + driver => { + display => true, + system => true + } + } +} +``` + +##### delete all power request overrides for/from vpn service + +```puppet +windows_power::device { 'VPN Service': + power_request_overrides => { + service => { + system => false + } + } +} +``` + +#### Parameters + +The following parameters are available in the `windows_power::device` defined type: + +* [`device`](#-windows_power--device--device) +* [`enable_wake`](#-windows_power--device--enable_wake) +* [`power_request_overrides`](#-windows_power--device--power_request_overrides) + +##### `device` + +Data type: `String[1]` + +name of the device/driver/service/process to handle, defaulting to resource's title (no need to set this manually); +also see the shipped fact `power_devices`! +note that the term "device" covers several things (due to the nature of Windows' power management): +- physical devices build in or connected to the machine (such as a network card or a mouse) +- logical devices or device groups (such as "HID-compliant system controller" or even "Volume (005)") +- drivers or driver groups (such as "High Definition Audio Device") +- services (e.g. your remote management software's service name) +- processes (e.g. "your_media_player.exe") + +Default value: `$title` + +##### `enable_wake` + +Data type: `Optional[Boolean]` + +allow (or prohibit) the device to wake the system from a sleep state; +devices capable of waking the system do have `$facts['power_devices'][$device]['wake_programmable'] == true`; +devices currently allowed waking the system do have `$facts['power_devices'][$device]['wake_armed'] == true`; +it's safe to set `enable_wake => true` even if the device is not able to do; +defined type only activates wake-up functionality if the device is reported to be capable of doing so! + +Default value: `undef` + +##### `power_request_overrides` + +Data type: `Optional[Hash[Enum['service', 'process', 'driver'], Hash[Enum['display', 'system', 'awaymode'], Boolean, 1, 3], 1, 3]]` + +set (or delete) one or more power request overrides for the device (see Microsoft documentation about power requests and overrides); +note that not defining a request type is similar to setting it to `false` but not defining any request type means "don't touch the +current state"; so explicitly setting (only) `false` values makes it possible to delete request overrides set outside of Puppet! +see examples for clarity. + +Default value: `undef` + From dd066891088076be0a2e15fa68ce8f7a40992914 Mon Sep 17 00:00:00 2001 From: Lightning Date: Fri, 3 May 2024 10:54:40 +0200 Subject: [PATCH 15/18] update README.md --- README.md | 193 +++++++----------------------------------------------- 1 file changed, 22 insertions(+), 171 deletions(-) diff --git a/README.md b/README.md index 8de93c0..4ca190c 100644 --- a/README.md +++ b/README.md @@ -1,187 +1,38 @@ # puppet-windows_power -#### Table of Contents +## Module description -1. [Overview](#overview) -2. [Module Description - What is the windows_power module?](#module-description) -3. [Setup - The basics of getting started with windows_power](#setup) - * [What windows_power affects](#what-power-affects) - * [Setup requirements](#setup-requirements) - * [Beginning with windows_power](#beginning-with-power) -4. [Usage - Configuration options and additional functionality](#usage) -5. [Reference - An under-the-hood peek at what the module is doing and how](#reference) -5. [Limitations - OS compatibility, etc.](#limitations) -6. [Development - Guide for contributing to the module](#development) +Puppet module for managing Windows hibernation settings, power schemes and power devices (or power request overrides respectively). -## Overview +## Setup, Usage, Reference -Puppet module for managing windows power settings +Summary: +- use class `windows_power` to manage Windows power devices (physical, logical and virtual); this wraps the defined type `windows_power::device` +- use class `windows_power::hibernate` to manage Windows hibernate settings +- use class `windows_power::scheme` to manage Windows power scheme +- shipped fact `power_devices` contains all kind of system's power devices, their various wakeup capabilities and their power request overrides +- shipped fact `power_schemes` contains the system's power schemes and their activation state -[![Build Status](https://travis-ci.org/voxpupuli/puppet-windows_power.svg?branch=master)](https://travis-ci.org/voxpupuli/puppet-windows_power) -## Module Description +See [REFERENCE.md](REFERENCE.md) for further details and practical examples. -The purpose of this module is to manage each of the windows power schemes and the various global power settings +## Special notice for v4.0.0 -## Setup - -### What windows_power affects - -* Creates new power schemes (which will alter registry settings) - -### Beginning with windows_power - -Create new power scheme: - -```puppet -windows_power::schemes::scheme { 'test scheme': - scheme_name => 'test', - scheme_guid => '381b4222-f694-41f0-9685-ff5bbxx65ddx', - template_scheme => '381b4222-f694-41f0-9685-ff5bb260df2e', - activation => 'active', - ensure => 'present', -} -``` - -Set monitor timeout in 'Balanced' power scheme to 10 minutes: - -```puppet -windows_power::schemes::settings { 'set monitor timeout': - scheme_name => 'SCHEME_BALANCED', - setting => 'monitor-timeout-ac', - value => '10', -} - -``` - -## Usage - -### Classes and Defined Types: - -#### Defined Type: `windows_power::schemes::scheme` - -**Parameters within `windows_power::schemes::scheme`:** - -##### `scheme_name` - -The name of the scheme to configure - -##### `scheme_guid` - -The windows guid used to uniquely identify the power scheme - -##### `template_scheme` - -The windows guid of an existing scheme to be used as a template for the current scheme - -##### `activation` - -Set the current scheme as the active scheme - -##### `ensure` - -Configure if the scheme is present or absent -The initial version - -#### Defined Type: `windows_power::schemes::settings` - -**Parameters within `windows_power::schemes::settings`:** - -##### `scheme_name` - -The name of the scheme to configure - -##### `setting` - -The setting to configure - -##### `value` - -The value set the setting to - minutes or throttle - -#### Defined Type: `windows_power::global::battery` - -**Parameters within `windows_power::global::battery`:** - -##### `setting` - -Battery alarm setting to The initial versionconfigure - -##### `status` - -Setting configuration (on/off) or percentage (in the case of the level setting) - -##### `criticality` - -The level of battery criticality at which to provide an alarm. LOW or HIGH. - -#### Defined Type: `windows_power::global::flags` - -**Parameters within `windows_power::global::flags`:** - -##### `setting` - -The global power flag to configure - -##### `status` - -Setting configuration (on/off) - -#### Defined Type: `windows_power::global::hiberation` - -**Parameters within `windows_power::global::hibernation`:** - -##### `status` - -Setting configuration (on/off) - -#### Defined Type: `windows_power::devices::override` - -**Parameters within `windows_power::devices::override`:** - -##### `type` - -Specifies one of the following caller types: PROCESS, SERVICE, DRIVER - -##### `request` - -Specifies one or more of the following Power Request Types: Display, System, Awaymode - -#### Defined Type: `windows_power::devices::wake` - -**Parameters within `windows_power::devices::wake`:** - -##### `device` - -Specifies the device name - -##### `ensure` - -Enable or disable the device for waking - -## Reference - -### Defined Types: - -#### Public Defined Types: - -* [`windows_power::schemes::scheme`](#define-schemes-scheme): Guides the management of windows power schemes -* [`windows_power::schemes::settings`](#define-schemes-settings): Configures individual settings with a given scheme. -* [`windows_power::global::battery`](#define-global-battery): Configure power battery alarms. -* [`windows_power::global::flags`](#define-global-flags): Configure the global settings for windows power schemes -* [`windows_power::global::hiberation`](#define-global-hibernation): Configure the hibernation setting -* [`windows_power::devices::override`](#define-devices-override): Configure power overrides for certain devices -* [`windows_power::devices::wake`](#define-devices-wake): Configure the device wake settings +Version 4.0.0 is a complete rewrite and modernization of the previous module and breaks with configuration compatibility. +It also drops support for legacy Windows systems and removes functionality targeting those legacy systems. If you have any of those systems in place (such as Windows XP or Windows Server 2008) don't update. ## Limitations -This module is tested on the following platforms: +Due to the nature of Windows' way to configure things, we can't just write a config file with desired settings and tell Windows to apply that (or something similar). As well we're not able to determine the current state of some settings (e.g. the timeouts in a power scheme) at all. +Therefore, some commands are applied with every Puppet run to ensure the desired state. Additionally, it might need more than one Puppet run to achieve the final state. +We know that this is very bad Puppet style and we did our best to keep this as minimal as possible. As well we put a lot of effort into making the module fail-safe (e.g. not configuring the wrong power scheme if a new one was created but hasn't been successfully activated yet) at the cost of "might need more than one run". -* Windows 2008 R2 +This module assumes that your system is not managed by an Active Directory controller. We have no way of testing what happens if your system (or parts of it including the power management) is controlled this way and you should use the AD's capabilities of managing power-things then anyways. -It is tested with the OSS version of Puppet only. +Also note that device names, scheme names and similar are subjected to localization (whyever ...). This means that you will have a device `System timer` on an English system and a `Systemzeitgeber` on a German system for example. Configuring devices and overrides in an environment with mixed installation languages is probably quite annoying as you will have to create different profiles for different languages with the corresponding device names. -## Development +If you have any ideas, suggestions, improvements ... please get in touch with us. -### Contributing +## Development -Please read CONTRIBUTING.md for full details on contributing to this project. +Development happens on GitHub in the well known way (fork, PR, issue, etc.). +Please feel free to report problems, suggest improvements, drop new ideas or teach us how to handle things better. From 850ca415e06d04794941101b8822d08cd25d7906 Mon Sep 17 00:00:00 2001 From: Lightning Date: Mon, 6 May 2024 16:34:21 +0200 Subject: [PATCH 16/18] fix linter warnings --- manifests/scheme.pp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/manifests/scheme.pp b/manifests/scheme.pp index 1186345..87350c6 100644 --- a/manifests/scheme.pp +++ b/manifests/scheme.pp @@ -54,14 +54,14 @@ Optional[Pattern[/\A[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}\z/]] $template = undef, Optional[String[1]] $description = undef, Optional[Hash[Enum[ - 'monitor-timeout-ac', - 'monitor-timeout-dc', - 'disk-timeout-ac', - 'disk-timeout-dc', - 'standby-timeout-ac', - 'standby-timeout-dc', - 'hibernate-timeout-ac', - 'hibernate-timeout-dc' + 'monitor-timeout-ac', + 'monitor-timeout-dc', + 'disk-timeout-ac', + 'disk-timeout-dc', + 'standby-timeout-ac', + 'standby-timeout-dc', + 'hibernate-timeout-ac', + 'hibernate-timeout-dc' ], Integer[0], 1, 8]] $settings = undef, ) { if $template !~ Undef { From 0ab971bbb427e9a6f4982353c805a3cb331a9e32 Mon Sep 17 00:00:00 2001 From: Lightning Date: Wed, 8 May 2024 15:10:03 +0200 Subject: [PATCH 17/18] reformat README.md voxpupuli CI pipeline treats trailing whitespaces in a markdown file as errors, which makes it impossible to have explicit linebreaks without starting a new paragraph; so we add a bunch of paragraphs instead. --- README.md | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 4ca190c..cfd051e 100644 --- a/README.md +++ b/README.md @@ -17,13 +17,16 @@ See [REFERENCE.md](REFERENCE.md) for further details and practical examples. ## Special notice for v4.0.0 -Version 4.0.0 is a complete rewrite and modernization of the previous module and breaks with configuration compatibility. +Version 4.0.0 is a complete rewrite and modernization of the previous module and breaks with configuration compatibility. + It also drops support for legacy Windows systems and removes functionality targeting those legacy systems. If you have any of those systems in place (such as Windows XP or Windows Server 2008) don't update. ## Limitations -Due to the nature of Windows' way to configure things, we can't just write a config file with desired settings and tell Windows to apply that (or something similar). As well we're not able to determine the current state of some settings (e.g. the timeouts in a power scheme) at all. -Therefore, some commands are applied with every Puppet run to ensure the desired state. Additionally, it might need more than one Puppet run to achieve the final state. +Due to the nature of Windows' way to configure things, we can't just write a config file with desired settings and tell Windows to apply that (or something similar). As well we're not able to determine the current state of some settings (e.g. the timeouts in a power scheme) at all. + +Therefore, some commands are applied with every Puppet run to ensure the desired state. Additionally, it might need more than one Puppet run to achieve the final state. + We know that this is very bad Puppet style and we did our best to keep this as minimal as possible. As well we put a lot of effort into making the module fail-safe (e.g. not configuring the wrong power scheme if a new one was created but hasn't been successfully activated yet) at the cost of "might need more than one run". This module assumes that your system is not managed by an Active Directory controller. We have no way of testing what happens if your system (or parts of it including the power management) is controlled this way and you should use the AD's capabilities of managing power-things then anyways. @@ -34,5 +37,6 @@ If you have any ideas, suggestions, improvements ... please get in touch with us ## Development -Development happens on GitHub in the well known way (fork, PR, issue, etc.). +Development happens on GitHub in the well known way (fork, PR, issue, etc.). + Please feel free to report problems, suggest improvements, drop new ideas or teach us how to handle things better. From 95368d1f057b21d943f9ae31d95588cad21f00c7 Mon Sep 17 00:00:00 2001 From: Lightning Date: Wed, 8 May 2024 17:19:14 +0200 Subject: [PATCH 18/18] satisfy rubocop --- lib/facter/power_devices.rb | 42 +++++---- lib/facter/power_schemes.rb | 6 ++ spec/classes/hibernate_spec.rb | 2 + spec/classes/scheme_spec.rb | 132 ++++++++++++++++----------- spec/classes/windows_power_spec.rb | 2 + spec/defines/device_spec.rb | 138 +++++++++++++++++------------ 6 files changed, 192 insertions(+), 130 deletions(-) diff --git a/lib/facter/power_devices.rb b/lib/facter/power_devices.rb index f564e16..ee5e279 100644 --- a/lib/facter/power_devices.rb +++ b/lib/facter/power_devices.rb @@ -1,23 +1,27 @@ -Facter.add(:power_devices, :type => :aggregate) do +# frozen_string_literal: true + +# rubocop:disable Style/RegexpLiteral + +Facter.add(:power_devices, type: :aggregate) do confine kernel: 'windows' chunk(:devices) do - all_devices = Facter::Core::Execution.execute('powercfg /devicequery all_devices').split(/[\n\r]+/).reject{|x| x.eql?('')} - wake_programmable = Facter::Core::Execution.execute('powercfg /devicequery wake_programmable').split(/[\n\r]+/).reject{|x| x.eql?('')} - wake_armed = Facter::Core::Execution.execute('powercfg /devicequery wake_armed').split(/[\n\r]+/).reject{|x| x.eql?('')} - wake_from_any = Facter::Core::Execution.execute('powercfg /devicequery wake_from_any').split(/[\n\r]+/).reject{|x| x.eql?('')} - wake_from_s1_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s1_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} - wake_from_s2_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s2_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} - wake_from_s3_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s3_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} - s1_supported = Facter::Core::Execution.execute('powercfg /devicequery s1_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} - s2_supported = Facter::Core::Execution.execute('powercfg /devicequery s2_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} - s3_supported = Facter::Core::Execution.execute('powercfg /devicequery s3_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} - s4_supported = Facter::Core::Execution.execute('powercfg /devicequery s4_supported').split(/[\n\r]+/).reject{|x| x.eql?('')} + all_devices = Facter::Core::Execution.execute('powercfg /devicequery all_devices').split(/[\n\r]+/).reject { |x| x.eql?('') } + wake_programmable = Facter::Core::Execution.execute('powercfg /devicequery wake_programmable').split(/[\n\r]+/).reject { |x| x.eql?('') } + wake_armed = Facter::Core::Execution.execute('powercfg /devicequery wake_armed').split(/[\n\r]+/).reject { |x| x.eql?('') } + wake_from_any = Facter::Core::Execution.execute('powercfg /devicequery wake_from_any').split(/[\n\r]+/).reject { |x| x.eql?('') } + wake_from_s1_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s1_supported').split(/[\n\r]+/).reject { |x| x.eql?('') } + wake_from_s2_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s2_supported').split(/[\n\r]+/).reject { |x| x.eql?('') } + wake_from_s3_supported = Facter::Core::Execution.execute('powercfg /devicequery wake_from_s3_supported').split(/[\n\r]+/).reject { |x| x.eql?('') } + s1_supported = Facter::Core::Execution.execute('powercfg /devicequery s1_supported').split(/[\n\r]+/).reject { |x| x.eql?('') } + s2_supported = Facter::Core::Execution.execute('powercfg /devicequery s2_supported').split(/[\n\r]+/).reject { |x| x.eql?('') } + s3_supported = Facter::Core::Execution.execute('powercfg /devicequery s3_supported').split(/[\n\r]+/).reject { |x| x.eql?('') } + s4_supported = Facter::Core::Execution.execute('powercfg /devicequery s4_supported').split(/[\n\r]+/).reject { |x| x.eql?('') } devices = {} all_devices.each do |device| - devices[device.to_sym] = {} unless devices.has_key?(device.to_sym) + devices[device.to_sym] = {} unless devices.key?(device.to_sym) devices[device.to_sym].store(:wake_programmable, true) if wake_programmable.include?(device) devices[device.to_sym].store(:wake_armed, true) if wake_armed.include?(device) devices[device.to_sym].store(:wake_from_any, true) if wake_from_any.include?(device) @@ -30,7 +34,7 @@ devices[device.to_sym].store(:s4_supported, true) if s4_supported.include?(device) end - devices.delete_if{|key, value| value.empty?} + devices.delete_if { |_key, value| value.empty? } devices end @@ -46,12 +50,12 @@ caller_type = match[1].downcase.to_sym next end - if caller_type != :unknown + if caller_type != :unknown # rubocop:disable Style/Next line.match(/^(.*?)((?:\s(DISPLAY|SYSTEM|AWAYMODE)(?!.*\b\3\b))+)$/) do |match| request = match[2].strip.split - devices[match[1].to_sym] = {} unless devices.has_key?(match[1].to_sym) - devices[match[1].to_sym][:power_request_overrides] = {} unless devices[match[1].to_sym].has_key?(:power_request_overrides) - devices[match[1].to_sym][:power_request_overrides][caller_type] = {} unless devices[match[1].to_sym][:power_request_overrides].has_key?(caller_type) + devices[match[1].to_sym] = {} unless devices.key?(match[1].to_sym) + devices[match[1].to_sym][:power_request_overrides] = {} unless devices[match[1].to_sym].key?(:power_request_overrides) + devices[match[1].to_sym][:power_request_overrides][caller_type] = {} unless devices[match[1].to_sym][:power_request_overrides].key?(caller_type) devices[match[1].to_sym][:power_request_overrides][caller_type].store(:display, true) if request.include?('DISPLAY') devices[match[1].to_sym][:power_request_overrides][caller_type].store(:system, true) if request.include?('SYSTEM') devices[match[1].to_sym][:power_request_overrides][caller_type].store(:awaymode, true) if request.include?('AWAYMODE') @@ -63,3 +67,5 @@ devices end end + +# rubocop:enable Style/RegexpLiteral diff --git a/lib/facter/power_schemes.rb b/lib/facter/power_schemes.rb index a245787..27cc512 100644 --- a/lib/facter/power_schemes.rb +++ b/lib/facter/power_schemes.rb @@ -1,3 +1,7 @@ +# frozen_string_literal: true + +# rubocop:disable Style/RegexpLiteral + Facter.add(:power_schemes) do confine kernel: 'windows' @@ -18,3 +22,5 @@ power_schemes end end + +# rubocop:enable Style/RegexpLiteral diff --git a/spec/classes/hibernate_spec.rb b/spec/classes/hibernate_spec.rb index e1e734e..e94bb00 100644 --- a/spec/classes/hibernate_spec.rb +++ b/spec/classes/hibernate_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'windows_power::hibernate' do diff --git a/spec/classes/scheme_spec.rb b/spec/classes/scheme_spec.rb index 04ed039..11bfa66 100644 --- a/spec/classes/scheme_spec.rb +++ b/spec/classes/scheme_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'windows_power::scheme' do @@ -14,13 +16,15 @@ context 'activate existing power scheme' do context 'which is not active yet' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { - active: false + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: false + } } } - }) + ) end let(:params) do @@ -40,13 +44,15 @@ context 'which is active already' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { - active: true + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: true + } } } - }) + ) end let(:params) do @@ -67,11 +73,13 @@ context 'activate non-existing power scheme' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + } } - }) + ) end let(:params) do @@ -92,14 +100,16 @@ context 'rename existing power scheme' do context 'to new name' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { - active: true, - name: 'High performance' + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: true, + name: 'High performance' + } } } - }) + ) end let(:params) do @@ -120,14 +130,16 @@ context 'to matching name' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { - active: true, - name: 'super power' + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: true, + name: 'super power' + } } } - }) + ) end let(:params) do @@ -150,11 +162,13 @@ context 'duplicate existing power scheme' do context 'as new scheme' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + } } - }) + ) end let(:params) do @@ -175,14 +189,16 @@ context 'as existing scheme' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}, - '3c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { - active: true + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {}, + '3c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: true + } } } - }) + ) end let(:params) do @@ -204,11 +220,13 @@ context 'duplicate non-existing power scheme' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + } } - }) + ) end let(:params) do @@ -230,13 +248,15 @@ context 'configure existing power scheme' do context 'which is active already' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { - active: true + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: true + } } } - }) + ) end let(:params) do @@ -262,13 +282,15 @@ context 'which is not active yet' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { - active: false + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': { + active: false + } } } - }) + ) end let(:params) do @@ -295,11 +317,13 @@ context 'configure non-existing power scheme' do let(:facts) do - super().merge({ - power_schemes: { - '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + super().merge( + { + power_schemes: { + '8c5e7fda-e8bf-4a96-9a85-a6e23a8c635c': {} + } } - }) + ) end let(:params) do diff --git a/spec/classes/windows_power_spec.rb b/spec/classes/windows_power_spec.rb index cc81a4c..138cc26 100644 --- a/spec/classes/windows_power_spec.rb +++ b/spec/classes/windows_power_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'windows_power' do diff --git a/spec/defines/device_spec.rb b/spec/defines/device_spec.rb index 1d87bb0..e107067 100644 --- a/spec/defines/device_spec.rb +++ b/spec/defines/device_spec.rb @@ -1,3 +1,5 @@ +# frozen_string_literal: true + require 'spec_helper' describe 'windows_power::device' do @@ -22,14 +24,16 @@ context 'if it doesn\'t already' do let(:facts) do - super().merge({ - power_devices: { - 'HID-compliant mouse (001)': { - wake_programmable: true, - wake_armed: false + super().merge( + { + power_devices: { + 'HID-compliant mouse (001)': { + wake_programmable: true, + wake_armed: false + } } } - }) + ) end it { is_expected.to compile.with_all_deps } @@ -41,14 +45,16 @@ context 'if it does already' do let(:facts) do - super().merge({ - power_devices: { - 'HID-compliant mouse (001)': { - wake_programmable: true, - wake_armed: true + super().merge( + { + power_devices: { + 'HID-compliant mouse (001)': { + wake_programmable: true, + wake_armed: true + } } } - }) + ) end it { is_expected.to compile.with_all_deps } @@ -60,9 +66,11 @@ context 'if it doesn\'t exist' do let(:facts) do - super().merge({ - power_devices: {} - }) + super().merge( + { + power_devices: {} + } + ) end it { is_expected.to compile.with_all_deps } @@ -74,13 +82,15 @@ context 'if it doesn\'t allow to' do let(:facts) do - super().merge({ - power_devices: { - 'HID-compliant mouse (001)': { - wake_programmable: false + super().merge( + { + power_devices: { + 'HID-compliant mouse (001)': { + wake_programmable: false + } } } - }) + ) end it { is_expected.to compile.with_all_deps } @@ -102,14 +112,16 @@ context 'if it does' do let(:facts) do - super().merge({ - power_devices: { - 'HID-compliant mouse (001)': { - wake_programmable: true, - wake_armed: true + super().merge( + { + power_devices: { + 'HID-compliant mouse (001)': { + wake_programmable: true, + wake_armed: true + } } } - }) + ) end it { is_expected.to compile.with_all_deps } @@ -121,14 +133,16 @@ context 'if it doesn\'t' do let(:facts) do - super().merge({ - power_devices: { - 'HID-compliant mouse (001)': { - wake_programmable: true, - wake_armed: false + super().merge( + { + power_devices: { + 'HID-compliant mouse (001)': { + wake_programmable: true, + wake_armed: false + } } } - }) + ) end it { is_expected.to compile.with_all_deps } @@ -141,9 +155,11 @@ context 'if device is not in facts' do let(:facts) do - super().merge({ - power_devices: {} - }) + super().merge( + { + power_devices: {} + } + ) end context 'don\'t allow Windows media player to keep system from turning off the display or from going to away mode' do @@ -196,18 +212,20 @@ let(:title) { 'Realtek PCIe GbE Family Controller' } let(:facts) do - super().merge({ - power_devices: { - 'Realtek PCIe GbE Family Controller': { - power_request_overrides: { - driver: { - display: true, - system: true + super().merge( + { + power_devices: { + 'Realtek PCIe GbE Family Controller': { + power_request_overrides: { + driver: { + display: true, + system: true + } } } } } - }) + ) end let(:params) do @@ -237,18 +255,20 @@ let(:title) { 'Realtek PCIe GbE Family Controller' } let(:facts) do - super().merge({ - power_devices: { - 'Realtek PCIe GbE Family Controller': { - power_request_overrides: { - driver: { - system: true, - awaymode: true + super().merge( + { + power_devices: { + 'Realtek PCIe GbE Family Controller': { + power_request_overrides: { + driver: { + system: true, + awaymode: true + } } } } } - }) + ) end let(:params) do @@ -277,17 +297,19 @@ let(:title) { 'VPN Service' } let(:facts) do - super().merge({ - power_devices: { - 'VPN Service': { - power_request_overrides: { - service: { - system: true + super().merge( + { + power_devices: { + 'VPN Service': { + power_request_overrides: { + service: { + system: true + } } } } } - }) + ) end let(:params) do