build_inf_prepare_jenkins.sh: split steps
[pti/rtp.git] / scripts / build_inf_centos / build_inf_prepare_jenkins.sh
1 #!/bin/sh
2 #
3 # Copyright (C) 2022 Wind River Systems, Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16 #
17
18 # Ensure we fail the job if any steps fail.
19 set -e -o pipefail
20
21 #########################################################################
22 # Variables
23 #########################################################################
24 WORKSPACE=""
25
26 SCRIPTS_DIR=$(dirname $(readlink -f $0))
27 SCRIPTS_NAME=$(basename $0)
28
29 TOOLS_DIR="${SCRIPTS_DIR}/tools"
30 STEP_STATUS_DIR="${SCRIPTS_DIR}/step_status"
31
32 BUILD_GROUP="jenkins"
33 WGET_OPENDEV="wget --no-check-certificate"
34
35 #########################################################################
36 # Common Functions
37 #########################################################################
38
39 help_info () {
40 cat << ENDHELP
41 Usage:
42 ${SCRIPTS_NAME} [-w WORKSPACE_DIR] [-h]
43 where:
44     -w WORKSPACE_DIR is the path for the builds
45     -h this help info
46 examples:
47 $0
48 $0 -w workspace_1234
49 ENDHELP
50 }
51
52 echo_step_start() {
53     [ -n "$1" ] && msg_step=$1
54     echo "#########################################################################################"
55     echo "## ${SCRIPTS_NAME} - STEP START: ${msg_step}"
56     echo "#########################################################################################"
57 }
58
59 echo_step_end() {
60     [ -n "$1" ] && msg_step=$1
61     echo "#########################################################################################"
62     echo "## ${SCRIPTS_NAME} - STEP END: ${msg_step}"
63     echo "#########################################################################################"
64     echo
65 }
66
67 echo_info () {
68     echo "INFO: $1"
69 }
70
71 while getopts "w:h" OPTION; do
72     case ${OPTION} in
73         w)
74             WORKSPACE=`readlink -f ${OPTARG}`
75             ;;
76         h)
77             help_info
78             exit
79             ;;
80     esac
81 done
82
83 #########################################################################
84 # Functions
85 #########################################################################
86 LOCALDISK="${WORKSPACE}/localdisk"
87 MIRROR_DIR="${WORKSPACE}/mirror"
88
89 prepare_env () {
90     msg_step="Prepare build directories"
91     echo_step_start
92
93     mkdir -p ${LOCALDISK}/loadbuild/mock-cache
94     mkdir -p ${LOCALDISK}/loadbuild/mock
95     mkdir -p ${LOCALDISK}/designer
96     mkdir -p ${LOCALDISK}/loadbuild
97     mkdir -p ${STEP_STATUS_DIR}
98
99     sudo chmod 775 ${LOCALDISK}/loadbuild/mock
100     sudo chown root:mock ${LOCALDISK}/loadbuild/mock
101     sudo chmod 775 ${LOCALDISK}/loadbuild/mock-cache
102     sudo chown root:mock ${LOCALDISK}/loadbuild/mock-cache
103
104     TMP_OPT_DIR=${SCRIPTS_DIR}/opt
105     mkdir -p ${TMP_OPT_DIR}
106
107     echo_step_end
108 }
109
110 install_pkgs () {
111     msg_step="Install/downlaod/config required dependencies by mirror/build processes."
112     echo_step_start
113
114     echo_info "Install required packages"
115     sudo yum install -y \
116         anaconda \
117         anaconda-runtime \
118         autoconf-archive \
119         autogen \
120         automake \
121         bc \
122         bind \
123         bind-utils \
124         bison \
125         cpanminus \
126         createrepo \
127         createrepo_c \
128         deltarpm \
129         expat-devel \
130         flex \
131         isomd5sum \
132         gcc \
133         gettext \
134         libguestfs-tools \
135         libtool \
136         libxml2 \
137         lighttpd \
138         lighttpd-fastcgi \
139         lighttpd-mod_geoip \
140         net-tools \
141         mkisofs \
142         mongodb \
143         mongodb-server \
144         pax \
145         perl-CPAN \
146         python-deltarpm \
147         python-pep8 \
148         python-pip \
149         python-psutil \
150         python2-psutil \
151         python36-psutil \
152         python36-requests \
153         python3-devel \
154         python-sphinx \
155         python-subunit \
156         python-virtualenv \
157         python-yaml \
158         python2-ruamel-yaml \
159         postgresql \
160         qemu-kvm \
161         quilt \
162         rpm-build \
163         rpm-sign \
164         rpm-python \
165         squashfs-tools \
166         sudo \
167         systemd \
168         syslinux \
169         udisks2 \
170         vim-enhanced \
171         wget
172
173     echo_info "Install required cpan modules"
174     # cpan modules, installing with cpanminus to avoid stupid questions since cpan is whack
175     sudo cpanm --notest Fatal
176     sudo cpanm --notest XML::SAX
177     sudo cpanm --notest XML::SAX::Expat
178     sudo cpanm --notest XML::Parser
179     sudo cpanm --notest XML::Simple
180
181     echo_info "Install repo tool"
182     sudo wget https://storage.googleapis.com/git-repo-downloads/repo -O /usr/local/bin/repo
183     sudo chmod a+x /usr/local/bin/repo
184
185     echo_info "Install go and setting paths"
186     export GOPATH="/usr/local/go"
187     export PATH="${GOPATH}/bin:${PATH}"
188     sudo yum install -y golang
189     sudo mkdir -p ${GOPATH}/bin
190     curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sudo -E sh
191
192     echo_info "Install pip packages"
193     # Install required python modules globally; versions are in the constraints file.
194     # Be careful not to replace modules provided by RPMs as it may break
195     # other system packages. Look for warnings similar to "Uninstalling a
196     # distutils installed project has been deprecated" from pip.
197     sudo pip install -c ${TOOLS_DIR}/toCOPY/builder-constraints.txt \
198         testrepository \
199         fixtures \
200         pbr \
201         git-review \
202         python-subunit \
203         junitxml \
204         testtools
205
206     # This image requires a set of scripts and helpers
207     # for working correctly, in this section they are
208     # copied inside the image.
209     sudo cp ${TOOLS_DIR}/toCOPY/finishSetup.sh /usr/local/bin
210     sudo cp ${TOOLS_DIR}/toCOPY/populate_downloads.sh /usr/local/bin
211     sudo cp ${TOOLS_DIR}/toCOPY/generate-local-repo.sh /usr/local/bin
212     sudo cp ${TOOLS_DIR}/toCOPY/generate-centos-repo.sh /usr/local/bin
213     sudo cp ${TOOLS_DIR}/toCOPY/lst_utils.sh /usr/local/bin
214
215     sudo chmod a+x /usr/local/bin/*
216
217     echo_step_end
218 }
219
220 get_tools_repo () {
221     msg_step="Clone the tools repo"
222     echo_step_start
223
224     cd ${SCRIPTS_DIR}
225     if [ -d tools/.git ]; then
226         echo_info "The 'tools' repo already exists, skipping"
227     else
228         git clone https://opendev.org/starlingx/tools.git
229     fi
230
231     echo_step_end
232 }
233
234 config_mock () {
235     msg_step="mock custumizations"
236     echo_step_start
237     # forcing chroots since a couple of packages naughtily insist on network access and
238     # we dont have nspawn and networks happy together.
239     sudo groupadd -f -g 9001 mockbuild
240     sudo useradd -s /sbin/nologin -u 9001 -g 9001 mockbuild || echo "User mockbuild already exists."
241     [ -L /var/lib/mock ] || {
242         sudo rmdir /var/lib/mock
243         sudo ln -s ${LOCALDISK}/loadbuild/mock /var/lib/mock
244     }
245
246     [ -L /var/cache/mock ] || {
247         sudo mv /var/cache/mock/* ${LOCALDISK}/loadbuild/mock-cache/
248         sudo rmdir /var/cache/mock
249         sudo ln -s ${LOCALDISK}/loadbuild/mock-cache /var/cache/mock
250     }
251     echo "config_opts['use_nspawn'] = False" | sudo tee -a /etc/mock/site-defaults.cfg
252     echo "config_opts['rpmbuild_networking'] = True" | sudo tee -a /etc/mock/site-defaults.cfg
253     echo | sudo tee -a /etc/mock/site-defaults.cfg
254
255     echo_step_end
256 }
257
258 install_mock_overlay () {
259     msg_step="Inherited tools for mock stuff"
260     echo_step_start
261
262     # we at least need the mock_cache_unlock tool
263     # they install into /usr/bin
264     cp -rf ${TOOLS_DIR}/toCOPY/mock_overlay ${TMP_OPT_DIR}/mock_overlay
265     cd ${TMP_OPT_DIR}/mock_overlay
266     make
267     sudo make install
268
269     echo_step_end
270 }
271
272 config_yum () {
273     msg_step="configs for yum and rpm"
274     echo_step_start
275
276     # Customizations for mirror creation
277     sudo rm -f /etc/yum.repos.d/*
278     sudo cp -f ${TOOLS_DIR}/centos-mirror-tools/yum.repos.d/* /etc/yum.repos.d/
279     sudo cp -f ${TOOLS_DIR}/centos-mirror-tools/rpm-gpg-keys/* /etc/pki/rpm-gpg/
280
281     # Import GPG keys
282     sudo rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY*
283
284     # Try to continue a yum command even if a StarlingX repo is unavailable.
285     sudo yum-config-manager --setopt=StarlingX\*.skip_if_unavailable=1 --save
286
287     echo_step_end
288 }
289
290 create_py27_venv () {
291     msg_step="Create a sane py27 virtualenv"
292     echo_step_start
293
294     virtualenv ${TMP_OPT_DIR}/py27
295     source ${TMP_OPT_DIR}/py27/bin/activate
296     pip install -c ${TOOLS_DIR}/toCOPY/builder-opt-py27-constraints.txt tox
297     sudo ln -s ${TMP_OPT_DIR}/py27/bin/tox /usr/bin
298
299     echo_step_end
300 }
301
302 setup_lighttpd () {
303     msg_step="Setup for lighttpd"
304     echo_step_start
305
306     sudo mkdir -p /www
307     sudo chown ${USER}:${BUILD_GROUP} /www
308     mkdir -p /www/run
309     mkdir -p /www/logs
310     mkdir -p /www/home
311     mkdir -p /www/root/htdocs/localdisk
312     mkdir -p /www/root/htdocs/$(dirname ${WORKSPACE})
313     ln -s ${LOCALDISK}/loadbuild /www/root/htdocs/localdisk/loadbuild
314     ln -s ${LOCALDISK}/designer /www/root/htdocs/localdisk/designer
315     ln -s ${MIRROR_DIR}/CentOS /www/root/htdocs/CentOS
316     ln -s ${WORKSPACE} /www/root/htdocs/${WORKSPACE}
317
318     # lighthttpd setup
319     # chmod for /var/log/lighttpd fixes a centos issue
320     # in place sed for server root since it's expanded soon thereafter
321     #     echo "server.bind = \"localhost\"" >> /etc/lighttpd/lighttpd.conf && \
322     sudo mkdir -p  /var/log/lighttpd
323     sudo chmod a+rwx /var/log/lighttpd/
324     sudo sed -i -e 's%^var\.log_root.*$%var.log_root = "/www/logs"%g' \
325         -e 's%^var\.server_root.*$%var.server_root = "/www/root"%g' \
326         -e 's%^var\.home_dir.*$%var.home_dir = "/www/home"%g' \
327         -e 's%^var\.state_dir.*$%var.state_dir = "/www/run"%g' \
328         -e "s/server.port/#server.port/g" \
329         -e "s/server.use-ipv6/#server.use-ipv6/g" \
330         -e "s/server.username/#server.username/g" \
331         -e "s/server.groupname/#server.groupname/g" \
332         -e "s/server.bind/#server.bind/g" \
333         -e "s/server.document-root/#server.document-root/g" \
334         -e "s/server.dirlisting/#server.dirlisting/g" \
335         /etc/lighttpd/lighttpd.conf
336
337     echo "server.port = 8088" | sudo tee -a /etc/lighttpd/lighttpd.conf
338     echo "server.use-ipv6 = \"disable\"" | sudo tee -a /etc/lighttpd/lighttpd.conf
339     echo "server.username = \"$USER\"" | sudo tee -a /etc/lighttpd/lighttpd.conf
340     echo "server.groupname = \"$BUILD_GROUP\"" | sudo tee -a /etc/lighttpd/lighttpd.conf
341     echo "server.bind = \"localhost\"" | sudo tee -a /etc/lighttpd/lighttpd.conf
342     echo "server.document-root   = \"/www/root/htdocs\"" | sudo tee -a /etc/lighttpd/lighttpd.conf
343
344     sudo sed -i "s/dir-listing.activate/#dir-listing.activate/g" \
345         /etc/lighttpd/conf.d/dirlisting.conf
346
347     echo "dir-listing.activate = \"enable\"" | sudo tee -a /etc/lighttpd/conf.d/dirlisting.conf
348
349     sudo /usr/sbin/lighttpd  -f /etc/lighttpd/lighttpd.conf
350
351     echo_step_end
352 }
353
354 #########################################################################
355 # Main process
356 #########################################################################
357
358 # centos locales are broken. this needs to be run after the last yum install/update
359 sudo localedef -i en_US -f UTF-8 en_US.UTF-8
360
361 prepare_env
362 get_tools_repo
363 install_pkgs
364 config_mock
365 install_mock_overlay
366 setup_lighttpd
367 config_yum
368 create_py27_venv