Skip to content

Commit fa67fde

Browse files
swordqiuQiu Jian
andauthored
fix: rpm/deb support postinst/preinst/prerm/postrm hooks (#23594)
Co-authored-by: Qiu Jian <[email protected]>
1 parent 9e2beac commit fa67fde

File tree

2 files changed

+120
-37
lines changed

2 files changed

+120
-37
lines changed

build/build.sh

Lines changed: 41 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -110,35 +110,68 @@ if [ -d $ROOT/root ]; then
110110
rsync -a $ROOT/root/ \$RPM_BUILD_ROOT
111111
fi
112112
113-
%pre
113+
%pre" > $SPEC_FILE
114+
115+
if [ -f $ROOT/preinst ]; then
116+
cat $ROOT/preinst >> $SPEC_FILE
117+
else
118+
echo "
114119
%if %{use_systemd}
115120
getent group %{owner} >/dev/null || /usr/sbin/groupadd -r %{owner}
116121
getent passwd %{owner} >/dev/null || /usr/sbin/useradd -r -s /sbin/nologin -d %{homedir} -M -g %{owner} %{owner}
117122
%endif
123+
" >> $SPEC_FILE
124+
fi
125+
126+
echo "
127+
%post" >> $SPEC_FILE
118128

119-
%post
129+
if [ -f $ROOT/postinst ]; then
130+
cat $ROOT/postinst >> $SPEC_FILE
131+
else
132+
echo "
120133
%if %{use_systemd}
121-
mkdir -p /var/run/%{owner}
122-
chown -R %{owner}:%{owner} /var/run/%{owner}
123134
/usr/bin/systemctl preset %{pkgname}.service >/dev/null 2>&1 ||:
124135
%endif
136+
" >> $SPEC_FILE
137+
fi
138+
139+
echo "
140+
%preun" >> $SPEC_FILE
125141

126-
%preun
142+
if [ -f $ROOT/prerm ]; then
143+
cat $ROOT/prerm >> $SPEC_FILE
144+
else
145+
echo "
127146
%if %{use_systemd}
128147
/usr/bin/systemctl --no-reload disable %{pkgname}.service >/dev/null 2>&1 || :
129148
/usr/bin/systemctl stop %{pkgname}.service >/dev/null 2>&1 ||:
130149
%endif
150+
" >> $SPEC_FILE
151+
fi
152+
153+
echo "
154+
%postun" >> $SPEC_FILE
131155

132-
%postun
156+
if [ -f $ROOT/postrm ]; then
157+
cat $ROOT/postrm >> $SPEC_FILE
158+
else
159+
echo "
133160
%if %{use_systemd}
134161
/usr/bin/systemctl daemon-reload >/dev/null 2>&1 ||:
135162
%endif
163+
" >> $SPEC_FILE
164+
fi
136165

166+
echo -n "
137167
%files
138168
%doc
139169
$BIN_PATH/$PKG
140-
$(for b in $EXTRA_BINS; do echo $BIN_PATH/$b; done)
141-
" > $SPEC_FILE
170+
" >> $SPEC_FILE
171+
172+
for b in $EXTRA_BINS; do
173+
echo $BIN_PATH/$b >> $SPEC_FILE
174+
done
142175

143176
if [ -d $ROOT/root/ ]; then
144177
find $ROOT/root/ -type f | sed -e "s:$ROOT/root::g" >> $SPEC_FILE

build/build_deb.sh

Lines changed: 79 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,26 @@ fi
3535

3636
. $ROOT/vars
3737

38+
case $(uname -m) in
39+
x86_64)
40+
CURRENT_ARCH=amd64
41+
;;
42+
aarch64)
43+
CURRENT_ARCH=arm64
44+
;;
45+
esac
46+
47+
if [[ -n "$GOARCH" ]]; then
48+
case "$GOARCH" in
49+
"arm64" | "arm" | "aarch64")
50+
CURRENT_ARCH="arm64"
51+
;;
52+
"x86" | "x86_64" | "i686" | "i386" | "amd64")
53+
CURRENT_ARCH="amd64"
54+
;;
55+
esac
56+
fi
57+
3858
if [ -z "$VERSION" ]; then
3959
TAG=$(git describe --abbrev=0 --tags || echo 000000)
4060
VERSION=${TAG/\//-}
@@ -43,7 +63,7 @@ if [ -z "$VERSION" ]; then
4363
fi
4464
RELEASE=`date +"%y%m%d%H"`
4565
FULL_VERSION=$VERSION-$RELEASE
46-
BUILDROOT=$OUTPUT_DIR/yunion-$PKG-$FULL_VERSION
66+
BUILDROOT=$OUTPUT_DIR/yunion-${PKG}-${FULL_VERSION}_${CURRENT_ARCH}
4767
function cleanup {
4868
rm -rf "$BUILDROOT"
4969
echo "Deleted temp working directory $BUILDROOT"
@@ -60,29 +80,8 @@ if [ -d $ROOT/root ]; then
6080
cp -rf $ROOT/root/* $BUILDROOT/
6181
fi
6282

63-
6483
echo "Build root ${BUILDROOT}"
6584

66-
case $(uname -m) in
67-
x86_64)
68-
CURRENT_ARCH=amd64
69-
;;
70-
aarch64)
71-
CURRENT_ARCH=arm64
72-
;;
73-
esac
74-
75-
if [[ -n "$GOARCH" ]]; then
76-
case "$GOARCH" in
77-
"arm64" | "arm" | "aarch64")
78-
CURRENT_ARCH="arm64"
79-
;;
80-
"x86" | "x86_64" | "i686" | "i386" | "amd64")
81-
CURRENT_ARCH="amd64"
82-
;;
83-
esac
84-
fi
85-
8685
echo "Package: yunion-$PKG
8786
Version: $FULL_VERSION
8887
Section: base
@@ -98,19 +97,70 @@ chmod 0755 $BUILDROOT/DEBIAN/control
9897

9998
cat $BUILDROOT/DEBIAN/control
10099

101-
if [ -n "$SERVICE" ]; then
102100
echo "#!/bin/bash
103-
104-
/usr/bin/systemctl --no-reload disable yunion-${PKG}.service >/dev/null 2>&1 || :
105-
/usr/bin/systemctl stop yunion-${PKG}.service >/dev/null 2>&1 ||:
106101
" > $BUILDROOT/DEBIAN/preinst
102+
if [ -f $ROOT/preinst ]; then
103+
cat $ROOT/preinst >> $BUILDROOT/DEBIAN/preinst
104+
else
105+
if [ "$SERVICE" == "yes" ] && [ -n "$OWNER" ]; then
106+
echo "
107+
getent group ${OWNER} >/dev/null || /usr/sbin/groupadd -r ${OWNER}
108+
getent passwd ${OWNER} >/dev/null || /usr/sbin/useradd -r -s /sbin/nologin -d /home/${OWNER} -M -g ${OWNER} ${OWNER}
109+
" >> $BUILDROOT/DEBIAN/preinst
110+
fi
111+
fi
107112
chmod 0755 $BUILDROOT/DEBIAN/preinst
108-
echo "#!/bin/bash
109113

110-
/usr/bin/systemctl preset yunion-${PKG}.service >/dev/null 2>&1 ||:
111-
/usr/bin/systemctl daemon-reload >/dev/null 2>&1 ||:
114+
echo "#!/bin/bash
112115
" > $BUILDROOT/DEBIAN/postinst
116+
if [ -f $ROOT/postinst ]; then
117+
cat $ROOT/postinst >> $BUILDROOT/DEBIAN/postinst
118+
else
119+
if [ "$SERVICE" == "yes" ]; then
120+
echo "
121+
/usr/bin/systemctl preset yunion-${PKG}.service >/dev/null 2>&1 ||:
122+
" >> $BUILDROOT/DEBIAN/postinst
123+
fi
124+
fi
113125
chmod 0755 $BUILDROOT/DEBIAN/postinst
126+
127+
echo "#!/bin/bash
128+
" > $BUILDROOT/DEBIAN/prerm
129+
if [ -f $ROOT/prerm ]; then
130+
cat $ROOT/prerm >> $BUILDROOT/DEBIAN/prerm
131+
else
132+
if [ "$SERVICE" == "yes" ]; then
133+
echo "
134+
/usr/bin/systemctl --no-reload disable yunion-${PKG}.service >/dev/null 2>&1 || :
135+
/usr/bin/systemctl stop yunion-${PKG}.service >/dev/null 2>&1 ||:
136+
" >> $BUILDROOT/DEBIAN/prerm
137+
fi
138+
fi
139+
chmod 0755 $BUILDROOT/DEBIAN/prerm
140+
141+
echo "#!/bin/bash
142+
" > $BUILDROOT/DEBIAN/postrm
143+
if [ -f $ROOT/postrm ]; then
144+
cat $ROOT/postrm >> $BUILDROOT/DEBIAN/postrm
145+
else
146+
if [ "$SERVICE" == "yes" ]; then
147+
echo "
148+
/usr/bin/systemctl daemon-reload >/dev/null 2>&1 ||:
149+
" >> $BUILDROOT/DEBIAN/postrm
150+
fi
114151
fi
152+
chmod 0755 $BUILDROOT/DEBIAN/postrm
115153

116154
dpkg-deb --build $BUILDROOT
155+
156+
case "$CURRENT_ARCH" in
157+
"amd64")
158+
DSTARCH="x86_64"
159+
;;
160+
"arm64")
161+
DSTARCH="aarch64"
162+
;;
163+
esac
164+
165+
mkdir -p ${OUTPUT_DIR}/${DSTARCH}
166+
mv ${BUILDROOT}.deb ${OUTPUT_DIR}/${DSTARCH}/

0 commit comments

Comments
 (0)