-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmake_rpm
executable file
·268 lines (225 loc) · 8.17 KB
/
make_rpm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
#!/usr/bin/env bash
##--------------------------------------------------------------------
## Copyright (c) 2019 OSIsoft, LLC
##
## Licensed under the Apache License, Version 2.0 (the "License");
## you may not use this file except in compliance with the License.
## You may obtain a copy of the License at
##
## http://www.apache.org/licenses/LICENSE-2.0
##
## Unless required by applicable law or agreed to in writing, software
## distributed under the License is distributed on an "AS IS" BASIS,
## WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
## See the License for the specific language governing permissions and
## limitations under the License.
##--------------------------------------------------------------------
##
## Author: Ivan Zoratti, Ashish Jabble, Stefano Simonelli
##
set -e
PKG_ROOT=$(pwd) # The script must be executed from the root git directory
repo_name="fledge" # Name of the Git repository
branch="main" # Default Git branch to use
pkg_name="fledge" # Name of the package to build
architecture="x86_64" # The architecture for which the rpm should be created
skip_build=0 # 1=skip Git repo extraction and Fledge build
# Check OS Name and OS Version
os_name=$(grep -o '^NAME=.*' /etc/os-release | cut -f2 -d\" | sed 's/"//g')
os_version=$(grep -o '^VERSION_ID=.*' /etc/os-release | cut -f2 -d\" | sed 's/"//g')
usage="$(basename "$0") [-h] [-c] [-a] [-s] [-b <branch>]
This script is used to create the RPM package of Fledge
Arguments:
-h - Display this help text
-c - Remove all the old versions saved in format .XXXX
-a - Remove all the versions, including the last one
-s - Skip Fledge building using the binaries already available
-b - Branch to base package on"
while getopts ":hcasb:" opt; do
case "$opt" in
h)
echo "${usage}"
exit 0
;;
c)
if [ -d "${GIT_ROOT}/packages/RPM/build" ]; then
echo -n "Cleaning the build folder from older versions..."
find "${PKG_ROOT}/packages/RPM/build/BUILDROOT" -maxdepth 1 | grep '.*\.[0-9][0-9][0-9][0-9]' | sudo xargs rm -rf
find "${PKG_ROOT}/packages/RPM/build/RPMS/${architecture}" -maxdepth 1 | grep '.*\.[0-9][0-9][0-9][0-9]' | sudo xargs rm -rf
echo "Done."
else
echo "No build folder, skipping clean old versions"
fi
exit 0
;;
a)
if [ -d "${PKG_ROOT}/packages/RPM/build" ]; then
echo -n "Cleaning the build folder..."
sudo rm -rf ${PKG_ROOT}/packages/RPM/build/*
echo "Done."
else
echo "No build folder, skipping cleanall"
fi
exit 0
;;
s)
skip_build=1
;;
b)
branch=$OPTARG
;;
\?)
echo "Invalid option -$OPTARG"
exit 1
;;
:)
echo "-$OPTARG requires an argument"
exit 1
esac
done
if [ ! -f packages/RPM/SPECS/fledge.spec ] ; then
echo You must run this script from the fledge-pkg directory
exit 1
fi
#
# Extracts/updates Git repository
#
if [[ $skip_build == 0 ]]; then
cd /tmp
if [ -d "${repo_name}" ]; then
echo WARNING: Repository ${repo_name} already exists, using the existing copy
(cd ${repo_name}; git fetch --all; git pull ; git checkout "$branch")
else
git clone -b "$branch" https://github.com/fledge-iot/${repo_name}.git
fi
GIT_ROOT=/tmp/"${repo_name}"
cd ${GIT_ROOT}
git_tag_info=$(cd ${GIT_ROOT} && git describe --tags | sed 's/-/_/g') && commit_count=$(echo ${git_tag_info} | cut -d_ -f2)
fi
#
# Builds Fledge
#
if [[ $skip_build == 0 ]]; then
# Backups FLEDGE_ROOT
export FLEDGE_ROOT_BCK=${FLEDGE_ROOT}
export FLEDGE_ROOT=${GIT_ROOT}
export FLEDGE_DATA=${FLEDGE_ROOT}/data
export PYTHONPATH=${FLEDGE_ROOT}/python
set +e
cd ${FLEDGE_ROOT}
sudo ./requirements.sh
# Use Red Hat packages and scl_source for postgres and newer g++ only if OS is RedHat/CentOS 7
if [[ ( $os_name == *"Red Hat"* || $os_name == *"CentOS"* ) && $os_version == *"7"* ]]
then
rhpg_pkg="rh-postgresql13" # Name of the Red Hat package for postgres
rhgcc_pkg="devtoolset-7" # Name of the Red Hat package for the newer g++
# Enables/verifies the environment for the build
source scl_source enable ${rhpg_pkg}
status_rh_postgresql=$?
if [[ $status_rh_postgresql != 0 ]]; then
echo "ERROR : it is not possible to enable the ${rhpg_pkg} environment"
exit 1
fi
source scl_source enable ${rhgcc_pkg}
status_devtoolset=$?
if [[ $status_devtoolset != 0 ]]; then
echo "ERROR : it is not possible to enable the ${rhgcc_pkg} environment"
exit 1
fi
fi
command -v pg_isready > /dev/null
status_pg_isready=$?
if [[ $status_pg_isready != 0 ]]; then
echo "ERROR : the command pg_isready is not available"
exit 1
fi
set -e
make
sudo make install
# Restores FLEDGE_ROOT
export FLEDGE_ROOT=${FLEDGE_ROOT_BCK}
fi
# Checks if the FLEDGE_ROOT environment is defined
if [[ "${FLEDGE_ROOT}" == "" ]]; then
# Set FLEDGE_ROOT as the default directory
export FLEDGE_ROOT="/usr/local/fledge"
fi
if [[ ! -d "${FLEDGE_ROOT}" ]]; then
echo "No FLEDGE_ROOT directory found in the path ${FLEDGE_ROOT} - Program exit."
exit 1
fi
version=`cat ${FLEDGE_ROOT}/VERSION | tr -d ' ' | grep 'fledge_version=' | head -1 | sed -e 's/\(.*\)=\(.*\)/\2/g'`
BUILD_ROOT="${PKG_ROOT}/packages/RPM/build/BUILDROOT"
mkdir -p ${PKG_ROOT}/packages/RPM/build/{BUILD,BUILDROOT,RPMS,SOURCES,SPECS,SRPMS}
mkdir -p ${PKG_ROOT}/packages/RPM/build/RPMS/x86_64
if [[ $skip_build == 1 ]] || ([[ ${branch} = "main" ]] || [[ ${branch} =~ ^[0-9]+\.[0-9]+\.[0-9]+RC ]] || [[ ${branch} =~ ^v[0-9]+\.[0-9]+\.[0-9]+ ]]); then commit_count=1; fi
# Final package name
package_name="${pkg_name}-${version}-${commit_count}.${architecture}"
# Print the summary of findings
echo "The package root directory is : ${PKG_ROOT}"
echo "The Fledge directory is : ${FLEDGE_ROOT}"
echo "The Fledge version is : ${version}"
echo "The package will be built in : ${PKG_ROOT}/packages/RPM/build/RPMS/${architecture}"
echo "The package name is : ${package_name}"
echo
# Create the package directory. If a directory with the same name exists,
# it is copied with a version number.
# First, create the BUILD_ROOT folder, if necessary
if [ ! -L "${BUILD_ROOT}" -a ! -d "${BUILD_ROOT}" ]; then
mkdir -p "${BUILD_ROOT}"
fi
cd "${BUILD_ROOT}"
existing_pkgs=`find . -maxdepth 1 -name "${package_name}.????" | wc -l`
existing_pkgs=$((existing_pkgs+1))
new_stored_pkg=$(printf "${package_name}.%04d" "${existing_pkgs}")
if [ -d "${package_name}" ]; then
echo "Saving the old working environment as ${new_stored_pkg}"
mv "${package_name}" "${new_stored_pkg}"
fi
mkdir "${package_name}"
# Populate the package directory with Debian files
# First with files common to all pla
echo -n "Populating the package and updating version in control file..."
cd "${package_name}"
sed -i "s/__NAME__/${pkg_name}/g" ${PKG_ROOT}/packages/RPM/SPECS/fledge.spec
sed -i "s/__VERSION__/${version}/g" ${PKG_ROOT}/packages/RPM/SPECS/fledge.spec
sed -i "s/__ARCH__/${architecture}/g" ${PKG_ROOT}/packages/RPM/SPECS/fledge.spec
sed -i "s/__RELEASE__/${commit_count}/g" ${PKG_ROOT}/packages/RPM/SPECS/fledge.spec
sed -i "s/__VCS__/${git_tag_info:1}/g" ${PKG_ROOT}/packages/RPM/SPECS/fledge.spec
mkdir -p usr/local/fledge
cd usr/local/fledge
cp -R ${FLEDGE_ROOT}/* .
echo "Done."
# Prepare new data directory
echo "Prepare data directory"
mv data data.new
cd data.new
rm -rf backup
rm -rf core.err
rm -rf ./etc/certs/*
rm -rf fledge.db*
rm -rf logs
rm -rf scripts
rm -rf support
rm -rf var
# Build the package
cd "${BUILD_ROOT}"
cd ..
cd RPMS
cd ${architecture}
# Save the old versions
existing_pkgs=`find . -maxdepth 1 -name "${package_name}.rpm.????" | wc -l`
existing_pkgs=$((existing_pkgs+1))
new_stored_pkg=$(printf "${package_name}.rpm.%04d" "${existing_pkgs}")
if [ -e "${package_name}.rpm" ]; then
echo "Saving the old package as ${new_stored_pkg}"
mv "${package_name}.rpm" "${new_stored_pkg}"
fi
echo "Building the new package..."
base_dir=${PKG_ROOT}/packages
rpmbuild --define "_topdir ${base_dir}/RPM/build" --noclean -bb ${base_dir}/RPM/SPECS/fledge.spec
echo "Building Complete."
if [[ $skip_build == 0 ]]; then
rm -rf /tmp/${repo_name}
fi
exit 0