summaryrefslogtreecommitdiffstats
path: root/time_wizard/Ntp.pm
diff options
context:
space:
mode:
Diffstat (limited to 'time_wizard/Ntp.pm')
-rw-r--r--time_wizard/Ntp.pm220
1 files changed, 220 insertions, 0 deletions
diff --git a/time_wizard/Ntp.pm b/time_wizard/Ntp.pm
new file mode 100644
index 00000000..dbc38909
--- /dev/null
+++ b/time_wizard/Ntp.pm
@@ -0,0 +1,220 @@
+#!/usr/bin/perl
+
+# Drakwizard
+
+# Copyright (C) 2002, 2003 Mandrakesoft
+#
+# Authors: Arnaud Desmons <adesmons@mandrakesoft.com>
+# Florent Villard <warly@mandrakesoft.com>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2, or (at your option)
+# any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, write to the Free Software
+# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+
+package MDK::Wizard::Ntp;
+use strict;
+
+use common;
+use services;
+require MDK::Wizard::Wizcommon;
+
+my $wiz = new MDK::Wizard::Wizcommon;
+
+my $o = {
+ name => N('Time wizard'),
+ var => {
+ varserver1 => '',
+ varserver2 => '',
+ wiz_timezone => ''
+ },
+ needed_rpm => [ 'ntp' ],
+ defaultimage => "$ENV{__WIZ_HOME__}time_wizard/images/Time.png"
+ };
+
+my %ntp_servers = (
+'pool.ntp.org' => "Randomly choosen ntp server (pool.ntp.org)",
+'bernina.ethz.ch' => "Swiss Fed. Inst. of Technology",
+'clock.nc.fukuoka-u.ac.jp' => "Fukuoka university, Fukuoka, Japan",
+'clock.psu.edu' => "Penn State University, University Park, PA",
+'clock.tricity.wsu.edu' => "Washington State University Tri-Cities, Richland, Wa",
+'constellation.ecn.uoknor.edu' => "University of Oklahoma, Norman, Oklahoma, USA",
+'fartein.ifi.uio.no' => "University of Oslo, Norway",
+'ntp.adelaide.edu.au' => "University of Adelaide, South Australia",
+'ntp.cs.strath.ac.uk' => "Dept. Computer Science, Strathclyde University, Glasgow, Scotland",
+'ntp.shim.org' => "Singapore",
+'ntp.tmc.edu' => "Baylor College of Medicine, Houston, Tx",
+'ntp1.cmc.ec.gc.ca' =>"Canadian Meteorological Centre, Dorval, Quebec, Canada"
+);
+
+my %actions = (
+ try_again => N('Try again'),
+ save_quit => N('Save config without test')
+);
+
+my %country;
+my @country;
+if (-d '/usr/share/zoneinfo') {
+ opendir my $dir, '/usr/share/zoneinfo';
+ foreach my $z (grep { /^[A-Z]/ } readdir $dir) {
+ if (-d "/usr/share/zoneinfo/$z") {
+ opendir my $dir2, "/usr/share/zoneinfo/$z";
+ push @{$country{$z}}, map { "$_" } grep { /^[A-Z]/ } readdir $dir2;
+ closedir $dir2
+ } else {
+ $country{$z} = 1
+ }
+ }
+ closedir $dir;
+}
+
+$o->{pages} = {
+ welcome => {
+ name => N('This wizard will help you to set the time of your server synchronized with an external time server.') . "\n\n" . N('Thus your server will be the local time server for your network.') . "\n\n" . N('press next to begin, or cancel to leave this wizard'),
+ no_back => 1,
+ next => 'config'
+ },
+ config => {
+ name => N('Time Servers') . "\n\n" . N('Select a primary and secondary server from the list.') . "\n\n" . N('(we recommand you to use pool.ntp.org twice as this server randomly points to available time servers)'),
+ pre => sub {
+ $o->{var}{varserver1} ||= 'pool.ntp.org';
+ $o->{var}{varserver2} ||= 'pool.ntp.org';
+ },
+ data => [
+ { label => N('Primary Time Server:'), val => \$o->{var}{varserver1}, list => [ keys %ntp_servers ], format => sub { $ntp_servers{$_[0]} } },
+ { label => N('Secondary Time Server:'), val => \$o->{var}{varserver2}, list => [ keys %ntp_servers ], format => sub { $ntp_servers{$_[0]} } },
+ ],
+ next => 'choose_region'
+ },
+ choose_region => {
+ name => N('Choose a timezone'),
+ pre => sub {
+ my (undef, $region, $country) = get_timezone();
+ $o->{var}{wiz_region} ||= $region;
+ $o->{var}{wiz_country} ||= $country
+ },
+ data => [
+ { label => N('Choose a region:'), val => \$o->{var}{wiz_region}, list => [ keys %country ] },
+ ],
+ post => sub {
+ 'choose_country' if ref $country{$o->{var}{wiz_region}}
+ },
+ next => 'test_server'
+ },
+ choose_country => {
+ name => N('Choose a timezone'),
+ pre => sub {
+ @country = ref $country{$o->{var}{wiz_region}} ? @{$country{$o->{var}{wiz_region}}} : ()
+ },
+ data => [
+ { label => N('Choose a country:'), val => \$o->{var}{wiz_country}, fixed_list => \@country },
+ ],
+ next => 'test_server'
+ },
+ test_server => {
+ name => N('Testing the time servers availability') . "\n\n" . N('If the time server is not immediately available (network or other reason), there will be about a 30 second delay.') . "\n\n" . N('Press next to start the time servers test.'),
+ pre => sub {
+ $o->{var}{wiz_timezone} = $o->{var}{wiz_region} . (ref $country{$o->{var}{wiz_region}} ? "/$o->{var}{wiz_country}" : "");
+ },
+ post => \&test,
+ data => [
+ { label => N('Primary Time Server:'), fixed_val => \$o->{var}{varserver1} },
+ { label => N('Secondary Time Server:'), fixed_val => \$o->{var}{varserver2} },
+ { label => N('Time zone:'), fixed_val => \$o->{var}{wiz_timezone} },
+ ],
+ next => 'end'
+ },
+ warning => {
+ name => N('Warning.'),
+ data => [ { label => N('') } ],
+ ignore => 1,
+ next => 'summary'
+ },
+ server_not_responding => {
+ name => N('Warming.'),
+ post => sub {
+ if ($o->{var}{action} eq 'save_quit') {
+ do_it();
+ 'end'
+ } else {
+ 'test_server'
+ }
+ },
+ data => [
+ { label => N('The time servers are not responding. The causes could be:') },
+ { label => N('- non existent time servers') },
+ { label => N('- no outside network') },
+ { label => N('- other reasons...') },
+ { label => N('- You can try again to contact time servers, or save configuration without actually setting time.') },
+ { val => \$o->{var}{action}, list => [ keys %actions ], format => sub { $actions{$_[0]} } },
+ ],
+ next => 'config'
+ },
+ end => {
+ name => N('Time server configuration saved') . "\n\n" . N('Your server can now act as a time server for your local network.'),
+ end => 1,
+ next => 0
+ },
+};
+
+sub new {
+ my ($class, $conf) = @_;
+ bless {
+ o => $o,
+ }, $class;
+}
+
+sub get_timezone {
+ -f "/etc/sysconfig/clock" or return;
+ my %conf = getVarsFromSh("/etc/sysconfig/clock");
+ my ($c,$r) = split "/", $conf{ZONE};
+ return ($conf{ZONE}, $c, $r)
+}
+
+sub test {
+ $::testing and return 'end';
+ system("/usr/sbin/ntpdate -q $o->{var}{varserver1} $o->{var}{varserver2}");
+ if (!($? >> 8)) {
+ do_it();
+ return 'end'
+ }
+ 'server_not_responding'
+}
+
+sub do_it {
+ $::testing and return;
+ my $file = "/etc/sysconfig/clock";
+ MDK::Common::cp_af($file, $file.".orig");
+ open(NEW, "> $file") or die "can not open $file: $!";
+ print NEW "UTC=true\n";
+ print NEW "ZONE=$o->{var}{wiz_timezone}\n";
+ print NEW "ARC=false\n";
+ close NEW or die "can not close $file: $!";
+ MDK::Common::cp_af("/usr/share/zoneinfo/$o->{var}{wiz_timezone}", "/etc/localtime");
+ -f "/etc/ntp/step-tickers" and MDK::Common::cp_af("/etc/ntp/step-tickers", "/etc/ntp/step-tickers.orig");
+ open(NEW, "> /etc/ntp/step-tickers") or die "can not open /etc/ntp/step-tickers: $!";
+ print NEW $o->{var}{varserver1}."\n";
+ print NEW $o->{var}{varserver2}."\n";
+ close NEW or die "can not close /etc/ntp/step-tickers: $!";
+ substInFile { s/(# server clock.via.net)/$1\nserver $o->{var}{varserver1}/ } '/etc/ntp.conf' if -f '/etc/ntp.conf';
+ standalone::explanations("Wrote /etc/ntp/step-tickers, starting services");
+ my @services = qw(crond atd ntpd);
+ foreach (@services) {
+ services::stop($_)
+ }
+ foreach (reverse @services) {
+ services::start($_)
+ }
+ system("/sbin/hwclock --systohc --utc");
+}
+
+1;