我用 LinuxLinux运维进阶-Python,Docker,Shell运维驿站

分享一个shell脚本用于“基于已安装的rpm包,创建tar压缩

2020-04-09  本文已影响0人  My熊猫眼

有时候,我们不想在系统中安装rpm包,但是又需要这个rpm包的功能,那么可否实现呢?
这个需求是可以实现的,当我们用tar包去安装这个功能的时候就可以实现了.因为我们知道,rpm 包本质上是 运行了如下的过程:

  1. 运行 pre-script, 进行依赖检查等.
  2. 释放文件到相应的目录.
  3. 运行 post-script 等.
    忽略pre-script 和 post-script, 那么其实就是文件的release 过程,所以可以把相应文件收集起来,然后打包就可以了.

下面的脚本在系统中没有安装 对应rpm包的时候,会查找对应的 tar包,然后释放到系统中,如果系统中已经安装了对应的rpm包,那么会打包生成相应rpm的tar 包. 如果把这个tar包放到目标系统中,那么目标系统需要和rpm包所在的系统是相同的.

#!/bin/bash
if [ $# -lt 1 ];then
    echo -e "Usage:\n`basename $0` {RPM_PACKAGE_NAME}  [OPTION]\n*************************************************************\nRPM_PACKAGE_NAME:\nThe rpm package name, this package must be installed already if you want to archive it. If you want to make portable version for this rpm package, then related .tar.gz file must be existing on /tmp/.\nOPTION:\nAvailable option value is : -i/-I. This option used for cp commands, it will come to interactive mode if need overwrite a existing file during release the RPM Packages. By default, it is '-n'.\n*************************************************************"
    exit -1
fi
PKGNAME=$1
FLAG="n"
if [[ -n $2 ]];then
    if [[ $2="-i" || $2="-I" ]];then
        FLAG="i"
    fi
fi
function OutTarPkg
{
set -e 
mkdir -p /tmp/Free_${PKGNAME}_FileList
for i in `rpm -qR $PKGNAME  | awk -F'[(]' '{print $1}' `;do rpm -q --whatprovides $i >/dev/null && echo $i;done  >/tmp/Free_${PKGNAME}_FileList/deps.txt
rpm -qR $PKGNAME >/tmp/Free_${PKGNAME}_FileList/deps.source
set +e
for i in `rpm -ql $PKGNAME`;
do 
    n_path=`echo $i | sed "s#^#/tmp/Free_${PKGNAME}_FileList#"`
    dir_name=`echo $n_path |  awk -F'/' '{for(i=1;i<NF;i++)printf("%s/",$i)}'`
    mkdir -p $dir_name 
    cp -L --preserve=all  $i  $n_path >/dev/null  2>/dev/null  #This parts, not use -R, because it will copy all the related files under the directory.
done 
cd /tmp
tar -czvf ./${PKGNAME}_RPM.tar.gz  ./Free_${PKGNAME}_FileList/ >/dev/null 
rm -rf /tmp/Free_${PKGNAME}_FileList 
}

function DepsChk
{
for i in `cat ./deps.txt`;
do
        rpm -q --whatprovides $i >/dev/null
        if [ $? -ne 0 ];then
            j=`echo $i | awk -F. '{print $1}'`
            rpm -q --whatprovides $j >/dev/null 
            if [ $? -ne 0 ];then
                echo "Dependency Required, no packages provides for: $i"
                exit 1
            fi
        fi
done
echo "1. The Dependency verify results: PASS."
}


function ReleaseFile
{
cd /tmp
if [ ! -s /tmp/${PKGNAME}_RPM.tar.gz ];
then
    echo -e "Please Verify the file: ${PKGNAME}_RPM.tar.gz exists in the path /tmp/.\nExit with code 2."
    exit 2
fi
set -e
tar -xzvf ./${PKGNAME}_RPM.tar.gz   >/dev/null 
cd ./Free_${PKGNAME}_FileList
set +e
DepsChk
rm -f deps.txt
set -e
for i in `find . -iname "*" -type f | grep -Evi "deps.source|deps.txt" | sed 's/.//'`;
do 
    s_file=`echo $i | sed 's/^/./'`
    if [ -f $i ];then
        mv $i  $i.`date +%Y%m%d_%H%M%S`    #Rename first if the file exists already. 
    fi
    mkdir -p ${i%/*}         #Create the directory first if it not exist by default. 
    cp -L --preserve=all -${FLAG}  ${s_file}  $i    
done 
cd /tmp
rm -rf ./Free_${PKGNAME}_FileList
}

#Start from here: 
rpm -qi $PKGNAME >/dev/null
if [ $? -eq 0 ];then
    echo "System installed packages: $PKGNAME, start get the Tar file for this package."
    OutTarPkg
    if [ $? -eq 0 ];then
        echo "Succeed get the tar.gz file for package: $PKGNAME on path /tmp/${PKGNAME}_RPM.tar.gz"
    else
        echo "Failed to get the tar.gz file."
        exit 3
    fi
else
    echo "Package: $PKGNAME not found in this system, try to release the rpm related files from /tmp/${PKGNAME}_RPM.tar.gz"
    ReleaseFile
    if [ $? -eq 0 ];then 
        echo "2. Release the package files results on the system: Succeed.You can use it now."
    else
        echo "Failed to release the rpm package files."
        exit 5
    fi
fi

本文原创,转载请著名出处.

上一篇下一篇

猜你喜欢

热点阅读