summaryrefslogtreecommitdiffstats
path: root/time_wizard/Ntp.pm
blob: b2fcd7a67d992da5fa90713aa907fa9f8087a409 (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
#!/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;
use MDK::Wizard::Wizcommon;
use MDK::Wizard::Wizcommon_gtk3;
use timezone;

my $wiz = new MDK::Wizard::Wizcommon;
my $in = interactive->vnew;

my $o = { 
    name => N("Time wizard"),
	var => { 
	    varserver1 => '',
 	    varserver2 => '',
 	    varserver3 => '',
	    wiz_timezone => ''
	},
	needed_rpm => [ 'ntp' ],
	defaultimage => "/usr/share/mcc/themes/default/time-mdk.png",
    };

my $ntp_servers = timezone::ntp_servers();

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("Select a primary a secondary and a third server from the list.") . "\n" . N("(we recommend using the server 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';
 			$o->{var}{varserver3} ||= '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]} } },
		      { label => N("Third time server:"), val => \$o->{var}{varserver3}, 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 {
		      return '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 city:"), val => \$o->{var}{wiz_country}, list_ref => \@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:"), val_ref => \$o->{var}{varserver1} },
		      { label => N("Secondary time server:"), val_ref => \$o->{var}{varserver2} },
		      { label => N("Third time server:"), val_ref => \$o->{var}{varserver3} },
		      { label => N("Time zone:"), val_ref => \$o->{var}{wiz_timezone} },
		      ],
		      next => 'end'
	      },
	      warning => {
		name => N("Warning."),
		data => [ { label => '' } ],
		ignore => 1,
	        next => 'summary'
	      },
	      server_not_responding => {
		name => join("\n",
                       N("Warning."),
                       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,
		      no_back => 1,
	  },
};

sub new {
    my ($class) = @_;
    bless $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 {
  if ($::testing) {
    do_it();
    return 'end';
  }
  my $command = "/usr/sbin/ntpdate -q $o->{var}{varserver1}"; # $o->{var}{varserver2}";
  my $_o = MDK::Wizard::Wizcommon_gtk3::gtk_log($command, "Test NTP server Responding...");

  #    system("/usr/sbin/ntpdate -q $o->{var}{varserver1} $o->{var}{varserver2}");
  if (!($? >> 8)) {
    do_it();
    return 'end';
  }
  return 'server_not_responding';
}

sub do_it {
  if ($::testing) {
    print "Would have written\nUTC=true\nZONE=$o->{var}{wiz_timezone}\nARC=false\n to /etc/sysconfig/clock\n\n";
    print "Would have copied /usr/share/zoneinfo/$o->{var}{wiz_timezone} to /etc/localtime\n";
    return;
  }
  my $file = "/etc/sysconfig/clock";
  !-f "$file.orig" and cp_af($file, $file . ".orig");
  local *NEW;
  output($file, qq(UTC=true
ZONE=$o->{var}{wiz_timezone}
ARC=false
));
  -f "/usr/share/zoneinfo/$o->{var}{wiz_timezone}" and cp_af("/usr/share/zoneinfo/$o->{var}{wiz_timezone}", "/etc/localtime");
  -f "/etc/ntp/step-tickers" and cp_af("/etc/ntp/step-tickers", "/etc/ntp/step-tickers.orig");
  output('/etc/ntp/step-tickers', join("\n", $o->{var}{varserver1},
				       $o->{var}{varserver2},
				       $o->{var}{varserver3},
				       undef));
  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 $command = "service crond stop; service atd stop; service ntpd stop; service ntpd start; service atd start; service crond start; /sbin/hwclock --systohc --utc";
  MDK::Wizard::Wizcommon_gtk3::gtk_log($command, "Configuring NTP, ATD and CROND services...");
}

1;