From 7d4134a7fdc5651e90b7aefbda95c5a1998ec3c6 Mon Sep 17 00:00:00 2001 From: Angelo Naselli Date: Wed, 25 Mar 2015 12:06:03 +0100 Subject: fixed ntp_program attribute initialization and added its test --- lib/ManaTools/Shared/TimeZone.pm | 41 +++++++++++++++++++++++++++++++++------- t/04-Shared_TimeZone.t | 2 ++ 2 files changed, 36 insertions(+), 7 deletions(-) diff --git a/lib/ManaTools/Shared/TimeZone.pm b/lib/ManaTools/Shared/TimeZone.pm index 155de20..d481bf2 100644 --- a/lib/ManaTools/Shared/TimeZone.pm +++ b/lib/ManaTools/Shared/TimeZone.pm @@ -105,17 +105,16 @@ has 'timezone_prefix' => ( has 'ntp_configuration_file' => ( is => 'rw', isa => 'Str', + lazy => 1, builder => '_ntp_configuration_file_init', ); sub _ntp_configuration_file_init { my $self = shift; - return "/etc/chrony.conf" if (-f "/etc/chrony.conf"); - - return "/etc/ntp.conf" if (-f "/etc/ntp.conf"); + my $curr = $self->ntp_program; - return "/etc/systemd/timesyncd.conf"; + return $self->getNTPServiceConfig($curr); } #============================================================= @@ -163,11 +162,39 @@ has 'ntp_program' => ( sub _ntp_program_init { my $self = shift; - return "chronyd" if ($self->ntp_configuration_file() eq "/etc/chrony.conf"); + # looks for a running service from the configured ones, + # if none is running chooses the first of the list + my $list = $self->ntpServiceList(); + return $self->loc->N("No NTP services") if !$list; - return "ntpd" if ($self->ntp_configuration_file() eq "/etc/ntp.conf"); + my $ntpd; + my $isRunning = 0; + foreach $ntpd (@{$list}) { + $isRunning = $self->sh_services->is_service_running($ntpd); + last if $isRunning; + } + + if (!$isRunning) { + # being sure systemd-timesyncd is not really running (or set to be) + if ($self->getEmbeddedNTP()) { + $ntpd = "systemd-timesyncd"; + Sys::Syslog::syslog( + 'info|local1', + $self->loc->N("%s enabled but stopped - disabling it", + $ntpd + ) + ); + # enabled but stopped, disabling it + # NOTE this happens tipically on VM if not well configured + $self->setEmbeddedNTP(0); + } + else { + # coosing the first one of the list that is not running + $ntpd = $list->[0]; + } + } - return "systemd-timesyncd" if ($self->ntp_configuration_file() eq "/etc/systemd/timesyncd.conf"); + return $ntpd; } #============================================================= diff --git a/t/04-Shared_TimeZone.t b/t/04-Shared_TimeZone.t index 9f286cc..21dd2e7 100644 --- a/t/04-Shared_TimeZone.t +++ b/t/04-Shared_TimeZone.t @@ -14,6 +14,8 @@ BEGIN { ok (my @l = $tz->getTimeZones(), 'getTimeZones'); ok (my $h = $tz->readConfiguration(), 'readConfiguration'); diag Dumper($h); + ok (my $currService = $tz->ntp_program(), 'ntp_program'); + diag "ntp_program got: < " . $currService . " >"; ok (my $s = $tz->ntpCurrentServer(), 'currentNTPServer'); diag "ntpCurrentServer got: < " . ($s ? $s : "none") . " >"; ok (my $a = ($tz->isNTPRunning() ? "running" : "not running"), 'isNTPRunning'); -- cgit v1.2.1