diff --git a/data/os/Archlinux.yaml b/data/os/Archlinux.yaml index e673ed9..dc4d4f3 100644 --- a/data/os/Archlinux.yaml +++ b/data/os/Archlinux.yaml @@ -1,8 +1,6 @@ --- timezone::package: tzdata timezone::zoneinfo_dir: /usr/share/zoneinfo timezone::localtime_file: /etc/localtime -timezone::timezone_file: null -timezone::timezone_file_template: null -timezone::timezone_file_supports_comment: null -timezone::timezone_update: 'timedatectl set-timezone ' +timezone::timezone_update: 'timedatectl set-timezone %s' +timezone::timezone_update_check_cmd: "timedatectl status | grep 'Time zone:' | grep -q %s" diff --git a/data/os/FreeBSD.yaml b/data/os/FreeBSD.yaml index 2919706..a376524 100644 --- a/data/os/FreeBSD.yaml +++ b/data/os/FreeBSD.yaml @@ -1,8 +1,3 @@ --- -timezone::package: null timezone::zoneinfo_dir: /usr/share/zoneinfo timezone::localtime_file: /etc/localtime -timezone::timezone_file: null -timezone::timezone_file_template: null -timezone::timezone_file_supports_comment: null -timezone::timezone_update: null diff --git a/data/os/Linux.yaml b/data/os/Linux.yaml index 03da63b..9d096e6 100644 --- a/data/os/Linux.yaml +++ b/data/os/Linux.yaml @@ -1,7 +1,6 @@ --- timezone::package: tzdata timezone::zoneinfo_dir: /usr/share/zoneinfo timezone::localtime_file: /etc/localtime timezone::timezone_file_template: timezone/clock.erb timezone::timezone_file_supports_comment: false -timezone::timezone_update: null diff --git a/data/os/RedHat.yaml b/data/os/RedHat.yaml index 03da63b..9d096e6 100644 --- a/data/os/RedHat.yaml +++ b/data/os/RedHat.yaml @@ -1,7 +1,6 @@ --- timezone::package: tzdata timezone::zoneinfo_dir: /usr/share/zoneinfo timezone::localtime_file: /etc/localtime timezone::timezone_file_template: timezone/clock.erb timezone::timezone_file_supports_comment: false -timezone::timezone_update: null diff --git a/data/os/RedHat/7.yaml b/data/os/RedHat/7.yaml new file mode 100644 index 0000000..67142f0 --- /dev/null +++ b/data/os/RedHat/7.yaml @@ -0,0 +1,3 @@ +--- +timezone::timezone_update: 'timedatectl set-timezone %s' +timezone::timezone_update_check_cmd: "timedatectl status | grep 'Time zone:' | grep -q %s" diff --git a/data/os/Suse.yaml b/data/os/Suse.yaml index aedf7ed..a905b2d 100644 --- a/data/os/Suse.yaml +++ b/data/os/Suse.yaml @@ -1,8 +1,5 @@ --- timezone::package: timezone timezone::zoneinfo_dir: /usr/share/zoneinfo timezone::localtime_file: /etc/localtime -timezone::timezone_file: null -timezone::timezone_file_template: null -timezone::timezone_file_supports_comment: null -timezone::timezone_update: 'zic -l ' +timezone::timezone_update: 'zic -l %s' diff --git a/manifests/init.pp b/manifests/init.pp index f44c80c..79b1e03 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,138 +1,136 @@ # 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, } - if $ensure == 'present' and $timezone_update { - $e_command = $facts['os']['family'] ? { - /(Suse|Archlinux)/ => "${timezone_update} ${timezone}", - default => $timezone_update, - } - exec { 'update_timezone': - command => $e_command, - path => '/usr/bin:/usr/sbin:/bin:/sbin', - subscribe => File[$timezone_file], - refreshonly => true, - } - } + + $exec_subscribe = File[$timezone_file] + $exec_unless = undef + $exec_refreshonly = true + } else { + $exec_subscribe = undef + $exec_unless = lookup('timezone::timezone_update_check_cmd', Optional[String], 'first', undef) + $exec_refreshonly = false } - else { - if $facts['os']['family'] == 'RedHat' { - $major = $facts['os']['release']['major'] + 0 # adding converts to number - if $major >= 7 { - exec { 'update_timezone': - command => "timedatectl set-timezone ${timezone}", - path => '/usr/bin:/usr/sbin:/bin:/sbin', - unless => "timedatectl status | grep 'Time zone:' | grep -q ${timezone}", - } - } + + if $ensure == 'present' and $timezone_update { + if $exec_unless { + $unless_cmd = sprintf($exec_unless, $timezone) + } 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, } - } diff --git a/spec/support/redhat.rb b/spec/support/redhat.rb index b7bcc3a..ad17fc4 100644 --- a/spec/support/redhat.rb +++ b/spec/support/redhat.rb @@ -1,60 +1,74 @@ shared_examples 'RedHat' do - let(:facts) do - { - :os => { - :name => 'CentOS', - :family => 'RedHat', - :release => { :major => '6' } - } - } - end - describe 'when using default class parameters' do + describe 'when using default class parameters with osfamily => RedHat and major release => 6' do let(:params) { {} } + let(:facts) do + { + :os => { + :name => 'CentOS', + :family => 'RedHat', + :release => { :major => '6' } + } + } + end it { is_expected.to create_class('timezone') } it do is_expected.to contain_package('tzdata').with(:ensure => 'present', :before => 'File[/etc/localtime]') end it { is_expected.to contain_file('/etc/sysconfig/clock').with_ensure('file') } it { is_expected.to contain_file('/etc/sysconfig/clock').with_content(%r{^ZONE="Etc/UTC"$}) } it { is_expected.not_to contain_exec('update_timezone') } it do is_expected.to contain_file('/etc/localtime').with(:ensure => 'file', :source => 'file:///usr/share/zoneinfo/Etc/UTC') end context 'when timezone => "Europe/Berlin"' do let(:params) { { :timezone => 'Europe/Berlin' } } it { is_expected.to contain_file('/etc/sysconfig/clock').with_content(%r{^ZONE="Europe/Berlin"$}) } it { is_expected.to contain_file('/etc/localtime').with_source('file:///usr/share/zoneinfo/Europe/Berlin') } end context 'when autoupgrade => true' do let(:params) { { :autoupgrade => true } } it { is_expected.to contain_package('tzdata').with_ensure('latest') } end context 'when ensure => absent' do let(:params) { { :ensure => 'absent' } } it { is_expected.to contain_package('tzdata').with_ensure('present') } it { is_expected.to contain_file('/etc/sysconfig/clock').with_ensure('absent') } it { is_expected.to contain_file('/etc/localtime').with_ensure('absent') } end - context 'when RHEL 7' do - let(:facts) { { :os => { :release => { :major => '7' } } } } + include_examples 'validate parameters' + end - it { is_expected.not_to contain_file('/etc/sysconfig/clock').with_ensure('file') } + describe 'when using default class parameters with osfamily => RedHat and major release => 7' do + let(:params) { {} } + let(:facts) do + { + :os => { + :name => 'CentOS', + :family => 'RedHat', + :release => { :major => '7' } + } + } end + it { is_expected.to create_class('timezone') } + it { is_expected.not_to contain_file('/etc/sysconfig/clock') } + it { is_expected.to contain_file('/etc/localtime').with_ensure('file') } + it { is_expected.to contain_exec('update_timezone').with_command('timedatectl set-timezone Etc/UTC').with_unless("timedatectl status | grep 'Time zone:' | grep -q Etc/UTC") } + include_examples 'validate parameters' end end