fix: rpm/deb support postinst/preinst/prerm/postrm hooks (#23595)

Co-authored-by: Qiu Jian <qiujian@yunionyun.com>
This commit is contained in:
Jian Qiu
2025-10-23 01:03:33 +08:00
committed by GitHub
parent d74b732b86
commit ea51d3f05f
2 changed files with 128 additions and 45 deletions
+41 -8
View File
@@ -110,35 +110,68 @@ if [ -d $ROOT/root ]; then
rsync -a $ROOT/root/ \$RPM_BUILD_ROOT
fi
%pre
%pre" > $SPEC_FILE
if [ -f $ROOT/preinst ]; then
cat $ROOT/preinst >> $SPEC_FILE
else
echo "
%if %{use_systemd}
getent group %{owner} >/dev/null || /usr/sbin/groupadd -r %{owner}
getent passwd %{owner} >/dev/null || /usr/sbin/useradd -r -s /sbin/nologin -d %{homedir} -M -g %{owner} %{owner}
%endif
" >> $SPEC_FILE
fi
%post
echo "
%post" >> $SPEC_FILE
if [ -f $ROOT/postinst ]; then
cat $ROOT/postinst >> $SPEC_FILE
else
echo "
%if %{use_systemd}
mkdir -p /var/run/%{owner}
chown -R %{owner}:%{owner} /var/run/%{owner}
/usr/bin/systemctl preset %{pkgname}.service >/dev/null 2>&1 ||:
%endif
" >> $SPEC_FILE
fi
%preun
echo "
%preun" >> $SPEC_FILE
if [ -f $ROOT/prerm ]; then
cat $ROOT/prerm >> $SPEC_FILE
else
echo "
%if %{use_systemd}
/usr/bin/systemctl --no-reload disable %{pkgname}.service >/dev/null 2>&1 || :
/usr/bin/systemctl stop %{pkgname}.service >/dev/null 2>&1 ||:
%endif
" >> $SPEC_FILE
fi
%postun
echo "
%postun" >> $SPEC_FILE
if [ -f $ROOT/postrm ]; then
cat $ROOT/postrm >> $SPEC_FILE
else
echo "
%if %{use_systemd}
/usr/bin/systemctl daemon-reload >/dev/null 2>&1 ||:
%endif
" >> $SPEC_FILE
fi
echo -n "
%files
%doc
$BIN_PATH/$PKG
$(for b in $EXTRA_BINS; do echo $BIN_PATH/$b; done)
" > $SPEC_FILE
" >> $SPEC_FILE
for b in $EXTRA_BINS; do
echo $BIN_PATH/$b >> $SPEC_FILE
done
if [ -d $ROOT/root/ ]; then
find $ROOT/root/ -type f | sed -e "s:$ROOT/root::g" >> $SPEC_FILE
+87 -37
View File
@@ -35,34 +35,6 @@ fi
. $ROOT/vars
if [ -z "$VERSION" ]; then
TAG=$(git describe --abbrev=0 --tags || echo 000000)
VERSION=${TAG/\//-}
VERSION=${VERSION/v/}
VERSION=${VERSION/master-/}
fi
RELEASE=`date +"%y%m%d%H"`
FULL_VERSION=$VERSION-$RELEASE
BUILDROOT=$OUTPUT_DIR/yunion-$PKG-$FULL_VERSION
function cleanup {
rm -rf "$BUILDROOT"
echo "Deleted temp working directory $BUILDROOT"
}
# register the cleanup function to be called on the EXIT signal
trap cleanup EXIT
rm -rf $BUILDROOT
mkdir -p $BUILDROOT/DEBIAN
mkdir -p $BUILDROOT/$BIN_PATH
cp -rf $BIN $BUILDROOT/$BIN_PATH
if [ -d $ROOT/root ]; then
cp -rf $ROOT/root/* $BUILDROOT/
fi
echo "Build root ${BUILDROOT}"
case $(uname -m) in
x86_64)
CURRENT_ARCH=amd64
@@ -83,6 +55,33 @@ if [[ -n "$GOARCH" ]]; then
esac
fi
if [ -z "$VERSION" ]; then
TAG=$(git describe --abbrev=0 --tags || echo 000000)
VERSION=${TAG/\//-}
VERSION=${VERSION/v/}
VERSION=${VERSION/master-/}
fi
RELEASE=`date +"%y%m%d%H"`
FULL_VERSION=$VERSION-$RELEASE
BUILDROOT=$OUTPUT_DIR/yunion-${PKG}-${FULL_VERSION}_${CURRENT_ARCH}
function cleanup {
rm -rf "$BUILDROOT"
echo "Deleted temp working directory $BUILDROOT"
}
# register the cleanup function to be called on the EXIT signal
trap cleanup EXIT
rm -rf $BUILDROOT
mkdir -p $BUILDROOT/DEBIAN
mkdir -p $BUILDROOT/$BIN_PATH
cp -rf $BIN $BUILDROOT/$BIN_PATH
if [ -d $ROOT/root ]; then
cp -rf $ROOT/root/* $BUILDROOT/
fi
echo "Build root ${BUILDROOT}"
echo "Package: yunion-$PKG
Version: $FULL_VERSION
Section: base
@@ -98,19 +97,70 @@ chmod 0755 $BUILDROOT/DEBIAN/control
cat $BUILDROOT/DEBIAN/control
if [ -n "$SERVICE" ]; then
echo "#!/bin/bash
" > $BUILDROOT/DEBIAN/preinst
if [ -f $ROOT/preinst ]; then
cat $ROOT/preinst >> $BUILDROOT/DEBIAN/preinst
else
if [ "$SERVICE" == "yes" ] && [ -n "$OWNER" ]; then
echo "
getent group ${OWNER} >/dev/null || /usr/sbin/groupadd -r ${OWNER}
getent passwd ${OWNER} >/dev/null || /usr/sbin/useradd -r -s /sbin/nologin -d /home/${OWNER} -M -g ${OWNER} ${OWNER}
" >> $BUILDROOT/DEBIAN/preinst
fi
fi
chmod 0755 $BUILDROOT/DEBIAN/preinst
echo "#!/bin/bash
" > $BUILDROOT/DEBIAN/postinst
if [ -f $ROOT/postinst ]; then
cat $ROOT/postinst >> $BUILDROOT/DEBIAN/postinst
else
if [ "$SERVICE" == "yes" ]; then
echo "
/usr/bin/systemctl preset yunion-${PKG}.service >/dev/null 2>&1 ||:
" >> $BUILDROOT/DEBIAN/postinst
fi
fi
chmod 0755 $BUILDROOT/DEBIAN/postinst
echo "#!/bin/bash
" > $BUILDROOT/DEBIAN/prerm
if [ -f $ROOT/prerm ]; then
cat $ROOT/prerm >> $BUILDROOT/DEBIAN/prerm
else
if [ "$SERVICE" == "yes" ]; then
echo "
/usr/bin/systemctl --no-reload disable yunion-${PKG}.service >/dev/null 2>&1 || :
/usr/bin/systemctl stop yunion-${PKG}.service >/dev/null 2>&1 ||:
" > $BUILDROOT/DEBIAN/preinst
chmod 0755 $BUILDROOT/DEBIAN/preinst
echo "#!/bin/bash
/usr/bin/systemctl preset yunion-${PKG}.service >/dev/null 2>&1 ||:
/usr/bin/systemctl daemon-reload >/dev/null 2>&1 ||:
" > $BUILDROOT/DEBIAN/postinst
chmod 0755 $BUILDROOT/DEBIAN/postinst
" >> $BUILDROOT/DEBIAN/prerm
fi
fi
chmod 0755 $BUILDROOT/DEBIAN/prerm
echo "#!/bin/bash
" > $BUILDROOT/DEBIAN/postrm
if [ -f $ROOT/postrm ]; then
cat $ROOT/postrm >> $BUILDROOT/DEBIAN/postrm
else
if [ "$SERVICE" == "yes" ]; then
echo "
/usr/bin/systemctl daemon-reload >/dev/null 2>&1 ||:
" >> $BUILDROOT/DEBIAN/postrm
fi
fi
chmod 0755 $BUILDROOT/DEBIAN/postrm
dpkg-deb --build $BUILDROOT
case "$CURRENT_ARCH" in
"amd64")
DSTARCH="x86_64"
;;
"arm64")
DSTARCH="aarch64"
;;
esac
mkdir -p ${OUTPUT_DIR}/${DSTARCH}
mv ${BUILDROOT}.deb ${OUTPUT_DIR}/${DSTARCH}/