Skip to main content.
home | support | download

Back to List Archive

CGI script to build Swish-E (without command line access)

From: SRE <eckert(at)not-real.climber.org>
Date: Sun Oct 06 2002 - 23:17:37 GMT
I asked about this topic some weeks ago, and I just finished
cleaning up my CGI to build/test/install swish-e without having
any command line access. Your sysadmin MIGHT NOT LIKE THIS...
and I'm sure it can be made more clever, but I'm posting here
so the maintainers can include it in the release if they find
it useful! (It's a step up from general procedural advice.)

The idea is to call this CGI and be presented a menu of steps
you need for unpacking, building, and testing swish-e. Clicking
on any of the provided links runs that command and presents the
menu again after displaying the transcript from the command.
An attempt is made to capture STDERR and server warnings, but
I'm not sure it will be 100% accurate. Please suggest improvements!

It is trivial to add new commands or alter the ones provided,
even if you don't know Perl very well. Just follow the pattern
and everything should be pretty automatic. The one most likely
to need editing is the "cp" command that "installs" the exe
by just copying it up one level. This CGI presumes you've
already created a directory called SwishSource and copied
the appropriate tarball into that directory with FTP. There
are some "sed" commands that get rid of makefile warnings,
but they are separate and you don't have to click on them!
Extensions for testing should be easy - see ShowTopMenu and
add more categories of your own.

Security threats may be a problem. Caveat Emptor! I only put
it on my server for a few minutes while I'm using it, then
delete it (and no, I don't call it BuildSwish.cgi). My simple
untainting process is "broad brush" and probably not safe.
Watch out for long lines wrapped by your mail reader.
(Take your raincoat. Remember to sign the check. Etc.)

Please let me know if you found this to be useful. As with Swish,
this script is "distributed with no warranty under the terms of
the GNU Public License."

Steve

----------------------------------------------

#!/usr/local/bin/perl -Tw

# NOTE: Can't use -T without forcing $ENV
# $ENV{"PATH"} = "/bin"; # or use path below, observed from "echo $path" WITHOUT using -T
$ENV{"PATH"} = "/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/sbin";

#  6 Oct 02 - written by SRE for use on Climber.Org

use strict;
use CGI::Carp qw(fatalsToBrowser);
use CGI::Carp qw(carpout);

my $thiscgi  = "../scripts/BuildSwish.cgi"; # this file, for calls from ShowTopMenu

# here are some commands for general testing of OS
my @general = (
"pwd",
"cat /etc/motd",        # FreeBSD 4.6.2-RELEASE (DTE) #0
"gcc -v",               # gcc version 2.95.3
"which gcc ; which cc", # /usr/bin/gcc  /usr/bin/cc
"which make",           # /usr/bin/make
"which gzip",           # /usr/bin/gzip
"ls -l",
);

# here are the commands to unpack
my @unpack = (
"cd SourceFolder",
"rm -rf swish-e-2.2",                      # clean up old dir
"gzip -d < swish-e-2.2.tar.gz | tar xf -", # unpack tarball
"chmod 777 swish-e-2.2",                   # open up new dir
);

# here are the commands to build
my @build = (
"cd SourceFolder/swish-e-2.2 ; set HOME `pwd` ; export HOME",
"sed 's,test \"\$withval\" == \"maybe\",test \"\$withval\" = \"maybe\",' < configure > configure.1",
"sed 's,mv \$tmp/out \$ac_file,cp \$tmp/out \$ac_file ; rm \$tmp/out,' < configure.1 > configure.2",
"sed 's,mv \$tmp/config.h \$ac_file,cp \$tmp/config.h \$ac_file ; rm \$tmp/config.h,' < configure.2 > configure",
"chmod +x configure ; ./configure",            # run configure
"make",
"make swish-search",
"make test",
"cp src/swish-e .. ; cp src/swish-search ..",
);


###############################################################################
sub RunCommand {
###############################################################################
my $kind = shift;
my $number = shift;

if($kind =~ m/^([a-z]+)$/)
  { $kind = $1; } # untaint
if($number =~ m/^([0-9]+)$/)
  { $number = $1; } # untaint

my @ref = eval "\@$kind";
my $cmd = "$ref[0] ; $ref[$number]";
my $stroke = $cmd;
$stroke =~ s, ; ,<br>,g;

print <<BLOCK;
<h1>Running Command</h1>
<hr>system("$cmd");<hr>$stroke<hr>
<pre>
BLOCK

my $stat = system($cmd);
if($stat)
  {
  print "<p><font color=#cc0000>ERROR: $!</font><p>\n";
  errorargs("status=$stat from running $cmd");
  }

print "</pre><p>\n";
ShowTopMenu();
print "</body></html>\n";

exit(0);
} # end of RunCommand


###############################################################################
sub ShowTopMenu {
###############################################################################

print "<h1>$_[0]</h1>\n" if(defined($_[0]));

print "<p><table border=1 cellpadding=2 cellspacing=0>\n";

foreach my $category ( qw ( general unpack build ) )
  {
  my @ref = eval "\@$category";

  print "<tr><td align=\"center\">$category commands</tr><tr bgcolor=#eeeeee><td>ALWAYS PREFIX WITH '$ref[0]'\n";
  for(my $item=1; defined($ref[$item]); $item++)
    {
    print "<br><a href=\"$thiscgi?$category+$item\">$category $item</a>: ", $ref[$item], "\n";
    }
  print "</tr>\n";
  }
print "</table><p>\n";

return;
} # end of ShowTopMenu


###############################################################################
sub errorargs {
###############################################################################
my $msg = shift;

print "<font color=#ff0000>ERROR! $msg</font><p>\nAborting...<p>\n</body></html>\n";
exit(1);
} # end of errorargs


###############################################################################
# from here down is "main", the only non-subroutine part of this script!
###############################################################################

print <<BLOCK;
Content-type: text/html

<html>
<title>Command Line Replacement Script</title>
<body>
BLOCK

# capture STDERR in the same transcript:
open(STDERR,">&STDOUT");

if(!defined($ARGV[0]))
  {
  ShowTopMenu("Top Menu / Entry Point");
  print "</body></html>\n";
  exit(0);
  }

# this routine never returns...
RunCommand($ARGV[0],$ARGV[1]) if(($ARGV[0] =~ m/^[a-z]+$/) && ($ARGV[1] =~ m/^[0-9]+$/));

# should exit at end of routine calls above...
errorargs("Invalid Mode Defined - $ARGV[0]");

exit(0); # should never get here

# end of file
Received on Sun Oct 6 23:21:35 2002