#! c:\perl\bin\perl.exe # configurationServer.pl # # Alan Booth aebooth@us.ibm.com # # This is a server program for Windows NT. It listens on port 9000. # There is a similar version for AIX, but that version doesn't start and # stop NT services. Instead, it invokes shell scripts that start or # stop WTP, WebSphere, or the HTTP server. # # This script handles requests from one socket connection at a time # # It can: # Start and Stop services running on the host # Configure IBM WebSphere Transcoding Publisher to run in either the # Proxy or Servlet model # Configure IBM WebSphere Transcoding Publisher to run with any set of # plugins that are needed. The currently built-in plugin sets are: # backbone, text, image, and both # # Special case: $0 = "--list" # This will list the short names of all the services running on the host # use Win32::Service; use IO::Socket; use Net::hostent; # for OO version of gethostbyaddr $transPubHome = "c:\\program files\\IBMTrans"; $baseConfigurationFile = "$transPubHome\\etc\\baseConfiguration.prop"; $proxyRegistryFile = "$transPubHome\\etc\\config\\proxy\\registry.prop"; $servletRegistryFile = "$transPubHome\\etc\\config\\servlet\\registry.prop"; $BuildIDFile = "$transPubHome\\Build.ID"; #These are the service names as known to Windows/NT $transpubServiceName = "IBM Transcoding Publisher Service"; $websphereServiceName = "WebSphere Servlet Service"; #$httpServiceName = "Apache"; $httpServiceName = "LotusDominoGoWebserver"; # prints the status of $service on $hostname # invoked if this script's input is --list. This is useful for determining the # names of NT services that can be started/stopped by this script sub getServiceStatus { my($hostname,$service) = @_; $status = Win32::Service::GetStatus($hostname, $service, \%status); foreach $key (keys %status) { # print "$key ==> @status{$key} \n"; if ($key eq "ServiceType") { $status = @status{$key}; } } return $status; } # starts $service on $hostname sub startService { my($hostname,$service) = @_; #apc additions to make this handle keywords proxy and servlet to make #this operate more like the unix version if ($service eq "proxy") { # test program will send this in $service = $transpubServiceName; $hostname = ""; } elsif ($service eq "servlet") { # test program will send this in $service = $httpServiceName; $hostname = ""; } $success = Win32::Service::StartService($hostname,$service); printf("Started $service on $hostname rc is $success\r\n"); return $success; } # stops $service on $hostname sub stopService { my($hostname,$service) = @_; #apc additions to make this handle keywords proxy and servlet to make #this operate more like the unix version if ($service eq "proxy") { # test program will send this in $service = $transpubServiceName; $hostname = ""; } elsif ($service eq "servlet") { # test program will send this in $service = $websphereServiceName; # first stop websphere $hostname = ""; $success = Win32::Service::StopService($hostname,$service); printf("Stopped $service on $hostname. rc is $success\r\n"); $service = $httpServiceName; # then stop webserver } $success = Win32::Service::StopService($hostname,$service); printf("Stopped $service on $hostname. rc is $success\r\n"); return $success; } # switch between proxy and servlet models. It writes the configuration files WTP needs sub modelSwap { my($newModel) = @_; $time = localtime(time); print "In modelSwap\($newModel\)\n"; open(FH_BASECONFIG,">$baseConfigurationFile"); if ($newModel eq "proxy") { print FH_BASECONFIG <<"END"; #version = 1.0 #$time ConfigKey=proxy END } elsif ($newModel eq "servlet") { print FH_BASECONFIG <<"END"; #version = 1.0 #$time ConfigKey=servlet END } close(FH_BASECONFIG); } # edit the appropriate WTP registry.prop file used by in the specified model sub editRegistry { my($model, $plugins) = @_; $time = localtime(time); if ($model eq "servlet") { print "Switching $servletRegistryFile to $plugins\n"; open(FH_REGISTRY,">$servletRegistryFile"); print FH_REGISTRY "# IBM Content Magic registry parameters for the servlet configuration\n"; print FH_REGISTRY "# File created by $0 at $time\n"; print FH_REGISTRY "ibm/PreferenceControl=true\n"; if ( ($plugins eq "text") || ($plugins eq "both") ) { print "TextEngine=true\n"; print FH_REGISTRY "ibm/TextEngine=true\n"; } else { print "TextEngine=false\n"; print FH_REGISTRY "ibm/TextEngine=false\n"; } if ( ($plugins eq "both") || ($plugins eq "image") ) { print "ImageEngine=true\n"; print FH_REGISTRY "ibm/ImageEngine=true\n"; } else { print "ImageEngine=false\n"; print FH_REGISTRY "ibm/ImageEngine=false\n"; } print FH_REGISTRY "ibm/DefaultHttpPlugin=true\n"; print FH_REGISTRY "ibm/HttpReflectorPlugin=true\n"; close(FH_REGISTRY); print "Done with alterations to $servletRegistryFile\n\n"; } elsif ($model eq "proxy") { print "Switching $proxyRegistryFile to $plugins\n"; open(FH_REGISTRY,">$proxyRegistryFile"); print FH_REGISTRY "# IBM Content Magic registry parameters for the proxy configuration\n"; print FH_REGISTRY "# File created by $0 at $time\n"; print FH_REGISTRY "ibm/ResourceRepositoryEngine=false\n"; print FH_REGISTRY "ibm/PreferenceControl=true\n"; if ( ($plugins eq "text") || ($plugins eq "both") ) { print FH_REGISTRY "ibm/TextEngine=true\n"; } else { print FH_REGISTRY "ibm/TextEngine=false\n"; } if ( ($plugins eq "both") || ($plugins eq "image") ) { print FH_REGISTRY "ibm/ImageEngine=true\n"; } else { print FH_REGISTRY "ibm/ImageEngine=false\n"; } print FH_REGISTRY "ibm/MEGletEngine=false\n"; print FH_REGISTRY "ibm/FragmentationEngine=false\n"; close(FH_REGISTRY); print "Done with alterations to $proxyRegistryFile\n\n"; } } # Switch to proxy model with $plugins sub proxy { #my($hostname,$service,$plugins) = @_; my($hostname,$plugins) = @_; # stop the service if it is running as a proxy print "Stopping $transpubServiceName\n"; $success = &stopService("",$transpubServiceName); print "$transpubServiceName successfully STOPPED\n\n" if ($success); # stop websphere print "Stopping $websphereServiceName\n"; $success = &stopService("",$websphereServiceName); print "$websphereServiceName successfully STOPPED\n\n" if ($success); sleep(2); # pause # Make certain we're in servlet model modelSwap("proxy"); # Set the requested plugins editRegistry("proxy",$plugins); sleep(2); # pause # Start the proxy print "Starting $transpubServiceName\n"; $success = &startService("",$transpubServiceName); print "$transpubServiceName successfully STARTED.\n" if $success != 0; } # Switch to servlet model with $plugins sub servlet { #my($hostname,$service,$plugins) = @_; my($hostname,$plugins) = @_; # stop the service if it is running as a proxy print "Stopping $transpubServiceName\n"; $success = &stopService("",$transpubServiceName); print "$transpubServiceName successfully STOPPED\n\n" if ($success); # stop websphere print "Stopping $websphereServiceName\n"; $success = &stopService("",$websphereServiceName); print "$websphereServiceName successfully STOPPED\n\n" if ($success); # stop http service print "Stopping $httpServiceName\n"; $success = &stopService("",$httpServiceName); print "$httpServiceName successfully STOPPED\n\n" if ($success); sleep(2); # pause # Make certain we're in servlet model modelSwap("servlet"); # Set the requested plugins editRegistry("servlet",$plugins); sleep(10); # pause # I should just have to restart the http server, right? print "Starting $httpServiceName\n"; $success = &startService("",$httpServiceName); print "$httpServiceName successfully STARTED.\n" if $success != 0; } ############################################################################### # Main # This is where the Server socket is opened and requests are received # from the client. The requests indicate which configuration is desired. # This script then sets up that configuration and starts WTP ############################################################################### if ($ARGV[0] eq "--list") { Win32::Service::GetServices('',\%services); foreach $key (sort keys %services) { #print "$key --> $services{$key}\n"; print "$services{$key} --> $key\n"; } } $PORT = 9000; # pick something not in use $server = IO::Socket::INET->new( Proto => 'tcp', LocalPort => $PORT, Listen => SOMAXCONN, Reuse => 1); die "can't setup server" unless $server; print "[Server $0 accepting clients on port $PORT]\n"; while ($client = $server->accept()) { $client->autoflush(1); $hostinfo = gethostbyaddr($client->peeraddr); printf "[Connect from %s]\n", $hostinfo->name || $client->peerhost; while ( <$client> ) { print $_; next unless /\S/; # blank line chomp(); ($directive,$service)=split(/\s+/, $_, 2); #split $_ on whitespace into 2 pieces print "Directive ->$directive\n"; #print out for informational purposes print "Service ->$service\n"; if (/quit|exit/i) { last; } elsif (/stop/) { $success = &stopService("",$service); print "$service successfully STOPPED\n" if ($success); } elsif (/start/) { $success = &startService("",$service); print "$service successfully STARTED.\n" if $success != 0; } elsif (/proxybackbone/) { &proxy("","backbone"); } elsif (/proxytext/) { &proxy("","text"); } elsif (/proxyboth/) { &proxy("","both"); } elsif (/proxyimage/) { &proxy("","image"); } elsif (/servletbackbone/) { &servlet("","backbone"); } elsif (/servlettext/) { &servlet("","text"); } elsif (/servletboth/) { &servlet("","both"); } elsif (/servletimage/) { &servlet("","image"); } elsif (/build/) { # if requesting build, this reads a WTP file that has the build ID in it and # it sends the build ID back to the client. open(BUILDFILE, $BuildIDFile); while () { $build = $_; print $build; } close(BUILDFILE); print("Sending back $build\n"); print $client "$build"; #send build ID back to requester } } continue { } close $client; }