diff --git a/manifests/init.pp b/manifests/init.pp index 465e54d..3429d3d 100644 --- a/manifests/init.pp +++ b/manifests/init.pp @@ -1,150 +1,154 @@ # 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 = 'link' $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 { $use_debconf = lookup('timezone::use_debconf', Boolean, 'first', false) if $package_ensure == 'present' and $use_debconf { $_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], } } + file { $localtime_file: + ensure => $localtime_ensure, + target => "${zoneinfo_dir}/${timezone}", + force => true, + notify => $notify_services, + } + if $timezone_file { file { $timezone_file: ensure => $timezone_ensure, content => template($timezone_file_template), notify => $notify_services, } if $ensure == 'present' and $timezone_update { exec { 'update_timezone': command => sprintf($timezone_update, $timezone), subscribe => File[$timezone_file], refreshonly => true, path => '/usr/bin:/usr/sbin:/bin:/sbin', + require => File[$localtime_file], } } } else { if $ensure == 'present' and $timezone_update { $unless_cmd = lookup('timezone::timezone_update_check_cmd', String, 'first') exec { 'update_timezone': command => sprintf($timezone_update, $timezone), unless => sprintf($unless_cmd, $timezone), path => '/usr/bin:/usr/sbin:/bin:/sbin', + require => File[$localtime_file], } } } if $ensure == 'present' and $hwutc != undef { $hwclock_cmd = lookup('timezone::hwclock_cmd', Optional[String], 'first', undef) $hwclock_check_enabled_cmd = lookup('timezone::check_hwclock_enabled_cmd', Optional[String], 'first', undef) $hwclock_check_disabled_cmd = lookup('timezone::check_hwclock_disabled_cmd', Optional[String], 'first', undef) if $hwclock_cmd != '' and $hwclock_cmd != undef { if ! $hwutc { $hwclock_unless = $hwclock_check_enabled_cmd } else { $hwclock_unless = $hwclock_check_disabled_cmd } exec { 'set_hwclock': command => sprintf($hwclock_cmd, (! $hwutc)), unless => $hwclock_unless, path => '/usr/bin:/usr/sbin:/bin:/sbin', } } } - file { $localtime_file: - ensure => $localtime_ensure, - source => "${zoneinfo_dir}/${timezone}", - notify => $notify_services, - } } diff --git a/spec/support/debian.rb b/spec/support/debian.rb index 9fb674e..0cb6c82 100644 --- a/spec/support/debian.rb +++ b/spec/support/debian.rb @@ -1,53 +1,53 @@ shared_examples 'Debian' do let(:facts) do { :os => { :name => 'Debian', :family => 'Debian', :release => { :major => '8' } } } end describe 'when using default class parameters' do let(:params) { {} } it { is_expected.to create_class('timezone') } it { is_expected.to contain_file('/etc/timezone') } it { is_expected.to contain_file('/etc/timezone').with_ensure('file') } it { is_expected.to contain_file('/etc/timezone').with_content(%r{Etc/UTC}) } it { is_expected.to contain_exec('update_timezone').with_command(%r{^dpkg-reconfigure -f noninteractive tzdata$}) } it do is_expected.to contain_package('tzdata').with(:ensure => 'present', :before => 'File[/etc/localtime]') end it do is_expected.to contain_file('/etc/localtime').with(:ensure => 'link', - :source => '/usr/share/zoneinfo/Etc/UTC') + :target => '/usr/share/zoneinfo/Etc/UTC') end context 'when timezone => "Europe/Berlin"' do let(:params) { { :timezone => 'Europe/Berlin' } } it { is_expected.to contain_file('/etc/timezone').with_content(%r{^Europe/Berlin$}) } - it { is_expected.to contain_file('/etc/localtime').with_source('/usr/share/zoneinfo/Europe/Berlin') } + it { is_expected.to contain_file('/etc/localtime').with_target('/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/timezone').with_ensure('absent') } it { is_expected.to contain_file('/etc/localtime').with_ensure('absent') } end include_examples 'validate parameters' end end diff --git a/spec/support/freebsd.rb b/spec/support/freebsd.rb index ca44d6d..006a1a8 100644 --- a/spec/support/freebsd.rb +++ b/spec/support/freebsd.rb @@ -1,41 +1,41 @@ shared_examples 'FreeBSD' do let(:facts) do { :os => { :family => 'FreeBSD', :release => { :major => '9' } } } end describe 'when using default class parameters' do let(:params) { {} } it { is_expected.to create_class('timezone') } it do is_expected.to contain_file('/etc/localtime').with(:ensure => 'link', - :source => '/usr/share/zoneinfo/Etc/UTC') + :target => '/usr/share/zoneinfo/Etc/UTC') end context 'when timezone => "Europe/Berlin"' do let(:params) { { :timezone => 'Europe/Berlin' } } - it { is_expected.to contain_file('/etc/localtime').with_source('/usr/share/zoneinfo/Europe/Berlin') } + it { is_expected.to contain_file('/etc/localtime').with_target('/usr/share/zoneinfo/Europe/Berlin') } end context 'when autoupgrade => true' do let(:params) { { :autoupgrade => true } } it { is_expected.to compile.with_all_deps } end context 'when ensure => absent' do let(:params) { { :ensure => 'absent' } } it { is_expected.to contain_file('/etc/localtime').with_ensure('absent') } end include_examples 'validate parameters' end end diff --git a/spec/support/gentoo.rb b/spec/support/gentoo.rb index 0027907..7f7f7f1 100644 --- a/spec/support/gentoo.rb +++ b/spec/support/gentoo.rb @@ -1,55 +1,55 @@ shared_examples 'Gentoo' do let(:facts) do { :os => { :family => 'Gentoo', :release => { :full => '4.8.0', :major => '4' } } } end describe 'when using default class parameters' do let(:params) { {} } it { is_expected.to create_class('timezone') } it do is_expected.to contain_package('sys-libs/timezone-data').with(:ensure => 'present', :before => 'File[/etc/localtime]') end it { is_expected.to contain_file('/etc/timezone').with_ensure('file') } it { is_expected.to contain_file('/etc/timezone').with_content(%r{^Etc/UTC$}) } it { is_expected.to contain_exec('update_timezone').with_command(%r{^emerge --config timezone-data$}) } it do is_expected.to contain_file('/etc/localtime').with(:ensure => 'link', - :source => '/usr/share/zoneinfo/Etc/UTC') + :target => '/usr/share/zoneinfo/Etc/UTC') end context 'when timezone => "Europe/Berlin"' do let(:params) { { :timezone => 'Europe/Berlin' } } it { is_expected.to contain_file('/etc/timezone').with_content(%r{^Europe/Berlin$}) } - it { is_expected.to contain_file('/etc/localtime').with_source('/usr/share/zoneinfo/Europe/Berlin') } + it { is_expected.to contain_file('/etc/localtime').with_target('/usr/share/zoneinfo/Europe/Berlin') } end context 'when autoupgrade => true' do let(:params) { { :autoupgrade => true } } it { is_expected.to contain_package('sys-libs/timezone-data').with_ensure('latest') } end context 'when ensure => absent' do let(:params) { { :ensure => 'absent' } } it { is_expected.to contain_package('sys-libs/timezone-data').with_ensure('present') } it { is_expected.to contain_file('/etc/timezone').with_ensure('absent') } it { is_expected.to contain_file('/etc/localtime').with_ensure('absent') } end include_examples 'validate parameters' end end diff --git a/spec/support/redhat.rb b/spec/support/redhat.rb index 05a6524..d285a2d 100644 --- a/spec/support/redhat.rb +++ b/spec/support/redhat.rb @@ -1,74 +1,74 @@ shared_examples 'RedHat' 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 => 'link', - :source => '/usr/share/zoneinfo/Etc/UTC') + :target => '/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('/usr/share/zoneinfo/Europe/Berlin') } + it { is_expected.to contain_file('/etc/localtime').with_target('/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 include_examples 'validate parameters' end 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('link') } it { is_expected.to contain_exec('update_timezone').with_command('timedatectl set-timezone Etc/UTC').with_unless('timedatectl status | grep "Timezone:\|Time zone:" | grep -q Etc/UTC') } include_examples 'validate parameters' end end