#!/usr/local/bin/perl
# do_install.cgi
# Install some package on all hosts, in parallel. If the package was
# downloaded from a URL, have the hosts do the same - otherwise, transfer
# it to each.

require './cluster-software-lib.pl';
&ReadParse();
&foreign_require("software", "software-lib.pl");
@packages = &foreign_call("software", "file_packages", $in{'file'});
foreach $p (@packages) {
	local ($n, $d) = split(/\s+/, $p, 2);
	push(@names, $n); push(@descs, $d);
	}
-r $in{'file'} || &error($text{'do_edeleted'});
&header($text{'do_title'}, "");
print "<hr>\n";

# Setup error handler for down hosts
sub inst_error
{
$inst_error_msg = join("", @_);
}
&remote_error_setup(\&inst_error);

@hosts = &list_software_hosts();
@servers = &list_servers();
if ($in{'server'} != -1) {
	@hosts = grep { $_->{'id'} == $in{'server'} } @hosts;
	local ($s) = grep { $_->{'id'} == $hosts[0]->{'id'} } @servers;
	print "<b>",&text('do_header2', join(" ", @names),
		  $s->{'desc'} ? $s->{'desc'} : $s->{'host'}),"</b><p>\n";
	}
else {
	print "<b>",&text('do_header', join(" ", @names)),"</b><p>\n";
	}

$p = 0;
foreach $h (@hosts) {
	# Install on this host
	local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
	local $d = $s->{'desc'} ? $s->{'desc'} : $s->{'host'};

	local ($rh = "READ$p", $wh = "WRITE$p");
	pipe($rh, $wh);
	select($wh); $| = 1; select(STDOUT);
	if (!fork()) {
		# Do the install in a subprocess
		close($rh);

		&remote_foreign_require($s->{'host'}, "software",
					"software-lib.pl");
		if ($inst_error_msg) {
			# Failed to contact host ..
			print $wh &serialise_variable([ $inst_error_msg ]);
			exit;
			}
		local $rfile;
		local $need_unlink = 1;
		if (!$s->{'id'}) {
			# This host, so we already have the file
			$rfile = $in{'file'};
			$need_unlink = 0;
			}
		elsif ($in{'source'} == 0) {
			# Is the file the same on remote (like if we have NFS)
			local @st = stat($in{'file'});
			local $rst = &remote_eval($s->{'host'}, "software",
						  "[ stat('$in{'file'}') ]");
			local @rst = @$rst;
			if (@st && @rst && $st[7] == $rst[7] &&
			    $st[9] == $rst[9]) {
				# File is the same! No need to download
				$rfile = $in{'file'};
				$need_unlink = 0;
				}
			else {
				# Need to copy the file across :(
				$rfile = &remote_write(
					$s->{'host'}, $in{'file'});
				}
			}
		elsif ($in{'source'} == 2 && $in{'down'}) {
			# Ask the remote server to download the file
			$rfile = &remote_eval($s->{'host'}, "software",
					      '&tempname()');
			if ($in{'ftpfile'}) {
				&remote_foreign_call($s->{'host'}, "software",
				    "ftp_download", $in{'host'}, $in{'ftpfile'},
				    $rfile);
				}
			else {
				&remote_foreign_call($s->{'host'}, "software",
				    "http_download", $in{'host'}, $in{'port'},
				    $in{'page'}, $rfile);
				}
			}
		else {
			# Need to copy the file across :(
			$rfile = &remote_write($s->{'host'}, $in{'file'});
			}

		# Do the install ..
		local @rv;
		for($i=0; $i<@names; $i++) {
			local $error = &remote_foreign_call($s->{'host'},
					"software", "install_package",
					$rfile, $names[$i], \%in);
			if ($error) {
				push(@rv, $error);
				}
			else {
				# Success .. get the package details
				push(@rv, [ &remote_foreign_call($s->{'host'},
				    "software", "package_info", $names[$i]) ] );
				}
			}
		&remote_eval($s->{'host'}, "software", "unlink('$rfile')")
			if ($need_unlink);
		print $wh &serialise_variable(\@rv);
		close($wh);
		exit;
		}
	close($wh);
	$p++;
	}

# Get back all the results
$p = 0;
foreach $h (@hosts) {
	local $rh = "READ$p";
	local $line = <$rh>;
	close($rh);
	local $rv = &unserialise_variable($line);

	local ($s) = grep { $_->{'id'} == $h->{'id'} } @servers;
	local $d = $s->{'desc'} ? $s->{'desc'} : $s->{'host'};

	if (!$line) {
		print &text('do_failed', $d, "Unknown reason"),"<br>\n";
		}
	else {
		$i=0;
		foreach $r (@$rv) {
			if (ref($r)) {
				# Install went ok!
				print &text('do_success', $d),"<br>\n";
				$pinfo[$i] = $r if (!$pinfo[$i]);
				if (&indexof($names[$i],
					     @{$h->{'packages'}}) < 0) {
					push(@{$h->{'packages'}},
					     { 'name' => $names[$i],
					       'desc' => $descs[$i],
					       'class' => $pinfo[$i]->[1] });
					&save_software_host($h);
					}
				}
			else {
				# Failed for some reason..
				print &text('do_failed', $d, $r),"<br>\n";
				}
			$i++;
			
			}
		}
	$p++;
	}

unlink($in{'file'}) if ($in{'need_unlink'});
print "<p><b>$text{'do_done'}</b><p>\n";

for($i=0; $i<@names; $i++) {
	next if (!$pinfo[$i]);
	print "<table border width=100%>\n";
	print "<tr $tb> <td><b>$text{'do_details'}</b></td> </tr>\n";
	print "<tr $cb> <td><table width=100%>\n";
	print "<tr> <td valign=top width=20%><b>$text{'do_desc'}</b></td>\n";
	print "<td colspan=3><pre>$pinfo[$i]->[2]</pre></td> </tr>\n";

	print "<tr> <td width=20%><b>$text{'do_pack'}</b></td> <td>$pinfo[$i]->[0]</td>\n";
	print "<td width=20%><b>$text{'do_class'}</b></td> <td>",
		$pinfo[$i]->[1] ? $pinfo[$i]->[1] : $text{'do_none'},"</td> </tr>\n";

	print "<tr> <td width=20%><b>$text{'do_ver'}</b></td> <td>$pinfo[$i]->[4]</td>\n";
	print "<td width=20%><b>$text{'do_vend'}</b></td> <td>$pinfo[$i]->[5]</td> </tr>\n";

	print "<tr> <td width=20%><b>$text{'do_arch'}</b></td> <td>$pinfo[$i]->[3]</td>\n";
	print "<td width=20%><b>$text{'do_inst'}</b></td> <td>$pinfo[$i]->[6]</td> </tr>\n";
	print "</table></td></tr></table><p>\n";
	}

&remote_finished();
print "<hr>\n";
&footer("", $text{'index_return'});

