#!/bin/bash # SPDX-license-identifier: Apache-2.0 ############################################################################## # Copyright (c) 2017-2018 # All rights reserved. This program and the accompanying materials # are made available under the terms of the Apache License, Version 2.0 # which accompanies this distribution, and is available at # http://www.apache.org/licenses/LICENSE-2.0 ############################################################################# source /var/onap/_commons source /var/onap/config/env-vars source /var/onap/_composed_functions source /var/onap/_onap_functions source /var/onap/_installers export MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' |sort -n | head -1) export NIC=$(ip route get 8.8.8.8 | awk '{ print $5; exit }') export IP_ADDRESS=$(ifconfig $NIC | grep "inet addr" | tr -s ' ' | cut -d' ' -f3 | cut -d':' -f2) mvn_conf_file=/root/.m2/settings.xml # configure_dns() - DNS/GW IP address configuration function configure_dns { echo "nameserver 10.0.0.1" >> /etc/resolvconf/resolv.conf.d/head resolvconf -u } # get_next_ip() - Function that provides the next ip function get_next_ip { local ip=${1:-$IP_ADDRESS} ip_hex=$(printf '%.2X%.2X%.2X%.2X\n' `echo $ip | sed -e 's/\./ /g'`) next_ip_hex=$(printf %.8X `echo $(( 0x$ip_hex + 1 ))`) echo $(printf '%d.%d.%d.%d\n' `echo $next_ip_hex | sed -r 's/(..)/0x\1 /g'`) } # _git_timed() - git can sometimes get itself infinitely stuck with transient network # errors or other issues with the remote end. This wraps git in a # timeout/retry loop and is intended to watch over non-local git # processes that might hang. function _git_timed { local count=0 local timeout=0 install_package git until timeout -s SIGINT ${timeout} git "$@"; do # 124 is timeout(1)'s special return code when it reached the # timeout; otherwise assume fatal failure if [[ $? -ne 124 ]]; then exit 1 fi count=$(($count + 1)) if [ $count -eq 3 ]; then exit 1 fi sleep 5 done } # clone_repo() - Clone Git repository into specific folder function clone_repo { local repo_url=${3:-$git_url} local repo=$1 local dest_folder=${2:-$git_src_folder/$repo} if [ ! -d $dest_folder ]; then if [[ "$debug" == "False" ]]; then _git_timed clone --quiet ${repo_url}${repo} $dest_folder else _git_timed clone ${repo_url}${repo} $dest_folder fi fi } # clone_repos() - Function that clones source repositories for a given project function clone_repos { local project=$1 local repo_name=${2:-$project} for repo in ${repos[$project]}; do clone_repo $repo ${src_folders[$project]}${repo#*$repo_name} done } # pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub function pull_docker_image { install_docker local image=$1 local tag=$2 echo "docker pull ${image}" docker pull ${image} if [ ${tag} ]; then docker tag ${image} $tag fi } # wait_docker_pull() - Function that waits for all docker pull processes function wait_docker_pull { local counter=60 local delay=${1:-60} sleep $delay while [ $(ps -ef | grep "docker pull" | wc -l) -gt 1 ]; do sleep $delay counter=$((counter - 1)) if [ "$counter" -eq 0 ]; then break fi done } # run_docker_image() - Starts a Docker instance function run_docker_image { install_docker docker run $@ } # run_docker_compose() - Ensures that docker compose is installed and run it in background function run_docker_compose { local folder=$1 install_docker_compose pushd $folder /opt/docker/docker-compose up -d popd } # start_ODL() - Start OpenDayLight SDN controller function start_ODL { _install_ODL if [ -d /opt/opendaylight ]; then export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre /opt/opendaylight/current/bin/start sleep 180 /opt/opendaylight/current/bin/client feature:install odl-dlux-all fi } # compile_src() - Function that compiles the java source code thru maven function compile_src { local src_folder=$1 pushd $src_folder local mvn_build='mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none' if [[ "$debug" == "False" ]]; then mvn_build+=" -q" fi if [ -f pom.xml ]; then install_maven echo "Compiling $src_folder folder..." eval $mvn_build fi popd } # compile_repos() - Function that compiles source repositories for a given project function compile_repos { local project=$1 for repo in ${repos[$project]}; do compile_src ${src_folders[$project]}${repo#*$project} done } # build_docker_image() - Build Docker container image from source code function build_docker_image { local src_folder=$1 local profile=$2 install_docker pushd $src_folder if [ -f pom.xml ]; then install_maven # Cleanup external repo sed -i 's|${docker.push.registry}/||g' pom.xml local docker_build="mvn package docker:build -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true" if [ $profile ]; then docker_build+=" -P $profile" fi if [[ "$debug" == "False" ]]; then docker_build+=" -q" fi if [ $http_proxy ]; then if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then docker_build+=" -Ddocker.buildArg.http_proxy=$http_proxy" fi if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then docker_build+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy" fi fi if [ $https_proxy ]; then if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then docker_build+=" -Ddocker.buildArg.https_proxy=$https_proxy" fi if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then docker_build+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy" fi fi elif [ -f Dockerfile ]; then # NOTE: Workaround for dmmapbc images sed -i '/LocalKey/d' Dockerfile sed -i "s/nexus3.onap.org\:10003\///g" Dockerfile local docker_build="docker build -t $profile -f ./Dockerfile ." if [ $http_proxy ]; then docker_build+=" --build-arg http_proxy=$http_proxy" docker_build+=" --build-arg HTTP_PROXY=$http_proxy" fi if [ $https_proxy ]; then docker_build+=" --build-arg https_proxy=$https_proxy" docker_build+=" --build-arg HTTPS_PROXY=$https_proxy" fi fi echo $docker_build eval $docker_build popd } # mount_external_partition() - Create partition and mount the external volume function mount_external_partition { local dev_name="/dev/$1" local mount_dir=$2 sfdisk $dev_name << EOF ; EOF mkfs -t ext4 ${dev_name}1 mkdir -p $mount_dir mount ${dev_name}1 $mount_dir echo "${dev_name}1 $mount_dir ext4 errors=remount-ro,noatime,barrier=0 0 1" >> /etc/fstab } # add_no_proxy_value() - Add no_proxy values into environment file, used for internal IPs generated at deploy time function add_no_proxy_value { if [[ `grep "no_proxy" /etc/environment` ]]; then sed -i.bak "s/^no_proxy.*$/&,$1/" /etc/environment else echo "no_proxy=$1" >> /etc/environment fi if [[ `grep "NO_PROXY" /etc/environment` ]]; then sed -i.bak "s/^NO_PROXY.*$/&,$1/" /etc/environment else echo "NO_PROXY=$1" >> /etc/environment fi }