#!/usr/bin/perl -T -I /h/COE/lib -U
#-
# Copyright (C) 2002, Federal Linux Systems
#
# The project home for this software is http://www.opencoe.org
#
# This segment is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This segment is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this segment; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
#
#-
# History:
# 20020210 - /usr/bin/perl
# 20020125 - Restructured TOC directory
# 20020115 - Restructured TOC directory
# 20020115 - Improved ability to install gz, Z files
# 20020115 - Add real dates to Installed file
#use strict;
use Coe;
use File::Copy;
use File::Path;
use Archive::Tar;
my $VERSION="1.1.4.1";
my $PRGM="COESegInstall";
# --+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
#
# Parse Command Line
#
# --+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
# save the display env
my $display=$ENV{'DISPLAY'};
delete @ENV{'IFS', 'CDPATH', 'ENV', 'BASH_ENV'};
$ENV{'DISPLAY'} = $display;
if (@ARGV < 2) {
&useage;
exit -1;
}
sub useage {
print "Usage: $PRGM [flags] {path} {segment}\n";
}
my $arg1=cleanString(shift,3);
my $SEGPATH="/h";
my $SEG="DUMMY";
my $DEINSTALL_FLAG=0;
my $sep="-";
my $flagdeinstall=0;
my $flagstage=0;
sub getCoeApps;
sub getCoeAcct;
sub getCoeCots;
sub getCoeHome;
if ("$arg1" eq "-h") {
useage;
exit 0;
} elsif ("$arg1" eq "-H") {
coeMan("COE_HOME/docs/man/$PRGM");
exit 0;
} elsif ("$arg1" eq "-V") {
print "VERSION: $VERSION\n";
exit 0;
} elsif ("$arg1" eq "-v") {
print "Setting output mode to verbose\n\n";
$SEG=cleanString(shift,3);
} elsif ("$arg1" eq "-d") {
$SEGPATH=coeFileConv(shift);
$SEG=cleanString(shift,3);
$flagdeinstall=1;
} elsif ("$arg1" eq "--stage") {
$REPPATH=coeFileConv(shift);
$SEGROOT=coeFileConv(shift);
$SEG=cleanString(shift,3);
$flagstage=1;
$SEGPATH=$REPPATH;
} elsif ("$arg1" eq "--seperator") {
$sep=cleanString(shift,3);
} else {
$SEGPATH=coeFileConv($arg1);
$SEG=cleanString(shift,3);
}
# --+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
#
# a) The installation directory and environment variables set
# b) If an old segment is in place, the DEINSTALL is run
# c) The new segment PreInstall is run before the segment is untarred
# d) If an old segment is in place, the segment is deinstalled (SegInfo process)
# e) The old segment is removed from the disk
# f) The new segment is loaded from the source and decompressed
# g) The new segment is processed (SegInfo)
# h) The new segment PostInstall is run
# i) Status is displayed
#
# --+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
# --+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
# COESegInstall has three modes
# 1 - Deinstall a segment
# args = {-p full path to segment home}
# {segment prefix}
# will remove link from h
# 2 - Stage a segment: used to move a segment to the directory
# where it will be installed
# args = {-r full path to repository}
# {-p root path to install}
# {segment package name}
# 3 - Install a segment (default)
# The segment is first looked for in the segment home dir
# and if it is not found, then in the local directory
# args = {-p root path to segment home}
# {segment name}
# will add link to h
# --+----+----+----+----+----+----+----+----+----+----+----+----+----+----+----+
open LOG, "> /tmp/COESegInstall.log" || die "Cannot open log file\n";
print LOG "Processing segment $SEGPATH/$SEG\n";
if ($flagdeinstall == 1) {
print LOG "deinstall: $SEG\n";
# ----------------------------------------------------------------------
# Check for directory
# ----------------------------------------------------------------------
$dir=coeFileConv($SEGPATH);
chdir "$dir" || die ("Cannot find segment path $SEGPATH") if ( -d $dir );
# ----------------------------------------------------------------------
# Process SegName
# ----------------------------------------------------------------------
$THISSEGSUBTYPE="dummy";
my $segname=coeFileConv("$dir/$SEG/SegDescrip/SegName");
print LOG "SegName = $segname\n";
my $SEGNAME = readFile("$segname");
while (<$SEGNAME>) {
if (/^\$NAME/) {
chomp($_);
@t=split(":");
$THISSEGNAME=$t[1];
} elsif (/^\$TYPE/) {
chomp($_);
@t=split(":");
$THISSEGTYPE=$t[1];
$THISSEGSUBTYPE=$t[2];
} elsif (/^\$PREFIX/) {
chomp($_);
@t=split(":");
$THISSEGPREFIX=$t[1];
} elsif (/^\$SEGMENT/) {
chomp($_);
@t=split(":");
$THISACCTGRP="$t[1]";
}
}
# ----------------------------------------------------------------------
# Delete Application Data, Process SegInfo
# 20020520rb: copied from install, still needs lotsa work for deinstall
# ----------------------------------------------------------------------
my $seginfo=coeFileConv("$dir/$SEG/SegDescrip/SegInfo");
print LOG "SegInfo = $seginfo\n";
my $SEGINFO = readFile("$seginfo");
while (<$SEGINFO>) {
#if (! /^$/ && !/^#/ ) {
if (/^\[/) {
s/\[//;
s/\]//;
chomp($_);
$DESCRIPTOR=$_;
print LOG "Desc = $DESCRIPTOR\n";
$subdesc="none";
} elsif ("$DESCRIPTOR" eq "COEServices" && /^\$/) {
chomp($_);
$subdesc=$_;
} elsif ("$DESCRIPTOR" eq "COEServices" && "$subdesc" eq "\$GROUPS") {
#- add a group */
chomp($_);
@t=split(":");
#print("/h/COE/Comp/OS/bin/OS_add_group -n $t[0] -g $t[1]\n");
#coeRun ("COE_COMP/OS/bin/OS_add_group -n $t[0] -g $t[1]");
} elsif ("$DESCRIPTOR" eq "COEServices" && "$subdesc" eq "\$PASSWORDS") {
#- add a user */
chomp($_);
@t=split(":");
#print("/h/COE/Comp/OS/bin/OS_add_user --username $t[0] --uid $t[1] --group $t[2] --fullname \"$t[3]\" --home $t[4] --shell $t[5]\n");
#coeRun ("COE_COMP/OS/bin/OS_add_user --username $t[0] --uid $t[1] --group $t[2] --fullname \"$t[3]\" --home $t[4] --shell $t[5]");
} elsif ("$DESCRIPTOR" eq "COEServices" && "$subdesc" eq "\$SERVICE") {
#- add a service */
} elsif ("$DESCRIPTOR" eq "AcctGroup") {
#- add a account group/default profile */
chomp($_);
@t=split(":");
$_=$t[5];
s/ /_/g;
$name=$_;
#print("/h/COE/bin/COE_add_profile_data \"$t[5]\" \"$t[0]\" $name local\n");
#coeRun ("COE_HOME/bin/COE_add_profile_data \"$t[5]\" \"$t[0]\" $name global");
} elsif ("$DESCRIPTOR" eq "Icons") {
#- add application data */
chomp($_);
@t=split(":");
my $iconfile=coeFileConv("$dir/$SEG/data/Icons/$t[0]");
my $ICON= readFile($iconfile);;
while (<$ICON>) {
$_=cleanString($_,3);
@t=split(":");
$name=$t[0];
# normalized app name
$_=$name;
s/ /_/g;
$norm=$_;
# icon
$icon=$t[1];
# account group found above
$acctgrp=$THISACCTGRP;
# exec
$_=$t[2];
@tt=split(" ");
$exec=$tt[0];
# args
$_=$t[2];
s/$exec//;
$args=$_;
if ("$icon" eq "") {
print LOG "no data for /h/COE/bin/COE_delete_app_data \n";
} else {
print LOG ("/h/COE/bin/COE_delete_app_data -a \"$name\" \"local\" \n");
coeRun ("COE_HOME/bin/COE_delete_app_data", "-a", "$name", "local");
}
}
}
#}
}
# ----------------------------------------------------------------------
# Run DEINSTALL script if available
# ----------------------------------------------------------------------
$INSTALL_DIR="$SEGPATH/$SEG";
print LOG "Deinstalling $INSTALL_DIR\n";
$file="$INSTALL_DIR/SegDescrip/DEINSTALL";
if ( -e "$file" ) {
coeRun("$INSTALL_DIR/SegDescrip/DEINSTALL");
}
# ----------------------------------------------------------------------
# Remove directory
# ----------------------------------------------------------------------
# can only run rmtree with -U when -T (taint) flag is active.
coeRun("/bin/rm", "-rf", "$INSTALL_DIR");
#rmtree ($INSTALL_DIR);
#- catch any /h links
#$mytmppath=coeFileConv("COE_APP/$SEG");
#if ( -l $mytmppath ) { unlink $mytmppath; }
#- catch any /h/COTS links
#$mytmppath=coeFileConv("COE_COTS/$SEG");
#if ( -l $mytmppath ) { unlink $mytmppath; }
} elsif ($flagstage==1) {
print LOG "stage: $SEG\n";
# ----------------------------------------------------------------------
# Verify segment exists
# ----------------------------------------------------------------------
# 20020125rb - reorganizing Repository path
#$dir="$REPPATH";
#chdir $dir || die ("Cannot find segment path $REPPATH") if dir;
#while (<$SEG>){
# $SEGMENT=$_;
#}
$dir= coeFileConv("$REPPATH/$SEG");
chdir $dir || die ("Cannot find segment path $REPPATH") if ( -d $dir );
while (<$SEG*>){
$SEGFILE=$_;
}
# ----------------------------------------------------------------------
# Which type is it: sw/cots/acctgroup/data/db/aggregate/comp
# While we are it, grab the segment prefix
# ----------------------------------------------------------------------
#20020125rb - modifying directory layout
#$file="$REPPATH/$SEGMENT/TOC/$SEGMENT/SegDescrip/SegName";
print LOG "$REPPATH/TOC/$SEG/SegDescrip/SegName\n";
my $segname="$REPPATH/$SEG/SegDescrip/SegName";
my $SEGNAME = readFile($segname);
my $type="dummy";
my $subtype="dummy";
while (<$SEGNAME>) {
if (/\$TYPE/) {
chomp($_);
@t=split(":");
$type=$t[1];
$_=$type;
tr/a-z/A-Z/;
$type=$_;
print LOG "type: $type\n";
$subtype=$t[2];
$_=$subtype;
$subtype=$_;
} elsif (/\$PREFIX/) {
chomp($_);
@p=split(":");
$PREFIX=$p[1];
}
}
# ----------------------------------------------------------------------
# Set install path based on type
# ----------------------------------------------------------------------
if ("$type" eq "ACCOUNT GROUP") {
$SEGPATH=getCoeAcct;
} elsif ("$subtype" eq "COE CHILD") {
$SEGPATH=getCoeHome."/COE/Comp";
} elsif ("$type" eq "COTS") {
$SEGPATH=getCoeCots;
} elsif ("$type" eq "SOFTWARE") {
$SEGPATH=getCoeApps;
} elsif ("$type" eq "DB") {
$SEGPATH=getCoeApps;
} elsif ("$type" eq "DATA") {
$SEGPATH=getCoeApps;
} elsif ("$subtype" eq "AGGREGATE") {
$SEGPATH=getCoeApps;
} else {
print LOG "Unknown segment type $type for $SEG\n";
$type="Unknown";
exit -1;
}
print LOG "TYPE = $type, SUB = $subtype, path = $SEGPATH\n";
# ----------------------------------------------------------------------
# Set the environment variable INSTALL_DIR
# ----------------------------------------------------------------------
$INSTALL_DIR="$SEGPATH/$PREFIX";
#coeRun("export $INSTALL_DIR");
# ----------------------------------------------------------------------
# If directory does exists bail, else make directories
# ----------------------------------------------------------------------
if ( -e $installed ) {
print LOG "$SEG is already installed !\n";
exit -1;
}
# ----------------------------------------------------------------------
# cp segment into directory
# ----------------------------------------------------------------------
#copy "$REPPATH/$SEGMENT/$SEGFILE", "$SEGPATH/$SEGFILE";
print LOG "copy $REPPATH/$SEG/$SEGFILE $SEGPATH/$SEGFILE\n";
my $cptgt = coeFileConv("$SEGPATH/$SEGFILE");
my $cpsrc = coeFileConv("$REPPATH/$SEG/$SEGFILE");
copy $cpsrc, $cptgt;
} else {
@s = split($sep, $SEG);
$SEG=$s[0];
print LOG "install: $SEG\n";
# ----------------------------------------------------------------------
# Install the segment
# ----------------------------------------------------------------------
# ----------------------------------------------------------------------
# Get the segment compressed file name
# ----------------------------------------------------------------------
$dir=coeFileConv($SEGPATH);
chdir $dir || die ("Error changing to directory $SEGPATH\n");
#- decompress
while (<$SEG$sep*>){
$SEGFILE=cleanString($_,3);
print LOG ("found segfile $SEGFILE\n");
if ("$SEGFILE" =~ /\.z$/i ) { coeRun ("/bin/uncompress", "$SEGFILE"); }
if ("$SEGFILE" =~ /\.tgz$/i ) { coeRun ("/bin/gunzip", "$SEGFILE"); }
if ("$SEGFILE" =~ /\.gz$/i ) { coeRun ("/bin/gunzip", "$SEGFILE"); }
if ("$SEGFILE" =~ /\.zip$/i ) { coeRun ("/usr/bin/unzip", "$SEGFILE"); }
if ("$SEGFILE" =~ /\.bz2$/i ) { coeRun ("/usr/bin/bunzip2", "$SEGFILE"); }
}
#- remove file
while (<$SEG$sep*>){
$SEGFILE=cleanString($_,3);
$mytmppath=coeFileConv("$dir/$SEGFILE");
print (" and it is now $SEGFILE\n");
$tarflags="--force-local -xpf";
#if ("$SEGFILE" =~ /\.tar$/i ) { coeRun ("/h/COE/Comp/PERL5/bin/ptar", "$tarflags", "$SEGFILE"); }
# can only user tar extract with -U when -T (taint) flag is active
# (creates lots of Insecure warnings!)
my $segtar = Archive::Tar->new();
$segtar->extract_archive($SEGFILE);
if ("$SEGFILE" =~ /\.tar$/i ) { unlink $mytmppath; }
if ("$SEGFILE" =~ /\.z$/i ) { unlink $mytmppath; }
if ("$SEGFILE" =~ /\.tgz$/i ) { unlink $mytmppath; }
if ("$SEGFILE" =~ /\.gz$/i ) { unlink $mytmppath; }
if ("$SEGFILE" =~ /\.zip$/i ) { unlink $mytmppath; }
if ("$SEGFILE" =~ /\.bz2$/i ) { unlink $mytmppath; }
}
# ----------------------------------------------------------------------
# Set the Top Level directory permissions
# ----------------------------------------------------------------------
#- as default, set segment ownership to COE
#- this does not work unless priveleged user
#- cannot change ownership to someone else
$perm=755;
chmod (oct($perm), "$SEGPATH/$SEG");
chown 10000, 10000, "$SEGPATH/$SEG";
# ----------------------------------------------------------------------
# Set the File Attribs
# ----------------------------------------------------------------------
my $fileat=coeFileConv("$SEGPATH/$SEG/SegDescrip/FileAttribs");
if ( -e $fileat ) {
my $FILEATTR = readFile($fileat);
while (<$FILEATTR>) {
@attr=split(":");
$perm=cleanString($attr[0],1);
$uid=cleanString($attr[1],1);
$gid=cleanString($attr[2],1);
$obj=cleanString($attr[3],3);
chomp ($obj);
$mytmppath=coeFileConv("$SEGPATH/$SEG/$obj");
chmod (oct($perm), $mytmppath);
chown $uid, $gid, $mytmppath;
}
}
# ----------------------------------------------------------------------
# Run PreInstall
# ----------------------------------------------------------------------
my $preinstall=coeFileConv("$SEGPATH/$SEG/SegDescrip/PreInstall");
if ( -e $preinstall ) {
coeRun ($preinstall);
}
# ----------------------------------------------------------------------
# Process SegName
# ----------------------------------------------------------------------
$THISSEGSUBTYPE="dummy";
my $segname=coeFileConv("$dir/$SEG/SegDescrip/SegName");
if ( -e $segname ) {
my $FILE = readFile($segname);
while (<$FILE>) {
if (/^\$NAME/) {
chomp($_);
@t=split(":");
$THISSEGNAME=$t[1];
} elsif (/^\$TYPE/) {
chomp($_);
@t=split(":");
$THISSEGTYPE=$t[1];
$THISSEGSUBTYPE=$t[2];
} elsif (/^\$PREFIX/) {
chomp($_);
@t=split(":");
$THISSEGPREFIX=$t[1];
} elsif (/^\$SEGMENT/) {
chomp($_);
@t=split(":");
$THISACCTGRP="$t[1]";
}
}
}
# ----------------------------------------------------------------------
# Process SegInfo
# ----------------------------------------------------------------------
my $seginfo=coeFileConv("$dir/$SEG/SegDescrip/SegInfo");
if ( -e $seginfo ) {
my $FILE = readFile($seginfo);
while (<$FILE>) {
if (/^\[/) {
s/\[//;
s/\]//;
chomp($_);
$DESCRIPTOR=$_;
print LOG "Desc = $DESCRIPTOR\n";
$subdesc="none";
} elsif ("$DESCRIPTOR" eq "COEServices" && /^\$/) {
chomp($_);
$subdesc=$_;
} elsif ("$DESCRIPTOR" eq "COEServices" && "$subdesc" eq "\$GROUPS") {
#- add a group */
chomp($_);
@t=split(":");
print("/h/COE/Comp/OS/bin/OS_add_group -n $t[0] -g $t[1]\n");
coeRun ("COE_COMP/OS/bin/OS_add_group", "-n", "$t[0]", "-g", "$t[1]");
} elsif ("$DESCRIPTOR" eq "COEServices" && "$subdesc" eq "\$PASSWORDS") {
#- add a user */
chomp($_);
@t=split(":");
print("/h/COE/Comp/OS/bin/OS_add_user --username $t[0] --uid $t[1] --group $t[2] --fullname \"$t[3]\" --home $t[4] --shell $t[5]\n");
coeRun ("COE_COMP/OS/bin/OS_add_user",
"--username", "$t[0]",
"--uid", "$t[1]",
"--group", "$t[2]",
"--fullname", "$t[3]",
"--home", "$t[4]",
"--shell", "$t[5]");
} elsif ("$DESCRIPTOR" eq "COEServices" && "$subdesc" eq "\$SERVICE") {
#- add a service */
} elsif ("$DESCRIPTOR" eq "AcctGroup") {
#- add a account group/default profile */
chomp($_);
@t=split(":");
$_=$t[5];
s/ /_/g;
$name=$_;
if ("$name" ne "" ) {
print("/h/COE/bin/COE_add_profile_data \"$t[5]\" \"$t[0]\" $name local\n");
coeRun ("COE_HOME/bin/COE_add_profile_data", "$t[5]", "$t[0]", "$name", "local");
#coeRun ("/h/COE/bin/COE_add_profile_data \"$t[5]\" \"$t[0]\" $name global");
}
} elsif ("$DESCRIPTOR" eq "Icons") {
#- add application data */
chomp($_);
@t=split(":");
$iconfile=coeFileConv("$dir/$SEG/data/Icons/$t[0]");
open ICON, "$iconfile" || die "$0: cannot open file $iconfile:$_\n";
while () {
chomp($_);
print LOG "==$_\n";
$_=cleanString($_,3);
print LOG "++$_\n";
@t=split(":");
$name=$t[0];
# normalized app name
$_=$name;
s/ /_/g;
$norm=$_;
# icon
$icon=$t[1];
# account group found above
$acctgrp=$THISACCTGRP;
# exec
$_=$t[2];
@tt=split(" ");
$exec=$tt[0];
# args
$_=$t[2];
s/$exec//;
$args=$_;
# substitute the InstallDir for /h/ in Icon paths
$_=$exec;
s/\/h\//$SEGPATH\//;
$exec=$_;
$_=$icon;
s/\/h\//$SEGPATH\//;
$icon=$_;
if ("$icon" eq "") {
print LOG "no data for /h/COE/bin/COE_add_app_data \n";
} else {
print("/h/COE/bin/COE_add_app_data \"$name\" \"$norm\" \"$acctgrp\" \"$icon\" \"$exec\" \"local\" \"$args\"\n");
coeRun ("COE_HOME/bin/COE_add_app_data", "$name", "$norm", "$acctgrp", "$icon", "$exec", "local", "$args");
}
}
}
}
}
# ----------------------------------------------------------------------
# Run PostInstall
# ----------------------------------------------------------------------
$postinstall=coeFileConv("$SEGPATH/$SEG/SegDescrip/PostInstall");
if ( -e $postinstall ) { coeRun($postinstall); }
# ----------------------------------------------------------------------
# Mark the segment installed
# ----------------------------------------------------------------------
my $installed=coeFileConv("$SEGPATH/$SEG/SegDescrip/Installed");
print LOG ("installed file = $installed\n");
open INSTALLED, ">$installed" || die ("Cannot create $installed\n");
#20020115rb: real time output
#print FILE "010101:01 Jan 01\n";
($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time);
$year=$year-100;
@month=("Jan","Feb","Mar","Apr","May","Jun",
"Jul","Aug","Sep","Oct","Nov","Dec");
$tmp=$month[$mon];
printf INSTALLED ("%02d%02d%02d:%02d %s %02d\n",$mday,$hour,$min,$sec,$tmp,$year);
close INSTALLED;
#- not sure if I want this unless I want to be able to install to
#- multiple file systems.
# ----------------------------------------------------------------------
# If InstallDir is not COE_HOME, place a link
# ----------------------------------------------------------------------
#my $coehome=getCoeHome();
#if ("$SEGPATH/" !~ /^$coehome\// ) {
#$_=$SEGPATH;
#if ("$SEGPATH" =~ /\/AcctGrps\// ) {
#s/^\/.*\/AcctGrps\//$coehome\/AcctGrps\//;
#$LNPATH=$_;
#} elsif ("$SEGPATH" =~ /\/COTS\// ) {
#s/^\/.*\/COTS\//$coehome\/COTS\//;
#$LNPATH=$_;
#} elsif ("$SEGPATH" =~ /\/Comp\// ) {
#s/^\/.*\/Comp\//$coehome\/Comp\//;
#$LNPATH=$_;
#} else {
#$LNPATH="$coehome";
#}
#print("ln -s $SEGPATH/$SEG $LNPATH/$SEG\n");
#coeRun("ln -s $SEGPATH/$SEG $LNPATH/$SEG");
#symlink("$SEGPATH/$SEG", "$LNPATH/$SEG") or die "$!";
#}
}
# ------------------------------------------------------------------------------
# Normal Exit
# ------------------------------------------------------------------------------
exit 0;