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/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/README.md b/README.md index 8de93c0..cfd051e 100644 --- a/README.md +++ b/README.md @@ -1,187 +1,42 @@ # 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 +Version 4.0.0 is a complete rewrite and modernization of the previous module and breaks with configuration compatibility. -### What windows_power affects +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. -* 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: +## Limitations -#### Public Defined Types: +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. -* [`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 +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. -## Limitations +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 is tested on the following platforms: +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. -* Windows 2008 R2 +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. -It is tested with the OSS version of Puppet only. +If you have any ideas, suggestions, improvements ... please get in touch with us. ## Development -### Contributing +Development happens on GitHub in the well known way (fork, PR, issue, etc.). -Please read CONTRIBUTING.md for full details on contributing to this project. +Please feel free to report problems, suggest improvements, drop new ideas or teach us how to handle things better. 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` + diff --git a/lib/facter/power_devices.rb b/lib/facter/power_devices.rb new file mode 100644 index 0000000..ee5e279 --- /dev/null +++ b/lib/facter/power_devices.rb @@ -0,0 +1,71 @@ +# 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?('') } + + devices = {} + + all_devices.each do |device| + 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) + 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 # 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.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') + end + end + end + end + + devices + end +end + +# rubocop:enable Style/RegexpLiteral diff --git a/lib/facter/power_schemes.rb b/lib/facter/power_schemes.rb new file mode 100644 index 0000000..27cc512 --- /dev/null +++ b/lib/facter/power_schemes.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +# rubocop:disable Style/RegexpLiteral + +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 + +# rubocop:enable Style/RegexpLiteral diff --git a/manifests/device.pp b/manifests/device.pp new file mode 100644 index 0000000..1f76a8e --- /dev/null +++ b/manifests/device.pp @@ -0,0 +1,136 @@ +# @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, + 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 9a4de2f..0000000 --- a/manifests/devices/override.pp +++ /dev/null @@ -1,41 +0,0 @@ -# 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. -# -# === 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 - - 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 => "${windows_power::params::powercfg} /requestsoverride ${type} ${name} ${request}", - provider => windows, - } - } - } -} diff --git a/manifests/devices/wake.pp b/manifests/devices/wake.pp deleted file mode 100644 index eeb9c3f..0000000 --- a/manifests/devices/wake.pp +++ /dev/null @@ -1,34 +0,0 @@ -# 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 -# -# === 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": - command => "${windows_power::params::powercfg} /device${ensure}wake \"${device}\"", - provider => windows, - } -} diff --git a/manifests/global/battery.pp b/manifests/global/battery.pp deleted file mode 100644 index 3ee9ed9..0000000 --- a/manifests/global/battery.pp +++ /dev/null @@ -1,51 +0,0 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.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 => "${windows_power::params::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 4ecf5f4..0000000 --- a/manifests/global/flags.pp +++ /dev/null @@ -1,43 +0,0 @@ -# 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 -# -# === 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 => "${windows_power::params::powercfg} /globalpowerflag /option:${setting} ${status}", - provider => windows, - } - } - default: {} - } -} diff --git a/manifests/global/hibernation.pp b/manifests/global/hibernation.pp deleted file mode 100644 index 2cf56ec..0000000 --- a/manifests/global/hibernation.pp +++ /dev/null @@ -1,29 +0,0 @@ -# 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 -# -# === 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': - command => "${windows_power::params::powercfg} -hibernate ${status}", - provider => windows, - } -} diff --git a/manifests/hibernate.pp b/manifests/hibernate.pp new file mode 100644 index 0000000..9e071ab --- /dev/null +++ b/manifests/hibernate.pp @@ -0,0 +1,63 @@ +# @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, + Optional[Enum['reduced', 'full']] $hiberfile_type = undef, +) { + 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 4ff1989..2da9cf8 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,16 +1,43 @@ -# Author:: Liam Bennett (mailto:liamjbennett@gmail.com) -# Copyright:: Copyright (c) 2014 Liam Bennett -# License:: Apache-2.0 - -# == Class: windows_power +# @summary class to manage Windows power devices (physical, logical and virtual) # -# Module to mananage the configuration of a machines autoupdate settings +# @see windows_power::device # -# === Parameters +# @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 +# } +# } +# } +# } +# } # -# === Examples -# -# -class windows_power { - include windows_power::params +# @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, +) { + if $devices !~ Undef { + each($devices) |$key, $value| { + windows_power::device { $key: + * => $value, + } + } + } } diff --git a/manifests/params.pp b/manifests/params.pp deleted file mode 100644 index 7863adc..0000000 --- a/manifests/params.pp +++ /dev/null @@ -1,45 +0,0 @@ -# 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` -# 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'] - - $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 } });' -} diff --git a/manifests/scheme.pp b/manifests/scheme.pp new file mode 100644 index 0000000..87350c6 --- /dev/null +++ b/manifests/scheme.pp @@ -0,0 +1,122 @@ +# @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, + 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 ($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 2ed46d0..0000000 --- a/manifests/schemes/scheme.pp +++ /dev/null @@ -1,92 +0,0 @@ -# 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 -# -# === 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[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, - Enum['active', 'inactive'] $activation, - Enum['present', 'absent'] $ensure = 'present', -) { - 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}'", - provider => powershell, - logoutput => true, - onlyif => $scheme_check, - } - } - default: { - exec { "create power scheme ${scheme_name}": - command => "& ${windows_power::params::powercfg} -duplicatescheme ${template_scheme} ${scheme_guid}", - provider => powershell, - logoutput => true, - unless => "${windows_power::params::powercfg} /query ${scheme_guid}", - } - - exec { "rename scheme to ${scheme_name}": - command => "& ${windows_power::params::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}"], - } - } - } - } - elsif $ensure == 'absent' { - exec { "delete power scheme ${scheme_name}": - command => "& ${windows_power::params::powercfg} -delete ${scheme_guid}", - provider => powershell, - logoutput => true, - onlyif => "${windows_power::params::powercfg} /query ${scheme_guid}", - } - } - - if $activation == 'active' { - exec { "set ${scheme_name} scheme as active": - command => "& ${windows_power::params::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 deleted file mode 100644 index 4664bbd..0000000 --- a/manifests/schemes/settings.pp +++ /dev/null @@ -1,51 +0,0 @@ -# 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 -# -# === 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, - String[1] $setting, - String[1] $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 => "& ${windows_power::params::powercfg} /change ${setting} ${value}", - provider => powershell, - logoutput => true, - unless => "${windows_power::params::nasty_ps} \$items.contains(${scheme_name})", - } -} diff --git a/metadata.json b/metadata.json index ed92498..8a95ed3 100644 --- a/metadata.json +++ b/metadata.json @@ -1,7 +1,7 @@ { "name": "puppet-windows_power", - "version": "3.0.3-rc0", - "author": "puppet", + "version": "4.0.0-rc0", + "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", @@ -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/classes/hibernate_spec.rb b/spec/classes/hibernate_spec.rb new file mode 100644 index 0000000..e94bb00 --- /dev/null +++ b/spec/classes/hibernate_spec.rb @@ -0,0 +1,60 @@ +# frozen_string_literal: true + +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..11bfa66 --- /dev/null +++ b/spec/classes/scheme_spec.rb @@ -0,0 +1,349 @@ +# frozen_string_literal: true + +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..138cc26 --- /dev/null +++ b/spec/classes/windows_power_spec.rb @@ -0,0 +1,56 @@ +# frozen_string_literal: true + +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..e107067 --- /dev/null +++ b/spec/defines/device_spec.rb @@ -0,0 +1,337 @@ +# frozen_string_literal: true + +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 8794fb8..0000000 --- a/spec/defines/devices/override_spec.rb +++ /dev/null @@ -1,53 +0,0 @@ -# frozen_string_literal: true - -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') } - 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 - end - 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' - ) - end - end -end diff --git a/spec/defines/devices/wake_spec.rb b/spec/defines/devices/wake_spec.rb deleted file mode 100644 index aa65581..0000000 --- a/spec/defines/devices/wake_spec.rb +++ /dev/null @@ -1,44 +0,0 @@ -# frozen_string_literal: true - -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 - { device: 'VMBus Enumerator (001)', ensure: 'enable' } - end - - it do - is_expected.to contain_exec('device VMBus Enumerator (001) enable wake').with( - 'command' => 'C:\Windows\System32\powercfg.exe /deviceenablewake "VMBus Enumerator (001)"' - ) - end - 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( - 'command' => 'C:\Windows\System32\powercfg.exe /devicedisablewake "VMBus Enumerator (001)"' - ) - end - 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 deleted file mode 100644 index 7c2c13f..0000000 --- a/spec/defines/global/hibernation_spec.rb +++ /dev/null @@ -1,31 +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( - 'command' => 'C:\Windows\System32\powercfg.exe -hibernate on' - ) - end - 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( - 'command' => 'C:\Windows\System32\powercfg.exe -hibernate off' - ) - end - end -end diff --git a/spec/defines/schemes/scheme_spec.rb b/spec/defines/schemes/scheme_spec.rb deleted file mode 100644 index 4b5d345..0000000 --- a/spec/defines/schemes/scheme_spec.rb +++ /dev/null @@ -1,140 +0,0 @@ -# frozen_string_literal: true - -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 - let(:params) do - { scheme_name: true, scheme_guid: '381b4222-f694-41f0-9685-ff5bb260df2e', template_scheme: true } - end - - it do - expect do - is_expected.to contain_exec('create power scheme test') - end.to raise_error(Puppet::Error) - end - 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 } - end - 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 - 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 - - 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 } - 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 { is_expected.not_to contain_exec('set test scheme as active') } - it { is_expected.to compile } - end - 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 - - it do - is_expected.to contain_exec('delete power scheme test').with( - 'provider' => 'powershell', - 'command' => '& C:\Windows\System32\powercfg.exe -delete 381b4222-f694-41f0-9685-ff5bb260df2f' - ) - end - - 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 4fe3021..0000000 --- a/spec/defines/schemes/settings_spec.rb +++ /dev/null @@ -1,58 +0,0 @@ -# frozen_string_literal: true - -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 - - it do - is_expected.to contain_exec('modify disk-timeout-ac setting for test').with( - 'provider' => 'powershell', - 'command' => '& C:\Windows\System32\powercfg.exe /change disk-timeout-ac 10' - ) - end - end -end