Skip to content

Commit 21af6ac

Browse files
committed
RPM packaging scripts revamped
Signed-off-by: ashish-jabble <[email protected]>
1 parent 5521366 commit 21af6ac

File tree

1 file changed

+119
-62
lines changed

1 file changed

+119
-62
lines changed

packages/RPM/SPECS/fledge.spec

Lines changed: 119 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,30 @@ AutoReqProv: no
1919
%description
2020
Fledge, the open source platform for the Internet of Things
2121

22+
23+
## -------------------------------------------------------------------------------------------------
24+
## Scriptlet values which we must use in our scripts
25+
26+
## scriptlet install upgrade uninstall
27+
## %pre $1 == 1 $1 == 2 (N/A)
28+
## %post $1 == 1 $1 == 2 (N/A)
29+
## %preun (N/A) $1 == 1 $1 == 0
30+
## %postun (N/A) $1 == 1 $1 == 0
31+
32+
33+
## On upgrade, the scripts are run in the following order:
34+
35+
## %pre of new package
36+
## %post of new package
37+
## %preun of old package
38+
## %postun of old package
39+
40+
## --------------------------------------------------------------------------------------------------
41+
2242
%pre
2343
#!/usr/bin/env bash
2444

25-
##--------------------------------------------------------------------
45+
##---------------------------------------------------------------------------------------------------
2646
## Copyright (c) 2019 Dianomic Systems Inc.
2747
##
2848
## Licensed under the Apache License, Version 2.0 (the "License");
@@ -36,23 +56,23 @@ Fledge, the open source platform for the Internet of Things
3656
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
3757
## See the License for the specific language governing permissions and
3858
## limitations under the License.
39-
##--------------------------------------------------------------------
59+
##---------------------------------------------------------------------------------------------------
4060

41-
##--------------------------------------------------------------------
61+
##---------------------------------------------------------------------------------------------------
4262
##
43-
## This script is used to execute pre installation tasks.
63+
## The %pre scriptlet executes just before the package is to be installed.
4464
##
45-
## Author: Ivan Zoratti, Ashwin Gopalakrishnan, Massimiliano Pinto, Stefano Simonelli
65+
## Author: Ivan Zoratti, Ashwin Gopalakrishnan, Massimiliano Pinto, Stefano Simonelli, Ashish Jabble
4666
##
47-
##--------------------------------------------------------------------
67+
##---------------------------------------------------------------------------------------------------
4868

4969
set -e
5070

5171
PKG_NAME="fledge"
5272

5373
is_fledge_installed () {
5474
set +e
55-
rc=`rpm -qa 2> /dev/null | grep -Fx ${PKG_NAME}`
75+
rc=$(rpm -ql ${PKG_NAME} | grep 'fledge/bin/fledge$')
5676
echo $rc
5777
set -e
5878
}
@@ -86,29 +106,59 @@ exists_schema_change_path () {
86106
echo 1
87107
}
88108

89-
# main
109+
stop_fledge_service () {
110+
systemctl stop fledge
111+
}
90112

91-
# check if fledge is installed
92-
IS_FLEDGE_INSTALLED=$(is_fledge_installed)
113+
kill_fledge () {
114+
set +e
115+
fledge_script=$(get_fledge_script)
116+
fledge_status_output=$($fledge_script kill 2>&1)
117+
set -e
118+
}
93119

94-
# if fledge is installed...
95-
if [ "$IS_FLEDGE_INSTALLED" -eq "1" ]
96-
then
97-
echo "Fledge is already installed: this is an upgrade/downgrade."
120+
disable_fledge_service () {
121+
set +e
122+
/sbin/chkconfig fledge off
123+
set -e
124+
}
98125

99-
# exit if fledge is running
126+
remove_fledge_service_file () {
127+
rm -rf /etc/init.d/fledge
128+
}
129+
130+
reset_systemctl () {
131+
systemctl daemon-reload
132+
systemctl reset-failed
133+
}
134+
135+
# main
136+
if [ $1 == 1 ];then
137+
echo "pre step: ${PKG_NAME} is getting installed."
138+
elif [ $1 == 2 ];then
139+
echo "preun step: ${PKG_NAME} is getting upgraded"
100140
IS_FLEDGE_RUNNING=$(is_fledge_running)
101141
if [ "$IS_FLEDGE_RUNNING" -eq "1" ]
102142
then
103-
echo "*** ERROR. Fledge is currently running. Stop Fledge and try again. ***"
104-
exit 1
143+
echo "${PKG_NAME} is currently running."
144+
echo "Stop ${PKG_NAME} service."
145+
stop_fledge_service
146+
echo "Kill ${PKG_NAME}."
147+
kill_fledge
105148
fi
106149

150+
echo "Disable ${PKG_NAME} service."
151+
disable_fledge_service
152+
echo "Remove ${PKG_NAME} service script"
153+
remove_fledge_service_file
154+
echo "Reset systemctl"
155+
reset_systemctl
156+
107157
# Persist current version in case of upgrade/downgrade
108158
installed_version=`rpm -qi ${PKG_NAME} | grep Version |awk '{print $3}'`
109159
if [ "${installed_version}" ]
110160
then
111-
# Persist current Fledge version: it will be removed by postinstall script
161+
# Persist current ${PKG_NAME} version: it will be removed by postinstall script
112162
this_dir=`pwd`
113163
cd /usr/local/fledge/
114164
echo "${installed_version}" > .current_installed_version
@@ -118,22 +168,20 @@ then
118168
# check schema version file, exit if schema change path does not exist
119169
CURRENT_VERSION_FILE=$(get_current_version_file)
120170
CURRENT_SCHEMA_VERSION=$(get_schema_version $CURRENT_VERSION_FILE)
121-
echo "Fledge currently has schema version $CURRENT_SCHEMA_VERSION"
171+
echo "${PKG_NAME} currently has schema version $CURRENT_SCHEMA_VERSION"
122172
EXISTS_SCHEMA_CHANGE_PATH=$(exists_schema_change_path)
123173
if [ "$EXISTS_SCHEMA_CHANGE_PATH" -eq "0" ]
124174
then
125175
echo "*** ERROR. There is no schema change path from the installed version to the new version. ***"
126176
exit 1
127177
fi
128-
129178
fi
130179

131180

132-
133181
%preun
134182
#!/usr/bin/env bash
135183

136-
##--------------------------------------------------------------------
184+
##--------------------------------------------------------------------------------
137185
## Copyright (c) 2019 Dianomic Systems Inc.
138186
##
139187
## Licensed under the Apache License, Version 2.0 (the "License");
@@ -147,15 +195,15 @@ fi
147195
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
148196
## See the License for the specific language governing permissions and
149197
## limitations under the License.
150-
##--------------------------------------------------------------------
198+
##--------------------------------------------------------------------------------
151199

152-
##--------------------------------------------------------------------
200+
##--------------------------------------------------------------------------------
153201
##
154-
## This script is used to execute before the removal of files associated with the package.
202+
## The %preun scriptlet executes just before the package is to be erased.
155203
##
156-
## Author: Ivan Zoratti, Ashwin Gopalakrishnan, Stefano Simonelli
204+
## Author: Ivan Zoratti, Ashwin Gopalakrishnan, Stefano Simonelli, Ashish Jabble
157205
##
158-
##----------------------------------------------------------------------------------------
206+
##--------------------------------------------------------------------------------
159207

160208
set -e
161209

@@ -202,30 +250,33 @@ reset_systemctl () {
202250
}
203251

204252
# main
253+
if [ $1 == 1 ];then
254+
echo "preun step: ${PKG_NAME} is getting upgraded. But Nothing to upgrade."
255+
elif [ $1 == 0 ];then
256+
echo "preun step: ${PKG_NAME} is getting removed/uninstalled"
257+
IS_FLEDGE_RUNNING=$(is_fledge_running)
258+
if [ "$IS_FLEDGE_RUNNING" -eq "1" ]
259+
then
260+
echo "${PKG_NAME} is currently running."
261+
echo "Stop ${PKG_NAME} service."
262+
stop_fledge_service
263+
echo "Kill ${PKG_NAME}."
264+
kill_fledge
265+
fi
205266

206-
IS_FLEDGE_RUNNING=$(is_fledge_running)
207-
208-
if [ "$IS_FLEDGE_RUNNING" -eq "1" ]
209-
then
210-
echo "Fledge is currently running."
211-
echo "Stop Fledge service."
212-
stop_fledge_service
213-
echo "Kill Fledge."
214-
kill_fledge
267+
echo "Disable ${PKG_NAME} service."
268+
disable_fledge_service
269+
echo "Remove ${PKG_NAME} service script"
270+
remove_fledge_service_file
271+
echo "Reset systemctl"
272+
reset_systemctl
215273
fi
216274

217-
echo "Disable Fledge service."
218-
disable_fledge_service
219-
echo "Remove Fledge service script"
220-
remove_fledge_service_file
221-
echo "Reset systemctl"
222-
reset_systemctl
223-
224275

225276
%post
226277
#!/usr/bin/env bash
227278

228-
##--------------------------------------------------------------------
279+
##----------------------------------------------------------------------------
229280
## Copyright (c) 2019 Dianomic Systems Inc.
230281
##
231282
## Licensed under the Apache License, Version 2.0 (the "License");
@@ -239,15 +290,15 @@ reset_systemctl
239290
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
240291
## See the License for the specific language governing permissions and
241292
## limitations under the License.
242-
##--------------------------------------------------------------------
293+
##----------------------------------------------------------------------------
243294

244-
##--------------------------------------------------------------------
295+
##----------------------------------------------------------------------------
245296
##
246-
## This script is used to execute post installation tasks.
297+
## The %post scriptlet executes just after the package is to be installed.
247298
##
248-
## Author: Ivan Zoratti, Massimiliano Pinto, Stefano Simonelli
299+
## Author: Ivan Zoratti, Massimiliano Pinto, Stefano Simonelli, Ashish Jabble
249300
##
250-
##--------------------------------------------------------------------
301+
##----------------------------------------------------------------------------
251302

252303
set -e
253304

@@ -274,7 +325,6 @@ copy_service_file() {
274325
}
275326

276327
enable_fledge_service() {
277-
278328
/sbin/chkconfig fledge on
279329
}
280330

@@ -351,9 +401,10 @@ install_pip3_packages () {
351401
fi
352402
source scl_source enable rh-python36
353403

354-
pip install -Ir /usr/local/fledge/python/requirements.txt
404+
# TODO: we may need with --no-cache-dir
405+
pip3 install -Ir /usr/local/fledge/python/requirements.txt
355406

356-
sudo bash -c 'source scl_source enable rh-python36;pip install dbus-python'
407+
sudo bash -c 'source scl_source enable rh-python36;pip3 install dbus-python'
357408
set -e
358409
}
359410

@@ -378,7 +429,6 @@ call_package_update_script () {
378429

379430

380431
# main
381-
382432
echo "Install python dependencies"
383433
install_pip3_packages
384434

@@ -418,7 +468,7 @@ start_fledge_service
418468
%postun
419469
#!/usr/bin/env bash
420470

421-
##--------------------------------------------------------------------
471+
##--------------------------------------------------------------------------
422472
## Copyright (c) 2019 Dianomic Systems Inc.
423473
##
424474
## Licensed under the Apache License, Version 2.0 (the "License");
@@ -432,17 +482,18 @@ start_fledge_service
432482
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
433483
## See the License for the specific language governing permissions and
434484
## limitations under the License.
435-
##--------------------------------------------------------------------
485+
##--------------------------------------------------------------------------
436486

437-
##--------------------------------------------------------------------
487+
##--------------------------------------------------------------------------
438488
##
439-
## This script is used to modifies links or other files associated with fledge, and/or removes files created by the package.
489+
## The %postun scriptlet executes just after the package is to be erased.
440490
##
441491
## Author: Ashish Jabble
442492
##
443-
##--------------------------------------------------------------------
493+
##--------------------------------------------------------------------------
444494

445495
set -e
496+
PKG_NAME="fledge"
446497

447498
remove_unused_files () {
448499
find /usr/local/fledge/ -maxdepth 1 -mindepth 1 -type d | egrep -v -w '(/usr/local/fledge/data)' | xargs rm -rf
@@ -452,11 +503,17 @@ remove_fledge_sudoer_file() {
452503
rm -rf /etc/sudoers.d/fledge
453504
}
454505

455-
echo "Cleanup of files"
456-
remove_unused_files
506+
# main
507+
if [ $1 == 1 ];then
508+
echo "postun step: ${PKG_NAME} is getting upgraded. But nothing to upgrade."
509+
elif [ $1 == 0 ];then
510+
echo "postun step: ${PKG_NAME} is getting removed/uninstalled"
511+
echo "Cleanup of files"
512+
remove_unused_files
513+
echo "Remove fledge sudoers file"
514+
remove_fledge_sudoer_file
515+
fi
457516

458-
echo "Remove fledge sudoers file"
459-
remove_fledge_sudoer_file
460517

461518
%files
462519
%{install_path}/*

0 commit comments

Comments
 (0)