通过例子解析perl脚本

来源:互联网 发布:金十数据原油直播室 编辑:程序博客网 时间:2024/05/17 04:15
#!/usr/bin/perl
###############################################################################
#
# Script:        server.pl
# Purpose:       start or stop application server
# Author:        #
# Copyright #
###############################################################################
# 引入perl模块(编译时引入)
use POSIX;
use Digest::MD5 qw(md5_hex);

#判断路径中是否存在该perl模块文件,存在则引入,require是运行时引入
if(-e $ENV{"EAS_HOME"}."/server/bin/common.pm"){
    require $ENV{"EAS_HOME"}."/server/bin/common.pm";
}

#判断执行脚本时有没有带参数
if(@ARGV < 0){
#没有带参数,则抛出错误,退出perl脚本,输出错误信息:Usage: perl server.pl start|stop|restart [debug|nohup]
  die "Usage: perl server.pl start|stop|restart [debug|nohup]";
}

#定义全局变量
$JVM_MEM_OPTIONS = "-Xms$common::JVM_INITIAL_HEAP_SIZE -Xmx$common::JVM_MAX_HEAP_SIZE $common::JVM_CUSTOM_PARAMS -Djava.net.preferIPv4Stack=true -Djava.io.tmpdir=$ENV{EAS_HOME}/iotmpdir";
if ((&common::isSunJDK() eq "true") or (&common::isHpJDK() eq "true") ) {
#perl拼接字符串用的是"."
  $JVM_MEM_OPTIONS = "-server -XX:PermSize=$common::JVM_PERM_SIZE -XX:MaxPermSize=$common::JVM_MAX_PERM_SIZE " . $JVM_MEM_OPTIONS;

 $DUMP_LOG_FILE_NAME = "javaheapdump_" . strftime("%Y-%m-%d_%H-%M-%S", localtime). ".hprof";
# $common::EAS_INSTANCE_HOME引用环境变量EAS_INSTANCE_HOME
$JVM_MEM_OPTIONS=$JVM_MEM_OPTIONS . "  -XX:HeapDumpPath=$common::EAS_INSTANCE_HOME/bin/" . $DUMP_LOG_FILE_NAME . " -XX:+HeapDumpOnOutOfMemoryError ";

}
if (&common::isBEAJDK() eq "true") {
  $JVM_MEM_OPTIONS = "-jrockit " . $JVM_MEM_OPTIONS;
}
if (&common::isIBMJDK() eq "true" && $common::APP_SERVER_TYPE eq "apusic"){
        $JVM_MEM_OPTIONS .= " -Dcom.apusic.corba.ORBFactory=com.apusic.corba.plugin.ee.ORBFactoryImpl ";
    }
if($common::JVM_VERBOSE_GC eq "true"){
    $GC_LOG_FILE_NAME = "jvm_gc_" . strftime("%Y-%m-%d_%H-%M-%S", localtime). ".log";
    if (&common::isBEAJDK() eq "true") {
        $JVM_MEM_OPTIONS .= " -verbose:gc -Xverboselog:$common::EAS_INSTANCE_HOME/logs/" . $GC_LOG_FILE_NAME;
    }elsif (&common::isIBMJDK() eq "true"){
        $JVM_MEM_OPTIONS .= " -verbose:gc -Xverbosegclog:$common::EAS_INSTANCE_HOME/logs/" . $GC_LOG_FILE_NAME . ",10,1000000";
    }elsif (&common::isHpJDK() eq "true"){
        $JVM_MEM_OPTIONS .= " -verbose:gc -Xverbosegc:file=$common::EAS_INSTANCE_HOME/logs/" . $GC_LOG_FILE_NAME;
    }else{
        $JVM_MEM_OPTIONS .= " -verbose:gc -Xloggc:$common::EAS_INSTANCE_HOME/logs/" . $GC_LOG_FILE_NAME;
    }
}

$JVM_DEBUG_OPTIONS = "";
if ($ARGV[1] eq "debug") {
  $JVM_DEBUG_OPTIONS = "-Xdebug -Xrunjdwp:transport=dt_socket,server=y,suspend=n,address=" . $common::EAS_SERVER_DEBUG_PORT;
}

$STARTUP_NOHUP = "false";
if ($ARGV[1] eq "nohup") {
    $STARTUP_NOHUP = "true";
}

$COMMAND_NAME=$ARGV[0];
$COPYRIGHT_TITLE="    Kingdee EAS Server";
# eq相当于java的equals
if($COMMAND_NAME eq "start"){
#打开outfile文件(没有则创建)
open (OUTFILE,">>./outfile");
#输入“”中的内容到outfile
print OUTFILE "***************************************************startApplicationServer\n";
#关闭文件
close(OUTFILE);
#调用startApplicationServer()函数
    &startApplicationServer();
}elsif ($COMMAND_NAME eq "stop"){
  &stopApplicationServer();
}elsif ($COMMAND_NAME eq "restart"){
  &stopApplicationServer();
  &startApplicationServer();
}elsif ($COMMAND_NAME eq "startdispatcher") {
  &startDispatcher();
}elsif ($COMMAND_NAME eq "stopdispatcher") {
  &stopDispatcher();
}elsif($COMMAND_NAME eq "registerapusicserver") {
    &registerapusicserver();
}elsif($COMMAND_NAME eq "unregisterapusicserver") {
    &unregisterapusicserver();
}elsif($COMMAND_NAME eq "startstandbydispatcher") {
    &startStandbyDispatcher();
}else {
    die "Unkown command name [$COMMAND_NAME]!";
}

#定义一个函数
sub printCopyRight(){
  print "\n\n";
  print "***************************************************\n";
  print "           $COPYRIGHT_TITLE                  \n";
  print "                ($common::APP_SERVER_TYPE Edition)\n";
  print "               Copyright (C) 2006                      \n";
  print "       Kingdee Software (China) Co., Ltd.          \n";
  print "***************************************************\n";
  print "[Platform  ]:$common::OS_PLATFORM\n";
  print "[JavaVendor]:$common::JDK_VERDOR\n";
 
  $DEVELOP_EAS = "";
  if ($common::DEVELOP_MODE eq "true"){
      $DEVELOP_EAS = " For developer";
  }
  # ne就是不等于
  if ($common::APP_SERVER_TYPE ne "websphere") {
    if ($JVM_DEBUG_OPTIONS ne "") {
      print "[ServerMode]:DEBUG (debug port:" . $common::EAS_SERVER_DEBUG_PORT . ")" . $DEVELOP_EAS . "\n";
    }else{
       print "[ServerMode]:PRODUCT" . $DEVELOP_EAS . "\n";
    }
  }
  print "\n\n";
}

#===============================================================
#
#     Apusic
#
#===============================================================
sub startApusic(){
#定义一个局部变量
  my $jvmMemoptions = $JVM_MEM_OPTIONS . " " . $JVM_DEBUG_OPTIONS;
  #join就是以第一个字符串为拼接符号,后面的参数为拼接内容,可以有多个拼接内容,这里只是在@common::JVM_OPTIONS加一个空格
  my $jvmProps = join(" ", @common::JVM_OPTIONS);
  $jvmProps = $jvmProps ." -Dapusic.connect.anytime=false -Dcom.apusic.domain.home=" .$ENV{"PROFILE_PATH"};
  $jvmProps = $jvmProps ." -Dcom.apusic.jvm.route=" . $common::EAS_NODEID . $common::EAS_INSTANCE_NAME;
#  $jvmProps = $jvmProps ." -Dcom.sun.management.jmxremote";
  if ((&common::isSunJDK() eq "true") or (&common::isOsHpUnix eq "true") ) {
    #$jvmProps = $jvmProps ." -Dcom.apusic.net.bio=true ";
  }
 open (OUTFILE,">>./outfile");
print OUTFILE "***************************************************1\n";
close(OUTFILE);
  if (&common::isIBMJDK eq "true") {
      $jvmProps = $jvmProps . " -Djavax.rmi.CORBA.UtilClass=com.apusic.corba.ee.impl.javax.rmi.CORBA.Util -Djavax.rmi.CORBA.StubClass=com.apusic.corba.ee.impl.javax.rmi.CORBA.StubDelegateImpl -Djavax.rmi.CORBA.PortableRemoteObjectClass=com.apusic.corba.ee.impl.javax.rmi.PortableRemoteObject ";
  }
  my $mainClass =  "com.kingdee.eas.tools.launcher.Start";
  my @progArgs = ("./server.pid","com.apusic.server.Main","-root", "$common::APP_SERVER_HOME");  
  #my $apusicConfigPath = $ENV{"PROFILE_PATH"};
  #$apusicConfigPath =~ s/\s*//;
  #if ( -d $apusicConfigPath ){
      #@progArgs = ("./server.pid","com.apusic.server.Main","-root", $apusicConfigPath);   
  #}
    my $classpath="";
    if( $common::DEVELOP_MODE eq "true"){
    if (-e $common::EAS_USER_LIB_FILE) {
      $classpath = &common::getClasspath($common::EAS_USER_LIB_FILE);
    }
    }
    $classpath = $classpath . &common::getClasspath($common::EAS_SERVER_LIB_FILE);
open (OUTFILE,">>./outfile");
print OUTFILE "***************************************************2\n";
close(OUTFILE);
  if(&common::isOsWindows eq "true"){
 open (OUTFILE,">>./outfile");
print OUTFILE "***************************************************runJava3\n";
close(OUTFILE);
    &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
open (OUTFILE,">>./outfile");
print OUTFILE "***************************************************runJava4\n";
close(OUTFILE);
  }else{
open (OUTFILE,">>./outfile");
print OUTFILE "***************************************************startServerBackground3\n";
close(OUTFILE);
    &startServerBackground($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
open (OUTFILE,">>./outfile");
print OUTFILE "***************************************************startServerBackground4\n";
close(OUTFILE);
  }
}
sub stopApusic(){
  my $jvmMemoptions = $JVM_DEBUG_OPTIONS;
  my $jvmProps = "-DEAS_SERVER_BOOT_PORT=" . $common::EAS_SERVER_BOOT_PORT;
  my $mainClass =  "com.kingdee.eas.tools.launcher.appserver.StopServer";
  my @progArgs = ("$common::APP_SERVER_HOME");
  my $apusicConfigPath = $ENV{"PROFILE_PATH"};
  $apusicConfigPath =~ s/\s*//;
  if ( -d $apusicConfigPath ){
      @progArgs = ($apusicConfigPath);   
  }
 
  my $classpath ="$common::EAS_HOME/server/bin/tl_launcher.jar";   
  &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
}

#===============================================================
#
#     JBoss
#
#===============================================================
sub startJBoss(){
  my $jvmMemoptions = $JVM_MEM_OPTIONS . " " . $JVM_DEBUG_OPTIONS;
  my $jvmProps = join(" ", @common::JVM_OPTIONS);
  $jvmProps = $jvmProps ." -Dcom.apusic.domain.home=" .$ENV{"PROFILE_PATH"};
  $jvmProps = $jvmProps ." -Dcom.apusic.jvm.route=" . $common::EAS_NODEID . $common::EAS_INSTANCE_NAME;
  if ((&common::isSunJDK() eq "true") or (&common::isOsHpUnix eq "true") ) {
    $jvmProps = $jvmProps ." -Dprogram.name=run.bat ";
  }
  if (&common::isIBMJDK eq "true") {
      $jvmProps = $jvmProps . " -Djavax.rmi.CORBA.UtilClass=com.apusic.corba.ee.impl.javax.rmi.CORBA.Util -Djavax.rmi.CORBA.StubClass=com.apusic.corba.ee.impl.javax.rmi.CORBA.StubDelegateImpl -Djavax.rmi.CORBA.PortableRemoteObjectClass=com.apusic.corba.ee.impl.javax.rmi.PortableRemoteObject ";
  }
  my $mainClass =  "com.kingdee.eas.tools.launcher.Start";
  my @progArgs = ("./server.pid","org.jboss.Main");  
    my $classpath="";
    if( $common::DEVELOP_MODE eq "true"){
    if (-e $common::EAS_USER_LIB_FILE) {
      $classpath = &common::getClasspath($common::EAS_USER_LIB_FILE);
    }
    }
    $classpath = $classpath . &common::getClasspath($common::EAS_SERVER_LIB_FILE);
    
  if(&common::isOsWindows eq "true"){
    &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
  }else{        
    &startServerBackground($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
  }
}

sub stopJBoss(){
    my $jvmMemoptions = $JVM_DEBUG_OPTIONS;
  my $mainClass =  "org.jboss.Shutdown";
  my @progArgs = ("-S");
 
  my $classpath ="$common::APP_SERVER_HOME/client/jbossall-client.jar" . &common::getClasspathSplitChar();
  $classpath = $classpath . "$common::APP_SERVER_HOME/bin/shutdown.jar";  
  &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
    
  if(&common::isOsWindows eq "true"){
  #system执行指定脚本
    system($common::APP_SERVER_HOME . "/bin/shutdown.bat -S");
  }else {
    system($common::APP_SERVER_HOME . "/bin/shutdown.sh -S");
  }
}

#===============================================================
#
#     WebSphere
#
#===============================================================
sub getWebSphereCmdPath(){
  $WAS_SERVER_NAME=$ENV{"SERVER_NAME"};
  $WAS_PROFILES_NAME=$ENV{"PROFILES_NAME"};
  $WAS_PROFILE_PATH=$ENV{"PROFILE_PATH"};

  $WAS_SERVER_NAME =~ s/\s*//;
  if($WAS_SERVER_NAME eq ""){
    $WAS_SERVER_NAME = "server1";
   }
  $WAS_PROFILES_NAME =~ s/\s*//;
  if($WAS_PROFILES_NAME eq ""){
    if (&common::isOsWindows eq "true") {
        $WAS_PROFILES_NAME = "default";
    }else{
        $WAS_PROFILES_NAME = "AppSvr01";
    }
    }
    my $wasCmdPath = $common::APP_SERVER_HOME . "/bin";
    if($common::APP_SERVER_VERSION !~ /5.1/){
       if ($WAS_PROFILE_PATH ne "") {
           $wasCmdPath = $WAS_PROFILE_PATH . "/bin";
       }else{
           $wasCmdPath = $common::APP_SERVER_HOME . "/profiles/" . $WAS_PROFILES_NAME . "/bin";
       }
       if (not (-d $wasCmdPath)){
           $wasCmdPath = $common::APP_SERVER_HOME . "/bin";
       }
    }

        if (not (-d $wasCmdPath)) {
            die "Websphere command path[$wasCmdPath] does not exist!";
        }
    return $wasCmdPath;
}

sub startWebSphere(){
    &printCopyRight();
    my $cmdPath = &getWebSphereCmdPath();
    chdir($cmdPath);
    if (&common::isOsWindows eq "true") {
       print "Lauch command: " . $cmdPath . "/startserver.bat " . $WAS_SERVER_NAME . "\n";
       system("startserver.bat " . $WAS_SERVER_NAME);
    }else{
       print "Lauch command: " .$cmdPath . "/startserver.sh " . $WAS_SERVER_NAME . "\n";
       system("sh startServer.sh " . $WAS_SERVER_NAME);
    }
    #chdir("../logs/$WAS_SERVER_NAME/");
    #open(FILE1,"<./$WAS_SERVER_NAME.pid");
      #open(FILE2,">$common::EAS_INSTANCE_HOME/bin/server.pid");
    #binmode(FILE1);
    #binmode(FILE2);
    #while (<FILE1>) { print FILE2; }
    #close(FILE1);
    #close(FILE2);
    #chdir($common::EAS_INSTANCE_HOME . "/bin");
}


sub decodePasssword( ){

  my $jvmMemoptions = $JVM_DEBUG_OPTIONS;

  my $jvmProps = "";

  my $mainClass =  "com.kingdee.eas.tools.admin.eas.util.PasswordUtil";

  my @progArgs = ("$common::APP_SERVER_PASSWORD","$common::EAS_INSTANCE_HOME"."/bin/psd.txt");

  my $classpath ="$common::EAS_HOME/admin/plugins/admin.eas_1.0.0.jar:$common::EAS_HOME/server/lib/common/bos/ksql.jar:$common::EAS_HOME/server/lib/common/trd/log4j-1.2.15.jar";   

  &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
}

sub stopWebSphere(){
    &printCopyRight();
    my $cmdPath = &getWebSphereCmdPath();
    chdir($cmdPath);
    if (&common::isOsWindows eq "true") {
       system($common::EAS_INSTANCE_HOME."/bin/decode.cmd ".$common::APP_SERVER_PASSWORD." ".$common::EAS_INSTANCE_HOME."/bin/psd.txt");
    }else
    {
       decodePasssword();
    }
    open(READ_FILE,$common::EAS_INSTANCE_HOME."/bin/psd.txt");
    my $uncodepsd;
    while(<READ_FILE>)
    {
       $uncodepsd= $_;
    }
    unlink($common::EAS_HOME."/admin/psd.txt");
    if (&common::isOsWindows eq "true") {
        if($common::APP_SERVER_SECURITY eq "true"){
            print "Lauch command: " . $cmdPath . "/stopserver.bat " . $WAS_SERVER_NAME . " -username " . $common::APP_SERVER_USER . " -password " . $uncodepsd . "\n";
            system("stopserver.bat " . $WAS_SERVER_NAME . " -username " . $common::APP_SERVER_USER . " -password " . $uncodepsd);
        }else{
            print "Lauch command: " . $cmdPath . "/stopserver.bat " . $WAS_SERVER_NAME . "\n";
            system("stopserver.bat " . $WAS_SERVER_NAME);
        }
    }else{
        if($common::APP_SERVER_SECURITY eq "true"){
            print "Lauch command: " . $cmdPath . "/stopserver.sh " . $WAS_SERVER_NAME . " -username " . $common::APP_SERVER_USER . " -password " . $uncodepsd . "\n";
            system("sh stopServer.sh " . $WAS_SERVER_NAME . " -username " . $common::APP_SERVER_USER . " -password " . $uncodepsd);
        }else{
            print "Lauch command: " . $cmdPath . "/stopserver.sh " . $WAS_SERVER_NAME . "\n";
            system("sh stopServer.sh " . $WAS_SERVER_NAME);
        }
    }
    chdir($common::EAS_INSTANCE_HOME . "/bin");
    unlink("$common::EAS_INSTANCE_HOME/bin/server.pid");
}

#===============================================================
#
#     Weblogic
#
#===============================================================
sub initWeblogicPath(){
    $WL_SERVER_NAME=$ENV{"SERVER_NAME"};
    $WL_DOMAIN_NAME=$ENV{"PROFILES_NAME"};
    $WL_DOMAIN_PATH=$ENV{"PROFILE_PATH"};
    $WL_ADMIN_URL="t3://127.0.0.1:" . $common::APP_SERVER_JMX_PORT;

    $WL_SERVER_NAME =~ s/\s*//;
    if ($WL_SERVER_NAME eq "") {
        $WL_SERVER_NAME = "myserver";
    }
    $WL_DOMAIN_NAME =~ s/\s*//;
    if ($WL_DOMAIN_NAME eq "") {
        $WL_DOMAIN_NAME = "mydomain";
    }
}

sub startWeblogic(){
    &initWeblogicPath();
    $WL_DOMAIN_PATH=$ENV{"PROFILE_PATH"};
    if ($WL_DOMAIN_PATH eq "") {
        print "Please input weblogic domain path: ";
        $WL_DOMAIN_PATH = <STDIN>;
        chop($WL_DOMAIN_PATH);
    }
    my $jvmMemoptions = $JVM_MEM_OPTIONS . " " . $JVM_DEBUG_OPTIONS;
    my $jvmProps = join(" ", @common::JVM_OPTIONS);
    $jvmProps = $jvmProps . " -Dweblogic.Name=" . $WL_SERVER_NAME;
    $jvmProps = $jvmProps . " -Dweblogic.RootDirectory=" . $WL_DOMAIN_PATH;
    $jvmProps = $jvmProps . " -Dweblogic.management.username=" . $common::APP_SERVER_USER;
    $jvmProps = $jvmProps . " -Dweblogic.management.password=" . $common::APP_SERVER_PASSWORD;
    $jvmProps = $jvmProps . " -DjvmStd=$common::EAS_INSTANCE_HOME/logs/weblogic_std.log ";
    
    if (&common::isIBMJDK() eq "true"){
        $jvmProps = $jvmProps . " -Dlogin.configuration.provider=com.ibm.security.auth.login.ConfigFile -Dweblogic.NativeIOEnabled=false";
    }
    
    my $mainClass =  "com.kingdee.eas.tools.launcher.Start";
  my @progArgs = ("$common::EAS_INSTANCE_HOME/bin/server.pid","weblogic.Server");   
    chdir($WL_DOMAIN_PATH);
    
    my $classpath = &common::getClasspath($common::EAS_SERVER_LIB_FILE);
    
    if(&common::isOsWindows eq "true"){
        &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
    }else{
        &startServerBackground($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
        chdir($common::EAS_INSTANCE_HOME . "/bin");
    }
}
sub stopWeblogic(){
    &initWeblogicPath();
    my $jvmMemoptions = $JVM_DEBUG_OPTIONS;
    my $jvmProps = join(" ", @common::JVM_OPTIONS);
    $jvmProps = $jvmProps . " -Dweblogic.Name=" . $WL_SERVER_NAME;
    $jvmProps = $jvmProps . " -Dweblogic.RootDirectory=" . $WL_DOMAIN_PATH;
    my $mainClass =  "weblogic.Admin";
    my @classList = (
                      "\${APP_SERVER_HOME}/lib/*.jar"
                );
    my $classpath =  &common::getClasspathByList(@classList);
    my @progArgs = ("FORCESHUTDOWN", "-url", $WL_ADMIN_URL, "-username", $common::APP_SERVER_USER, "-password", $common::APP_SERVER_PASSWORD, $WL_SERVER_NAME);
    chdir($WL_DOMAIN_PATH);
    &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
    chdir($common::EAS_INSTANCE_HOME . "/bin");
    unlink("$common::EAS_INSTANCE_HOME/bin/server.pid");
}

#===============================================================
#
#     tomcat
#
#===============================================================
sub startTomcat(){
    my $jvmMemoptions = $JVM_MEM_OPTIONS . " " . $JVM_DEBUG_OPTIONS;
    
  my $jvmProps = join(" ", @common::JVM_OPTIONS);
  $jvmProps = $jvmProps ." -Dcatalina.base=" .$ENV{"PROFILE_PATH"};
  $jvmProps = $jvmProps ." -Dcatalina.home=" .$ENV{"APP_SERVER_HOME"};
  $jvmProps = $jvmProps ." -Djava.io.tmpdir=" .$ENV{"PROFILE_PATH"} . "/temp";
  my $mainClass =  "com.kingdee.eas.tools.launcher.Start";
  my @progArgs = ("./server.pid","org.apache.catalina.startup.Bootstrap"," ", "start");  

    my $classpath="";
    if( $common::DEVELOP_MODE eq "true"){
    if (-e $common::EAS_USER_LIB_FILE) {
      $classpath = &common::getClasspath($common::EAS_USER_LIB_FILE);
    }
    }
    $classpath = $classpath . &common::getClasspath($common::EAS_SERVER_LIB_FILE);
    
  if(&common::isOsWindows eq "true"){
    &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
  }else{        
    &startServerBackground($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
  }
}

sub stopTomcat(){
    my $jvmMemoptions = $JVM_MEM_OPTIONS . " " . $JVM_DEBUG_OPTIONS;
  my $jvmProps = join(" ", @common::JVM_OPTIONS);
  $jvmProps = $jvmProps ." -Dcatalina.base=" .$ENV{"PROFILE_PATH"};
  $jvmProps = $jvmProps ." -Dcatalina.home=" .$ENV{"APP_SERVER_HOME"};
  $jvmProps = $jvmProps ." -Djava.io.tmpdir=" .$ENV{"PROFILE_PATH"} . "/temp";
  my $mainClass =  "com.kingdee.eas.tools.launcher.Start";
  my @progArgs = ("./server.pid","org.apache.catalina.startup.Bootstrap"," ", "stop");  

    my $classpath="";
    if( $common::DEVELOP_MODE eq "true"){
    if (-e $common::EAS_USER_LIB_FILE) {
      $classpath = &common::getClasspath($common::EAS_USER_LIB_FILE);
    }
    }
    $classpath = $classpath . &common::getClasspath($common::EAS_SERVER_LIB_FILE);

  &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
}

#===============================================================
#
#     Application Server Control
#
#===============================================================

sub startApplicationServer(){
open (OUTFILE,">>./outfile");
print OUTFILE "***************************************************doLogClean\n";
close(OUTFILE);
    &doLogClean();
open (OUTFILE,">>./outfile");
print OUTFILE "***************************************************server.trace\n";
close(OUTFILE);
    open(LAUNCHCMD,">> $common::EAS_INSTANCE_HOME/logs/server.trace");
    print "***************************************************time\n";
    my $curTime = time();
    print "***************************************************md5_hex\n";
    print LAUNCHCMD "[Start]". strftime("%Y-%m-%d %H:%M:%S", localtime) ." $curTime ". getlogin ." ".md5_hex(getlogin . $curTime."[Start]")." \n";
    close(LAUNCHCMD);
 
    if($common::APP_SERVER_TYPE eq "apusic"){
open (OUTFILE,">>./outfile");
print OUTFILE "***************************************************startApusic\n";
close(OUTFILE);
        &startApusic();
    } elsif ($common::APP_SERVER_TYPE eq "jboss") {
      &startJBoss();
    } elsif ($common::APP_SERVER_TYPE eq "websphere") {
        &startWebSphere();
    }elsif ($common::APP_SERVER_TYPE eq "weblogic"){
        &startWeblogic();
    }elsif ($common::APP_SERVER_TYPE eq "tomcat"){
        &startTomcat();
    }else{
        die "Unsupported application server: $common::APP_SERVER_TYPE !";
    }

        
}

sub stopApplicationServer(){
     open(LAUNCHCMD,">> $common::EAS_INSTANCE_HOME/logs/server.trace");
     my $curTime = time();
     print LAUNCHCMD "[Stop ]". strftime("%Y-%m-%d %H:%M:%S", localtime) ." $curTime " . getlogin ." ".md5_hex(getlogin . $curTime."[Stop]")." \n";
   close(LAUNCHCMD);
   
    if($common::APP_SERVER_TYPE eq "apusic"){
        &stopApusic();
    } elsif ($common::APP_SERVER_TYPE eq "jboss") {
      &stopJBoss();
    } elsif ($common::APP_SERVER_TYPE eq "websphere") {
        &stopWebSphere();
     }elsif ($common::APP_SERVER_TYPE eq "weblogic"){
        &stopWeblogic();
    }elsif ($common::APP_SERVER_TYPE eq "tomcat"){
        &stopTomcat();
    }else{
        die "Unsupported application server: $common::APP_SERVER_TYPE !";
    }
}

#===============================================================
#
#     ORM-RPC loadbalance Server Control
#
#===============================================================
sub startDispatcher(){
    my $jvmMemoptions = "-Xms64m -Xmx256m -Djava.io.tmpdir=$ENV{EAS_HOME}/iotmpdir" . $JVM_DEBUG_OPTIONS;
    my $jvmProps = "-Dlog4j.configuration=file:" . $common::EAS_HOME ."/server/cluster/log4j_loadbalance.properties";
    $jvmProps = $jvmProps . " -Dormrpc.config=" . $common::EAS_HOME ."/server/cluster/ormrpc_loadbalance.properties";
    $jvmProps = $jvmProps . " -Djmxconnector.properties=" . $common::EAS_HOME ."/server/cluster/jmxconnector.properties";
    my $mainClass =  "com.kingdee.eas.tools.launcher.Start";
  my @progArgs = ("$common::EAS_HOME/server/cluster/loadbalance.pid","com.kingdee.bos.rpcwrapper.LoadBalanceServerMain");   
    my @classList = (
                        "\${EAS_HOME}/server/bin/tl_launcher.jar",
                            "\${EAS_HOME}/server/lib/patch/sp-classfile.jar",
                             "\${EAS_HOME}/server/lib/patch/sp-common.jar",
                        "\${EAS_HOME}/server/lib/patch/sp-ormrpc.jar",
                         "\${EAS_HOME}/server/lib/common/bos/classfile.jar",
                        "\${EAS_HOME}/server/lib/common/bos/common.jar",
                        "\${EAS_HOME}/server/lib/common/bos/ormrpc.jar",
                        "\${EAS_HOME}/server/lib/common/trd/log4j-1.2.15.jar",
                        "\${EAS_HOME}/server/lib/common/trd/js.jar",
                        "\${EAS_HOME}/server/lib/common/trd/jmxri.jar",
                        "\${EAS_HOME}/server/lib/common/trd/jmxremote.jar",
                        "\${EAS_HOME}/server/lib/common/trd/jmxtools.jar",
                        "\${EAS_HOME}/server/lib/common/trd/sigar.jar"
                );
    my $classpath =  &common::getClasspathByList(@classList);
    $COPYRIGHT_TITLE = "Kingdee ORM-RPC LoadBalance";
    if(&common::isOsWindows eq "true"){
        &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
    }else{
        my $standby = "false";
        &startLoadBalanceBackground($standby, $jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
    }
}

sub startStandbyDispatcher(){
    my $jvmMemoptions = "-Xms64m -Xmx256m -Djava.io.tmpdir=$ENV{EAS_HOME}/iotmpdir" . $JVM_DEBUG_OPTIONS;
    my $jvmProps = "-Dlog4j.configuration=file:" . $common::EAS_HOME ."/server/cluster/log4j_loadbalance_standby.properties";
    $jvmProps = $jvmProps . " -Dormrpc.config=" . $common::EAS_HOME ."/server/cluster/ormrpc_loadbalance.properties";
    $jvmProps = $jvmProps . " -Djmxconnector.properties=" . $common::EAS_HOME ."/server/cluster/jmxconnector_standby.properties";
    my $mainClass =  "com.kingdee.eas.tools.launcher.Start";
  my @progArgs = ("$common::EAS_HOME/server/cluster/loadbalance_standby.pid","com.kingdee.bos.rpcwrapper.LoadBalanceServerMain");   
    my @classList = (
                        "\${EAS_HOME}/server/bin/tl_launcher.jar",
                        "\${EAS_HOME}/server/lib/patch/sp-classfile.jar",
                             "\${EAS_HOME}/server/lib/patch/sp-common.jar",
                        "\${EAS_HOME}/server/lib/patch/sp-ormrpc.jar",
                         "\${EAS_HOME}/server/lib/common/bos/classfile.jar",
                        "\${EAS_HOME}/server/lib/common/bos/common.jar",
                        "\${EAS_HOME}/server/lib/common/bos/ormrpc.jar",
                        "\${EAS_HOME}/server/lib/common/trd/log4j-1.2.15.jar",
                        "\${EAS_HOME}/server/lib/common/trd/js.jar",
                        "\${EAS_HOME}/server/lib/common/trd/jmxri.jar",
                        "\${EAS_HOME}/server/lib/common/trd/jmxremote.jar",
                        "\${EAS_HOME}/server/lib/common/trd/jmxtools.jar",
                        "\${EAS_HOME}/server/lib/common/trd/sigar.jar"
                );
    my $classpath =  &common::getClasspathByList(@classList);
    $COPYRIGHT_TITLE = "Kingdee ORM-RPC LoadBalance";
    if(&common::isOsWindows eq "true"){
        &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
    }else{
        my $standby = "true";
        &startLoadBalanceBackground($standby, $jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
    }
}

sub stopDispatcher(){
    my $jvmMemoptions =  $JVM_DEBUG_OPTIONS;
    my $jvmProps = "-Dlog4j.configuration=file:" . $common::EAS_HOME ."/server/cluster/log4j_loadbalance.properties";
    $jvmProps = $jvmProps . " -Dormrpc.config=" . $common::EAS_HOME ."/server/cluster/ormrpc_loadbalance.properties";
    $jvmProps = $jvmProps . " -Djmxconnector.properties=" . $common::EAS_HOME ."/server/cluster/jmxconnector.properties";
    my $mainClass =  "com.kingdee.bos.rpcwrapper.lbmon.LBMonClient";
    my @classList = (
                        "\${EAS_HOME}/server/bin/tl_launcher.jar",
                        "\${EAS_HOME}/server/lib/patch/sp-classfile.jar",
                             "\${EAS_HOME}/server/lib/patch/sp-common.jar",
                        "\${EAS_HOME}/server/lib/patch/sp-ormrpc.jar",
                         "\${EAS_HOME}/server/lib/common/bos/classfile.jar",
                        "\${EAS_HOME}/server/lib/common/bos/common.jar",
                        "\${EAS_HOME}/server/lib/common/bos/ormrpc.jar",
                        "\${EAS_HOME}/server/lib/common/trd/log4j-1.2.15.jar",
                        "\${EAS_HOME}/server/lib/common/trd/js.jar",
                        "\${EAS_HOME}/server/lib/common/trd/jmxri.jar",
                        "\${EAS_HOME}/server/lib/common/trd/jmxremote.jar",
                        "\${EAS_HOME}/server/lib/common/trd/jmxtools.jar",
                        "\${EAS_HOME}/server/lib/common/trd/sigar.jar"
                );
    my $classpath =  &common::getClasspathByList(@classList);
    my @progArgs = ("STOP");
    $COPYRIGHT_TITLE = "Kingdee ORM-RPC LoadBalance";
    &runJava($jvmMemoptions, $jvmProps, $classpath, $mainClass, @progArgs);
    unlink("$common::EAS_HOME/server/cluster/loadbalance.pid");
}

sub registerapusicserver(){
    if($common::APP_SERVER_TYPE eq "apusic"){
        my $installoptions = $common::APP_SERVER_HOME . "/bin/apusicsvc.exe -install ";
        my $jvmmemoptions = "-Xms$common::JVM_INITIAL_HEAP_SIZE -Xmx$common::JVM_MAX_HEAP_SIZE $common::JVM_CUSTOM_PARAMS ";
      my $jvmMemoptions = $jvmmemoptions . " " . $JVM_DEBUG_OPTIONS;
    my $jvmProps = join(" ", @common::JVM_OPTIONS);
    if (&common::isSunJDK eq "true") {
      #$jvmProps = $jvmProps ." -Dcom.apusic.net.bio=true";
    }
    $jvmProps = $jvmProps ." -Djava.system.class.loader=com.kingdee.eas.exetools.SystemClassLoader";
    $jvmProps = $jvmProps ." -Dkingdee.classpathFile=" . $common::EAS_HOME ."/server/properties/apusic_server-lib.list";
    my $mainClass =  "";
    my @progArgs = ("-root", "$common::APP_SERVER_HOME");   
      my $jclasspath=$common::EAS_HOME . "/server/tools/lib/exetools.jar";
      my $exeCmd = $installoptions . $jvmMemoptions . " " . $jvmProps . " -cp " .$jclasspath . " " . $mainClass;
      foreach $arg(@progArgs) {
          $exeCmd = $exeCmd . " " .$arg;
      }
      print "[EXE_CMD]" . $exeCmd . "\n";
      system($exeCmd);
  }
}

sub unregisterapusicserver(){
    if($common::APP_SERVER_TYPE eq "apusic"){
        my $exeCmd = $common::APP_SERVER_HOME . "/bin/apusicsvc.exe -uninstall";
        print "[EXE_CMD]" . $exeCmd . "\n";
        system($exeCmd);
    }
}

sub initJavaCmd(){
    my($jvmMemoptions, $jvmProps, $jclasspath, $mainClass, @progArgs) = @_;
open (OUTFILE,">>./outfile");
print OUTFILE "initJavaCmd_1";
close(OUTFILE);
    my $javaCMD = $common::JAVA_HOME . "/bin/java";
open (OUTFILE,">>./outfile");
print OUTFILE "initJavaCmd_2";
close(OUTFILE);
    if(&common::isOsHpUnix eq "true"){
        $javaCMD = $common::JAVA_HOME . "/bin/java -d64 ";
open (OUTFILE,">>./outfile");
print OUTFILE "initJavaCmd_3";
close(OUTFILE);
    }
    my $exeCmd = $javaCMD . " " . $jvmMemoptions . " -Djava.net.preferIPv4Stack=true -Djava.security.policy=".$common::EAS_HOME."/server/properties/eas.policy -Duser.timezone=Asia/Shanghai -Duser.language=en -Duser.country=US " . $jvmProps . " -cp " .$jclasspath . " " . $mainClass;
open (OUTFILE,">>./outfile");
print OUTFILE "initJavaCmd_4";
close(OUTFILE);
    foreach $arg(@progArgs) {
        $exeCmd = $exeCmd . " " .$arg;
    }
open (OUTFILE,">>./outfile");
print OUTFILE "initJavaCmd_5";
close(OUTFILE);
    print "[EXE_CMD]" . $exeCmd . "\n";
open (OUTFILE,">>./outfile");
print OUTFILE "initJavaCmd_6";
close(OUTFILE);
    printCopyRight();
open (OUTFILE,">>./outfile");
print OUTFILE "initJavaCmd_7";
close(OUTFILE);
open (OUTFILE,">>./outfile");
print OUTFILE $exeCmd;
close(OUTFILE);

    return $exeCmd;
}

sub runJava(){
open (OUTFILE,">>./outfile");
print OUTFILE "runJava_1";
close(OUTFILE);
    my $exeCmd = &initJavaCmd(@_);
    open (OUTFILE,">>./outfile");
print OUTFILE "runJava_2";
close(OUTFILE);

  system($exeCmd);
  open (OUTFILE,">>./outfile");
print OUTFILE "runJava_3";
close(OUTFILE);

}

sub startServerBackground(){
open (OUTFILE,">>./outfile");
print OUTFILE "startServerBackground_1";
close(OUTFILE);

     my $exeCmd = &initJavaCmd(@_);
  open(LAUNCHCMD,">./start.sh");
    print LAUNCHCMD "#!/bin/sh \n";
    if ( $STARTUP_NOHUP eq "true"){
    open (OUTFILE,">>./outfile");
print OUTFILE "startServerBackground_2";
close(OUTFILE);
        print LAUNCHCMD "echo Lauch server background, and run server in nohup mode .....\n";
        print LAUNCHCMD "nohup " . $exeCmd . " >/dev/null & \n";
    }else{
    open (OUTFILE,">>./outfile");
print OUTFILE "startServerBackground_3";
close(OUTFILE);
        print LAUNCHCMD "echo Lauch server background .....\n";
        print LAUNCHCMD $exeCmd . " &";
  }
  print LAUNCHCMD "rm -f $common::EAS_INSTANCE_HOME/bin/server.pid\n";
  print LAUNCHCMD "echo \$\! > $common::EAS_INSTANCE_HOME/bin/server.pid \n";
  close(LAUNCHCMD);
  if(&common::isOsSun eq "true"){
  open (OUTFILE,">>./outfile");
print OUTFILE "startServerBackground_4";
close(OUTFILE);
      exec("sh","start.sh");
  }else{
  open (OUTFILE,">>./outfile");
print OUTFILE "startServerBackground_5";
close(OUTFILE);
    exec("sh","-C", "start.sh");
    open (OUTFILE,">>./outfile");
print OUTFILE "startServerBackground_6";
close(OUTFILE);
  }
}

sub startLoadBalanceBackground(){
    my($standby, $jvmMemoptions, $jvmProps, $jclasspath, $mainClass, @progArgs) = @_;
    my $exeCmd = &initJavaCmd($jvmMemoptions, $jvmProps, $jclasspath, $mainClass, @progArgs);
    print "******************************************\n";
    print "=====================" . $standby . "======\n";
    print "******************************************\n";
    if($standby eq "true"){
        open(LAUNCHCMD,">./startStandbyLB.sh");
    }else {
     open(LAUNCHCMD,">./startLB.sh");
  }
    print LAUNCHCMD "#!/bin/sh \n";
#    if ( $STARTUP_NOHUP eq "true"){
        print LAUNCHCMD "echo Lauch LoadBalance background, and run LoadBalance in nohup mode .....\n";
        print LAUNCHCMD "nohup " . $exeCmd . " >/dev/null & \n";
#    }else{
#        print LAUNCHCMD "echo Lauch LoadBalance background .....\n";
#        print LAUNCHCMD $exeCmd . " &";
#  }
  if($standby eq "true"){
      print LAUNCHCMD "rm -f ./loadbalanceStandby.pid\n";
      print LAUNCHCMD "echo \$\! > ./loadbalanceStandby.pid \n";
  }else {
    print LAUNCHCMD "rm -f ./loadbalance.pid\n";
    print LAUNCHCMD "echo \$\! > ./loadbalance.pid \n";
  }
  close(LAUNCHCMD);
  if(&common::isOsSun eq "true"){
      if($standby eq "true"){
          exec("sh","startStandbyLB.sh");
      }else {
      exec("sh","startLB.sh");
    }
  }else {
      if($standby eq "true"){
          exec("sh","-C", "startStandbyLB.sh");
      }else {
        exec("sh","-C", "startLB.sh");
      }
  }
}

sub doLogClean(){
    if ( $ENV{"SKIP_CLEAN_LOGS"} eq "true" ){    
        print "SKIP_CLEAN_LOGS has been set, skip clean eas logs!";
        return;
    }else{
      print "SKIP_CLEAN_LOGS not set, begin clean eas logs ......";
  }

    &cleanDir($common::EAS_HOME . "/admin/logs");
    &cleanDir($common::EAS_HOME . "/server/cluster/logs");
    
    my @profilesDir;
    opendir(DIR, $common::EAS_HOME . "/server/profiles") || die ("Cannot open profiles directory");
    @profilesDir = readdir(DIR);
    closedir(DIR);
    foreach $file (@profilesDir)
    {
        next if $file=~ /^\.*$/;
        &cleanDir($common::EAS_HOME . "/server/profiles/". $file ."/logs");
    }
}

sub cleanDir(){
  my ($dir_to_open)=@_;
  -d $dir_to_open || return;
    my @dir_contents;
  opendir(DIR,$dir_to_open) || return;
  @dir_contents= readdir(DIR);
  closedir(DIR);
  # Now loop through array and print file names
  foreach $file (@dir_contents) {
   next if (-d $dir_to_open ."/".$file);
   my $mtime = (stat($dir_to_open ."/".$file))[9];
   my $diffDate = (time - $mtime) - ( 60*60*24*10);
   if( $diffDate > 0  && $mtime > 0){
       print "File[$dir_to_open/$file] lastmodified[$mtime] time_diff[$diffDate]\n";
       unlink($dir_to_open ."/". $file);
   }
  }
}
原创粉丝点击