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