Skip to content

Commit 9d925d3

Browse files
authored
Merge pull request #462 from jcpunk/timedatectl
Add support for timezone and hardware clock
2 parents 7deb3bd + b66daac commit 9d925d3

File tree

4 files changed

+128
-0
lines changed

4 files changed

+128
-0
lines changed

REFERENCE.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
* `systemd::oomd`: This class manages and configures oomd.
2424
* `systemd::resolved`: This class provides an abstract way to trigger resolved.
2525
* `systemd::system`: This class provides a solution to enable accounting
26+
* `systemd::timedatectl`: This class provides an abstract way to set elements with timedatectl
2627
* `systemd::timesyncd`: This class provides an abstract way to trigger systemd-timesyncd
2728
* `systemd::udevd`: This class manages systemd's udev config
2829

@@ -121,6 +122,8 @@ The following parameters are available in the `systemd` class:
121122
* [`timesyncd_package`](#-systemd--timesyncd_package)
122123
* [`ntp_server`](#-systemd--ntp_server)
123124
* [`fallback_ntp_server`](#-systemd--fallback_ntp_server)
125+
* [`timezone`](#-systemd--timezone)
126+
* [`set_local_rtc`](#-systemd--set_local_rtc)
124127
* [`manage_journald`](#-systemd--manage_journald)
125128
* [`journald_settings`](#-systemd--journald_settings)
126129
* [`manage_udevd`](#-systemd--manage_udevd)
@@ -416,6 +419,30 @@ systemd-networkd take precedence over this setting. requires puppetlabs-inifile
416419

417420
Default value: `undef`
418421

422+
##### <a name="-systemd--timezone"></a>`timezone`
423+
424+
Data type: `Optional[String[1]]`
425+
426+
Set the system time zone to the specified value.
427+
Available timezones can be listed with list-timezones.
428+
If the RTC is configured to be in the local time, this will also update
429+
the RTC time. This call will alter the /etc/localtime symlink.
430+
431+
Default value: `undef`
432+
433+
##### <a name="-systemd--set_local_rtc"></a>`set_local_rtc`
434+
435+
Data type: `Optional[Boolean]`
436+
437+
Takes a boolean argument. If "false", the system is configured to maintain
438+
the RTC in universal time.
439+
If "true", it will maintain the RTC in local time instead.
440+
Note that maintaining the RTC in the local timezone is not fully supported
441+
and will create various problems with time zone changes and daylight saving
442+
adjustments. If at all possible, keep the RTC in UTC mode.
443+
444+
Default value: `undef`
445+
419446
##### <a name="-systemd--manage_journald"></a>`manage_journald`
420447

421448
Data type: `Boolean`

manifests/init.pp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,20 @@
112112
# as the fallback NTP servers. Any per-interface NTP servers obtained from
113113
# systemd-networkd take precedence over this setting. requires puppetlabs-inifile
114114
#
115+
# @param timezone
116+
# Set the system time zone to the specified value.
117+
# Available timezones can be listed with list-timezones.
118+
# If the RTC is configured to be in the local time, this will also update
119+
# the RTC time. This call will alter the /etc/localtime symlink.
120+
#
121+
# @param set_local_rtc
122+
# Takes a boolean argument. If "false", the system is configured to maintain
123+
# the RTC in universal time.
124+
# If "true", it will maintain the RTC in local time instead.
125+
# Note that maintaining the RTC in the local timezone is not fully supported
126+
# and will create various problems with time zone changes and daylight saving
127+
# adjustments. If at all possible, keep the RTC in UTC mode.
128+
#
115129
# @param manage_journald
116130
# Manage the systemd journald
117131
#
@@ -230,6 +244,8 @@
230244
Optional[String[1]] $timesyncd_package = undef,
231245
Optional[Variant[Array,String]] $ntp_server = undef,
232246
Optional[Variant[Array,String]] $fallback_ntp_server = undef,
247+
Optional[Boolean] $set_local_rtc = undef,
248+
Optional[String[1]] $timezone = undef,
233249
Boolean $manage_accounting = false,
234250
Boolean $purge_dropin_dirs = true,
235251
Boolean $manage_journald = true,
@@ -313,6 +329,8 @@
313329
Class['systemd::install'] -> Class['systemd::networkd']
314330
}
315331

332+
contain systemd::timedatectl
333+
316334
if $manage_timesyncd and $facts['systemd_internal_services'] and $facts['systemd_internal_services']['systemd-timesyncd.service'] {
317335
contain systemd::timesyncd
318336
}

manifests/timedatectl.pp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# @api private
2+
#
3+
# @summary This class provides an abstract way to set elements with timedatectl
4+
#
5+
# @param set_local_rtc
6+
# Takes a boolean argument. If "false", the system is configured to maintain
7+
# the RTC in universal time.
8+
# If "true", it will maintain the RTC in local time instead.
9+
# Note that maintaining the RTC in the local timezone is not fully supported
10+
# and will create various problems with time zone changes and daylight saving
11+
# adjustments. If at all possible, keep the RTC in UTC mode.
12+
#
13+
# @param timezone
14+
# Set the system time zone to the specified value.
15+
# Available timezones can be listed with list-timezones.
16+
# If the RTC is configured to be in the local time, this will also update
17+
# the RTC time. This call will alter the /etc/localtime symlink.
18+
class systemd::timedatectl (
19+
Optional[Boolean] $set_local_rtc = $systemd::set_local_rtc,
20+
Optional[String[1]] $timezone = $systemd::timezone,
21+
) {
22+
assert_private()
23+
24+
if $timezone {
25+
exec { 'set system timezone':
26+
command => "timedatectl set-timezone ${timezone}",
27+
unless => "test $(timedatectl show --property Timezone --value) = ${timezone}",
28+
path => $facts['path'],
29+
}
30+
}
31+
32+
# if $set_local_rtc is undef, this remains unmanaged
33+
if $set_local_rtc {
34+
exec { 'set local hardware clock to local time':
35+
command => 'timedatectl set-local-rtc 1',
36+
onlyif => 'test $(timedatectl show --property LocalRTC --value) = no',
37+
path => $facts['path'],
38+
}
39+
} elsif $set_local_rtc == false {
40+
exec { 'set local hardware clock to UTC time':
41+
command => 'timedatectl set-local-rtc 0',
42+
unless => 'test $(timedatectl show --property LocalRTC --value) = no',
43+
path => $facts['path'],
44+
}
45+
}
46+
}

spec/classes/init_spec.rb

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -386,6 +386,43 @@
386386
it { is_expected.to contain_ini_setting('fallback_ntp_server') }
387387
end
388388

389+
context 'when setting timezone' do
390+
let(:params) do
391+
{
392+
timezone: 'America/Chicago',
393+
}
394+
end
395+
396+
it { is_expected.to compile.with_all_deps }
397+
it { is_expected.to contain_exec('set system timezone').with_command('timedatectl set-timezone America/Chicago') }
398+
it { is_expected.not_to contain_exec('set local hardware clock to local time') }
399+
it { is_expected.not_to contain_exec('set local hardware clock to UTC time') }
400+
end
401+
402+
context 'when setting rtc-local is true' do
403+
let(:params) do
404+
{
405+
set_local_rtc: true
406+
}
407+
end
408+
409+
it { is_expected.to compile.with_all_deps }
410+
it { is_expected.to contain_exec('set local hardware clock to local time') }
411+
it { is_expected.not_to contain_exec('set local hardware clock to UTC time') }
412+
end
413+
414+
context 'when setting rtc-local is false' do
415+
let(:params) do
416+
{
417+
set_local_rtc: false
418+
}
419+
end
420+
421+
it { is_expected.to compile.with_all_deps }
422+
it { is_expected.not_to contain_exec('set local hardware clock to local time') }
423+
it { is_expected.to contain_exec('set local hardware clock to UTC time') }
424+
end
425+
389426
context 'when passing service limits' do
390427
let(:params) do
391428
{

0 commit comments

Comments
 (0)