interactivelyInstallPanINM.pl

上一篇 / 下一篇  2008-11-12 15:01:05 / 个人分类:Perl silent installation

use Net::Telnet ();

$emsVersion = readEmsVersion("emsversion.ini");

#$prompt = "PROMPT";
$hostname = "HOSTNAME";
$username = "USERNAME";
$passwd = "PASSWD";
$emsDir = "EMSDIR";
$patchDir = "PATCHDIR";
$afcDir = "AFCDIR";
$zFile = "ZFILE";
$setupFile = "SETUPFILE";
$yourPCIPAddress = "YOURPCIPADDRESS";
$reboot = "REBOOT";
$envFile = "env.ini";
$uninstallFile = "uninstall.ini";
$installFile = "install.ini";
$inputLog = 'installation.log';
$outputLog = 'output.log';
$dumpLog = 'dumplog.txt';
$endOfFile = "Log_is_the_end_of_file";
$doneInstall = 0;
$doneUninstall = 0;

##Read environment value from env file.
&readEnv;

##Create a telnet session
$conn = &spawn($hostname, $username, $passwd, $prompt);

##You have to run this scrīpt as a root user.
&isRoot;

##Configure your DISPLAY variable to be your PC IP address
&display;

##Change directory to emsdir
&cd($emsDir);

##unzip the *.Z file
&tar;

##Kick emsadmin user
&kickEmsadmin;

##Restart oracle. need fix prompt > issue
&restartDB;

##Uninstall the previous Pan INM load.
&uninstall;

##Quit if uninstallation didn't complete
&quit unless $doneUninstall;

##Setup the installation files.
&setup;

##Change directory to afcdir
&cd($afcDir);

#Kick emsadmin user
&kickEmsadmin;

##Install Pan INM
&install;

##Quit if installation didn't complete
&quit unless $doneInstall;

##Install patches
&installPatches;

##Reboot solaris
&reboot;

$conn->cmd($endOfFile);

##Close the session and exit this scrīpt
&quit;

sub readEnv {
 my ($name, $value, $num, @field);
 open FILE, $envFile
  or die "Can't open the file: $envFile! ($!)";
 while(<FILE>){
  chomp;
  # go to next while if a line in $envFile is an empty or its first char is #
  next if ($_ eq "" || /^#/);
  @field = split /=/, $_;
  # get the amount of @field's element
  $num = @field;
  die "The value should be inside the \"\", please check your $envFile file.\n" if ($num < 2);
  $value = pop @field;
  $name = pop @field;
  $name =~ s/^\s+|\s+$//g;
  $value =~ s/^"|"$|^\s+|\s+$//g;
  $value =~ s/EMSVERSION/$emsVersion/g;
  die "Find an empty attribute name, please check your $envFile file.\n" if ($name eq "");
  if ($name eq $hostname){
   $hostname = $value;
   print "$name is $value in env sub\n";
  }elsif ($name eq $username){
   $username = $value;
   print "$name is $value in env sub\n";
  }elsif ($name eq $passwd){
   $passwd = $value;
   print "$name is $value in env sub\n";
  }elsif ($name eq $emsDir){
   $emsDir = $value;
   print "$name is $value in env sub\n";
  }elsif ($name eq $patchDir){
   $patchDir = $value;
   print "$name is $value in env sub\n";
  }elsif ($name eq $afcDir){
   $afcDir = $value;
   print "$name is $value in env sub\n";
  }elsif ($name eq $emsVersion){
   $emsVersion = $value;
   print "$name is $value in env sub\n";
  }elsif ($name eq $zFile){
   $zFile = $value;
   print "$name is $value in env sub\n";
  }elsif ($name eq $setupFile){
   $setupFile = $value;
   print "$name is $value in env sub\n";
  }elsif ($name eq $yourPCIPAddress){
   $yourPCIPAddress = $value;
   print "$name is $value in env sub\n";
  }elsif ($name eq $reboot){
   $reboot = $value;
   print "$name is $value in env sub\n";
  }
#  elsif ($name eq $prompt){
#   $prompt = $value;
#   #print "$name is $value in env sub\n";
#  }
  else{
   die "Can't find the attribute name: $name, please check your $envFile file.\n"
  }
  ## next will goto here
 }
 close FILE;
} # end sub readEnv

sub readFromFile {
 my ($filename) = @_;
 my @arrays;
 open FILE, $filename
  or die "Can't open the file: $filename! ($!)";
 while(<FILE>){
  chomp;
  if ($_ ne ""){
   push @arrays, $_;
  }
 }
 close FILE;
 @arrays;
} # readFromFile

sub spawn {
 $t = new Net::Telnet (Timeout => 3,);
            #Prompt => '/'.$prompt.' $/');
 $t->max_buffer_length(10_485_760);# 10M
 #all of input,output will be written in the file           
 $t->input_log($inputLog);
 
 

 $t->dump_log($dumpLog);
 $t->output_log($outputLog);
 $t->open($hostname);
 my $ok = $t->login($username, $passwd);
 $t; 
} # end sub spawn

sub fileExist {
 my ($filename) = @_;
 my $cmdStr = 'ls "'. $filename .'"';
 $conn->cmd($cmdStr);
  chomp (my $line = $conn->lastline);
  my $match = $filename . ': No such file or directory';
  if ($line =~ /$match/){
   return 0; 
  }
 1;
} # end sub fileExist

sub isRoot {
 my $cmdStr = "/usr/ucb/whoami";
 $conn->cmd($cmdStr);
 chomp (my $line = $conn->lastline);
  my $match = "root";
  if ($line ne $match){
   die "You must be root to execute this scrīpt. Please modify $envFile file"; 
  }
} # end sub isRoot

sub cd {
 my ($dir) = @_;
 if (&fileExist($dir)){
  my $cmdStr = "cd ". $dir;
  $conn->cmd($cmdStr);
 }else{
  die "$dir: No such file or directory";
 } 
} # end sub cd

sub tar {
 if (&fileExist($zFile)){
  my $cmdStr = "gunzip -c ". $zFile ." | tar -xvf -";
  $conn->cmd(String => $cmdStr,
        Timeout => 120);
 }else{
  die "$zFile: No such file or directory";
 }
} # end sub tar

sub setup {
 if (&fileExist($setupFile)){
  my $cmdStr = "./". $setupFile;
  $conn->cmd(String => $cmdStr,
        Timeout => 120);
 }else{
  die "$setupFile: No such file or directory";
 }
} # end sub setup

sub display {
 my $cmdStr = "DISPLAY=". $yourPCIPAddress .":0.0;export DISPLAY";
 $conn->cmd($cmdStr);
} # end sub display

sub kickEmsadmin {
 my $cmdStr = "pkill -9 -u emsadmin";
 for (1..3) { $conn->cmd($cmdStr);}
} # end sub kickEmsadmin

sub arrayToHash {
 my @arrays = @_;
 my %io;
 my $input = 0;
 my $output;
 foreach (@arrays){
  if ($input){
   $io{$output} = $_;
   $input = 0;
  }else{
   $io{$_} = undef;
   $input = 1;
   $output = $_;
  }
 }
 %io;
} # end sub arrayToHash

sub uninstall {
 ##Use the undefined value to turn off timing-out completely.
 $conn->timeout(180);
 $conn->errmode('return');
 
 my @arrays = &readFromFile($uninstallFile);
 my $cmdStr = shift @arrays;
 my $doUninstall = $arrays[-1];
 
 my %io = &arrayToHash(@arrays);
 my @output = keys %io;

 my $i = 0;
 $conn->print($cmdStr);
 while(1){
  print "$i in while\n";
  $i += 1;
  ## The amount of @output's element must equal to the number of input line in uninstall.ini file.
  my ($prematch, $match) = $conn->waitfor(String => $output[0],
                      String => $output[1],
                      String => $output[2],
                      String => $output[3],
                      );
  print "\$match is $match\n";
  # check if uninstall complete
  if ($match eq $doUninstall){
   print "Found $match\n";
   $doneUninstall = 1; 
  }
  if ($match && $io{$match}){
   $conn->print($io{$match});
   print "$match => $io{$match}\n";
  }else{
   open FH, "< $inputLog"
    or die "Can't open $inputLog\n";
   my @tmp = <FH>;
   close FH;
   my $lastline = pop @tmp;
   if ($lastline =~ /[\$%#>] $/){
    print "The lastline is $lastline exit uninstall...\n";
    last;
   }
   print "Can't find the following line for waiting in $uninstallFile. Please enter the answer manually.\n$lastline";
   my $enter = <STDIN>;
   $conn->print($enter); 
  }  
 }
} # end sub uninstall

sub install {
 ##Use the undefined value to turn off timing-out completely.
 $conn->timeout(360);
 $conn->errmode('return');
 
 my @arrays = &readFromFile($installFile);  
 my $cmdStr = shift @arrays;
 my $doInstall = $arrays[-1];
 
 my %io = &arrayToHash(@arrays);
 my @output = keys %io;
 my $i =0;

 $conn->print($cmdStr);
 while(1){
  print "$i in while\n";
  $i += 1;
  ## The amount of @output's element must equal to the number of input line in install.ini file.
  my ($prematch, $match) = $conn->waitfor(String => $output[0],
                      String => $output[1],
                      String => $output[2],
                      String => $output[3],
                      String => $output[4],
                      String => $output[5],
                      String => $output[6],
                      String => $output[7],
                      String => $output[8],
                      String => $output[9],
                      String => $output[10],
                      String => $output[11],
                      String => $output[12],
                      String => $output[13],
                      String => $output[14],
                      String => $output[15],
                      String => $output[16],
                      String => $output[17],
                      String => $output[18],
                      String => $output[19],
                      String => $output[20],
                      String => $output[21],
                      String => $output[22],
                      String => $output[23],
                      String => $output[24],
                      String => $output[25],
                      String => $output[26],
                      String => $output[27],                      
                      ## Remove a '#' in next line if add a pair of lines in the install.ini file
                      #String => $output[28],
                      #String => $output[29],
                      #String => $output[29],
                      );
  print "\$match is $match\n";  
  # check if install complete
  if ($match eq $doInstall){
   print "Found $match\n";
   $doneInstall = 1;
  }
  if ($match && $io{$match}){
   $conn->print($io{$match});
   print "$match => $io{$match}\n";
  }else{
   open FH, "< $inputLog"
    or die "Can't open $inputLog\n";
   my @tmp = <FH>;
   close FH;
   my $lastline = pop @tmp;
   if ($lastline =~ /[\$%#>] $/){
    print "The lastline is $lastline exit install...\n";
    last;
   }
   print "Can't find the following line for waiting in $installFile. Please enter the answer manually.\n$lastline";
   my $enter = <STDIN>;
   $conn->print($enter); 
  }
 }
} # end sub install

sub reboot {
 if ($reboot =~ /yes|y/i){
  my $cmdStr = "sync;sync;/etc/shutdown -y -g0 -i6";
  $conn->cmd($cmdStr);
 }
} # end sub reboot

sub restartDB {
 my $cmdStop = "./dbora.ems stop";
 my $cmdStart = "./dbora.ems start";
 $conn->cmd(String => $cmdStop,
       Prompt => '/[#] $/',
       Timeout => 120);
 $conn->cmd(String => $cmdStart,
       Prompt => '/[#] $/',
       Timeout => 120);
 $conn->prompt('/[\$%#>] $/');
} # end sub restartDB

sub installPatches {
 if (&fileExist($patchDir)){
  my $cmdStr = "cp $patchDir/* /opt/afc/bbmgr/patchJars/";
  $conn->cmd(String => $cmdStr,
        Timeout => 120);
 }else{
  warn "$patchDir: No such file or directory";
 }
} # end sub installPatches

sub readEmsVersion {
 my ($filename, $emsVersion) = @_;
 open FILE, $filename
  or die "Can't open the file: $filename! ($!)";
 chomp($emsVersion = <FILE>);
 close FILE;
 $emsVersion;
} # end sub readEmsVersion

sub quit {
 $conn->close;
 exit;
} # end sub quit


TAG: Perl interactively install

 

评分:0

我来说两句

日历

« 2024-04-27  
 123456
78910111213
14151617181920
21222324252627
282930    

数据统计

  • 访问量: 47737
  • 日志数: 80
  • 建立时间: 2008-10-27
  • 更新时间: 2009-07-17

RSS订阅

Open Toolbar