Bug 1858: Adding APT proxy
[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="$( cd "${HOME}/snap/qhttp/common" &> /dev/null && pwd )"
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 OSM_TESTS_IMAGE_TAG="devel"
29
30 function check_arguments(){
31 while [ $# -gt 0 ] ; do
32 case $1 in
33 --debug) set -x ;;
34 --apt-proxy) APT_PROXY="$2" && shift ;;
35 --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 ;;
47 --vim-vca) VIM_VCA="$2" && shift;;
48 --osm-tests-image-tag) OSM_TESTS_IMAGE_TAG="$2" && shift;;
49 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
64 function show_help() {
65 cat << EOF
66 Usage: $0 [OPTIONS]
67 Perform a local build and potential installation of OSM from sources, using the
68 same process as Jenkins.
69
70 OPTIONS:
71 --help display this help message
72 --apt-proxy provide an apt proxy to docker build steps
73 --debug enable set -x for this script
74 --install-local-registry install and enable Microk8s local registry on port 32000
75 --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
83 --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
85 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
91 EOF
92 }
93
94 function print_section() {
95 echo "$@"
96 }
97
98 function install_local_registry() {
99 sudo snap install microk8s --classic
100 microk8s status --wait-ready
101 microk8s.enable registry
102 }
103
104 function install_microstack() {
105 sudo snap install microstack --devmode --edge
106 sudo snap set microstack config.network.ports.dashboard=8080
107 sudo microstack.init --auto --control
108
109 sudo snap alias microstack.openstack openstack
110 . /var/snap/microstack/common/etc/microstack.rc
111
112 for i in $(microstack.openstack security group list | awk '/default/{ print $2 }'); do
113 microstack.openstack security group rule create $i --protocol icmp --remote-ip 0.0.0.0/0
114 microstack.openstack security group rule create $i --protocol tcp --remote-ip 0.0.0.0/0
115 done
116
117 microstack.openstack network create --enable --no-share osm-ext
118 microstack.openstack subnet create osm-ext-subnet --network osm-ext --dns-nameserver 8.8.8.8 \
119 --subnet-range 172.30.0.0/24
120 microstack.openstack router create external-router
121 microstack.openstack router add subnet external-router osm-ext-subnet
122 microstack.openstack router set --external-gateway external external-router
123
124 curl -L https://github.com/cirros-dev/cirros/releases/download/0.3.5/cirros-0.3.5-x86_64-disk.img \
125 | microstack.openstack image create --public --container-format=bare \
126 --disk-format=qcow2 cirros-0.3.5-x86_64-disk.img
127 curl https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img \
128 | microstack.openstack image create --public --container-format=bare \
129 --disk-format=qcow2 ubuntu16.04
130 curl https://cloud-images.ubuntu.com/xenial/current/xenial-server-cloudimg-amd64-disk1.img \
131 | microstack.openstack image create --public --container-format=bare \
132 --disk-format=qcow2 US1604
133 curl https://cloud-images.ubuntu.com/bionic/current/bionic-server-cloudimg-amd64.img \
134 | microstack.openstack image create --public --container-format=bare \
135 --disk-format=qcow2 ubuntu18.04
136 curl https://cloud-images.ubuntu.com/focal/current/focal-server-cloudimg-amd64.img \
137 | microstack.openstack image create --public --container-format=bare \
138 --disk-format=qcow2 ubuntu20.04
139 }
140
141 function install_qhttpd() {
142 sudo snap install qhttp
143 EXISTING_PID=$(ps auxw | grep "http.server 11480" | grep -v grep | awk '{print $2}')
144 if [ ! -z $EXISTING_PID ] ; then
145 kill $EXISTING_PID
146 fi
147 qhttp -p ${HTTPPORT} &
148 }
149
150 function stage_2() {
151 print_section "Performing Stage 2"
152 MODULES="common devops IM LCM MON N2VC NBI NG-UI osmclient PLA POL RO tests"
153 if [ ! -z ${1} ] ; then
154 POSSIBLE_MODULES=$(echo ${1} | sed "s/,/ /g")
155 for MODULE in ${POSSIBLE_MODULES}; do
156 if ! echo "${MODULES}" | grep -q "${MODULE}" ; then
157 echo "Unknown stage 2 module ${MODULE}"
158 echo "Must be one of ${MODULES}"
159 exit 1
160 fi
161 done
162 MODULES=${POSSIBLE_MODULES}
163 else
164 print_section "Cleaning HTTP Directory for full build"
165 rm -fv ${HTTPDDIR}/*.deb
166 fi
167
168 for MODULE in ${MODULES} ; do
169 cd "${ROOTDIR}"
170 if [ ! -d ${MODULE} ] ; then
171 echo "Directory ${ROOTDIR}/${MODULE} does not exist"
172 exit 1
173 fi
174 print_section "Building ${MODULE}"
175 cd ${MODULE}
176 find . -name '*.deb' -exec rm -v {} \;
177
178 BUILD_ARGS=""
179 if [ ! -z $APT_PROXY ] ; then
180 BUILD_ARGS="${BUILD_ARGS}--build-arg APT_PROXY=${APT_PROXY} "
181 fi
182 docker build ${NO_CACHE} ${BUILD_ARGS} -t ${MODULE,,}-stage2 .
183
184 STAGES="stage-build.sh"
185 if [ ! -z $TESTS ] ; then
186 STAGES="stage-test.sh ${STAGES}"
187 fi
188 for STAGE in $STAGES ; do
189 docker run -ti \
190 -v "$(pwd):/build" \
191 -w /build \
192 ${MODULE,,}-stage2 \
193 bash -c "groupadd -o -g $(id -g) -r $USER ;
194 useradd -o -u $(id -u) -d /build -r -g $USER $USER ;
195 runuser $USER -c devops-stages/${STAGE}"
196 if [ $? -ne 0 ] ; then
197 print_section "Failed to build ${MODULE}"
198 exit 1
199 fi
200 done
201
202 find . -name '*.deb' -exec mv -v {} ${HTTPDDIR}/ \;
203 done
204 }
205
206 function _find_module_dockerfile() {
207 cd "${ROOTDIR}/devops/docker"
208 MODULES=`find . -name Dockerfile -printf '%h\n' |sed 's|\./||' |sort |tr '\n' ' '`
209 if [ ! -z ${1} ] ; then
210 POSSIBLE_MODULES=$(echo ${1} | sed "s/,/ /g")
211 for MODULE in ${POSSIBLE_MODULES}; do
212 if ! echo "${MODULES}" | grep -q "${MODULE}" ; then
213 echo "Unknown stage 3 module ${MODULE}"
214 echo "Must be one of ${MODULES}"
215 exit 1
216 fi
217 done
218 echo ${POSSIBLE_MODULES}
219 else
220 echo ${MODULES}
221 fi
222 }
223
224 function stage_3() {
225 print_section "Performing Stage 3"
226 MODULES=$(_find_module_dockerfile $1)
227 BUILD_ARGS=""
228 if [ ! -z $APT_PROXY ] ; then
229 BUILD_ARGS="${BUILD_ARGS}--build-arg APT_PROXY=${APT_PROXY} "
230 fi
231
232 HOSTIP=$(ip -4 addr show docker0 | grep -Po 'inet \K[\d.]+')
233 for file in ~/snap/qhttp/common/*.deb ; do
234 file=`basename ${file}`
235 name=`echo ${file} | cut -d_ -f1 | sed "s/-/_/g"`;
236 name=${name^^}_URL
237 BUILD_ARGS="${BUILD_ARGS}--build-arg ${name}=http://$HOSTIP:${HTTPPORT}/$file "
238 echo Added ${name} as http://$HOSTIP:${HTTPPORT}/$file
239 done
240
241 for MODULE in ${MODULES} ; do
242 cd "${ROOTDIR}/devops/docker"
243 if [ ! -d ${MODULE} ] ; then
244 echo "Directory ${ROOTDIR}/${MODULE} does not exist"
245 exit 1
246 fi
247 print_section "Building ${MODULE}"
248 cd ${MODULE}
249 MODULE=${MODULE,,}
250 docker build ${NO_CACHE} -t opensourcemano/${MODULE}:devel ${BUILD_ARGS} .
251 if [ $? -ne 0 ] ; then
252 print_section "Failed to build ${MODULE}"
253 exit 1
254 fi
255 done
256 }
257
258 function local_registry_push() {
259 print_section "Pushing to local registry"
260 cd "${ROOTDIR}/devops/docker"
261 MODULES=`find . -name Dockerfile -printf '%h\n' |sed 's|\./||' |sort |tr '\n' ' '`
262 if [ ! -z ${1} ] ; then
263 POSSIBLE_MODULES=$(echo ${1} | sed "s/,/ /g")
264 for MODULE in ${POSSIBLE_MODULES}; do
265 echo "${MODULE}"
266 if ! echo "${MODULES}" | grep -q "${MODULE}" ; then
267 echo "Unknown stage 3 module ${MODULE}"
268 echo "Must be one of ${MODULES}"
269 exit 1
270 fi
271 done
272 MODULES=${POSSIBLE_MODULES}
273 fi
274 for MODULE in ${MODULES} ; do
275 MODULE=${MODULE,,}
276 docker tag opensourcemano/${MODULE}:devel ${REGISTRY}/opensourcemano/${MODULE}:devel
277 docker push ${REGISTRY}/opensourcemano/${MODULE}:devel
278 done
279 }
280
281 function install_osm() {
282 cd "${ROOTDIR}/devops/installers"
283 VCA=""
284 if juju controllers 2>/dev/null| grep osm-vca ; then
285 VCA="--vca osm-vca"
286 fi
287 ./charmed_install.sh --registry localhost:32000 --tag devel ${VCA}
288 }
289
290 function start_robot() {
291 mkdir -p "${ROOTDIR}/tests/local"
292 cd "${ROOTDIR}/tests/local"
293
294 . ${OPENSTACKRC}
295
296 # Workaround for microstack auth URL
297 if [ ${OPENSTACKRC} == "/var/snap/microstack/common/etc/microstack.rc" ] ; then
298 export OS_AUTH_URL=${OS_AUTH_URL}/v3
299 fi
300
301 export OSM_HOSTNAME=$(juju config -m osm nbi site_url | sed "s/http.*\?:\/\///"):443
302 export PROMETHEUS_HOSTNAME=$(juju config -m osm prometheus site_url | sed "s/http.*\?:\/\///")
303 export PROMETHEUS_PORT=80
304 export JUJU_PASSWORD=`juju gui 2>&1 | grep password | awk '{print $2}'`
305 export HOSTIP=$(echo $PROMETHEUS_HOSTNAME | sed "s/prometheus.//" | sed "s/.nip.io//")
306
307 rm robot-systest.cfg
308 for line in `env | grep "^OS_" | sort` ; do echo $line >> robot-systest.cfg ; done
309 cat << EOF >> robot-systest.cfg
310 VIM_TARGET=osm
311 VIM_MGMT_NET=osm-ext
312 ENVIRONMENTS_FOLDER=environments
313 PACKAGES_FOLDER=/robot-systest/osm-packages
314 OS_CLOUD=openstack
315 LC_ALL=C.UTF-8
316 LANG=C.UTF-8
317 EOF
318
319 cat << EOF > robot.etc.hosts
320 127.0.0.1 localhost
321 ${HOSTIP} prometheus.${HOSTIP}.nip.io nbi.${HOSTIP}.nip.io
322 EOF
323 cat << EOF > clouds.yaml
324 clouds:
325 openstack:
326 auth:
327 auth_url: $OS_AUTH_URL
328 project_name: $OS_PROJECT_NAME
329 username: $OS_USERNAME
330 password: $OS_PASSWORD
331 user_domain_name: $OS_USER_DOMAIN_NAME
332 project_domain_name: $OS_PROJECT_DOMAIN_NAME
333 EOF
334
335 VIM_AUTH_URL=$(osm vim-show osm | grep vim_url | awk '{print $4}' | tr -d \")
336 if [[ ! -z ${VIM_AUTH_URL} && "$OS_AUTH_URL" != "${VIM_AUTH_URL}" ]] ; then
337 echo "Deleting existing VIM osm as auth URLs have changed"
338 osm vim-delete osm
339 fi
340
341 if ! osm vim-show osm &> /dev/null ; then
342 echo "Creating VIM osm"
343 if [ -v VIM_VCA ]; then
344 VCA_OPT="--vca $VIM_VCA"
345 fi
346 osm vim-create --name osm $VCA_OPT --user "$OS_USERNAME" --password "$OS_PASSWORD" \
347 --auth_url "$OS_AUTH_URL" --tenant "$OS_USERNAME" --account_type openstack \
348 --config='{use_floating_ip: True,
349 management_network_name: osm-ext}'
350 fi
351
352 if [ ! -z $ROBOT_LOCAL ] ; then
353 LOCAL_MOUNT_1="/robot-systest/lib"
354 LOCAL_MOUNT_2="/robot-systest/resources"
355 LOCAL_MOUNT_3="/robot-systest/testsuite"
356 else
357 LOCAL_MOUNT_1="/tmp/lib"
358 LOCAL_MOUNT_2="/tmp/resources"
359 LOCAL_MOUNT_3="/tmp/testsuite"
360 fi
361
362 mkdir -p reports
363
364 docker run -ti --entrypoint /bin/bash \
365 --env OSM_HOSTNAME=${OSM_HOSTNAME} \
366 --env PROMETHEUS_HOSTNAME=${PROMETHEUS_HOSTNAME} \
367 --env PROMETHEUS_PORT=${PROMETHEUS_PORT} \
368 --env JUJU_PASSWORD=${JUJU_PASSWORD} \
369 --env HOSTIP=${HOSTIP} \
370 --env-file robot-systest.cfg \
371 -v "$(pwd)/robot.etc.hosts":/etc/hosts \
372 -v ~/.osm/microk8s-config.yaml:/root/.kube/config \
373 -v "$(pwd)/clouds.yaml":/etc/openstack/clouds.yaml \
374 -v "${HOME}/snap/qhttp/common"/robot-systest/reports \
375 -v "${HOME}/snap/qhttp/common:"/robot-systest/conformance-tests/reports \
376 -v "${ROOTDIR}/tests/robot-systest/lib":${LOCAL_MOUNT_1} \
377 -v "${ROOTDIR}/tests/robot-systest/resources":${LOCAL_MOUNT_2} \
378 -v "${ROOTDIR}/tests/robot-systest/testsuite":${LOCAL_MOUNT_3} \
379 opensourcemano/tests:$OSM_TESTS_IMAGE_TAG
380 }
381
382 function update_osm_module() {
383 MODULES=$(_find_module_dockerfile $1)
384 for MODULE in ${MODULES} ; do
385 MODULE=${MODULE,,}
386 echo "Updating ${MODULE}"
387 juju attach-resource ${MODULE} image=localhost:32000/opensourcemano/${MODULE}:devel
388 done
389 }
390
391 if [ "$0" != "$BASH_SOURCE" ]; then
392
393 _osm_local_build()
394 {
395 OPTIONS=$(show_help | sed '0,/^OPTIONS:$/d' | awk '{print $1}')
396 COMPREPLY=($(compgen -W "${OPTIONS}" -- "${COMP_WORDS[-1]}"))
397 }
398
399 THIS_SCRIPT="$(basename ${BASH_SOURCE[0]})"
400 echo "Setting up bash completion for ${THIS_SCRIPT}"
401 complete -F _osm_local_build "${THIS_SCRIPT}"
402 else
403 check_arguments $@
404
405 eval "${INSTALL_HTTPD}"
406 eval "${INSTALL_LOCAL_REGISTRY}"
407 eval "${INSTALL_MICROSTACK}"
408 eval "${STAGE_2}"
409 eval "${STAGE_3}"
410 eval "${REGISTRY_PUSH}"
411 eval "${INSTALL_OSM}"
412 eval "${UPDATE_INSTALL}"
413 eval "${START_ROBOT}"
414 fi