From 72e8ffa7bc218e163d889812d7f00f738b7aefd2 Mon Sep 17 00:00:00 2001 From: Angelo Naselli Date: Tue, 2 Feb 2016 23:22:34 +0100 Subject: manaclock - Added local NTP server setup mga#17569 --- Changes | 1 + lib/ManaTools/Module/Clock.pm | 113 +++++++++++++++++++++++++++++++-------- lib/ManaTools/Shared/TimeZone.pm | 39 ++++++++------ t/04-Shared_TimeZone.t | 4 +- 4 files changed, 117 insertions(+), 40 deletions(-) diff --git a/Changes b/Changes index 03268a4b..7669ee87 100644 --- a/Changes +++ b/Changes @@ -1,3 +1,4 @@ + - manaclock - Added local NTP server setup mga#17569 1.1.0 Sun Jan 24 11:19:27 CET 2016 - ReplacePoint module has been added - policy and desktop files are now localized diff --git a/lib/ManaTools/Module/Clock.pm b/lib/ManaTools/Module/Clock.pm index bc1300c3..73d2c82d 100644 --- a/lib/ManaTools/Module/Clock.pm +++ b/lib/ManaTools/Module/Clock.pm @@ -168,7 +168,8 @@ sub _get_NTPservers { # ## returns 'info', a HASH references containing: ## time_zone => time zone hash reference to be restored -## ntp_server => ntp server address +## ntp_servers => ntp server address +## pool_server => ntp pool server address ## date => date string ## time => time string ## ntp_running => is NTP running? @@ -179,9 +180,8 @@ sub _restoreValues { my $info; if (!$datetime_only) { $info->{time_zone} = $self->sh_tz->readConfiguration(); - $info->{ntp_server} = $self->sh_tz->ntpCurrentServer(); - #- strip digits from \d+.foo.pool.ntp.org - $info->{ntp_server} =~ s/^\d+\.// if $info->{ntp_server}; + $DB::single = 1; + $info->{ntp_servers} = [ $self->sh_tz->ntpCurrentServers() ]; $info->{ntp_running} = $self->sh_tz->isNTPRunning(); } my $t = localtime; @@ -255,8 +255,8 @@ sub _adminClockPanel { else { $dialog->widget('timeZoneLbl')->setValue($self->loc->N("not defined")); } - if ($info->{ntp_server}) { - $dialog->widget('ntpLabel')->setValue($info->{ntp_server}); + if (scalar @{$info->{ntp_servers}}) { + $dialog->widget('ntpLabel')->setValue(join (',', @{$info->{ntp_servers}})); } else { $dialog->widget('ntpLabel')->setValue($self->loc->N("not defined")); @@ -314,17 +314,17 @@ sub _adminClockPanel { my $ntpFrame = $dialog->widget('ntpFrame'); if ($ntpFrame->value()) { # (2) - my $currentServer = $self->sh_tz->ntpCurrentServer(); - #- strip digits from \d+.foo.pool.ntp.org - $currentServer =~ s/^\d+\.// if $currentServer; + my @currentServers = $self->sh_tz->ntpCurrentServers(); + my $currentServer = join(',', @currentServers) if scalar @currentServers; my $isRunning = $self->sh_tz->isNTPRunning(); my $currentService = $self->sh_tz->ntp_program(); my $ntpService = $dialog->widget('ntpService'); my $selectedService = $ntpService->selectedItem(); + my $newServers = join(',', sort @{$info->{ntp_servers}}) if scalar @{$info->{ntp_servers}}; my $sameService = ($currentService && $currentService eq $selectedService->label()); - my $sameConfig = $sameService && ((!$currentServer && !$info->{ntp_server}) || - ($currentServer && $info->{ntp_server} && $currentServer eq $info->{ntp_server}) + my $sameConfig = $sameService && ((!$currentServer && !$newServers) || + ($currentServer && $newServers && $currentServer eq $newServers) ); my $nothingToDo = ($isRunning && $sameConfig); @@ -339,7 +339,7 @@ sub _adminClockPanel { $self->sh_tz->ntp_program($selectedService->label()); } if (!$sameConfig) { - eval { $self->sh_tz->setNTPConfiguration($info->{ntp_server}) }; + eval { $self->sh_tz->setNTPConfiguration($info->{ntp_servers}) }; my $errors = $@; if ($errors) { $self->sh_gui->warningMsgBox({ @@ -351,7 +351,10 @@ sub _adminClockPanel { $ydialog->pollEvent(); } # and finally enabling the service - eval { $self->sh_tz->enableAndStartNTP($info->{ntp_server}) }; + eval { + my $ntp_server = $info->{ntp_servers}->[0]; + $self->sh_tz->enableAndStartNTP($ntp_server); + }; my $errors = $@; if ($errors) { $finished = 0; @@ -456,27 +459,36 @@ sub _adminClockPanel { ); my $hbox1 = $factory->createHBox($ntpFrame); - my $changeNTPButton = $factory->createPushButton($hbox1, $self->loc->N("Change &NTP server")); + my $vbx = $factory->createVBox($hbox1); + my $chooseNTPButton = $factory->createPushButton($vbx, $self->loc->N("Choose &NTP server")); $self->addWidget( - "changeNTPButton", - $changeNTPButton, sub { + "chooseNTPButton", + $chooseNTPButton, sub { my $event = shift; #ManaTools::Shared::GUI::Event my $dialog = $event->parentDialog(); my $self = $dialog->module(); #this object + my $info = $dialog->info(); # get time to calculate elapsed my $t0 = localtime; + # let's guess it's a pool for selecting item + my $pool_server = $info->{ntp_servers}->[0] if scalar @{$info->{ntp_servers}}; + #- strip digits from \d+.foo.pool.ntp.org + $pool_server =~ s/^\d+\.// if $pool_server; + my $item = $self->sh_gui->ask_fromTreeList({title => $self->loc->N("NTP server - DrakClock"), header => $self->loc->N("Choose your NTP server"), default_button => 1, item_separator => '|', - default_item => $info->{ntp_server}, + default_item => $pool_server, skip_path => 1, list => $self->NTPServers}); if ($item) { my $ntpLabel = $dialog->widget('ntpLabel'); - $ntpLabel->setValue($item); - $info->{ntp_server} = $item; + my $pool_match = qr/\.pool\.ntp\.org$/; + my $server = $item; + $info->{ntp_servers} = [ $server =~ $pool_match ? (map { "$_.$server" } 0 .. 2) : $server ]; + $ntpLabel->setValue(join (',', @{$info->{ntp_servers}})); } # fixing elapsed time (dialog is modal) my $t1 = localtime; @@ -493,6 +505,63 @@ sub _adminClockPanel { return 1; }, ); + $chooseNTPButton->setStretchable(0,1); + + my $localNTPButton = $factory->createPushButton($vbx, $self->loc->N("&Local NTP server")); + $self->addWidget( + "localNTPButton", + $localNTPButton, sub { + my $event = shift; #ManaTools::Shared::GUI::Event + my $dialog = $event->parentDialog(); + my $self = $dialog->module(); #this object + my $info = $dialog->info(); + + my $factory = $dialog->factory(); + ## push application title + my $appTitle = yui::YUI::app()->applicationTitle(); + ## set new title to get it in dialog + yui::YUI::app()->setApplicationTitle($self->loc->N("Set local NTP server")); + + my $dlg = $factory->createPopupDialog($yui::YDialogNormalColor); + my $layout = $factory->createVBox($dlg); + my $input = $factory->createInputField($layout, $self->loc->N("Please set your local NTP server")); + $input->setStretchable(0,1); + my $hbox = $factory->createHBox($layout); + + my $cancelButton = $factory->createPushButton($hbox, $self->loc->N("&Cancel")); + my $okButton = $factory->createPushButton($hbox, $self->loc->N("&Ok")); + $dlg->setDefaultButton($okButton); + + while (1) { + my $event = $dlg->waitForEvent(); + + my $eventType = $event->eventType(); + #event type checking + if ($eventType == $yui::YEvent::CancelEvent) { + last; + } + elsif ($eventType == $yui::YEvent::WidgetEvent) { + # widget selected + my $widget = $event->widget(); + + if ($widget == $cancelButton) { + last; + } + elsif ($widget == $okButton) { + my $server = $input->value(); + my $ntpLabel = $dialog->widget('ntpLabel'); + $info->{ntp_servers} = [ $server ]; + $ntpLabel->setValue(join (',', @{$info->{ntp_servers}})); + last; + } + } + } + + $dlg->destroy(); + } + ); + $localNTPButton->setStretchable(0,1); + $factory->createHSpacing($hbox1, 1.0); my $ntpService = $factory->createComboBox($hbox1, "", ); $self->addWidget( @@ -518,8 +587,8 @@ sub _adminClockPanel { return 1; }, ); - if ($info->{ntp_server}) { - $ntpLabel->setValue($info->{ntp_server}); + if ($info->{ntp_servers}) { + $ntpLabel->setValue(join (',', @{$info->{ntp_servers}})); } $ntpFrame->setValue($info->{ntp_running}); $dateTimeFrame->setEnabled(!$info->{ntp_running}); @@ -527,7 +596,7 @@ sub _adminClockPanel { $factory->createHSpacing($hbox1, 1.0); $ntpLabel->setWeight($yui::YD_HORIZ, 2); - $changeNTPButton->setWeight($yui::YD_HORIZ, 1); + $chooseNTPButton->setWeight($yui::YD_HORIZ, 1); $factory->createHSpacing($hbox, 1.0); $factory->createVSpacing($layoutstart, 1.0); diff --git a/lib/ManaTools/Shared/TimeZone.pm b/lib/ManaTools/Shared/TimeZone.pm index b8e82d31..cebf3d96 100644 --- a/lib/ManaTools/Shared/TimeZone.pm +++ b/lib/ManaTools/Shared/TimeZone.pm @@ -832,30 +832,40 @@ sub ntpServers { #============================================================= -=head2 ntpCurrentServer +=head2 ntpCurrentServers -=head3 INPUT +=head3 OUTPUT -Input_Parameter: in_par_description + @servers: arrey of configured server =head3 DESCRIPTION -Returns the current ntp server address read from configuration file + Returns the current ntp server address list read from configuration file =cut #============================================================= -sub ntpCurrentServer { +sub ntpCurrentServers { my $self = shift; my $configFile = $self->ntp_configuration_file || $self->getNTPServiceConfig($self->ntp_program); + my @serv = (); if ($self->ntp_program eq "systemd-timesyncd") { - return MDK::Common::Func::find { $_ ne '127.127.1.0' } map { MDK::Common::Func::if_(/^\s*NTP=\s*(\S*)\s*(\S*)/, $1) } MDK::Common::File::cat_($configFile); + @serv = map { MDK::Common::Func::if_(/^\s*NTP=\s*(\S*)\s*(\S*)/, $1) } MDK::Common::File::cat_($configFile); } else { - return MDK::Common::Func::find { $_ ne '127.127.1.0' } map { MDK::Common::Func::if_(/^\s*server\s+(\S*)/, $1) } MDK::Common::File::cat_($configFile); + @serv = map { MDK::Common::Func::if_(/^\s*server\s+(\S*)/, $1) } MDK::Common::File::cat_($configFile); + } + + my @s = (); + foreach (sort @serv) { + if ($_ ne '127.127.1.0') { + push (@s, $_); + } } + + return @s; } #============================================================= @@ -917,7 +927,7 @@ sub isNTPRunning { =head3 INPUT - $server: server address to be configured as NTP server + $servers: Array reference containing NTP server addresses =head3 DESCRIPTION @@ -929,7 +939,7 @@ sub isNTPRunning { #============================================================= sub setNTPConfiguration { - my ($self, $server) = @_; + my ($self, $servers) = @_; my $f = $self->ntp_configuration_file || $self->getNTPServiceConfig($self->ntp_program);; -f $f or return; @@ -937,20 +947,17 @@ sub setNTPConfiguration { die $self->loc->N("user does not have the rights to change configuration file, skipped") if (!(-w $f)); - my $pool_match = qr/\.pool\.ntp\.org$/; - my @servers = $server =~ $pool_match ? (map { "$_.$server" } 0 .. 2) : $server; - if ($self->ntp_program eq "systemd-timesyncd") { my $added = 0; MDK::Common::File::substInFile { if (/^#?\s*NTP=\s*(\S*)/ && $1 ne '127.127.1.0') { - $_ = $added ? $_ =~ $pool_match ? undef : "#NTP=$1\n" : join(' ', 'NTP=', @servers, "\n"); + $_ = $added ? "#NTP=$1\n" : join(' ', 'NTP=', @{$servers}, "\n"); $added = 1; } } $f; if ($self->ntp_program eq "ntpd") { my $ntp_prefix = $self->ntp_conf_dir; - MDK::Common::File::output_p("$ntp_prefix/step-tickers", join('', map { "$_\n" } @servers)); + MDK::Common::File::output_p("$ntp_prefix/step-tickers", join('', map { "$_\n" } @{$servers})); } } else { @@ -958,13 +965,13 @@ sub setNTPConfiguration { my $servername_config_suffix = $self->servername_config_suffix ? $self->servername_config_suffix : " "; MDK::Common::File::substInFile { if (/^#?\s*server\s+(\S*)/ && $1 ne '127.127.1.0') { - $_ = $added ? $_ =~ $pool_match ? undef : "#server $1\n" : join('', map { "server $_$servername_config_suffix\n" } @servers); + $_ = $added ? "#server $1\n" : join('', map { "server $_$servername_config_suffix\n" } @{$servers}); $added = 1; } } $f; if ($self->ntp_program eq "ntpd") { my $ntp_prefix = $self->ntp_conf_dir; - MDK::Common::File::output_p("$ntp_prefix/step-tickers", join('', map { "$_\n" } @servers)); + MDK::Common::File::output_p("$ntp_prefix/step-tickers", join('', map { "$_\n" } @{$servers})); } } diff --git a/t/04-Shared_TimeZone.t b/t/04-Shared_TimeZone.t index c96165a4..8a3578f7 100644 --- a/t/04-Shared_TimeZone.t +++ b/t/04-Shared_TimeZone.t @@ -23,8 +23,8 @@ BEGIN { diag "ntp_program got: < " . $currService . " >"; ok (my $a = ($tz->isNTPRunning() ? "running" : "not running"), 'isNTPRunning'); diag "Check if " . $currService . " is running got: < " . $a . " >"; - ok (my $s = $tz->ntpCurrentServer(), 'currentNTPServer'); - diag "ntpCurrentServer got: < " . ($s ? $s : "none") . " >"; + ok (my @s = $tz->ntpCurrentServers(), 'currentNTPServers'); + diag "ntpCurrentServers got: < " . join(',', @s) . " >"; ok (my @pairs = $tz->ntpServiceConfigPairs(), 'ntpServiceConfigPairs'); diag Dumper(@pairs); for my $pair (@pairs) { -- cgit v1.2.1