blob: ce686c8d707ab09bc6ccb160c393c07d54096cca [file] [log] [blame]
beierlm85dc2392021-04-14 13:51:53 -04001#!/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
beierlme7646252022-01-13 10:53:08 -050019APT_PROXY=""
beierlm85dc2392021-04-14 13:51:53 -040020DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )"
21HTTPDDIR="$( cd "${HOME}/snap/qhttp/common" &> /dev/null && pwd )"
veleza05f3d9e2021-11-22 11:26:50 +000022HTTPPORT=8000
beierlm85dc2392021-04-14 13:51:53 -040023KUBECFG="~/.osm/microk8s-config.yaml"
24NO_CACHE=""
25OPENSTACKRC="/var/snap/microstack/common/etc/microstack.rc"
26REGISTRY="localhost:32000"
27ROOTDIR="$( cd "${DIR}/../../" &> /dev/null && pwd)"
David Garciaf0014df2021-06-16 10:37:34 +020028OSM_TESTS_IMAGE_TAG="devel"
beierlm85dc2392021-04-14 13:51:53 -040029
30function check_arguments(){
31 while [ $# -gt 0 ] ; do
32 case $1 in
33 --debug) set -x ;;
beierlme7646252022-01-13 10:53:08 -050034 --apt-proxy) APT_PROXY="$2" && shift ;;
beierlm85dc2392021-04-14 13:51:53 -040035 --help | -h) show_help && exit 0 ;;
36 --httpddir) HTTPDIR="$2" && shift;;
37 --install-local-registry) 'install_local_registry' ;;
38 --install-microstack) 'install_microstack' ;;
39 --install-qhttpd) INSTALL_HTTPD='install_qhttpd' ;;
40 --kubecfg) KUBECFG="$2" && shift ;;
41 --module) TARGET_MODULE="$2" && shift;;
42 --no-cache) NO_CACHE="--no-cache" ;;
43 --openstackrc) OPENSTACKRC="$2" && shift ;;
44 --registry) REGISTRY="$2" && shift;;
45 --robot-local-mounts) ROBOT_LOCAL=YES ;;
46 --run-tests) TESTS=YES ;;
David Garciaf0014df2021-06-16 10:37:34 +020047 --vim-vca) VIM_VCA="$2" && shift;;
48 --osm-tests-image-tag) OSM_TESTS_IMAGE_TAG="$2" && shift;;
beierlm85dc2392021-04-14 13:51:53 -040049 stage-2) STAGE_2='stage_2 ${TARGET_MODULE}' ;;
50 stage-3) STAGE_3='stage_3 ${TARGET_MODULE}' ;;
51 registry-push) REGISTRY_PUSH='local_registry_push ${TARGET_MODULE}' ;;
52 install-osm) INSTALL_OSM='install_osm' ;;
53 start-robot) START_ROBOT='start_robot' ;;
54 update-install) UPDATE_INSTALL='update_osm_module ${TARGET_MODULE}'
55 REGISTRY_PUSH='local_registry_push ${TARGET_MODULE}' ;;
56 *) echo "Unknown option $1"
57 show_help
58 exit 1;;
59 esac
60 shift
61 done
62}
63
64function show_help() {
65 cat << EOF
66Usage: $0 [OPTIONS]
67Perform a local build and potential installation of OSM from sources, using the
68same process as Jenkins.
69
70OPTIONS:
71 --help display this help message
beierlme7646252022-01-13 10:53:08 -050072 --apt-proxy provide an apt proxy to docker build steps
beierlm85dc2392021-04-14 13:51:53 -040073 --debug enable set -x for this script
beierlme7646252022-01-13 10:53:08 -050074 --install-local-registry install and enable Microk8s local registry on port 32000
beierlm85dc2392021-04-14 13:51:53 -040075 --install-microstack install Microstack and configure to run robot tests
76 --install-qhttpd install QHTTPD as an HTTP server on port ${HTTPPORT}
77 --kubecfg path to kubecfg.yaml (uses Charmed OSM by default)
78 --no-cache do not use any cache when building docker images
79 --module only build this comma delimited list of modules
80 --openstackrc path to Openstack RC file (uses Microstack by default)
81 --registry use this alternate docker registry
82 --run-tests run stage 2 tests
David Garciaf0014df2021-06-16 10:37:34 +020083 --vim-vca name of the a vca registered in OSM to use in the VIM account
84 --osm-tests-image-tag tag to be used in the osm/tests docker image
beierlm85dc2392021-04-14 13:51:53 -040085 stage-2 run the stage 2 build
86 stage-3 run the stage 3 build
87 registry-push push to the local registry
88 install-osm perform full installation of Charmed OSM from registry
89 start-robot start the Robot test container and leave you at prompt
90 update-install update Charmed OSM with new module container
garciadeblas9f5e6d62022-04-22 10:20:08 +020091
92A typical use could be the following:
93
94Let's assume that we have different repos cloned in the folder workspace:
95
96 cd workspace
97 git clone https://osm.etsi.org/gerrit/osm/devops
98 git clone https://osm.etsi.org/gerrit/osm/NBI
99 git clone https://osm.etsi.org/gerrit/osm/LCM
100 git clone "https://osm.etsi.org/gerrit/osm/RO
101 git clone "https://osm.etsi.org/gerrit/osm/common
102 git clone "https://osm.etsi.org/gerrit/osm/IM
103 git clone "https://osm.etsi.org/gerrit/osm/N2VC
104
105First we install a light HTTP server to serve the artifacts:
106
107 devops/tools/local-build.sh --install-qhttpd
108
109Then we generate the artifacts (debian packages) for the different repos: common, IM, N2VC, RO, LCM, NBI
110
111 devops/tools/local-build.sh --module common,IM,N2VC,RO,LCM,NBI stage-2
112
113Then new docker images are generated locally with the tag "devel" (e.g.: opensourcemano/lcm:devel):
114
115 devops/tools/local-build.sh --module RO,LCM,NBI stage-3
116
117Finally, the deployment of OSM will have to be updated to use the new docker images.
118
beierlm85dc2392021-04-14 13:51:53 -0400119EOF
120}
121
David Garciaf0014df2021-06-16 10:37:34 +0200122function print_section() {
123 echo "$@"
124}
125
beierlm85dc2392021-04-14 13:51:53 -0400126function install_local_registry() {
127 sudo snap install microk8s --classic
128 microk8s status --wait-ready
129 microk8s.enable registry
130}
131
132function install_microstack() {
133 sudo snap install microstack --devmode --edge
beierlm6535de32021-06-30 08:05:37 -0400134 sudo snap set microstack config.network.ports.dashboard=8080
beierlm85dc2392021-04-14 13:51:53 -0400135 sudo microstack.init --auto --control
136
137 sudo snap alias microstack.openstack openstack
138 . /var/snap/microstack/common/etc/microstack.rc
139
beierlm6535de32021-06-30 08:05:37 -0400140 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
141 microstack.openstack security group rule create $i --protocol icmp --remote-ip 0.0.0.0/0
142 microstack.openstack security group rule create $i --protocol tcp --remote-ip 0.0.0.0/0
beierlm85dc2392021-04-14 13:51:53 -0400143 done
144
beierlm6535de32021-06-30 08:05:37 -0400145 microstack.openstack network create --enable --no-share osm-ext
146 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 \
beierlm85dc2392021-04-14 13:51:53 -0400147 --subnet-range 172.30.0.0/24
beierlm6535de32021-06-30 08:05:37 -0400148 microstack.openstack router create external-router
149 microstack.openstack router add subnet external-router osm-ext-subnet
150 microstack.openstack router set --external-gateway external external-router
beierlm85dc2392021-04-14 13:51:53 -0400151
152 curl -L https://github.com/cirros-dev/cirros/releases/download/0.3.5/cirros-0.3.5-x86_64-disk.img \
beierlm6535de32021-06-30 08:05:37 -0400153 | microstack.openstack image create --public --container-format=bare \
beierlm85dc2392021-04-14 13:51:53 -0400154 --disk-format=qcow2 cirros-0.3.5-x86_64-disk.img
155 curl https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img \
beierlm6535de32021-06-30 08:05:37 -0400156 | microstack.openstack image create --public --container-format=bare \
beierlm85dc2392021-04-14 13:51:53 -0400157 --disk-format=qcow2 ubuntu16.04
158 curl https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img \
beierlm6535de32021-06-30 08:05:37 -0400159 | microstack.openstack image create --public --container-format=bare \
beierlm85dc2392021-04-14 13:51:53 -0400160 --disk-format=qcow2 US1604
161 curl https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img \
beierlm6535de32021-06-30 08:05:37 -0400162 | microstack.openstack image create --public --container-format=bare \
beierlm85dc2392021-04-14 13:51:53 -0400163 --disk-format=qcow2 ubuntu18.04
164 curl https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \
beierlm6535de32021-06-30 08:05:37 -0400165 | microstack.openstack image create --public --container-format=bare \
beierlm85dc2392021-04-14 13:51:53 -0400166 --disk-format=qcow2 ubuntu20.04
167}
168
169function install_qhttpd() {
170 sudo snap install qhttp
garciadeblas9f5e6d62022-04-22 10:20:08 +0200171 EXISTING_PID=$(ps auxw | grep "http.server $HTTPPORT" | grep -v grep | awk '{print $2}')
beierlm85dc2392021-04-14 13:51:53 -0400172 if [ ! -z $EXISTING_PID ] ; then
173 kill $EXISTING_PID
174 fi
175 qhttp -p ${HTTPPORT} &
176}
177
178function stage_2() {
David Garciaf0014df2021-06-16 10:37:34 +0200179 print_section "Performing Stage 2"
beierlm85dc2392021-04-14 13:51:53 -0400180 MODULES="common devops IM LCM MON N2VC NBI NG-UI osmclient PLA POL RO tests"
181 if [ ! -z ${1} ] ; then
182 POSSIBLE_MODULES=$(echo ${1} | sed "s/,/ /g")
183 for MODULE in ${POSSIBLE_MODULES}; do
184 if ! echo "${MODULES}" | grep -q "${MODULE}" ; then
185 echo "Unknown stage 2 module ${MODULE}"
186 echo "Must be one of ${MODULES}"
187 exit 1
188 fi
189 done
190 MODULES=${POSSIBLE_MODULES}
191 else
David Garciaf0014df2021-06-16 10:37:34 +0200192 print_section "Cleaning HTTP Directory for full build"
beierlm85dc2392021-04-14 13:51:53 -0400193 rm -fv ${HTTPDDIR}/*.deb
194 fi
195
196 for MODULE in ${MODULES} ; do
197 cd "${ROOTDIR}"
198 if [ ! -d ${MODULE} ] ; then
199 echo "Directory ${ROOTDIR}/${MODULE} does not exist"
200 exit 1
201 fi
David Garciaf0014df2021-06-16 10:37:34 +0200202 print_section "Building ${MODULE}"
beierlm85dc2392021-04-14 13:51:53 -0400203 cd ${MODULE}
204 find . -name '*.deb' -exec rm -v {} \;
beierlme7646252022-01-13 10:53:08 -0500205
206 BUILD_ARGS=""
207 if [ ! -z $APT_PROXY ] ; then
208 BUILD_ARGS="${BUILD_ARGS}--build-arg APT_PROXY=${APT_PROXY} "
209 fi
210 docker build ${NO_CACHE} ${BUILD_ARGS} -t ${MODULE,,}-stage2 .
beierlm85dc2392021-04-14 13:51:53 -0400211
212 STAGES="stage-build.sh"
213 if [ ! -z $TESTS ] ; then
214 STAGES="stage-test.sh ${STAGES}"
215 fi
216 for STAGE in $STAGES ; do
217 docker run -ti \
218 -v "$(pwd):/build" \
219 -w /build \
220 ${MODULE,,}-stage2 \
221 bash -c "groupadd -o -g $(id -g) -r $USER ;
222 useradd -o -u $(id -u) -d /build -r -g $USER $USER ;
223 runuser $USER -c devops-stages/${STAGE}"
224 if [ $? -ne 0 ] ; then
David Garciaf0014df2021-06-16 10:37:34 +0200225 print_section "Failed to build ${MODULE}"
beierlm85dc2392021-04-14 13:51:53 -0400226 exit 1
227 fi
228 done
229
230 find . -name '*.deb' -exec mv -v {} ${HTTPDDIR}/ \;
231 done
232}
233
234function _find_module_dockerfile() {
235 cd "${ROOTDIR}/devops/docker"
236 MODULES=`find . -name Dockerfile -printf '%h\n' |sed 's|\./||' |sort |tr '\n' ' '`
237 if [ ! -z ${1} ] ; then
238 POSSIBLE_MODULES=$(echo ${1} | sed "s/,/ /g")
239 for MODULE in ${POSSIBLE_MODULES}; do
240 if ! echo "${MODULES}" | grep -q "${MODULE}" ; then
241 echo "Unknown stage 3 module ${MODULE}"
242 echo "Must be one of ${MODULES}"
243 exit 1
244 fi
245 done
246 echo ${POSSIBLE_MODULES}
247 else
248 echo ${MODULES}
249 fi
250}
251
252function stage_3() {
David Garciaf0014df2021-06-16 10:37:34 +0200253 print_section "Performing Stage 3"
beierlm85dc2392021-04-14 13:51:53 -0400254 MODULES=$(_find_module_dockerfile $1)
255 BUILD_ARGS=""
beierlme7646252022-01-13 10:53:08 -0500256 if [ ! -z $APT_PROXY ] ; then
257 BUILD_ARGS="${BUILD_ARGS}--build-arg APT_PROXY=${APT_PROXY} "
258 fi
259
beierlm85dc2392021-04-14 13:51:53 -0400260 HOSTIP=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')
261 for file in ~/snap/qhttp/common/*.deb ; do
262 file=`basename ${file}`
263 name=`echo ${file} | cut -d_ -f1 | sed "s/-/_/g"`;
264 name=${name^^}_URL
265 BUILD_ARGS="${BUILD_ARGS}--build-arg ${name}=http://$HOSTIP:${HTTPPORT}/$file "
266 echo Added ${name} as http://$HOSTIP:${HTTPPORT}/$file
267 done
268
269 for MODULE in ${MODULES} ; do
270 cd "${ROOTDIR}/devops/docker"
271 if [ ! -d ${MODULE} ] ; then
272 echo "Directory ${ROOTDIR}/${MODULE} does not exist"
273 exit 1
274 fi
David Garciaf0014df2021-06-16 10:37:34 +0200275 print_section "Building ${MODULE}"
beierlm85dc2392021-04-14 13:51:53 -0400276 cd ${MODULE}
277 MODULE=${MODULE,,}
278 docker build ${NO_CACHE} -t opensourcemano/${MODULE}:devel ${BUILD_ARGS} .
279 if [ $? -ne 0 ] ; then
David Garciaf0014df2021-06-16 10:37:34 +0200280 print_section "Failed to build ${MODULE}"
beierlm85dc2392021-04-14 13:51:53 -0400281 exit 1
282 fi
283 done
284}
285
286function local_registry_push() {
David Garciaf0014df2021-06-16 10:37:34 +0200287 print_section "Pushing to local registry"
beierlm85dc2392021-04-14 13:51:53 -0400288 cd "${ROOTDIR}/devops/docker"
289 MODULES=`find . -name Dockerfile -printf '%h\n' |sed 's|\./||' |sort |tr '\n' ' '`
290 if [ ! -z ${1} ] ; then
291 POSSIBLE_MODULES=$(echo ${1} | sed "s/,/ /g")
292 for MODULE in ${POSSIBLE_MODULES}; do
293 echo "${MODULE}"
294 if ! echo "${MODULES}" | grep -q "${MODULE}" ; then
295 echo "Unknown stage 3 module ${MODULE}"
296 echo "Must be one of ${MODULES}"
297 exit 1
298 fi
299 done
300 MODULES=${POSSIBLE_MODULES}
301 fi
302 for MODULE in ${MODULES} ; do
303 MODULE=${MODULE,,}
304 docker tag opensourcemano/${MODULE}:devel ${REGISTRY}/opensourcemano/${MODULE}:devel
305 docker push ${REGISTRY}/opensourcemano/${MODULE}:devel
306 done
307}
308
309function install_osm() {
310 cd "${ROOTDIR}/devops/installers"
311 VCA=""
312 if juju controllers 2>/dev/null| grep osm-vca ; then
313 VCA="--vca osm-vca"
314 fi
315 ./charmed_install.sh --registry localhost:32000 --tag devel ${VCA}
316}
317
318function start_robot() {
319 mkdir -p "${ROOTDIR}/tests/local"
320 cd "${ROOTDIR}/tests/local"
321
322 . ${OPENSTACKRC}
323
324 # Workaround for microstack auth URL
325 if [ ${OPENSTACKRC} == "/var/snap/microstack/common/etc/microstack.rc" ] ; then
326 export OS_AUTH_URL=${OS_AUTH_URL}/v3
327 fi
328
329 export OSM_HOSTNAME=$(juju config -m osm nbi site_url | sed "s/http.*\?:\/\///"):443
330 export PROMETHEUS_HOSTNAME=$(juju config -m osm prometheus site_url | sed "s/http.*\?:\/\///")
331 export PROMETHEUS_PORT=80
332 export JUJU_PASSWORD=`juju gui 2>&1 | grep password | awk '{print $2}'`
David Garciaf0014df2021-06-16 10:37:34 +0200333 export HOSTIP=$(echo $PROMETHEUS_HOSTNAME | sed "s/prometheus.//" | sed "s/.nip.io//")
beierlm85dc2392021-04-14 13:51:53 -0400334
335 rm robot-systest.cfg
336 for line in `env | grep "^OS_" | sort` ; do echo $line >> robot-systest.cfg ; done
337 cat << EOF >> robot-systest.cfg
338VIM_TARGET=osm
339VIM_MGMT_NET=osm-ext
340ENVIRONMENTS_FOLDER=environments
341PACKAGES_FOLDER=/robot-systest/osm-packages
342OS_CLOUD=openstack
343LC_ALL=C.UTF-8
344LANG=C.UTF-8
345EOF
346
347 cat << EOF > robot.etc.hosts
348127.0.0.1 localhost
David Garciaf0014df2021-06-16 10:37:34 +0200349${HOSTIP} prometheus.${HOSTIP}.nip.io nbi.${HOSTIP}.nip.io
beierlm85dc2392021-04-14 13:51:53 -0400350EOF
351 cat << EOF > clouds.yaml
352clouds:
353 openstack:
354 auth:
355 auth_url: $OS_AUTH_URL
356 project_name: $OS_PROJECT_NAME
357 username: $OS_USERNAME
358 password: $OS_PASSWORD
359 user_domain_name: $OS_USER_DOMAIN_NAME
360 project_domain_name: $OS_PROJECT_DOMAIN_NAME
361EOF
362
363 VIM_AUTH_URL=$(osm vim-show osm | grep vim_url | awk '{print $4}' | tr -d \")
David Garciaf0014df2021-06-16 10:37:34 +0200364 if [[ ! -z ${VIM_AUTH_URL} && "$OS_AUTH_URL" != "${VIM_AUTH_URL}" ]] ; then
beierlm85dc2392021-04-14 13:51:53 -0400365 echo "Deleting existing VIM osm as auth URLs have changed"
366 osm vim-delete osm
367 fi
368
369 if ! osm vim-show osm &> /dev/null ; then
370 echo "Creating VIM osm"
David Garciaf0014df2021-06-16 10:37:34 +0200371 if [ -v VIM_VCA ]; then
372 VCA_OPT="--vca $VIM_VCA"
373 fi
374 osm vim-create --name osm $VCA_OPT --user "$OS_USERNAME" --password "$OS_PASSWORD" \
beierlm85dc2392021-04-14 13:51:53 -0400375 --auth_url "$OS_AUTH_URL" --tenant "$OS_USERNAME" --account_type openstack \
376 --config='{use_floating_ip: True,
377 management_network_name: osm-ext}'
378 fi
379
380 if [ ! -z $ROBOT_LOCAL ] ; then
381 LOCAL_MOUNT_1="/robot-systest/lib"
382 LOCAL_MOUNT_2="/robot-systest/resources"
383 LOCAL_MOUNT_3="/robot-systest/testsuite"
384 else
385 LOCAL_MOUNT_1="/tmp/lib"
386 LOCAL_MOUNT_2="/tmp/resources"
387 LOCAL_MOUNT_3="/tmp/testsuite"
388 fi
389
390 mkdir -p reports
391
392 docker run -ti --entrypoint /bin/bash \
393 --env OSM_HOSTNAME=${OSM_HOSTNAME} \
394 --env PROMETHEUS_HOSTNAME=${PROMETHEUS_HOSTNAME} \
395 --env PROMETHEUS_PORT=${PROMETHEUS_PORT} \
396 --env JUJU_PASSWORD=${JUJU_PASSWORD} \
397 --env HOSTIP=${HOSTIP} \
398 --env-file robot-systest.cfg \
399 -v "$(pwd)/robot.etc.hosts":/etc/hosts \
400 -v ~/.osm/microk8s-config.yaml:/root/.kube/config \
401 -v "$(pwd)/clouds.yaml":/etc/openstack/clouds.yaml \
veleza05f3d9e2021-11-22 11:26:50 +0000402 -v "${HOME}/snap/qhttp/common"/robot-systest/reports \
403 -v "${HOME}/snap/qhttp/common:"/robot-systest/conformance-tests/reports \
beierlm85dc2392021-04-14 13:51:53 -0400404 -v "${ROOTDIR}/tests/robot-systest/lib":${LOCAL_MOUNT_1} \
405 -v "${ROOTDIR}/tests/robot-systest/resources":${LOCAL_MOUNT_2} \
406 -v "${ROOTDIR}/tests/robot-systest/testsuite":${LOCAL_MOUNT_3} \
David Garciaf0014df2021-06-16 10:37:34 +0200407 opensourcemano/tests:$OSM_TESTS_IMAGE_TAG
beierlm85dc2392021-04-14 13:51:53 -0400408}
409
410function update_osm_module() {
411 MODULES=$(_find_module_dockerfile $1)
412 for MODULE in ${MODULES} ; do
413 MODULE=${MODULE,,}
414 echo "Updating ${MODULE}"
415 juju attach-resource ${MODULE} image=localhost:32000/opensourcemano/${MODULE}:devel
416 done
417}
418
419if [ "$0" != "$BASH_SOURCE" ]; then
420
421 _osm_local_build()
422 {
423 OPTIONS=$(show_help | sed '0,/^OPTIONS:$/d' | awk '{print $1}')
424 COMPREPLY=($(compgen -W "${OPTIONS}" -- "${COMP_WORDS[-1]}"))
425 }
426
427 THIS_SCRIPT="$(basename ${BASH_SOURCE[0]})"
428 echo "Setting up bash completion for ${THIS_SCRIPT}"
429 complete -F _osm_local_build "${THIS_SCRIPT}"
430else
431 check_arguments $@
432
433 eval "${INSTALL_HTTPD}"
434 eval "${INSTALL_LOCAL_REGISTRY}"
435 eval "${INSTALL_MICROSTACK}"
436 eval "${STAGE_2}"
437 eval "${STAGE_3}"
438 eval "${REGISTRY_PUSH}"
439 eval "${INSTALL_OSM}"
440 eval "${UPDATE_INSTALL}"
441 eval "${START_ROBOT}"
442fi