summaryrefslogtreecommitdiffstats
path: root/time_wizard/Ntp.pm
blob: a505b377b665cbd0980fa027c43636bb52e17d6a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
#!/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 => '' } ],
		ignore => 1,
	        next => 'summary'
	      },
	      server_not_responding => {
		name => join("\n",
                       N("Warming."),
                       N("The time servers are not responding. The causes could be:"),
                       N("- non existent time servers"),
                       N("- no outside network"),
                       N("- other reasons..."),
                       N("- You can try again to contact time servers, or save configuration without actually setting time."),
                      ) ,
		post => sub { 
		     if ($o->{var}{action} eq 'save_quit') {
			do_it();
			'end'
		     } else {
			'test_server'
		     }		 
		},
		data => [ 
		    { 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) {
	if (services::is_service_running($_)) {
	    services::stop($_)
	}
    }
    foreach (reverse @services) {
        services::start($_)
    }
    system("/sbin/hwclock --systohc --utc");
}

1;