RPM自动化打包脚本 - Mr.Ding

RPM自动化打包脚本

天天记事 710 / 2021-03-30 16:46:42


RPM创建方法(CentOS)

1. 环境安装
yum install rpmbuild

2. 设置rpm工作根目录
vim ~/.rpmmacros
%_topdir /opt/rpm-workspace

3. 设置环境变量
sudo vim /etc/profile
export RPM_WORKDIR=/opt/rpm-workspace

4. 初始化目录
rpmbuild test.spec

5. 编写编译脚本

#############################################################################################
## script start

#!/bin/sh

################################################
# create sepc file
bappname="$1"
bversion="$2"
barch="$3"
builddate=$(date "+%Y-%m-%d %H:%M:%S")

if [ "$RPM_WORKDIR" == "" ]; then
RPMDIR="`~`"
else
RPMDIR=$RPM_WORKDIR
fi
RELSDIR=$RPMDIR/RELEASE/`date +%Y-%m-%d`
RELSDIR=$RPMDIR/RELEASE

if [ -d $RELSDIR ]; then
mkdir -p $RELSDIR
fi

rpm_show_help() {
echo "Useage:"
echo " umsp-build <name> <version> <platform>"
}

function read_platform_files() {
for file in `ls $2`
do
if [ -d $2"/"$file ]; then
read_platform_files $1 $2"/"$file $3/$file
else
echo "%{prefix}"$3"/"$file >> $1
fi
done
}

function to_renameto_format_deb_name() {
barch=$2
bvers=$3
bname=$4

for filepath in $1
do
# filedirs=$(dirname ${filepath%/*})
if [ ! -d $RELSDIR ]; then
mkdir -p $RELSDIR
fi
tdebfile=$RELSDIR/$bname-$bvers-$barch-linux.deb
echo "deb: $tdebfile"
mv -f $filepath $tdebfile
done
}

function set_builder_info() {
tofile=$1
bname=$2
bvers=$3
barch=$4
bdate=$5

echo "Huws build detail" > $tofile
echo "-------------------------------------------" >> $tofile
echo "Name : $bname-$bvers-$barch" >> $tofile
echo "Version : $bvers" >> $tofile
echo "Arch : $barch" >> $tofile
echo "Datetime : $bdate" >> $tofile
echo "-------------------------------------------" >> $tofile
}

#now=`date +%Y-%m-%d %H:%M:%S`
function convert_one_from_rpm_to_deb() {
rpmfile=$1
barch=$2
bvers=$3
bname=$4
olddir=`pwd`
filename=${rpmfile##*/}
# filedirs=${rpmfile%/*}
nowdate=`date +%Y%m%d%H%M%S`
debtmpdir=$RPMDIR/RPMS/$nowdate

if [ ! -d $RELSDIR ]; then
mkdir -p $RELSDIR
fi

if [ ! -d $debtmpdir ]; then
mkdir -p $debtmpdir
fi

mv -f $rpmfile $debtmpdir/
cd $debtmpdir

if [ `uname -p` = 'x86_64' -a $barch = 'x86_64_xxxxx' ]; then
alien $filename
fi
mv -f $filename $RELSDIR/$bname-$bvers-$barch-linux.rpm
cd $olddir

if [ `uname -p` = 'x86_64' -a $barch = 'x86_64_xxxx' ]; then
to_renameto_format_deb_name `ls $debtmpdir/*.deb` $barch $bvers $bname
fi
echo "clear: $debtmpdir"
rm -rf $debtmpdir
}

function convert_from_rpm_to_deb() {
rpmexpr=$1
barch=$2
bvers=$3
bname=$4
for file in `ls $rpmexpr`
do
convert_one_from_rpm_to_deb $file $barch $bvers $bname
done
rm -rf "$RPMDIR/RPMS/$barch/"
}

#create spec file
#args: specfile, reldir, appname, relversion, relplatform
function create_rpm_spec_file() {
specfile=$1
reldir=$2
relname=$3
relversion=$4
relplatform=$5
echo "Name: $relname" > $specfile
echo "Version: $relversion" >> $specfile
echo "Release: 1%{?dist}" >> $specfile
# echo "Release: 1" >> $specfile
echo "Summary: $relname for linux %{_target}" >> $specfile

echo "" >> $specfile
echo "Group: UMSP R & D department" >> $specfile
echo "License: HCPL" >> $specfile
echo "URL: https://www.umsp.cn/" >> $specfile
echo "Source0: %{name}-%{version}-%{_target}.tar.gz" >> $specfile
echo "BuildRoot: %{_tmppath}/%{name}-%{version}" >> $specfile

echo "" >> $specfile
echo "Prefix: /opt/%{name}-%{version}" >> $specfile
echo "%define debug_package %{nil}" >> $specfile

echo "" >> $specfile
echo "%description" >> $specfile
echo "$relname for linux %{_target}" >> $specfile

echo "" >> $specfile
echo "%prep" >> $specfile
echo "rm -rf %{buildroot}" >> $specfile
echo "rm -rf %{_builddir}/%{name}-%{version}" >> $specfile
echo "%setup -b 0" >> $specfile

echo "" >> $specfile
echo "%install" >> $specfile
echo "mkdir -p %{buildroot}/%{prefix}" >> $specfile
echo "cp -rf * %{buildroot}/%{prefix}/" >> $specfile

echo "" >> $specfile
echo "%files" >> $specfile
read_platform_files "$specfile" "$reldir"

echo "" >> $specfile
echo "%clean" >> $specfile
echo "rm -rf %{buildroot}" >> $specfile
echo "rm -rf %{_builddir}/%{name}-%{version}" >> $specfile
echo "" >> $specfile
}

_tmpdir=`pwd`
cd ~
basedir=`pwd`
cd $_tmpdir

if [ -z "$bappname" ]; then
# echo usepage:
# echo " $0 <name> <version> <platform>"
rpm_show_help
exit
fi

if [ -z "$bversion" ]; then
# echo usepage:
# echo " $0 <name> <version> <platform>"
rpm_show_help
exit
fi

if [ -z "$barch" ]; then
barch="ALL"
elif [ "mips64" = "$barch" -o "aarch64" = "$barch" -o "x86_64" = "$barch" -o "amd64" = "$barch" ]; then
echo "Check platform ok"
else
echo "Support only for: aarch64|mips64|x86_64|amd64"
exit
fi


specfile="$RPMDIR/SPECS/build-spec.spec"

#####################################################
# begin create spec file

function build_task() {
bname=$1
bvers=$2
barch=$3

echo "building -> $bappname-$bversion-$barch ..."
if [ ! -d $RPMDIR/SOURCES/$barch/$bname-$bvers ]; then
echo "source dir is not exits: "$RPMDIR/SOURCES/$barch/$bname-$bvers
exit
fi
oldpath=`pwd`
cd $RPMDIR/SOURCES/$barch/
rm -rf ../$bname-$bvers-$barch-linux.tar.gz
set_builder_info "$bname-$bvers/build.readme" $bname $bvers $barch "$builddate"
tar zcvf ../$bname-$bvers-$barch-linux.tar.gz $bname-$bvers/
cd $oldpath

create_rpm_spec_file \
$specfile \
$RPMDIR/SOURCES/$barch/$bname-$bvers \
$bname \
$bvers \
$barch
rpmbuild -bb --target=$barch $specfile

convert_from_rpm_to_deb "$RPMDIR/RPMS/$barch/$bname-$bvers-*$barch.rpm" \
$barch \
$bvers \
$bname

mv -f $RPMDIR/SOURCES/$bname-$bvers-$barch-linux.tar.gz $RELSDIR/
rm -rf $specfile
}

function build_amd_task() {
bname=$1
bvers=$2
barch=$3

echo "building -> $bappname-$bversion-$barch ..."
if [ ! -d $RPMDIR/SOURCES/$barch/$bname-$bvers ]; then
echo "source dir is not exits: "$RPMDIR/SOURCES/$barch/$bname-$bvers
exit
fi
oldpath=`pwd`
cd $RPMDIR/SOURCES/$barch/
rm -rf ../$bname-$bvers-$barch-window.tar.gz
rm -rf ../$bname-$bvers-$barch-window.zip
set_builder_info "$bname-$bvers/build.readme" $bname $bvers $barch "$builddate"
tar zcvf ../$bname-$bvers-$barch-window.tar.gz $bname-$bvers/
zip -r ../$bname-$bvers-$barch-window.zip $bname-$bvers/
cd $oldpath

mv -f $RPMDIR/SOURCES/$bname-$bvers-$barch-window.tar.gz $RELSDIR/
mv -f $RPMDIR/SOURCES/$bname-$bvers-$barch-window.zip $RELSDIR/
rm -rf $specfile
}

if [ "$barch" = "ALL" ]; then
build_task $bappname $bversion "x86_64"
build_task $bappname $bversion "aarch64"
build_task $bappname $bversion "mips64"
build_amd_task $bappname $bversion "amd64"
elif [ "$barch" = "amd64" ]; then
build_task $bappname $bversion $barch
else
build_task $bappname $bversion $barch
fi

echo "build finished."


## script end
#############################################################################################

rpmshell