Oily Rag 1: CD Part 4 Scripts

From The Uniform Server Wiki
Jump to navigation Jump to search

MPG UniCenter

MPG UniCenter

Oily Rag: Be prepared to get your hands dirty.


Return to CD Part 4

cd_mode.pl

###############################################
### Uniform Server - CD Configuration       ###
###############################################
# File Name: cd_mode.pl
# Created By: UniCenter MPG
# Edited Last By: Mike Gleaves (Ric)
# Comment: Run US from CD 
# Example plugin USCD2 For 3.5-Apollo
# 12-7-07 V1.0
# 2-7-08  V1.1
################################################

#=1 =============================================================
# Read set-up data and check previous mode used 
$cd_data = "./uscd/cd_data.txt";   # set-up and tracking file

open(INFILE,$cd_data) or (print "Could not open $cd_data for reading: $!\n"); # open file
  @cd_us_data=<INFILE>;            # load file data to array
close(INFILE);                     # close file

$temp_drive = $cd_us_data[0];      # Drive currently used for temp folder
chomp $temp_drive;                 # clean
$temp_folder = $cd_us_data[1];     # Folder used for temp data 
chomp $temp_folder;                # clean
$current_mode = $cd_us_data[2];    # Current mode set
chomp $current_mode;               # clean
$previous_drive = $cd_us_data[3];  # previous drive
chomp $previous_drive;             # clean
$previous_folder = $cd_us_data[4]; # previous folder
chomp $previous_folder;            # clean

# check current mode
if ($current_mode eq "cd"){        # Are we trying to run in the same mode
 system( 'cls' );                  # yes well not a good idea
 print "You are already running in CD mode!\n";
 print "This script will now stop\n\n";
  system( 'pause' );
  exit;
}
# Previous mode was US so continue

#=2 ============================================================
#Get new user temporary folder or use previous/default set path

system( 'cls' );                                                 #clear screen
print "What temporary folder path would you like to use?\n";     #display message
print "Press enter key to accept default\n";
print "                or\n";
print "       enter a new value\n\n";

print "Default drive letter is ".$temp_drive." New value = ";    #display default drive 
my $new_drive = <STDIN>;                                         #get new drive letter
print "\nDefault folder is ".$temp_folder." New value = ";       #display default folder
my $new_folder = <STDIN>;                                        #get new folder

chomp $new_drive;                                                #clean input
chomp $new_folder;

if($new_drive eq ""){                                            #no input
  $new_drive = $temp_drive;                                      #use default
}

if($new_folder eq ""){                                           #no input
  $new_folder = $temp_folder;                                    #use default
}
print "\n\n";
#=3 ============================================================
# Set cd_data and save to file

$cd_us_data[0] = $new_drive . "\n";          # set new or use old
$cd_us_data[1] = $new_folder . "\n";         # set new or use old
$cd_us_data[2] = "cd\n";                     # change mode to cd
$cd_us_data[3] = $temp_drive . "\n";         # save previous drive
$cd_us_data[4] = $temp_folder . "\n";        # save previous path

# Save cd data to file
open(OUTFILE,">$cd_data") or (print "Unable to open $file for writing:");
 foreach $save(@cd_us_data){                 # loop through the array
  print OUTFILE $save;                       # write each line
 }
close(OUTFILE);                              # finished close file

#=4 ===========================================================
# Update new_Server_Start and new_Stop bat files with new path

@mpg=();                                          #clear array
push(@mpg,"./uscd/new_Server_Start.bat");         # path to new start bat
push(@mpg,"set uscddrive=".$temp_drive);          # search string
push(@mpg,"set uscddrive=".$new_drive);           # replace string
push(@mpg,$st1 = "set uscdfolder=".$temp_folder); # search string
push(@mpg,$st2 = "set uscdfolder=".$new_folder);  # replace string
update_files(@mpg);                               # search replace save file

@mpg=();                                          #clear array
push(@mpg,"./uscd/new_Stop.bat");                 # path to new start bat
push(@mpg,"set uscddrive=".$temp_drive);          # search string
push(@mpg,"set uscddrive=".$new_drive);           # replace string
push(@mpg,"set uscdfolder=".$temp_folder);        # search string
push(@mpg,"set uscdfolder=".$new_folder);         # replace string
update_files(@mpg);                               # search replace save file

#=5 ============================================================
#Copy bat files to Uniform Server folder

coppy ("./uscd/new_Stop.bat","../../../Stop.bat");
coppy ("./uscd/new_Server_Start.bat","../../../Server_Start.bat"); 

#=6 ============================================================
#.............. DELETE THIS SECTION IF NOT REQUIRED ............
# Also delete corresponding section in file us_mpde.pl
# Replace: Admin menu with CD version limited functions
# Replace: Admin home page.
# Copy:    Read_me to docs folder.

coppy ("./uscd/new_navigation.php","../../home/admin/www/navigation.php");
coppy ("./uscd/new_start.php","../../home/admin/www/start.php");
coppy ("./uscd/read_me_cd.txt","../../docs/read_me_cd.txt"); 
#...............................................................

#=7 ===========================================================
#Build path to temporary folder
$tpath  = $new_drive . ":/" . $new_folder;    #build path

#=8 ============================================================
# Update Uniserver configuration files
# with new path information.

#=== Apache
@mpg=();                                              # clear array
push(@mpg,"../../usr/local/apache2/conf/httpd.conf"); # filepath
push(@mpg,"PidFile logs/httpd.pid");                  # search for
push(@mpg,"PidFile $tpath/apache/logs/httpd.pid");    # replace with
push(@mpg,"ErrorLog logs/error.log");                 # search for
push(@mpg,"ErrorLog $tpath/apache/logs/error.log");   # replace with
push(@mpg,"CustomLog logs/access.log combined");      # search for
push(@mpg,"#CustomLog logs/access.log combined");     # replace with
update_files(@mpg);                                   # search replace save file
#=== END Apache

#===MySQL
@mpg=();                                              # clear array
push(@mpg,"../../usr/local/mysql/bin/my-small.cnf");  # filepath
push(@mpg,"datadir = \"/usr/local/mysql/data/\"");    # search for
push(@mpg,"datadir = \"$tpath/mysql/data/\"");        # replace with
push(@mpg,"tmpdir = \"/tmp\"");                       # search for
push(@mpg,"tmpdir = \"$tpath/mysql/tmp\"");           # replace with
update_files(@mpg);                                   # search replace save file
#=== END MySQL

#===php
@mpg=();                                              # clear array
push(@mpg,"../../usr/local/php/php.ini");             # filepath
push(@mpg,"error_log = logs/logs.log");               # search for
push(@mpg,"error_log = $tpath/php/logs/logs.log");    # replace with
push(@mpg,"upload_tmp_dir = /tmp");                   # search for
push(@mpg,"upload_tmp_dir = $tpath/php/tmp");         # replace with

# The default is /tmp/ extra slash at the end? the next two sections capture
# either. When restored using us-mode does not add the extra slash
 
push(@mpg,"session.save_path = \"/tmp/\"");           ## delete line for us-mode search for
push(@mpg,"session.save_path = \"$tpath/php/tmp\"");  ## delete line for us-modereplace with
push(@mpg,"session.save_path = \"/tmp\"");            # search for
push(@mpg,"session.save_path = \"$tpath/php/tmp\"");  # replace with
update_files(@mpg);                                   # search replace save file
#===END php

#===phpMyAdmin
@mpg=();                                                    # clear array
push(@mpg,"../../home/admin/www/phpMyAdmin/config.inc.php");# filepath
push(@mpg,"/etc/phpmyadmin");                               # search string
push(@mpg,"$tpath/phpmyadmin/etc/phpmyadmin");              # replace string
push(@mpg,"/tmp");                                          # search string
push(@mpg,"$tpath/phpmyadmin/tmp");                         # replace string
update_files(@mpg);                                         # search replace save file
#===END phpMyAdmin

#=9 ================================================================
system( 'pause' ); # wait for user

#=10 ==============================================================
# Function: Search and replace all string matches in a file.
# Call using: update_files(@myarray) @myarray ordered as follows:
# full file path, search str 1,replace str 1, search str 2,replace str 2..
#...................................................................
 
sub update_files {

my @search_info = @_;                # from flattened array @_ create new array 
@search_info=reverse(@search_info);  # re-order to give file path first
my $file = pop(@search_info);        # get first element of array. File to search

open(INFILE,$file) or (print "Could not open $file for reading: $!\n" and return);
my @text=<INFILE>;                   # we have file handle, load file to array for searching
close(INFILE);                       # close file

my $found = 0;                       # Set if string found in array loaded
while (@search_info !=0){            # loop through replacement array until all data used

#==== Difference between cd-mode and us-mode change string name =============
# This allows common use of the reference cd-mode array @mpg.
# Just copy section 8 Update Uniserver configuration from cd-mode to us-mode
# Once copied delete the lines mentioned in the php section.
# and make sure these strings are renamed switched. 
#   
 $str1 = pop(@search_info);          # cd_mode $str1 us-mode $str2 string to search for
 $str2 = pop(@search_info);          # cd mode $str2 us-mode $str1 replace with string

 print "Searching $file for data\n"; # provide feedback to user

 foreach $line (@text){              # loop through array
  if($line=~/$str1/){                # check we have found a match
   $line=~s/$str1/$str2/ig;          # yes do the match again and replace
   $found = 1;                       # set tracking variable
   print " -Found match in file\n";  # print conformation
  }
 }#end foreach
}#end while

 if($found){                         # A match was found hence write to file
  open(OUTFILE,">$file") or (print "Unable to open $file for writing:" and return);
    foreach $save(@text){            # loop through array set each line to variable $save
     print OUTFILE $save;            # write each line contained in variable $save
    }
  close(OUTFILE);                    # finished close file
 }
}#end sub
#========================================= End sub update_files ====

#=11 ===============================================================
# Function: Copy any file to a new location. Call using:
# copy (source, destination)
# Binary transfer mode (bit level)hence image (any) files are allowed.
#...................................................................
sub coppy {
my $source = $_[0];             # full source path including file name
my $destination = $_[1];        # full destination including file name

   open (FILE,"$source");       # Open file for input to read source file
   binmode FILE;                # Binary mode. Bit level for text and image
   @lines=<FILE>;               # Save bits to array
   close (FILE);                # Close file handle
   open (FILE,">$destination"); # Open or create destination
   binmode FILE;                # Transfer mode will be binary (bit level)
   print FILE @lines;           # Copy everything to this file
   close (FILE);                # Close file
}
#================================================= End sub copy ====

exit;

Return to CD Part 4

Top

us_mode.pl

##################################################
### Uniform Server - US Standard Configuration ###
##################################################
# File Name: us_mode.pl
# Created By: UniCenter MPG
# Edited Last By: Mike Gleaves (Ric)
# Comment: Run US from CD 
# Example plugin USCD2 For 3.5-Apollo
# 12-7-07 V1.0
# 2-7-08  V1.1
###################################################

#=1 ============================================================
# Read set-up data and check previous mode used 
$cd_data = "./uscd/cd_data.txt"; # set-up and tracking file

open(INFILE,$cd_data) or (print "Could not open $cd_data for reading: $!\n"); # open file
  @cd_us_data=<INFILE>;          # load file data to array
close(INFILE);                   # close file

$temp_drive = $cd_us_data[0];    # Drive currently used for temp folder
chomp $temp_drive;               # clean
$temp_folder = $cd_us_data[1];   # Folder used for temp data 
chomp $temp_folder;              # clean
$current_mode = $cd_us_data[2];  # Current mode set
chomp $current_mode;             # clean


# check current mode
if ($current_mode eq "us"){      # Are we trying to run in the same mode
 system( 'cls' );                # yes well not a good idea
 print "You are already running in US mode!\n";
 print "This script will now stop\n\n";
  system( 'pause' );
  exit;
}
# Previous mode was CD so continue

#=3 ============================================================
# Set cd_data and save to file

$cd_us_data[2] = "us\n";                     # change mode to us

# Save cd data to file
open(OUTFILE,">$cd_data") or (print "Unable to open $file for writing:");
 foreach $save(@cd_us_data){                 # loop through the array
  print OUTFILE $save;                       # write each line
 }
close(OUTFILE);  
 
#=5 ============================================================
#Copy bat files to Uniform Server folder

coppy ("./uscd/old_Stop.bat","../../../Stop.bat");
coppy ("./uscd/old_Server_Start.bat","../../../Server_Start.bat"); 

#=6 ============================================================
#.............. DELETE THIS SECTION IF NOT REQUIRED ............
#Also delete corresponding section in file cd_mode.pl
#Restore: Original Admin Menu.
#Restore: Original Admin Home Page
#Delete:  Read_me doc. 

coppy ("./uscd/old_navigation.php","../../home/admin/www/navigation.php");
coppy ("./uscd/old_start.php","../../home/admin/www/start.php");
unlink "../../docs/read_me_cd.txt"; # delete read_me_cd text file

#...............................................................
#=7 ============================================================
#Build path to temporary folder

$tpath = $temp_drive . ":/" . $temp_folder;    #build path

#=8 ============================================================
#Update Uniserver configuration files
# with new path information.

#=== Apache
@mpg=();                                              # clear array
push(@mpg,"../../usr/local/apache2/conf/httpd.conf"); # filepath
push(@mpg,"PidFile logs/httpd.pid");                  # search for
push(@mpg,"PidFile $tpath/apache/logs/httpd.pid");    # replace with
push(@mpg,"ErrorLog logs/error.log");                 # search for
push(@mpg,"ErrorLog $tpath/apache/logs/error.log");   # replace with
push(@mpg,"CustomLog logs/access.log combined");      # search for
push(@mpg,"#CustomLog logs/access.log combined");     # replace with
update_files(@mpg);                                   # search replace save file
#=== END Apache

#===MySQL
@mpg=();                                              # clear array
push(@mpg,"../../usr/local/mysql/bin/my-small.cnf");  # filepath
push(@mpg,"datadir = \"/usr/local/mysql/data/\"");    # search for
push(@mpg,"datadir = \"$tpath/mysql/data/\"");        # replace with
push(@mpg,"tmpdir = \"/tmp\"");                       # search for
push(@mpg,"tmpdir = \"$tpath/mysql/tmp\"");           # replace with
update_files(@mpg);                                   # search replace save file
#=== END MySQL

#===php
@mpg=();                                              # clear array
push(@mpg,"../../usr/local/php/php.ini");             # filepath
push(@mpg,"error_log = logs/logs.log");               # search for
push(@mpg,"error_log = $tpath/php/logs/logs.log");    # replace with
push(@mpg,"upload_tmp_dir = /tmp");                   # search for
push(@mpg,"upload_tmp_dir = $tpath/php/tmp");         # replace with

# The default is /tmp/ extra slash at the end? CD-Mode remove this
# hence only */temp is search for and replaced with this file

push(@mpg,"session.save_path = \"/tmp\"");            # search for
push(@mpg,"session.save_path = \"$tpath/php/tmp\"");  # replace with
update_files(@mpg);                                   # search replace save file
#===END php

#===phpMyAdmin
@mpg=();                                                    # clear array
push(@mpg,"../../home/admin/www/phpMyAdmin/config.inc.php");# filepath
push(@mpg,"/etc/phpmyadmin");                               # search string
push(@mpg,"$tpath/phpmyadmin/etc/phpmyadmin");              # replace string
push(@mpg,"/tmp");                                          # search string
push(@mpg,"$tpath/phpmyadmin/tmp");                         # replace string
update_files(@mpg);                                         # search replace save file
#===END phpMyAdmin


#=9 ===============================================================
system( 'pause' ); # wait for user

#=10 ==============================================================
# Function: Search and replace all string matches in a file.
# Call using: update_files(@myarray) @myarray ordered as follows:
# full file path, search str 1,replace str 1, search str 2,replace str 2..
#...................................................................
 
sub update_files {

my @search_info = @_;                # from flattened array @_ create new array 
@search_info=reverse(@search_info);  # re-order to give file path first
my $file = pop(@search_info);        # get first element of array. File to search

open(INFILE,$file) or (print "Could not open $file for reading: $!\n" and return);
my @text=<INFILE>;                   # we have file handle, load file to array for searching
close(INFILE);                       # close file

my $found = 0;                       # Set if string found in array loaded
while (@search_info !=0){            # loop through replacement array until all data used

#==== Difference between cd-mode and us-mode change string name =============
# This allows common use of the reference cd-mode array @mpg.
# Just copy section 8 Update Uniserver configuration from cd-mode to us-mode
# Once copied delete the lines mentioned in the php section.
# and make sure these strings are renamed switched. 
#   
 $str2 = pop(@search_info);          # cd_mode $str1 us-mode $str2 string to search for
 $str1 = pop(@search_info);          # cd mode $str2 us-mode $str1 replace with string

 print "Searching $file for data\n"; # provide feedback to user

 foreach $line (@text){              # loop through array
  if($line=~/$str1/){                # check we have found a match
   $line=~s/$str1/$str2/ig;          # yes do the match again and replace
   $found = 1;                       # set tracking variable
   print " -Found match in file\n";  # print conformation
  }
 }#end foreach
}#end while

 if($found){                         # A match was found hence write to file
  open(OUTFILE,">$file") or (print "Unable to open $file for writing:" and return);
    foreach $save(@text){            # loop through array set each line to variable $save
     print OUTFILE $save;            # write each line contained in variable $save
    }
  close(OUTFILE);                    # finished close file
 }
}#end sub
#========================================= End sub update_files ====

#=11 ===============================================================
# Function: Copy any file to a new location. Call using:
# copy (source, destination)
# Binary transfer mode (bit level)hence image (any) files are allowed.
#...................................................................
sub coppy {
my $source = $_[0];             # full source path including file name
my $destination = $_[1];        # full destination including file name

   open (FILE,"$source");       # Open file for input to read source file
   binmode FILE;                # Binary mode. Bit level for text and image
   @lines=<FILE>;               # Save bits to array
   close (FILE);                # Close file handle
   open (FILE,">$destination"); # Open or create destination
   binmode FILE;                # Transfer mode will be binary (bit level)
   print FILE @lines;           # Copy everything to this file
   close (FILE);                # Close file
}
#================================================= End sub copy ====

exit;

Return to CD Part 4

Top


Ric