Handle hostnames with upper-case letters
[webmin.git] / cluster-copy / cluster-copy-lib.pl
1 # cluster-copy-lib.pl
2 # XXX add to released modules list
3
4 BEGIN { push(@INC, ".."); };
5 use WebminCore;
6 &init_config();
7 &foreign_require("cron", "cron-lib.pl");
8 &foreign_require("mailboxes", "mailboxes-lib.pl");
9
10 $cron_cmd = "$module_config_directory/copy.pl";
11 $copies_dir = "$module_config_directory/copies";
12
13 # list_copies()
14 # Returns an array of scheduled copies to multiple servers
15 sub list_copies
16 {
17 local (@rv, $f);
18 opendir(DIR, $copies_dir);
19 foreach $f (sort { $a cmp $b } readdir(DIR)) {
20         next if ($f !~ /^(\S+)\.copy$/);
21         push(@rv, &get_copy($1));
22         }
23 closedir(DIR);
24 return @rv;
25 }
26
27 # get_copy(id)
28 sub get_copy
29 {
30 local %copy;
31 &read_file("$copies_dir/$_[0].copy", \%copy) || return undef;
32 $copy{'id'} = $_[0];
33 if (!defined($copy{'email'})) {
34         # compat - continue email to root
35         $copy{'email'} = "root";
36         }
37 return \%copy;
38 }
39
40 # save_copy(&copy)
41 sub save_copy
42 {
43 $_[0]->{'id'} ||= time().$$;
44 mkdir($copies_dir, 0700);
45 &lock_file("$copies_dir/$_[0]->{'id'}.copy");
46 &write_file("$copies_dir/$_[0]->{'id'}.copy", $_[0]);
47 &unlock_file("$copies_dir/$_[0]->{'id'}.copy");
48 }
49
50 # delete_copy(&copy)
51 sub delete_copy
52 {
53 &lock_file("$copies_dir/$_[0]->{'id'}.copy");
54 unlink("$copies_dir/$_[0]->{'id'}.copy");
55 &unlock_file("$copies_dir/$_[0]->{'id'}.copy");
56 }
57
58 # run_cluster_job(&job, &callback)
59 # Runs a cluster cron job on all configured servers, and for each result calls
60 # the callback function with parameters 0/1, a server object, and the list of
61 # files copied or an error message
62 sub run_cluster_job
63 {
64 local @rv;
65 local $func = $_[1];
66
67 # Work out which servers to run on
68 &foreign_require("servers", "servers-lib.pl");
69 local @servers = &servers::list_servers_sorted();
70 local @groups = &servers::list_all_groups(\@servers);
71 local @run;
72 foreach $s (split(/\s+/, $_[0]->{'servers'})) {
73         if ($s =~ /^group_(.*)$/) {
74                 # All members of a group
75                 ($group) = grep { $_->{'name'} eq $1 } @groups;
76                 foreach $m (@{$group->{'members'}}) {
77                         push(@run, grep { $_->{'host'} eq $m && $_->{'user'} }
78                                         @servers);
79                         }
80                 }
81         elsif ($s eq '*') {
82                 # This server
83                 push(@run, ( { 'desc' => $text{'edit_this'} } ));
84                 }
85         elsif ($s eq 'ALL') {
86                 # All servers with login
87                 push(@run, grep { $_->{'user'} } @servers);
88                 }
89         else {
90                 # A single remote server
91                 push(@run, grep { $_->{'host'} eq $s } @servers);
92                 }
93         }
94 @run = &unique(@run);
95
96 # Setup error handler for down hosts
97 sub inst_error
98 {
99 $inst_error_msg = join("", @_);
100 }
101 &remote_error_setup(\&inst_error);
102
103 # Run the pre command on local
104 if ($_[0]->{'before'} && $_[0]->{'beforelocal'}) {
105         &system_logged("$_[0]->{'before'} >/dev/null 2>&1");
106         }
107
108 # Run one each one in parallel and display the output
109 $p = 0;
110 foreach $s (@run) {
111         local ($rh = "READ$p", $wh = "WRITE$p");
112         pipe($rh, $wh);
113         select($wh); $| = 1; select(STDOUT);
114         if (!fork()) {
115                 # Run the command in a subprocess
116                 close($rh);
117
118                 &remote_foreign_require($s->{'host'}, "webmin",
119                                         "webmin-lib.pl");
120                 if ($inst_error_msg) {
121                         # Failed to contact host ..
122                         print $wh &serialise_variable([ 0, $inst_error_msg ]);
123                         exit;
124                         }
125
126                 # Run the pre command on remote
127                 local $bout;
128                 if ($_[0]->{'before'} && !$_[0]->{'beforelocal'}) {
129                         $bout = &remote_foreign_call($s->{'host'},
130                                 "webmin", "backquote_logged", $_[0]->{'before'});
131                         }
132
133                 # Work out which files to transfer, and which directories
134                 # to create
135                 local @allnames = split(/\t+/, $_[0]->{'files'});
136                 local (@allfiles, @alldirs);
137                 foreach $f (@allnames) {
138                         if (-d $f) {
139                                 # Expand this directory
140                                 &expand_dir($f, \@allfiles, \@alldirs);
141                                 }
142                         else {
143                                 push(@allfiles, $f);
144                                 }
145                         }
146
147                 # Create each of the needed directories
148                 local (@errs, $d);
149                 foreach $d (@alldirs) {
150                         local $dest;
151                         if ($_[0]->{'dmode'}) {
152                                 # Relative to directory
153                                 $d =~ /([^\/]+)$/;
154                                 $dest = $_[0]->{'dest'}."/".$1;
155                                 }
156                         else {
157                                 # Full path under directory
158                                 $dest = $_[0]->{'dest'}.$d;
159                                 }
160                         local $qdest = quotemeta($dest);
161                         local $rmd = &remote_eval($s->{'host'}, "webmin",
162                                      "-d \"$qdest\" ? \"already\" : &make_dir(\"$qdest\", 0755, 1) ? undef : \$!");
163                         if (!$rmd) {
164                                 push(@dirs, $d);
165                                 }
166                         elsif ($rmd ne "already") {
167                                 push(@errs, [ $d, "Failed to create $dest : $rmd" ]);
168                                 }
169                         }
170
171                 # Transfer each of the files, and make sure each was done OK
172                 local (@files, $f);
173                 foreach $f (@allfiles) {
174                         local $dest;
175                         if ($_[0]->{'dmode'}) {
176                                 # Relative to directory
177                                 $f =~ /([^\/]+)$/;
178                                 $dest = $_[0]->{'dest'}."/".$1;
179                                 }
180                         else {
181                                 # Full path under directory
182                                 $dest = $_[0]->{'dest'}.$f;
183                                 }
184                         if (!-r $f) {
185                                 push(@errs, [ $f, "Source file not found" ]);
186                                 next;
187                                 }
188                         if (&simplify_path($f) eq &simplify_path($dest) &&
189                             $s->{'id'} == 0) {
190                                 push(@errs, [ $f, "Cannot overwrite same file on this server" ]);
191                                 next;
192                                 }
193                         &remote_write($s->{'host'}, $f, $dest);
194                         if ($inst_error_msg) {
195                                 push(@errs, [ $f, "Copy failed : $inst_error_msg" ]);
196                                 next;
197                                 }
198                         local $qdest = quotemeta($dest);
199                         local $rst = &remote_eval($s->{'host'}, "webmin",
200                                                    "[ stat(\"$qdest\") ]");
201                         local @st = stat($f);
202                         if ($st[7] == $rst->[7]) {
203                                 push(@files, $f);
204                                 }
205                         else {
206                                 push(@errs, [ $f, "Copy was incomplete" ]);
207                                 }
208                         }
209
210                 # Run the post command on remote
211                 local $out;
212                 if ($_[0]->{'cmd'} && !$_[0]->{'cmdlocal'}) {
213                         $out = &remote_foreign_call($s->{'host'},
214                                 "webmin", "backquote_logged", $_[0]->{'cmd'});
215                         }
216
217                 print $wh &serialise_variable([ 1, \@files, \@errs, \@dirs,
218                                                 $out, $bout ]);
219                 close($wh);
220                 exit;
221                 }
222         close($wh);
223         $p++;
224         }
225
226 # Get back all the results
227 $p = 0;
228 foreach $s (@run) {
229         local $rh = "READ$p";
230         local $line = <$rh>;
231         close($rh);
232         local $rv = &unserialise_variable($line);
233
234         if (!$line) {
235                 &$func(0, $s, "Unknown reason");
236                 }
237         else {
238                 &$func($rv->[0], $s, $rv->[1], $rv->[2], $rv->[3],
239                        $rv->[4], $rv->[5]);
240                 }
241         $p++;
242         }
243 unlink($ltemp);
244
245 # Run the post command on local
246 if ($_[0]->{'cmd'} && $_[0]->{'cmdlocal'}) {
247         &system_logged("$_[0]->{'cmd'} >/dev/null 2>&1");
248         }
249
250 return @run;
251 }
252
253 # find_cron_job(&copy)
254 sub find_cron_job
255 {
256 local @jobs = &cron::list_cron_jobs();
257 local ($job) = grep { $_->{'user'} eq 'root' &&
258                 $_->{'command'} eq "$cron_cmd $_[0]->{'id'}" } @jobs;
259 return $job;
260 }
261
262 # expand_dir(dir, &files, &dirs)
263 sub expand_dir
264 {
265 push(@{$_[2]}, $_[0]);
266 opendir(DIR, $_[0]);
267 local $f;
268 foreach $f (readdir(DIR)) {
269         next if ($f eq "." || $f eq "..");
270         local $fp = "$_[0]/$f";
271         next if (-l $fp);
272         if (-d $fp) {
273                 &expand_dir($fp, $_[1], $_[2]);
274                 }
275         else {
276                 push(@{$_[1]}, $fp);
277                 }
278         }
279 closedir(DIR);
280 }
281
282 1;
283