#!/opt/casp/tools/bin/linux2/perl5/bin/perl -I/opt/casp/tools/bin/linux2/perl5/lib -I/opt/casp use INSTALL::common; use INSTALL::global; use INSTALL::parameter; use INSTALL::os; use INSTALL::process_info; my $pParameters = \%INSTALL::parameter::variables; #============================================================================== #------------------------ Default global values #============================================================================== $pParameters->{asphome} = "/opt/casp"; $pParameters->{com_layer} = "chilicom"; INSTALL::common::set_standard_globals; my $pGlobals = \%INSTALL::global::variables; my $pOS = \%INSTALL::os::Details; sub usage { report("Usage: $0 binary=... conf=... install_root=... start|stop|restart|status"); } #============================================================================== #------------------------ Validate all the required parameters. #============================================================================== my %PARAM_REQUIREMENTS = ( REQUIRED => [ 'binary', 'conf', 'install_root', 'start|stop|restart|status'], ); $pParameters->{non_interactive} = 'true'; # This tool will _NOT_ run interactively. if ((not @ARGV) || boolean_value($pParameters->{'--help'})) { usage(); } unless(INSTALL::parameter::verify(%PARAM_REQUIREMENTS)) { usage(); exit (1); } if (not (-f $pParameters->{conf})) { report("Error: The specified configuration file '$pParameters->{conf}' does not exist."); } #------------------------------------------------------------------------------ # Read the Zeus pid database we maintain, storing each pid to #------------------------------------------------------------------------------ sub read_database { my @pid_lines; my $pid_file = "$pGlobals->{log_dir}/zeus-pid.database"; open(PIDFILE,"<$pid_file"); @pid_lines = ; close(PIDFILE); my %pid_map; chomp @pid_lines; foreach(@pid_lines) { if (m@^([^|]+\|[^|]+)\|([^|]+)$@) { $pid_map{$1} = $2; } } return(%pid_map); } #------------------------------------------------------------------------------ # Save the pid information for the specified process in the zeus pid database #------------------------------------------------------------------------------ sub save_database { my (%pid_map) = @_; my $pid_file = "$pGlobals->{log_dir}/zeus-pid.database"; unlink($pid_file); open(PIDFILE,">$pid_file"); foreach(keys %pid_map) { print PIDFILE ("$_|$pid_map{$_}\n"); } close(PIDFILE); return(1); } #------------------------------------------------------------------------------ # Confirm that the specified is associated with a binary / conf pair of # and . #------------------------------------------------------------------------------ sub confirm_process { my ($pid,$binary,$conf) = @_; my %process_info = INSTALL::process_info::process_info($pid); if (scalar(keys %process_info)) { my $binary_path = INSTALL::common::realpath($binary); my $conf_path = INSTALL::common::realpath($conf); my $conf_dir; if (not defined $binary_path) { $binary_path = $binary; } if (not defined $conf_path) { $conf_path = $conf; } # the process list will have the config_directory instead of the config # file. $conf_dir = $conf_path; $conf_dir =~ s@(.*)/\S+@$1@; # for now this is hard coded. we need to change this. if ($process_info{binary} =~ "zeus.web") { return(1); } } return(undef); } #------------------------------------------------------------------------------ # Searches the entire process space, attempting to find an zeus running with # the specified binary / conf pair. If such a process is found, its pid is # returned. Otherwise, undef is returned. # # Note: The returned process is guaranteed to have an owning (parent) pid of # 1 (which denotes the parent of all Apache processes). #------------------------------------------------------------------------------ sub find_process { ($#_ == 1) || die "Invalid number of arguments."; my ($binary,$conf) = @_; my $binary_name; if ( -x "$binary" ) { $binary_name = "$binary"; $binary_name =~ s@(.*)/(\S+)$@$2@; } else { #let us hard code this if required. $binary_name = "zeus.web"; } foreach(INSTALL::process_info::retrieve_pids) { if (confirm_process($_,$binary_name,$conf)) { return($_); } } return(undef); } #------------------------------------------------------------------------------ # this routine, will parse the config/magnus.dir and fill the hash object with # with all required informations. # # Following are some of the values we are interested as of now. # # $pServerInfo->{webserver_install_root} - # which will point to the location of the Zeus installation which is attached # to Sun Chili!Soft ASP server. # # $pServerInfo->{webserver_start_script} - # this should point to the start script of the zeus server. # # $pServerInfo->{webserver_stop_script} - # this should point to the stop script of the zeus server. # # Returns 1 if success otherwise undef is returned. #------------------------------------------------------------------------------ sub process_nsconfig { my ($pServerInfo) = @_; #### # unless(open(CONFIG, "<$pServerInfo->{webserver_conf}")) { # return(undef); # } # while() { # if (/^\s*\#ServerRoot\s+(\S+)/) { # $pServerInfo->{webserver_install_root} = $1; # $pServerInfo->{webserver_install_root} =~ s/(\S.*)\/(\S+)$/$1/ ; # } # } # close(CONFIG); ##### if ( not exists $pServerInfo->{webserver_start_script}) { if ( -f "$pServerInfo->{webserver_install_root}/start" ) { # We must make certain that the library path for the appropriate # chiliasp binaries is on the libpath already. $pServerInfo->{webserver_start_script}= ("$pServerInfo->{webserver_install_root}/start"); } else { # As of now, I have no idea of how to start # without the Zeus's start script. return undef; } } if ( not exists $pServerInfo->{webserver_stop_script}) { if ( -f "$pServerInfo->{webserver_install_root}/stop" ) { $pServerInfo->{webserver_stop_script}= ("$pServerInfo->{webserver_install_root}/stop"); } else { # As of now, I have no idea of how to start # without the Zeus's stop script. return undef; } } } #------------------------------------------------------------------------------ # Returns 1 if the webserver is running, undef otherwise. #------------------------------------------------------------------------------ sub status { my %database = read_database; my $key = INSTALL::common::realpath($pParameters->{binary})."|".INSTALL::common::realpath($pParameters->{conf}); if (exists $database{$key}) { if (confirm_process($database{$key},$pParameters->{binary},$pParameters->{conf})) { report("status: The webserver is running."); return(1); } } if (find_process($pParameters->{binary},$pParameters->{conf})) { report("status: The webserver is running."); return(1); } else { report("status: The webserver is not running."); return(undef); } return 1; } #------------------------------------------------------------------------------ # Returns 1 if the start succeeds. #------------------------------------------------------------------------------ sub start { my $LIBPATH_VAR = $pOS->{libpath_variable}; my %database = read_database; my $key = INSTALL::common::realpath($pParameters->{binary})."|".INSTALL::common::realpath($pParameters->{conf}); if (exists $database{$key}) { if (confirm_process($database{$key},$pParameters->{binary},$pParameters->{conf})) { report("start: The webserver was already running."); return(undef); } } if (find_process($pParameters->{binary},$pParameters->{conf})) { report("start: The webserver was already running."); return(undef); } else { # Now look for various combinations of inputs possible for calling this webserver my %server_info; if (-f $pParameters->{conf}) { $server_info{conf} = "$pParameters->{conf}"; } elsif ((-d "$pParameters->{conf}") && (-f "$pParameters->{conf}/config/magnus.conf")) { $server_info{conf} = "$pParameters->{conf}/config/magnus.conf"; } else { return undef; } if (not process_nsconfig(\%server_info)) { return undef; } my $pid = fork; if (not $pid) { # To start the Zeus web server, we will presently support only the # conventional way - i.e, we support this , only if the start script # is present at the location. my @library_additions = ( $pGlobals->{com_lib_dir}, $pGlobals->{lib_dir} ); if (exists $ENV{$LIBPATH_VAR}) { my @library_order = split(/:/,$ENV{$LIBPATH_VAR}); my @library_paths; foreach my $libpath (@library_order) { if (not grep { $_ eq $libpath} @library_additions) { push @library_paths, $libpath; } } $ENV{$LIBPATH_VAR}=join(":",@library_paths); } $ENV{$LIBPATH_VAR}=join(":",@library_additions) . ":$ENV{$LIBPATH_VAR}"; if (exists $server_info{start_script}) { exec("$server_info{start_script}"); } exit(1); } else { wait; if ($?) { report("start: The webserver failed to start."); return(undef); } else { $database{$key} = $pid; save_database(%database); report("start: The webserver was successfully started."); return(1); } } } } #------------------------------------------------------------------------------ # Returns 1 if the stop succeeds. #------------------------------------------------------------------------------ sub stop { my %database = read_database; my $key = INSTALL::common::realpath($pParameters->{binary})."|".INSTALL::common::realpath($pParameters->{conf}); if (exists $database{$key}) { if (confirm_process($database{$key},$pParameters->{binary},$pParameters->{conf})) { # Now look for various combinations of inputs possible for calling this webserver my %server_info; if (-f $pParameters->{conf}) { $server_info{conf} = "$pParameters->{conf}"; } elsif ((-d "$pParameters->{conf}") && (-f "$pParameters->{conf}/config/magnus.conf")) { $server_info{conf} = "$pParameters->{conf}/config/magnus.conf"; } else { return undef; } if (not process_nsconfig(\%server_info)) { return undef; } my $forkpid = fork; if (not $forkpid) { # To stop the Zeus web server, we will presently support only the # conventional way - i.e, we support this , only if the start script # is present at the location. if (exists $server_info{stop_script}) { exec("$server_info{stop_script}"); } exit(1); } else { wait; if ($?) { report("start: The webserver failed to stop."); return(undef); } else { $database{$key} = $pid; save_database(%database); report("start: The webserver was successfully stopped."); return(1); } } } } my $pid = find_process($pParameters->{binary},$pParameters->{conf}); if ($pid) { # Now look for various combinations of inputs possible for calling this webserver my %server_info; if (-f $pParameters->{conf}) { $server_info{conf} = "$pParameters->{conf}"; } elsif ((-d "$pParameters->{conf}") && (-f "$pParameters->{conf}/config/magnus.conf")) { $server_info{conf} = "$pParameters->{conf}/config/magnus.conf"; } else { return undef; } if (not process_nsconfig(\%server_info)) { return undef; } my $forkpid = fork; if (not $forkpid) { # To stop the Zeus web server, we will presently support only the # conventional way - i.e, we support this , only if the start script # is present at the location. if (exists $server_info{stop_script}) { exec("$server_info{stop_script}"); } exit(1); } else { wait; if ($?) { report("start: The webserver failed to stop."); return(undef); } else { $database{$key} = $pid; delete $database{$key}; save_database(%database); report("start: The webserver was successfully stopped."); return(1); } } } else { report("stop: No webserver found to stop."); return(undef); } } #------------------------------------------------------------------------------ # Returns 1 if the start succeeds. #------------------------------------------------------------------------------ sub restart { my $result; stop; $result = start; return($result); } my $result = 1; my @directives = grep { /^(?:start|stop|restart|status)$/ } @ARGV; foreach (@directives) { /^start$/ && do { $result &= start; }; /^restart$/ && do { $result &= restart; }; /^stop$/ && do { $result &= stop; }; /^status$/ && do { $result &= status; }; } exit(not $result);