4ff12f2ba267a4f173de223187cab420cce2fa6f
[osm/devops.git] / tools / local-build.sh
1 #!/bin/bash
2 #######################################################################################
3 # Copyright ETSI Contributors and Others.
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
14 # implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 #######################################################################################
18
19 APT_PROXY=""
20 DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
21 HTTPDDIR="${HOME}/.osm/httpd"
22 HTTPPORT=8000
23 KUBECFG="~/.osm/microk8s-config.yaml"
24 NO_CACHE=""
25 OPENSTACKRC="/var/snap/microstack/common/etc/microstack.rc"
26 REGISTRY="localhost:32000"
27 ROOTDIR="$( cd "${DIR}/../../" &> /dev/null && pwd)"
28 DEVEL_TAG="devel"
29 OSM_TESTS_IMAGE_TAG="devel"
30
31 function check_arguments(){
32 while [ $# -gt 0 ] ; do
33 case $1 in
34 --debug) set -x ;;
35 --apt-proxy) APT_PROXY="$2" && shift ;;
36 --devel-tag) DEVEL_TAG="$2" && shift ;;
37 --help | -h) show_help && exit 0 ;;
38 --httpddir) HTTPDDIR="$2" && shift;;
39 --install-local-registry) 'install_local_registry' ;;
40 --install-microstack) 'install_microstack' ;;
41 --install-qhttpd) INSTALL_HTTPD='install_qhttpd' ;;
42 --run-httpserver) INSTALL_HTTPD='run_httpserver' ;;
43 --kubecfg) KUBECFG="$2" && shift ;;
44 --module) TARGET_MODULE="$2" && shift;;
45 --no-cache) NO_CACHE="--no-cache" ;;
46 --openstackrc) OPENSTACKRC="$2" && shift ;;
47 --registry) REGISTRY="$2" && shift;;
48 --robot-local-mounts) ROBOT_LOCAL=YES ;;
49 --run-tests) TESTS=YES ;;
50 --vim-vca) VIM_VCA="$2" && shift;;
51 --osm-tests-image-tag) OSM_TESTS_IMAGE_TAG="$2" && shift;;
52 stage-2) STAGE_2='stage_2 ${TARGET_MODULE}' ;;
53 stage-3) STAGE_3='stage_3 ${TARGET_MODULE}' ;;
54 registry-push) REGISTRY_PUSH='local_registry_push ${TARGET_MODULE}' ;;
55 install-osm) INSTALL_OSM='install_osm' ;;
56 start-robot) START_ROBOT='start_robot' ;;
57 update-install) UPDATE_INSTALL='update_osm_module ${TARGET_MODULE}'
58 REGISTRY_PUSH='local_registry_push ${TARGET_MODULE}' ;;
59 *) echo "Unknown option $1"
60 show_help
61 exit 1;;
62 esac
63 shift
64 done
65 }
66
67 function show_help() {
68 cat << EOF
69 Usage: $0 [OPTIONS]
70 Perform a local build and potential installation of OSM from sources, using the
71 same process as Jenkins.
72
73 OPTIONS:
74 --help display this help message
75 --apt-proxy provide an apt proxy to docker build steps
76 --debug enable set -x for this script
77 --install-local-registry install and enable Microk8s local registry on port 32000
78 --install-microstack install Microstack and configure to run robot tests
79 --install-qhttpd (deprecated, use --run-httpserver instead) install QHTTPD as an HTTP server on port ${HTTPPORT}
80 --run-httpserver run HTTP server on port ${HTTPPORT}
81 --kubecfg path to kubecfg.yaml (uses Charmed OSM by default)
82 --no-cache do not use any cache when building docker images
83 --module only build this comma delimited list of modules
84 --openstackrc path to Openstack RC file (uses Microstack by default)
85 --registry use this alternate docker registry
86 --run-tests run stage 2 tests
87 --vim-vca name of the a vca registered in OSM to use in the VIM account
88 --osm-tests-image-tag tag to be used in the osm/tests docker image
89 stage-2 run the stage 2 build
90 stage-3 run the stage 3 build
91 registry-push push to the local registry
92 install-osm perform full installation of Charmed OSM from registry
93 start-robot start the Robot test container and leave you at prompt
94 update-install update Charmed OSM with new module container
95
96 A typical use could be the following:
97
98 Let's assume that we have different repos cloned in the folder workspace:
99
100 cd workspace
101 git clone https://osm.etsi.org/gerrit/osm/devops
102 git clone https://osm.etsi.org/gerrit/osm/NBI
103 git clone https://osm.etsi.org/gerrit/osm/LCM
104 git clone "https://osm.etsi.org/gerrit/osm/RO
105 git clone "https://osm.etsi.org/gerrit/osm/common
106 git clone "https://osm.etsi.org/gerrit/osm/IM
107 git clone "https://osm.etsi.org/gerrit/osm/N2VC
108
109 First we run a light HTTP server to serve the artifacts:
110
111 devops/tools/local-build.sh --run-httpserver
112
113 Then we generate the artifacts (debian packages) for the different repos: common, IM, N2VC, RO, LCM, NBI
114
115 devops/tools/local-build.sh --module common,IM,N2VC,RO,LCM,NBI stage-2
116
117 Then new docker images are generated locally with the tag "devel" (e.g.: opensourcemano/lcm:devel):
118
119 devops/tools/local-build.sh --module RO,LCM,NBI stage-3
120
121 Finally, the deployment of OSM will have to be updated to use the new docker images.
122
123 EOF
124 }
125
126 function print_section() {
127 echo "$@"
128 }
129
130 function install_local_registry() {
131 sudo snap install microk8s --classic
132 microk8s status --wait-ready
133 microk8s.enable registry
134 }
135
136 function install_microstack() {
137 sudo snap install microstack --devmode --edge
138 sudo snap set microstack config.network.ports.dashboard=8080
139 sudo microstack.init --auto --control
140
141 sudo snap alias microstack.openstack openstack
142 . /var/snap/microstack/common/etc/microstack.rc
143
144 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
145 microstack.openstack security group rule create $i --protocol icmp --remote-ip 0.0.0.0/0
146 microstack.openstack security group rule create $i --protocol tcp --remote-ip 0.0.0.0/0
147 done
148
149 microstack.openstack network create --enable --no-share osm-ext
150 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 \
151 --subnet-range 172.30.0.0/24
152 microstack.openstack router create external-router
153 microstack.openstack router add subnet external-router osm-ext-subnet
154 microstack.openstack router set --external-gateway external external-router
155
156 curl -L https://github.com/cirros-dev/cirros/releases/download/0.3.5/cirros-0.3.5-x86_64-disk.img \
157 | microstack.openstack image create --public --container-format=bare \
158 --disk-format=qcow2 cirros-0.3.5-x86_64-disk.img
159 curl https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img \
160 | microstack.openstack image create --public --container-format=bare \
161 --disk-format=qcow2 ubuntu16.04
162 curl https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img \
163 | microstack.openstack image create --public --container-format=bare \
164 --disk-format=qcow2 US1604
165 curl https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img \
166 | microstack.openstack image create --public --container-format=bare \
167 --disk-format=qcow2 ubuntu18.04
168 curl https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \
169 | microstack.openstack image create --public --container-format=bare \
170 --disk-format=qcow2 ubuntu20.04
171 }
172
173 function create_httpddir() {
174 mkdir -p ${HTTPDDIR}
175 }
176
177 function install_qhttpd() {
178 sudo snap install qhttp
179 EXISTING_PID=$(ps auxw | grep "http.server $HTTPPORT" | grep -v grep | awk '{print $2}')
180 if [ ! -z $EXISTING_PID ] ; then
181 kill $EXISTING_PID
182 fi
183 nohup qhttp -p ${HTTPPORT} &
184 }
185
186 function run_httpserver() {
187 EXISTING_PID=$(ps auxw | grep "http.server $HTTPPORT" | grep -v grep | awk '{print $2}')
188 if [ ! -z $EXISTING_PID ] ; then
189 kill $EXISTING_PID
190 fi
191 nohup python3 -m http.server ${HTTPPORT} --directory "${HTTPDDIR}" &>/dev/null &
192 }
193
194 function stage_2() {
195 print_section "Performing Stage 2"
196 MODULES="common devops IM LCM MON N2VC NBI NG-UI NG-SA osmclient PLA POL RO tests"
197 if [ ! -z ${1} ] ; then
198 POSSIBLE_MODULES=$(echo ${1} | sed "s/,/ /g")
199 for MODULE in ${POSSIBLE_MODULES}; do
200 if ! echo "${MODULES}" | grep -q "${MODULE}" ; then
201 echo "Unknown stage 2 module ${MODULE}"
202 echo "Must be one of ${MODULES}"
203 exit 1
204 fi
205 done
206 MODULES=${POSSIBLE_MODULES}
207 else
208 print_section "Cleaning HTTP Directory for full build"
209 rm -fv ${HTTPDDIR}/*.deb
210 fi
211
212 for MODULE in ${MODULES} ; do
213 cd "${ROOTDIR}"
214 if [ ! -d ${MODULE} ] ; then
215 echo "Directory ${ROOTDIR}/${MODULE} does not exist"
216 exit 1
217 fi
218 print_section "Building ${MODULE}"
219 cd ${MODULE}
220 find . -name '*.deb' -exec rm -v {} \;
221
222 BUILD_ARGS=""
223 if [ ! -z $APT_PROXY ] ; then
224 BUILD_ARGS="${BUILD_ARGS}--build-arg APT_PROXY=${APT_PROXY} "
225 fi
226 docker build ${NO_CACHE} ${BUILD_ARGS} -t ${MODULE,,}-stage2 .
227
228 STAGES="stage-build.sh"
229 if [ ! -z $TESTS ] ; then
230 STAGES="stage-test.sh ${STAGES}"
231 fi
232 for STAGE in $STAGES ; do
233 docker run -i \
234 -v "$(pwd):/build" \
235 -w /build \
236 ${MODULE,,}-stage2 \
237 bash -c "groupadd -o -g $(id -g) -r $USER ;
238 useradd -o -u $(id -u) -d /build -r -g $USER $USER ;
239 runuser $USER -c devops-stages/${STAGE}"
240 if [ $? -ne 0 ] ; then
241 print_section "Failed to build ${MODULE}"
242 exit 1
243 fi
244 done
245 for file in `find . -name '*.deb'` ; do
246 name=`basename ${file} | cut -d_ -f1`;
247 rm -v ~/snap/qhttp/common/${name}*
248 cp -v $file ~/snap/qhttp/common/${name}_$(date "+%H%M%S").deb
249 rm -v $file
250 done
251 done
252 }
253
254 function _find_module_dockerfile() {
255 cd "${ROOTDIR}/devops/docker"
256 MODULES=`find . -name Dockerfile -printf '%h\n' |sed 's|\./||' |sort |tr '\n' ' '`
257 if [ ! -z ${1} ] ; then
258 POSSIBLE_MODULES=$(echo ${1} | sed "s/,/ /g")
259 for MODULE in ${POSSIBLE_MODULES}; do
260 if ! echo "${MODULES}" | grep -q "${MODULE}" ; then
261 echo "Unknown stage 3 module ${MODULE}"
262 echo "Must be one of ${MODULES}"
263 exit 1
264 fi
265 done
266 echo ${POSSIBLE_MODULES}
267 else
268 echo ${MODULES}
269 fi
270 }
271
272 function stage_3() {
273 print_section "Performing Stage 3"
274 MODULES=$(_find_module_dockerfile $1)
275 BUILD_ARGS=""
276 if [ ! -z $APT_PROXY ] ; then
277 BUILD_ARGS="${BUILD_ARGS}--build-arg APT_PROXY=${APT_PROXY} "
278 fi
279
280 HOSTIP=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')
281 [ -z "$DEFAULT_IF" ] && DEFAULT_IF=$(ip route list|awk '$1=="default" {print $5; exit}')
282 [ -z "$DEFAULT_IF" ] && DEFAULT_IF=$(route -n |awk '$1~/^0.0.0.0/ {print $8; exit}')
283 DEFAULT_IP=$(ip -o -4 a s ${DEFAULT_IF} |awk '{split($4,a,"/"); print a[1]; exit}')
284 HOSTIP=${HOSTIP:=${DEFAULT_IP}}
285 echo $HOSTIP
286
287 for file in ${HTTPDDIR}/*.deb ; do
288 file=`basename ${file}`
289 name=`echo ${file} | cut -d_ -f1 | sed "s/-/_/g" | sed "s/.deb//"`;
290 name=${name^^}_URL
291 BUILD_ARGS="${BUILD_ARGS}--build-arg ${name}=http://$HOSTIP:${HTTPPORT}/$file "
292 echo Added ${name} as http://$HOSTIP:${HTTPPORT}/$file
293 done
294
295 for MODULE in ${MODULES} ; do
296 cd "${ROOTDIR}/devops/docker"
297 if [ ! -d ${MODULE} ] ; then
298 echo "Directory ${ROOTDIR}/devops/docker/${MODULE} does not exist"
299 exit 1
300 fi
301 print_section "Building ${MODULE}"
302 cd ${MODULE}
303 MODULE=${MODULE,,}
304 docker build ${NO_CACHE} -t opensourcemano/${MODULE}:${DEVEL_TAG} ${BUILD_ARGS} .
305 if [ $? -ne 0 ] ; then
306 print_section "Failed to build ${MODULE}"
307 exit 1
308 fi
309 done
310 }
311
312 function local_registry_push() {
313 print_section "Pushing to local registry"
314 cd "${ROOTDIR}/devops/docker"
315 MODULES=`find . -name Dockerfile -printf '%h\n' |sed 's|\./||' |sort |tr '\n' ' '`
316 if [ ! -z ${1} ] ; then
317 POSSIBLE_MODULES=$(echo ${1} | sed "s/,/ /g")
318 for MODULE in ${POSSIBLE_MODULES}; do
319 echo "${MODULE}"
320 if ! echo "${MODULES}" | grep -q "${MODULE}" ; then
321 echo "Unknown stage 3 module ${MODULE}"
322 echo "Must be one of ${MODULES}"
323 exit 1
324 fi
325 done
326 MODULES=${POSSIBLE_MODULES}
327 fi
328 for MODULE in ${MODULES} ; do
329 MODULE=${MODULE,,}
330 docker tag opensourcemano/${MODULE}:${DEVEL_TAG} ${REGISTRY}/opensourcemano/${MODULE}:${DEVEL_TAG}
331 docker push ${REGISTRY}/opensourcemano/${MODULE}:${DEVEL_TAG}
332 done
333 }
334
335 function install_osm() {
336 cd "${ROOTDIR}/devops/installers"
337 VCA=""
338 if juju controllers 2>/dev/null| grep osm-vca ; then
339 VCA="--vca osm-vca"
340 fi
341 ./charmed_install.sh --registry localhost:32000 --tag ${DEVEL_TAG} ${VCA}
342 }
343
344 function start_robot() {
345 mkdir -p "${ROOTDIR}/tests/local"
346 cd "${ROOTDIR}/tests/local"
347
348 . ${OPENSTACKRC}
349
350 # Workaround for microstack auth URL
351 if [ ${OPENSTACKRC} == "/var/snap/microstack/common/etc/microstack.rc" ] ; then
352 export OS_AUTH_URL=${OS_AUTH_URL}/v3
353 fi
354
355 export OSM_HOSTNAME=$(juju config -m osm nbi site_url | sed "s/http.*\?:\/\///"):443
356 export PROMETHEUS_HOSTNAME=$(juju config -m osm prometheus site_url | sed "s/http.*\?:\/\///")
357 export PROMETHEUS_PORT=80
358 export JUJU_PASSWORD=`juju gui 2>&1 | grep password | awk '{print $2}'`
359 export HOSTIP=$(echo $PROMETHEUS_HOSTNAME | sed "s/prometheus.//" | sed "s/.nip.io//")
360
361 rm robot-systest.cfg
362 for line in `env | grep "^OS_" | sort` ; do echo $line >> robot-systest.cfg ; done
363 cat << EOF >> robot-systest.cfg
364 VIM_TARGET=osm
365 VIM_MGMT_NET=osm-ext
366 ENVIRONMENTS_FOLDER=environments
367 PACKAGES_FOLDER=/robot-systest/osm-packages
368 OS_CLOUD=openstack
369 LC_ALL=C.UTF-8
370 LANG=C.UTF-8
371 EOF
372
373 cat << EOF > robot.etc.hosts
374 127.0.0.1 localhost
375 ${HOSTIP} prometheus.${HOSTIP}.nip.io nbi.${HOSTIP}.nip.io
376 EOF
377 cat << EOF > clouds.yaml
378 clouds:
379 openstack:
380 auth:
381 auth_url: $OS_AUTH_URL
382 project_name: $OS_PROJECT_NAME
383 username: $OS_USERNAME
384 password: $OS_PASSWORD
385 user_domain_name: $OS_USER_DOMAIN_NAME
386 project_domain_name: $OS_PROJECT_DOMAIN_NAME
387 EOF
388
389 VIM_AUTH_URL=$(osm vim-show osm | grep vim_url | awk '{print $4}' | tr -d \")
390 if [[ ! -z ${VIM_AUTH_URL} && "$OS_AUTH_URL" != "${VIM_AUTH_URL}" ]] ; then
391 echo "Deleting existing VIM osm as auth URLs have changed"
392 osm vim-delete osm
393 fi
394
395 if ! osm vim-show osm &> /dev/null ; then
396 echo "Creating VIM osm"
397 if [ -v VIM_VCA ]; then
398 VCA_OPT="--vca $VIM_VCA"
399 fi
400 osm vim-create --name osm $VCA_OPT --user "$OS_USERNAME" --password "$OS_PASSWORD" \
401 --auth_url "$OS_AUTH_URL" --tenant "$OS_USERNAME" --account_type openstack \
402 --config='{use_floating_ip: True,
403 management_network_name: osm-ext}'
404 fi
405
406 if [ ! -z $ROBOT_LOCAL ] ; then
407 LOCAL_MOUNT_1="/robot-systest/lib"
408 LOCAL_MOUNT_2="/robot-systest/resources"
409 LOCAL_MOUNT_3="/robot-systest/testsuite"
410 else
411 LOCAL_MOUNT_1="/tmp/lib"
412 LOCAL_MOUNT_2="/tmp/resources"
413 LOCAL_MOUNT_3="/tmp/testsuite"
414 fi
415
416 mkdir -p reports
417
418 docker run -ti --entrypoint /bin/bash \
419 --env OSM_HOSTNAME=${OSM_HOSTNAME} \
420 --env PROMETHEUS_HOSTNAME=${PROMETHEUS_HOSTNAME} \
421 --env PROMETHEUS_PORT=${PROMETHEUS_PORT} \
422 --env JUJU_PASSWORD=${JUJU_PASSWORD} \
423 --env HOSTIP=${HOSTIP} \
424 --env-file robot-systest.cfg \
425 -v "$(pwd)/robot.etc.hosts":/etc/hosts \
426 -v ~/.osm/microk8s-config.yaml:/root/.kube/config \
427 -v "$(pwd)/clouds.yaml":/etc/openstack/clouds.yaml \
428 -v "${HOME}/snap/qhttp/common"/robot-systest/reports \
429 -v "${HOME}/snap/qhttp/common:"/robot-systest/conformance-tests/reports \
430 -v "${ROOTDIR}/tests/robot-systest/lib":${LOCAL_MOUNT_1} \
431 -v "${ROOTDIR}/tests/robot-systest/resources":${LOCAL_MOUNT_2} \
432 -v "${ROOTDIR}/tests/robot-systest/testsuite":${LOCAL_MOUNT_3} \
433 opensourcemano/tests:$OSM_TESTS_IMAGE_TAG
434 }
435
436 function update_osm_module() {
437 MODULES=$(_find_module_dockerfile $1)
438 for MODULE in ${MODULES} ; do
439 MODULE=${MODULE,,}
440 echo "Updating ${MODULE}"
441 juju attach-resource ${MODULE} ${MODULE}-image=localhost:32000/opensourcemano/${MODULE}:${DEVEL_TAG}
442 done
443 }
444
445 if [ "$0" != "$BASH_SOURCE" ]; then
446
447 _osm_local_build()
448 {
449 OPTIONS=$(show_help | sed '0,/^OPTIONS:$/d' | awk '{print $1}')
450 COMPREPLY=($(compgen -W "${OPTIONS}" -- "${COMP_WORDS[-1]}"))
451 }
452
453 THIS_SCRIPT="$(basename ${BASH_SOURCE[0]})"
454 echo "Setting up bash completion for ${THIS_SCRIPT}"
455 complete -F _osm_local_build "${THIS_SCRIPT}"
456 else
457 check_arguments $@
458
459 create_httpddir
460 eval "${INSTALL_HTTPD}"
461 eval "${INSTALL_LOCAL_REGISTRY}"
462 eval "${INSTALL_MICROSTACK}"
463 eval "${STAGE_2}"
464 eval "${STAGE_3}"
465 eval "${REGISTRY_PUSH}"
466 eval "${INSTALL_OSM}"
467 eval "${UPDATE_INSTALL}"
468 eval "${START_ROBOT}"
469 fi