#!/bin/bash
#filename: rpm_miv.sh
#Use this shell script to :
#1. make rpm package
#2. install rpm package
#3. Verify the files' attributes which installed by the rpm package baaed on spec file
if [ $# -ne 1 ]; then
echo "Usage: $0 full source code path";
exit -1
fi
if [ ! -d $1 ]; then
echo source code path : $1 is not existing !!!
exit -1
fi
cd $1
find . -name "*.spec" > /dev/null
if [ $? -ne 0 ]; then
echo NO sepc file defined in the soource code !!!
exit -1
fi
rpm -q make > /dev/null
if [ $? -ne 0 ]; then
echo 'install make tool...'
yum -y install make
if [ $? -ne 0 ]; then
echo 'install make tool failed!'
exit -1
fi
fi
rpm -q rpm-build > /dev/null
if [ $? -ne 0 ]; then
echo 'install rpm-build tool...'
yum -y install rpm-build
if [ $? -ne 0 ]; then
echo 'install rpm-build tool failed!'
exit -1
fi
fi
#Generate the rpm package
make
#install the rpm package
#cd output/rpm
cd output
rpm_file=`find . -name "*.rpm"`
rpm=`echo $rpm_file | sed 's/.\///' | sed 's/.rpm//'| sed 's/\// /' |awk '{print $NF}'`
echo $rpm
#if the rpm package has been installed, then remove it firstly
rpm -q $rpm > /dev/null
if [ $? -eq 0 ]; then
echo "remove installed $rpm package..."
rpm -e $rpm
if [ $? -ne 0 ]; then
echo "remove installed $rpm package failed!"
exit -1
fi
fi
#install the rpm package
rpm -ivh $rpm_file
if [ $? -ne 0 ]; then
echo "Install $rpm package failed!"
exit -1
else
echo "Install $rpm package success!"
fi
echo $1
cd $1
#get out files defattr from spec file
spec_file=`find . -name "*.spec"`
echo $spec_file
filesnum=`sed -n "/%files/=" $spec_file`
defattrnum=`expr $filesnum + 1`
defattr=`sed -n "${defattrnum}p" $spec_file`
printf "Defined file attributes: %s in %s\n" $defattr $spec_file
def_mod=`echo $defattr | sed 's/\%defattr(//' | sed 's/,/ /g' | awk '{print $1}'`
def_mod=`echo $def_mod | sed 's/0//'`
def_user=`echo $defattr | sed 's/\%defattr(//' | sed 's/,/ /g' | awk '{print $2}'`
def_group=`echo $defattr | sed 's/\%defattr(//' | sed 's/,/ /g' | awk '{print $3}'`
printf "%s %s %s\n" $def_mod $def_user $def_group
#compare defattr with file attr by stat command in the installed files list
i=0
flag=0
while read line;
do
let i+=1
if [ $i -gt $defattrnum ] && [ -n "$line" ];
then
printf "Checking %s...\n" $line
if [ ! -f $line ];
then
flag=1
printf 'File: %s is not existing!\n' $line
break
fi
linenum_all=`stat $line | sed -n "/Access/="`
line1=`echo $linenum_all | awk '{print $1}'`
content=`stat $line | sed -n "${line1}p"`
echo $content
mod=`echo $content | awk '{print $2}' | sed 's/\/-[rwx-]*)//' \
| sed 's/(0//'`
user=`echo $content | awk '{print $6}' | sed 's/)//'`
group=`echo $content | awk '{print $10}'| sed 's/)//'`
printf "%s %s %s\n" $mod $user $group
if [ "$def_mod" != "$mod" ] || [ "$def_user" != "$user" ] || [ "$def_group" != "$group" ];
then
flag=1
break
fi
fi
done < $spec_file
#print out check result
if [ $flag -ne 1 ];
then
echo 'All Passed!'
exit 0
else
printf "Failed @ file: %s\n!" $line
exit 1
fi