Handle hostnames with upper-case letters
[webmin.git] / cluster-webmin / refresh.cgi
1 #!/usr/local/bin/perl
2 # refresh.cgi
3 # Reload the list of modules from all managed hosts
4
5 require './cluster-webmin-lib.pl';
6 &ReadParse();
7 &ui_print_header(undef, $text{'refresh_title'}, "");
8 $| = 1;
9
10 # Setup error handler for down hosts
11 sub ref_error
12 {
13 $ref_error_msg = join("", @_);
14 }
15 &remote_error_setup(\&ref_error);
16
17 # Work out which hosts
18 @hosts = &list_webmin_hosts();
19 @servers = &list_servers();
20 if (defined($in{'id'})) {
21         @hosts = grep { $_->{'id'} == $in{'id'} } @hosts;
22         local ($s) = grep { $_->{'id'} == $hosts[0]->{'id'} } @servers;
23         print "<b>",&text('refresh_header5', undef,
24                           &server_name($s)),"</b><p>\n";
25         }
26 else {
27         @hosts = &create_on_parse("refresh_header");
28         #print "<b>$text{'refresh_header'}</b><p>\n";
29         }
30 $p = 0;
31 foreach $h (@hosts) {
32         local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
33
34         local ($rh = "READ$p", $wh = "WRITE$p");
35         pipe($rh, $wh);
36         if (!fork()) {
37                 close($rh);
38                 if ($s) {
39                         # Refresh the list
40                         &remote_foreign_require($s->{'host'}, "webmin",
41                                                 "webmin-lib.pl");
42                         if ($ref_error_msg) {
43                                 # Host is down ..
44                                 print $wh &serialise_variable($ref_error_msg);
45                                 exit;
46                                 }
47                         &remote_foreign_require($s->{'host'}, "acl",
48                                                 "acl-lib.pl");
49                         local $gconfig = &remote_foreign_config($s->{'host'},
50                                                                 undef);
51                         foreach $g ('os_type', 'os_version',
52                                     'real_os_type', 'real_os_version') {
53                                 $h->{$g} = $gconfig->{$g};
54                                 }
55                         $h->{'version'} = &remote_foreign_call($s->{'host'},
56                                                 "webmin", "get_webmin_version");
57
58                         # Refresh modules and themes
59                         local @old = map { $_->{'dir'} } ( @{$h->{'modules'}},
60                                                            @{$h->{'themes'}} );
61                         undef($h->{'modules'});
62                         local @mods = &remote_foreign_call($s->{'host'},
63                                         "webmin", "get_all_module_infos", 1);
64                         @mods = grep { !$_->{'clone'} } @mods;
65                         local @themes = &remote_foreign_call($s->{'host'},
66                                          "webmin", "list_themes");
67                         local @added;
68                         foreach $m (@mods, @themes) {
69                                 $idx = &indexof($m->{'dir'}, @old);
70                                 if ($idx < 0) {
71                                         push(@added, $m->{'dir'});
72                                         }
73                                 else {
74                                         splice(@old, $idx, 1);
75                                         }
76                                 }
77                         $h->{'modules'} = \@mods;
78                         $h->{'themes'} = \@themes;
79
80                         # Refresh users and groups
81                         local @users = &remote_foreign_call($s->{'host'},
82                                         "acl", "list_users");
83                         local $ud = scalar(@users) - scalar(@{$h->{'users'}});
84                         $h->{'users'} = \@users;
85                         local @groups = &remote_foreign_call($s->{'host'},
86                                         "acl", "list_groups");
87                         local $gd = scalar(@groups) - scalar(@{$h->{'groups'}});
88                         $h->{'groups'} = \@groups;
89
90                         &save_webmin_host($h);
91                         $rv = [ \@added, \@old, $ud, $gd ];
92                         }
93                 else {
94                         # remove from managed list
95                         &delete_webmin_host($h);
96                         $rv = undef;
97                         }
98                 print $wh &serialise_variable($rv);
99                 close($wh);
100                 exit;
101                 }
102         close($wh);
103         $p++;
104         }
105
106 # Read back results
107 $p = 0;
108 foreach $h (@hosts) {
109         local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
110         local $d = &server_name($s);
111         local $rh = "READ$p";
112         local $line = <$rh>;
113         local $rv = &unserialise_variable($line);
114         close($rh);
115
116         if ($rv && ref($rv)) {
117                 @added = @{$rv->[0]};
118                 @old = @{$rv->[1]};
119                 if (@added && @old) {
120                         print &text('refresh_1', $d,
121                                     join(" ", @added),join(" ", @old)),"\n";
122                         }
123                 elsif (@added) {
124                         print &text('refresh_2', $d,
125                                     join(" ", @added)),"\n";
126                         }
127                 elsif (@old) {
128                         print &text('refresh_3', $d, join(" ", @old)),"\n";
129                         }
130                 else {
131                         print &text('refresh_4', $d),"\n";
132                         }
133                 if ($rv->[2] > 0) {
134                         print &text('refresh_u1', $rv->[2]),"\n";
135                         }
136                 elsif ($rv->[2] < 0) {
137                         print &text('refresh_u2', -$rv->[2]),"\n";
138                         }
139                 if ($rv->[3] > 0) {
140                         print &text('refresh_g1', $rv->[3]),"\n";
141                         }
142                 elsif ($rv->[3] < 0) {
143                         print &text('refresh_g2', -$rv->[3]),"\n";
144                         }
145                 print "<br>\n";
146                 }
147         elsif ($rv) {
148                 print &text('refresh_failed', $d, $rv),"<br>\n";
149                 }
150         else {
151                 print &text('refresh_del', $h->{'id'}),"<br>\n";
152                 }
153
154         $p++;
155         }
156
157 print "<p><b>$text{'refresh_done'}</b><p>\n";
158
159 &remote_finished();
160 if (defined($in{'id'})) {
161         &ui_print_footer("edit_host.cgi?id=$in{'id'}", $text{'host_return'},
162                          "", $text{'index_return'});
163         }
164 else {
165         &ui_print_footer("", $text{'index_return'});
166         }
167