diff --git a/README.md b/README.md index e0988ca..f92615a 100644 --- a/README.md +++ b/README.md @@ -1,27 +1,36 @@ # puppet-timezone [![Build Status](https://secure.travis-ci.org/saz/puppet-timezone.png)](http://travis-ci.org/saz/puppet-timezone) Manage timezone settings via Puppet ### Supported Puppet versions * Puppet >= 4 * Last version supporting Puppet 3: v3.5.0 ## Usage ### Set timezone to UTC ``` class { 'timezone': timezone => 'UTC', } ``` ### Set timezone to Europe/Berlin ``` class { 'timezone': timezone => 'Europe/Berlin', } ``` ## Other class parameters * ensure: present or absent, default: present * autoupgrade: true or false, default: false. Auto-upgrade package, if there is a newer version +* hwutc: true or false, default: undef. This parameter will ensure that the real time clock is set to UTC rather than the local timezone. This is not supported on all platforms and will fail if you try and set it on an unsupported platform. A typical error message from the ```timedatectl``` command, if this value is set to false would be: +``` +Warning: The system is configured to read the RTC time in the local time zone. + This mode can not be fully supported. It will create various problems + with time zone changes and daylight saving time adjustments. The RTC + time is never updated, it relies on external facilities to maintain it. + If at all possible, use RTC in UTC by calling + 'timedatectl set-local-rtc 0'. +``` diff --git a/data/os/Debian/16.04.yaml b/data/os/Debian/16.04.yaml new file mode 100644 index 0000000..6075424 --- /dev/null +++ b/data/os/Debian/16.04.yaml @@ -0,0 +1,4 @@ +--- +timezone::check_hwclock_enabled_cmd: 'grep LOCAL /etc/adjtime' +timezone::check_hwclock_disabled_cmd: 'grep UTC /etc/adjtime' +timezone::hwclock_cmd: 'timedatectl set-local-rtc %s' diff --git a/data/os/RedHat/7.yaml b/data/os/RedHat/7.yaml index 67142f0..2027d53 100644 --- a/data/os/RedHat/7.yaml +++ b/data/os/RedHat/7.yaml @@ -1,3 +1,6 @@ --- timezone::timezone_update: 'timedatectl set-timezone %s' -timezone::timezone_update_check_cmd: "timedatectl status | grep 'Time zone:' | grep -q %s" +timezone::timezone_update_check_cmd: "timedatectl status | grep 'Timezone:' | grep -q %s" +timezone::check_hwclock_enabled_cmd: 'grep LOCAL /etc/adjtime' +timezone::check_hwclock_disabled_cmd: 'grep UTC /etc/adjtime' +timezone::hwclock_cmd: 'timedatectl set-local-rtc %s' diff --git a/manifests/init.pp b/manifests/init.pp index 79b1e03..fd4cbc2 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,136 +1,158 @@ # This module manages timezone settings # # @param timezone # The name of the timezone. # # @param ensure # Ensure if present or absent. # # @param autoupgrade # Upgrade package automatically, if there is a newer version. # # @param package # Name of the package. # Only set this, if your platform is not supported or you know, what you're doing. # # @param config_file # Main configuration file. # Only set this, if your platform is not supported or you know, what you're doing. # # @param zoneinfo_dir # Source directory of zoneinfo files. # Only set this, if your platform is not supported or you know, what you're doing. # Default: auto-set, platform specific # # @param hwutc # Is the hardware clock set to UTC? (true or false) # # @param notify_services # List of services to notify # # @example # class { 'timezone': # timezone => 'Europe/Berlin', # } # class timezone ( String $timezone = 'Etc/UTC', Enum['present','absent'] $ensure = 'present', Optional[Boolean] $hwutc = undef, Boolean $autoupgrade = false, Optional[Array[String]] $notify_services = undef, Optional[String] $package = undef, String $zoneinfo_dir = '/usr/share/zoneinfo/', String $localtime_file = '/etc/localtime', Optional[String] $timezone_file = undef, Optional[String] $timezone_file_template = 'timezone/clock.erb', Optional[Boolean] $timezone_file_supports_comment = undef, Optional[String] $timezone_update = undef ) { case $ensure { /(present)/: { if $autoupgrade == true { $package_ensure = 'latest' } else { $package_ensure = 'present' } $localtime_ensure = 'file' $timezone_ensure = 'file' } /(absent)/: { # Leave package installed, as it is a system dependency $package_ensure = 'present' $localtime_ensure = 'absent' $timezone_ensure = 'absent' } default: { fail('ensure parameter must be present or absent') } } if $package { if $package_ensure == 'present' and $facts['os']['family'] == 'Debian' { $_tz = split($timezone, '/') $area = $_tz[0] $zone = $_tz[1] debconf { 'tzdata/Areas': package => 'tzdata', item => 'tzdata/Areas', type => 'select', value => $area; "tzdata/Zones/${area}": package => 'tzdata', item => "tzdata/Zones/${area}", type => 'select', value => $zone; } -> Package[$package] } package { $package: ensure => $package_ensure, before => File[$localtime_file], } } if $timezone_file { file { $timezone_file: ensure => $timezone_ensure, content => template($timezone_file_template), notify => $notify_services, } $exec_subscribe = File[$timezone_file] $exec_unless = undef $exec_refreshonly = true - } else { + } + else { $exec_subscribe = undef $exec_unless = lookup('timezone::timezone_update_check_cmd', Optional[String], 'first', undef) $exec_refreshonly = false } if $ensure == 'present' and $timezone_update { if $exec_unless { $unless_cmd = sprintf($exec_unless, $timezone) - } else { + } + else { $unless_cmd = undef } exec { 'update_timezone': command => sprintf($timezone_update, $timezone), path => '/usr/bin:/usr/sbin:/bin:/sbin', unless => $unless_cmd, subscribe => $exec_subscribe, refreshonly => $exec_refreshonly, } } file { $localtime_file: ensure => $localtime_ensure, source => "file://${zoneinfo_dir}/${timezone}", links => follow, notify => $notify_services, } + + if $ensure == 'present' and $hwutc != undef { + $hwclock_cmd = lookup('timezone::hwclock_cmd', Optional[String], 'first', undef) + if ($hwclock_cmd == '') or ($hwclock_cmd == undef) { fail("")} + $check_hwclock_enabled_cmd = lookup('timezone::check_hwclock_enabled_cmd', Optional[String], 'first', undef) + $check_hwclock_disabled_cmd = lookup('timezone::check_hwclock_disabled_cmd', Optional[String], 'first', undef) + # The logic gets reversed to allow for the parameter to make more sense to + # the end user. + if ! $hwutc { + $hwclock_unless = $check_hwclock_enabled_cmd + } + else { + $hwclock_unless = $check_hwclock_disabled_cmd + } + exec { 'set_hwclock': + command => sprintf($hwclock_cmd, (! $hwutc)), + unless => $hwclock_unless, + path => '/usr/bin:/usr/sbin:/bin:/sbin', + } + } }