Tuesday, August 9, 2016

Script that run's when you install RPM Files



                              Script that run's when you install RPM Files

        

Sometimes we need to find specific file or jars belong to which package while troubleshooting and would like to get same package installed on problematic node as well usually "rpm -qf  <File-name>" works fine. 

Like say i need to find which package in mapr packages mapr fsck utility via below command we get the package name or some jar we see the package where the jar came from.

[root@node1 mapr]# rpm -qf /opt/mapr/server/fsck
mapr-core-internal-4.0.2.29870.GA-1.x86_64

[root@node1 pig-0.14]# rpm -qf pig-0.14.0-mapr-SNAPSHOT.jar
mapr-pig-0.14.201608040131-1.noarch

But now lets take below case where we get pig-core-h2.jar is not owned by any package but we are certain this jar was part of a rpm install yet we don't get rite answer when we query using the file.

[root@node1 pig-0.14]# rpm -qf pig-core-h2.jar 
file /opt/mapr/pig/pig-0.14/pig-core-h2.jar is not owned by any package

This is because rpm packages can run scripts after a package is added or removed . How do we view pre/post install and uninstall rpm file scripts (RHEL)?

The option –scripts to rpm command displays the package specific script(s) that are used as part of the installation and uninstallation processes. Use the following command line option to view rpm file scripts for .rpm file.


rpm -qp --scripts <RPM-Name>.rpm
Example : To list all scripts which will be run as part of "mapr-pig-0.14.201608040131-1.noarch.rpm" install.

[root@node1 pig-0.14]# rpm -qp --scripts /tmp/mapr-pig-0.14.201608040131-1.noarch.rpm 
postinstall scriptlet (using /bin/sh):

# this is install
mkdir -p "/opt/mapr/pig/pig-0.14"/conf
diffR=$(diff -r "/opt/mapr/pig/pig-0.14"/conf "/opt/mapr/pig/pig-0.14"/conf.new | grep "^Only in " | grep "conf.new" | sed "s/^Only in //" | sed "s/: /\//")
for i in `echo $diffR`; do
    j=$(echo $i | sed 's/conf.new/conf/g')
    cp -Rp $i $j 
done

ln -sf /opt/mapr/pig/pig-0.14/bin/pig /opt/mapr/bin/
ln -sf /opt/mapr/pig/pig-0.14/bin/pig /usr/bin/pig


#
# single pig jar for post-yarn releases
#
if [ -f /opt/mapr/MapRBuildVersion ]; then
    MAPR_VERSION=`cat /opt/mapr/MapRBuildVersion | awk -F "." '{print $1"."$2}'`

    #if mapr-core release >=4.0 (yarn beta) returns boolean 1, else returns boolean 0
    POST_YARN=`echo | awk -v cur=$MAPR_VERSION -v min=4.0 '{if (cur >= min) printf("1"); else printf ("0");}'`
    if [[ $POST_YARN == 0 ]]; then
echo POST_YARN=$POST_YARN, removing yarn jars    
        rm -f  /opt/mapr/pig/pig-0.14/pig-core-h1.jar
        mv /opt/mapr/pig/pig-0.14/pig-*-h1.jar /opt/mapr/pig/pig-0.14/pig-core-h1.jar
        mv /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank-h1.jar /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank.jar
        rm /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank-h2.jar
        rm /opt/mapr/pig/pig-0.14/pig-*-h2.jar
        mv /opt/mapr/pig/pig-0.14/conf/pig.properties.3.x /opt/mapr/pig/pig-0.14/conf/pig.properties
    else
# mv *-h2.jar to *.jar
echo POST_YARN=$POST_YARN, replacing MR1 jars
 rm -f  /opt/mapr/pig/pig-0.14/pig-core-h2.jar
        mv /opt/mapr/pig/pig-0.14/pig-*-h2.jar /opt/mapr/pig/pig-0.14/pig-core-h2.jar
        mv /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank-h2.jar /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank.jar
        rm /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank-h1.jar
        rm /opt/mapr/pig/pig-0.14/pig-*-h1.jar
        rm /opt/mapr/pig/pig-0.14/conf/pig.properties.3.x
    fi    
fi

#
# change ownership
#
DAEMON_CONF="/opt/mapr/conf/daemon.conf"

if [ -f "$DAEMON_CONF" ]; then
    MAPR_USER=$( awk -F = '$1 == "mapr.daemon.user" { print $2 }' $DAEMON_CONF)

    if [ ! -z "$MAPR_USER" ]; then
        chown -R $MAPR_USER /opt/mapr/pig/pig-0.14
    fi
fi

##
## end of post install section
##
preuninstall program: /bin/sh
postuninstall scriptlet (using /bin/sh):

#
# last version is removed
# if package is getting purged remove entire directory
# we may need to remove pig/pig-X/logs
#
if [ "$1" = "0" ]; then
    rm -Rf /opt/mapr/pig/pig-0.14/logs
    rm -Rf /opt/mapr/pig/pig-0.14/conf

    rm -f /opt/mapr/bin/pig
    rm -f /usr/bin/pig
fi

##
## end of post uninstall section
##
[root@node1 pig-0.14]# 

To view scripts for installed packages, enter:

rpm -q --scripts <Package name>

Example :

[root@node1 pig-0.14]# rpm -qa | grep pig
mapr-pig-0.14.201608040131-1.noarch
[root@node1 pig-0.14]# rpm -q --scripts mapr-pig-0.14.201608040131-1.noarch
postinstall scriptlet (using /bin/sh):

# this is install
mkdir -p "/opt/mapr/pig/pig-0.14"/conf
diffR=$(diff -r "/opt/mapr/pig/pig-0.14"/conf "/opt/mapr/pig/pig-0.14"/conf.new | grep "^Only in " | grep "conf.new" | sed "s/^Only in //" | sed "s/: /\//")
for i in `echo $diffR`; do
    j=$(echo $i | sed 's/conf.new/conf/g')
    cp -Rp $i $j 
done

ln -sf /opt/mapr/pig/pig-0.14/bin/pig /opt/mapr/bin/
ln -sf /opt/mapr/pig/pig-0.14/bin/pig /usr/bin/pig


#
# single pig jar for post-yarn releases
#
if [ -f /opt/mapr/MapRBuildVersion ]; then
    MAPR_VERSION=`cat /opt/mapr/MapRBuildVersion | awk -F "." '{print $1"."$2}'`

    #if mapr-core release >=4.0 (yarn beta) returns boolean 1, else returns boolean 0
    POST_YARN=`echo | awk -v cur=$MAPR_VERSION -v min=4.0 '{if (cur >= min) printf("1"); else printf ("0");}'`
    if [[ $POST_YARN == 0 ]]; then
echo POST_YARN=$POST_YARN, removing yarn jars    
        rm -f  /opt/mapr/pig/pig-0.14/pig-core-h1.jar
        mv /opt/mapr/pig/pig-0.14/pig-*-h1.jar /opt/mapr/pig/pig-0.14/pig-core-h1.jar
        mv /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank-h1.jar /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank.jar
        rm /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank-h2.jar
        rm /opt/mapr/pig/pig-0.14/pig-*-h2.jar
        mv /opt/mapr/pig/pig-0.14/conf/pig.properties.3.x /opt/mapr/pig/pig-0.14/conf/pig.properties
    else
# mv *-h2.jar to *.jar
echo POST_YARN=$POST_YARN, replacing MR1 jars
 rm -f  /opt/mapr/pig/pig-0.14/pig-core-h2.jar
        mv /opt/mapr/pig/pig-0.14/pig-*-h2.jar /opt/mapr/pig/pig-0.14/pig-core-h2.jar
        mv /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank-h2.jar /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank.jar
        rm /opt/mapr/pig/pig-0.14/contrib/piggybank/java/piggybank-h1.jar
        rm /opt/mapr/pig/pig-0.14/pig-*-h1.jar
        rm /opt/mapr/pig/pig-0.14/conf/pig.properties.3.x
    fi    
fi

#
# change ownership
#
DAEMON_CONF="/opt/mapr/conf/daemon.conf"

if [ -f "$DAEMON_CONF" ]; then
    MAPR_USER=$( awk -F = '$1 == "mapr.daemon.user" { print $2 }' $DAEMON_CONF)

    if [ ! -z "$MAPR_USER" ]; then
        chown -R $MAPR_USER /opt/mapr/pig/pig-0.14
    fi
fi

##
## end of post install section
##
preuninstall program: /bin/sh
postuninstall scriptlet (using /bin/sh):

#
# last version is removed
# if package is getting purged remove entire directory
# we may need to remove pig/pig-X/logs
#
if [ "$1" = "0" ]; then
    rm -Rf /opt/mapr/pig/pig-0.14/logs
    rm -Rf /opt/mapr/pig/pig-0.14/conf

    rm -f /opt/mapr/bin/pig
    rm -f /usr/bin/pig
fi

##
## end of post uninstall section
##
[root@node1 pig-0.14]# 


No comments:

Post a Comment