This repository has been archived by the owner on Mar 13, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-redirus-package.sh
271 lines (223 loc) · 6.81 KB
/
create-redirus-package.sh
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
269
270
271
#!/bin/bash
function log_shell
{
printf "$1... "
output=$($2 2>&1)
output_exitcode=$?
if [[ $output_exitcode == 0 ]]; then
printf "\033[32msucceed\033[0m\n"
printf " ------------\n"
echo "${output}"
printf " ------------\n"
else
printf "\033[31mfailed\033[0m\n"
printf " ------------\n"
echo "${output}"
printf " ------------\n"
exit $output_exitcode
fi
}
function create_directories
{
__id=$(id -u)
mkdir -p ${__dir}/build-redirus-${__version}/{BUILD,RPMS,SOURCES,SPECS,SRPMS,tmp}
[ -d /opt/redirus ] || sudo mkdir -p /opt/redirus
[ "$(stat --format=\"%u\" /opt/redirus/)" == "${__id}" ] || sudo chown -R ${__id}:${__id} /opt/redirus
exit $?
}
function create_rpmmacros
{
cat <<EOF > ~/.rpmmacros
%packager ${__packager}
%_topdir ${__dir}/build-redirus-${__version}
%_tmppath ${__dir}/build-redirus-${__version}/tmp
EOF
exit $?
}
function create_spec
{
cat <<EOF > ${__dir}/build-redirus-${__version}/SPECS/redirus.spec
Name: redirus
Version: ${__version}
Release: 0
Epoch: ${__serial}
License: MIT
URL: https://github.com/dice-cyfronet/redirus
Summary: Redirus
Group: Redirus
BuildArch: x86_64
Requires: libcap nginx
BuildRequires: epel-release ruby ruby(rubygems) autoconf bison gcc gcc-c++ make openssl-devel libyaml-devel readline-devel zlib-devel pcre pcre-devel libcap
%description
Redirus software.
##
# Preinstall script.
##
%pre
function log_shell
{
printf "\$1... "
output=\$(bash -c "\$2" 2>&1)
if [[ \$? == 0 ]]; then
printf "\033[32msucceed\033[0m\n"
else
printf "\033[31mfailed\033[0m\n------------\n\${output}\n------------\n"
exit 1
fi
}
echo "Run before installation frodm the new package."
log_shell "Create 'redirus' user (if does not exist)" \
"id -u redirus &>/dev/null || adduser --home-dir /opt/redirus --system redirus"
##
# Postinstall script.
##
%post
function log_shell
{
printf "\$1... "
output=\$(bash -c "\$2" 2>&1)
if [[ \$? == 0 ]]; then
printf "\033[32msucceed\033[0m\n"
else
printf "\033[31mfailed\033[0m\n------------\n\${output}\n------------\n"
exit 1
fi
}
echo "Run after installation from the new package."
echo "Link redirus binaries"
pushd /usr/bin
ln -s /opt/redirus/bin/redirus redirus
ln -s /opt/redirus/bin/redirus-init redirus-init
ln -s /opt/redirus/bin/redirus-client redirus-client
popd
echo "Setting capabilities to allow nginx to bind to low-numbered ports."
setcap 'cap_net_bind_service=+ep' /usr/sbin/nginx
log_shell "Enable 'nginx-redirus' service" "systemctl enable /opt/redirus/resources/nginx-redirus.service"
log_shell "Enable 'redirus' service" "systemctl enable /opt/redirus/resources/redirus.service"
echo "Remember to either set SELinux enforce to 0 or write appropriate SELinux policy."
##
# Preuninstall script.
##
%preun
function log_shell
{
printf "\$1... "
output=\$(bash -c "\$2" 2>&1)
if [[ \$? == 0 ]]; then
printf "\033[32msucceed\033[0m\n"
:q
else
printf "\033[31mfailed\033[0m\n------------\n\${output}\n------------\n"
exit 1
fi
}
echo "Run before uninstallation from the old package."
##
# Postuninstall script.
##
%postun
function log_shell
{
printf "\$1... "
output=\$(bash -c "\$2" 2>&1)
if [[ \$? == 0 ]]; then
printf "\033[32msucceed\033[0m\n"
else
printf "\033[31mfailed\033[0m\n------------\n\${output}\n------------\n"
exit 1
fi
}
echo "Run after uninstallation from the old package."
##
# Prepbuild script.
##
%prep
echo "Run to prepare the package for building."
if [ ! -d "ruby-2.1.3" ]
then
echo "Prepare ruby environment."
wget http://cache.ruby-lang.org/pub/ruby/2.1/ruby-2.1.3.tar.gz
tar zxvf ruby-2.1.3.tar.gz
fi
pushd ruby-2.1.3
./configure --prefix=/opt/redirus/ruby --disable-install-rdoc
make
make install
popd
gem fetch redirus -v 0.2.1
rm -rf redirus-rpm
git clone https://github.com/dice-cyfronet/redirus-rpm.git
##
# Build script.
##
%build
echo "Run to build the package."
PATH=/opt/redirus/ruby/bin:\${PATH}
echo "Install redirus gem."
gem install --no-rdoc --no-ri --install-dir /opt/redirus/ruby/lib/ruby/gems/2.1.0 --bindir bin --force redirus-0.2.1.gem
##
# Install script.
##
%install
echo "Run to install the built files."
echo "Copy application and ruby files."
rm -rf \${RPM_BUILD_ROOT}/opt
mkdir -p \${RPM_BUILD_ROOT}/opt/redirus
mkdir -p \${RPM_BUILD_ROOT}/opt/redirus/bin
mkdir -p \${RPM_BUILD_ROOT}/opt/redirus/ruby
mkdir -p \${RPM_BUILD_ROOT}/opt/redirus/resources
mkdir -p \${RPM_BUILD_ROOT}/opt/redirus/resources/configurations
mkdir -p \${RPM_BUILD_ROOT}/opt/redirus/resources/log
mkdir -p \${RPM_BUILD_ROOT}/opt/redirus/resources/tmp
cp -r %{_builddir}/bin/* \${RPM_BUILD_ROOT}/opt/redirus/bin
cp -r %{_builddir}/redirus-rpm/resources/* \${RPM_BUILD_ROOT}/opt/redirus/resources
cp -r /opt/redirus/ruby/* \${RPM_BUILD_ROOT}/opt/redirus/ruby
%files
%defattr(755, redirus, redirus, 755)
%dir /opt/redirus
/opt/redirus/*
%clean
#rm -rf \${RPM_BUILD_ROOT}
EOF
exit $?
}
function install_required_packages
{
packages=$(grep BuildRequires ${__dir}/build-redirus-${__version}/SPECS/redirus.spec | sed 's/BuildRequires\:\s//g')
sudo yum install -y ${packages}
}
function build_rpm
{
pushd ${__dir}/build-redirus-${__version} > /dev/null 2>&1
rpmbuild -ba ${__dir}/build-redirus-${__version}/SPECS/redirus.spec
output_exitcode=$?
popd > /dev/null 2>&1
exit $output_exitcode
}
function create_repo
{
pushd ${__dir} > /dev/null 2>&1
rm -f ${__dir}/*.rpm
find ${__dir}/ -name *.rpm -exec cp {} ${__dir}/ \;
rm -f repo-redirus.tar.gz
mkdir -p ${__dir}/repo-redirus-${__serial}/redirus/x86_64/
mv *.rpm ${__dir}/repo-redirus-${__serial}/redirus/x86_64/
createrepo ${__dir}/repo-redirus-${__serial}/redirus/x86_64/
createrepo ${__dir}/repo-redirus-${__serial}/redirus/
tar zcvf ${__dir}/repo-redirus-${__serial}.tar.gz repo-redirus-${__serial}/
rm -rf ${__dir}/repo-redirus-${__serial}/
popd
exit 0
}
__dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
command -v rpmbuild > /dev/null 2>&1 || { echo "No 'rpm-build'" >&2; log_shell "Install rpm-build tools" "sudo yum install -y rpm-build"; }
command -v createrepo > /dev/null 2>&1 || { echo "No 'createrepo'" >&2; log_shell "Install createrepo tools" "sudo yum install -y createrepo"; }
__packager=b.wilk@cyfronet.pl
__serial=$(date +%s)
__version=latest
log_shell "Create directories to packaging" "create_directories"
log_shell "Create rpm macro" "create_rpmmacros"
log_shell "Create spec file" "create_spec"
log_shell "Install build required packages" "install_required_packages"
log_shell "Build RPM packages" "build_rpm"
log_shell "Create repo with packages" "create_repo"