更新(-Uvh)RPM软件包(修改版本号)时如何删除以前版本的app文件夹?

时间:2014-05-15 20:02:32

标签: rpmbuild rpm-spec

#define program installation destination
%define app_destination   /opt
%define app_name    MY_APP_NAME
%define app_version    2.1
%define app_release    7%{?dist}
%define app_dir    %{app_name}-%{app_version}
%define compress_file    %{app_dir}.tar.gz
%define app_service_softlink    /etc/init.d/%{app_name}
%define app_dir_softlink    %{app_destination}/%{app_name}


Name:    %{app_name}
Version:    %{app_version}
Release:    %{app_release}
Summary:    MY APP ONE-SENTENCE SUMMARY %{app_version}

# An open source software license
License:    GPLv3+
URL:    http://www.starscriber.com/
Source0:    http://ftp.gnu.org/gnu/%{compress_file}


%description 
MY APP DESCRIPTION

%pre
#each time before install/upgrade RPM, check and remove the softlinks provided below
echo "pre..."

if [ -L %{app_service_softlink} ];then
    rm %{app_service_softlink}
elif [ -f %{app_service_softlink} ];then
    rm %{app_service_softlink}
fi

if [ -L %{app_dir_softlink} ]; then 
    rm %{app_dir_softlink}
elif [ -d %{app_dir_softlink} ]; then
    rmdir %{app_dir_softlink}
fi



%prep
%setup -q
echo "prep..."

# Script commands to "build" the program (e.g. to compile it) and 
# get it ready for installing. The program should come with 
# instructions on how to do this. 
%build


%install
echo "install..."
# uses relative paths 
# creates buildroot/destination directory 
mkdir -p %{buildroot}%{app_destination}

# copies tar.gz file from source directory to buildroot/destination directory
cp %{_sourcedir}/%{compress_file} %{buildroot}%{app_destination}

# changes directory to buildroot/destination
cd %{buildroot}%{app_destination}

# extracts compression file
tar xf %{compress_file}

# removes the compression file
rm -rf %{compress_file}

cd %{buildroot}%{app_destination}


#invoked after %post when RPM pkg is removal or upgrade
%preun
echo "preun..."
#leftover cleanup


#invoked after %preun when RPM pkg is removal or upgrade
%postun
echo "postun..."
if [ "$1" == "0" ]; then
    rm -rf %{app_destination}/%{app_dir}
fi

if [ ! -d %{app_destination}/%{app_dir} ]; then
    if [ -L %{app_service_softlink} ]; then
        rm %{app_service_softlink}
    elif [ -f %{app_service_softlink} ]; then
        rm %{app_service_softlink}
    fi

    if [ -L %{app_dir_softlink} ]; then
        rm %{app_dir_softlink}
    elif [ -d %{app_dir_softlink} ]; then
        rmdir %{app_dir_softlink}
    fi
fi

%files
#all files under the provided folder will be gathered up to create RPM pkg
%{app_destination}/%{app_dir}/bin
%{app_destination}/%{app_dir}/conf
%{app_destination}/%{app_dir}/misc



%post
echo "post"
#symbolic link to the new appdir with version
echo "builds new symbolic link for the app folder"
ln -sf  %{app_destination}/%{app_dir}  %{app_dir_softlink}

echo "builds new symbolic link for the app service"
# make a symbolic for the service file using the new created softlink
ln -sf %{app_destination}/%{app_name}/misc/%{app_name} %{app_service_softlink}

我正在尝试创建自己的RPM包,这里是SPEC文件,它在安装时正常工作(rpm -ivh app-2.1-6.el6.x86_64.rpm)或升级(rpm - Uvh app-2.1-7.el6.x86_64.rpm)或删除(rpm -e app-2.1-7.el6.x86_64.rpm)

For RPM package app-2.1-7.el6.x86_64.rpm, the version is 2.1 and release number is 7. 

我的问题是,无论我如何修改版本号,安装/升级/删除工作正常,但如果我将版本号修改为2.2或3.2,则以前的版本文件夹(/opt/app-2.1)将不能删除,任何人都可以帮助我,当我更新(-Uvh)RPM包时,我应该如何删除以前的版本文件夹(/opt/app-2.1)?

1 个答案:

答案 0 :(得分:1)

问题是你的包裹没有“拥有” 目录/opt/2.1目录。

就像tar一样,rpm会创建所有“缺失”的目录 为了在路径上安装内容。

但是在擦除时,rpm只会删除那些目录 在%files manifest中明确提到。

简短回答:     如果你想要rpm --erase删除目录路径,     在%files中提及。

较短的anser:     加         %dir / opt / app - %{version}     到%文件。如果目录为空(即所有其他文件     在/ opt / app - %{version}是“拥有”并且可以删除),     “own / opt / app =%{version}也将被删除。