Add OOM provisioning script
[integration.git] / bootstrap / vagrant-onap / lib / functions
index de6f9ee..9531fc7 100755 (executable)
@@ -1,14 +1,15 @@
 #!/bin/bash
 
 source /var/onap/commons
+source /var/onap/config/env-vars
 source /var/onap/_composed_functions
 source /var/onap/_onap_functions
 
 export MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' |sort -n | head -1)
-export IP_ADDRESS=$(ifconfig eth0 | grep "inet addr" | tr -s ' ' | cut -d' ' -f3 | cut -d':' -f2)
+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
-git_src_folder=/opt
 
 # configure_dns() - DNS/GW IP address configuration
 function configure_dns {
@@ -16,6 +17,14 @@ function configure_dns {
     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
@@ -42,7 +51,7 @@ function _git_timed {
 
 # clone_repo() - Clone Git repository into specific folder
 function clone_repo {
-    local repo_url=https://git.onap.org/
+    local repo_url=${3:-"https://git.onap.org/"}
     local repo=$1
     local dest_folder=${2:-$git_src_folder/$repo}
     if [ ! -d $dest_folder ]; then
@@ -58,9 +67,14 @@ function clone_repo {
     fi
 }
 
-# install_dev_tools() - Install basic dependencies
-function install_dev_tools {
-    install_packages apt-transport-https ca-certificates curl
+# 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
 }
 
 # _install_bind() - Install bind utils
@@ -73,8 +87,17 @@ function install_java {
     if is_package_installed openjdk-8-jdk; then
         return
     fi
-    install_package software-properties-common
-    add-apt-repository -y ppa:openjdk-r/ppa
+    source /etc/os-release || source /usr/lib/os-release
+    case ${ID,,} in
+        *suse)
+        ;;
+        ubuntu|debian)
+            install_package software-properties-common
+            add-apt-repository -y ppa:openjdk-r/ppa
+        ;;
+        rhel|centos|fedora)
+        ;;
+    esac
     update_repos
 
     # Remove Java 7
@@ -91,8 +114,17 @@ function install_maven {
         return
     fi
     install_java
-    install_package software-properties-common
-    add-apt-repository -y ppa:andrei-pozolotin/maven3
+    source /etc/os-release || source /usr/lib/os-release
+    case ${ID,,} in
+        *suse)
+        ;;
+        ubuntu|debian)
+            install_package software-properties-common
+            add-apt-repository -y ppa:andrei-pozolotin/maven3
+        ;;
+        rhel|centos|fedora)
+        ;;
+    esac
     update_repos
     install_package maven3
 
@@ -107,6 +139,7 @@ function _configure_docker_settings {
     local docker_conf_backup=/tmp/docker.backup
     local docker_conf=/etc/default/docker
     local chameleonsocks_filename=chameleonsocks.sh
+    local max_concurrent_downloads=${1:-3}
 
     cp $docker_conf $docker_conf_backup
     if [ $http_proxy ]; then
@@ -128,8 +161,20 @@ function _configure_docker_settings {
     fi
     rm $docker_conf_backup
 
-    echo "DOCKER_OPTS=\"-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock\"" >> $docker_conf
-    usermod -a -G docker vagrant
+    echo "DOCKER_OPTS=\"-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --max-concurrent-downloads $max_concurrent_downloads \"" >> $docker_conf
+    usermod -aG docker $USER
+
+    source /etc/os-release || source /usr/lib/os-release
+    case ${ID,,} in
+        *suse)
+        ;;
+        ubuntu|debian)
+            service docker restart
+            sleep 10
+        ;;
+        rhel|centos|fedora)
+        ;;
+    esac
 }
 
 # install_nodejs() - Download and install NodeJS
@@ -152,35 +197,52 @@ function install_python {
 # _install_pip() - Install Python Package Manager
 function _install_pip {
     install_python
-    if [ ! -f /usr/local/bin/pip ]; then
+    if ! which pip; then
         curl -sL https://bootstrap.pypa.io/get-pip.py | python
     fi
 }
 
-# install_python_package() - Install a python module
+# install_python_package() - Install python modules
 function install_python_package {
-    local python_package=$1
+    local python_packages=$@
 
     _install_pip
-    pip install $python_package
+    pip install $python_packages
+}
+
+# install_python_requirements() - Install a list of python modules defined in requirement.txt file
+function install_python_requirements {
+    local python_project_path=$1
+
+    _install_pip
+    pushd $python_project_path
+    pip install -r requirements.txt
+    popd
 }
 
 # install_docker() - Download and install docker-engine
 function install_docker {
-    if is_package_installed docker-ce; then
+    if $(docker version &>/dev/null); then
         return
     fi
-    install_package software-properties-common
-    curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
-        add-apt-repository \
-        "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
-        $(lsb_release -cs) stable"
+    source /etc/os-release || source /usr/lib/os-release
+    case ${ID,,} in
+        *suse)
+        ;;
+        ubuntu|debian)
+            install_packages software-properties-common linux-image-extra-$(uname -r) linux-image-extra-virtual apt-transport-https ca-certificates curl
+            curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
+            add-apt-repository \
+            "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
+            $(lsb_release -cs) stable"
+        ;;
+        rhel|centos|fedora)
+        ;;
+    esac
     update_repos
 
     install_package docker-ce
     _configure_docker_settings
-    service docker restart
-    sleep 10
 }
 
 # pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub
@@ -194,6 +256,12 @@ function pull_docker_image {
     fi
 }
 
+# run_docker_image() - Starts a Docker instance
+function run_docker_image {
+    install_docker
+    docker run $@
+}
+
 # install_docker_compose() - Download and install docker-engine 
 function install_docker_compose {
     local docker_compose_version=${1:-1.12.0}
@@ -209,8 +277,8 @@ function _install_ODL {
     if [ ! -d /opt/opendaylight/current ]; then
         mkdir -p /opt/opendaylight/
         wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/
-        tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /opt/
-        mv "/opt/distribution-karaf-"$odl_version /opt/opendaylight/current
+        tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /tmp/
+        mv "/tmp/distribution-karaf-"$odl_version /opt/opendaylight/current
         rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz"
     fi
 }
@@ -242,36 +310,75 @@ function compile_src {
     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_maven
     install_docker
     pushd $src_folder
 
-    # Cleanup external repo
-    sed -i 's|${docker.push.registry}/||g' pom.xml
-    local mvn_docker="mvn clean package docker:build"
-    if [ $profile ]; then
-        mvn_docker+=" -P $profile"
-    fi
-    if [ $http_proxy ]; then
-        if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then
-            mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy"
+    if [ -f pom.xml ]; then
+        install_maven
+        # Cleanup external repo
+        sed -i 's|${docker.push.registry}/||g' pom.xml
+        local docker_build="mvn clean package docker:build"
+        if [ $profile ]; then
+            docker_build+=" -P $profile"
         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
-            mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy"
+            docker_build+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy"
         fi
-    fi
-    if [ $https_proxy ]; then
-        if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then
-            mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy"
         fi
-        if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then
-            mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy"
+        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
-    eval $mvn_docker
+    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
+}