Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 1 | #!/bin/bash |
| 2 | |
| 3 | set -o xtrace |
| 4 | |
| 5 | source /var/onap/commons |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 6 | source /var/onap/_composed_functions |
| 7 | source /var/onap/_onap_functions |
| 8 | |
| 9 | mvn_conf_file=/root/.m2/settings.xml |
| 10 | git_src_folder=/opt |
| 11 | |
| 12 | # export_env_vars() - Export environment variables |
| 13 | function export_env_vars { |
| 14 | export MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' |sort -n | head -1) |
| 15 | } |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 16 | |
| 17 | # configure_dns() - DNS/GW IP address configuration |
| 18 | function configure_dns { |
| 19 | echo "nameserver 10.0.0.1" >> /etc/resolvconf/resolv.conf.d/head |
| 20 | resolvconf -u |
| 21 | } |
| 22 | |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 23 | # _git_timed() - git can sometimes get itself infinitely stuck with transient network |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 24 | # errors or other issues with the remote end. This wraps git in a |
| 25 | # timeout/retry loop and is intended to watch over non-local git |
| 26 | # processes that might hang. |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 27 | function _git_timed { |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 28 | local count=0 |
| 29 | local timeout=0 |
| 30 | |
| 31 | install_package git |
| 32 | until timeout -s SIGINT ${timeout} git "$@"; do |
| 33 | # 124 is timeout(1)'s special return code when it reached the |
| 34 | # timeout; otherwise assume fatal failure |
| 35 | if [[ $? -ne 124 ]]; then |
| 36 | exit 1 |
| 37 | fi |
| 38 | |
| 39 | count=$(($count + 1)) |
| 40 | if [ $count -eq 3 ]; then |
| 41 | exit 1 |
| 42 | fi |
| 43 | sleep 5 |
| 44 | done |
| 45 | } |
| 46 | |
| 47 | # clone_repo() - Clone Git repository into specific folder |
| 48 | function clone_repo { |
| 49 | local repo_url=https://git.onap.org/ |
| 50 | local repo=$1 |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 51 | local dest_folder=${2:-$git_src_folder/$repo} |
| 52 | if [ ! -d $dest_folder ]; then |
| 53 | _git_timed clone -b $gerrit_branch --single-branch ${repo_url}${repo} $dest_folder |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 54 | else |
| 55 | pushd $dest_folder |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 56 | _git_timed pull |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 57 | popd |
| 58 | fi |
| 59 | } |
| 60 | |
| 61 | # install_dev_tools() - Install basic dependencies |
| 62 | function install_dev_tools { |
| 63 | install_package apt-transport-https |
| 64 | install_package ca-certificates |
| 65 | install_package curl |
| 66 | } |
| 67 | |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 68 | # _install_bind() - Install bind utils |
| 69 | function _install_bind { |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 70 | install_package bind9 |
| 71 | install_package bind9utils |
| 72 | } |
| 73 | |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 74 | # install_java() - Install java binaries |
| 75 | function install_java { |
| 76 | install_package software-properties-common |
| 77 | add-apt-repository -y ppa:openjdk-r/ppa |
| 78 | install_package openjdk-8-jdk |
| 79 | } |
| 80 | |
| 81 | # install_maven() - Install maven binaries |
| 82 | function install_maven { |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 83 | if is_package_installed maven3; then |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 84 | return |
| 85 | fi |
| 86 | if ! is_package_installed openjdk-8-jdk; then |
| 87 | install_java |
| 88 | fi |
| 89 | install_package software-properties-common |
| 90 | add-apt-repository -y ppa:andrei-pozolotin/maven3 |
| 91 | install_package maven3 |
| 92 | |
| 93 | # Force Maven3 to use jdk8 |
| 94 | apt-get purge openjdk-7-jdk -y |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 95 | |
| 96 | _configure_maven |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 99 | # _configure_docker_proxy() - Configures proxy in Docker from ENV |
| 100 | function _configure_docker_proxy { |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 101 | if [ $http_proxy ]; then |
| 102 | echo "export http_proxy=$http_proxy" >> /etc/default/docker |
| 103 | fi |
| 104 | if [ $https_proxy ]; then |
| 105 | echo "export https_proxy=$https_proxy" >> /etc/default/docker |
| 106 | fi |
| 107 | } |
| 108 | |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 109 | # install_nodejs() - Download and install NodeJS |
| 110 | function install_nodejs { |
| 111 | if is_package_installed nodejs; then |
| 112 | return |
| 113 | fi |
| 114 | curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - |
| 115 | install_package nodejs |
| 116 | |
| 117 | # Update NPM to latest version |
| 118 | npm install npm -g |
| 119 | } |
| 120 | |
| 121 | # install_python() - Install Python 2.7 and other tools necessary for development. |
| 122 | function install_python { |
| 123 | install_package python2.7 |
| 124 | install_package python-dev |
| 125 | curl -sL https://bootstrap.pypa.io/get-pip.py | python |
| 126 | pip install tox |
| 127 | } |
| 128 | |
| 129 | # install_docker() - Download and install docker-engine |
| 130 | function install_docker { |
| 131 | if is_package_installed docker-ce; then |
| 132 | return |
| 133 | fi |
| 134 | install_package software-properties-common |
| 135 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - |
| 136 | add-apt-repository \ |
| 137 | "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ |
| 138 | $(lsb_release -cs) \ |
| 139 | stable" |
| 140 | install_package docker-ce |
| 141 | _configure_docker_proxy |
| 142 | service docker restart |
| 143 | sleep 10 |
| 144 | } |
| 145 | |
| 146 | # pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub |
| 147 | function pull_docker_image { |
| 148 | install_docker |
| 149 | local image=$1 |
| 150 | local tag=$2 |
| 151 | docker pull ${image} |
| 152 | if [ ${tag} ]; then |
| 153 | docker tag ${image} $tag |
| 154 | fi |
| 155 | } |
| 156 | |
| 157 | # install_docker_compose() - Download and install docker-engine |
| 158 | function install_docker_compose { |
| 159 | local docker_compose_version=${1:-1.12.0} |
| 160 | if [ ! -d /opt/docker ]; then |
| 161 | mkdir /opt/docker |
| 162 | curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose |
| 163 | chmod +x /opt/docker/docker-compose |
| 164 | fi |
| 165 | } |
| 166 | |
| 167 | # _install_ODL() - Download and Install OpenDayLight SDN controller |
| 168 | function _install_ODL { |
| 169 | if [ ! -d /opt/opendaylight/current ]; then |
| 170 | mkdir -p /opt/opendaylight/ |
| 171 | wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/ |
| 172 | tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /opt/ |
| 173 | mv "/opt/distribution-karaf-"$odl_version /opt/opendaylight/current |
| 174 | rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz" |
| 175 | fi |
| 176 | } |
| 177 | |
| 178 | # start_ODL() - Start OpenDayLight SDN controller |
| 179 | function start_ODL { |
| 180 | _install_ODL |
| 181 | if [ -d /opt/opendaylight ]; then |
| 182 | export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre |
| 183 | /opt/opendaylight/current/bin/start |
| 184 | sleep 180 |
| 185 | /opt/opendaylight/current/bin/client feature:install odl-dlux-all |
| 186 | fi |
| 187 | } |
| 188 | |
| 189 | # compile_src() - Function that compiles the java source code thru maven |
| 190 | function compile_src { |
| 191 | local src_folder=$1 |
| 192 | pushd $src_folder |
| 193 | if [ -f pom.xml ]; then |
| 194 | install_maven |
| 195 | mvn clean install -U -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none |
| 196 | fi |
| 197 | popd |
| 198 | } |
| 199 | |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 200 | # build_docker_image() - Build Docker container image from source code |
| 201 | function build_docker_image { |
| 202 | local src_folder=$1 |
| 203 | local profile=$2 |
| 204 | install_maven |
| 205 | install_docker |
| 206 | pushd $src_folder |
| 207 | |
| 208 | # Cleanup external repo |
| 209 | sed -i 's|${docker.push.registry}/||g' pom.xml |
| 210 | local mvn_docker="mvn clean package docker:build" |
| 211 | if [ $profile ]; then |
| 212 | mvn_docker+=" -P $profile" |
| 213 | fi |
| 214 | if [ $http_proxy ]; then |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 215 | if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then |
| 216 | mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy" |
| 217 | fi |
| 218 | if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then |
| 219 | mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy" |
| 220 | fi |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 221 | fi |
| 222 | if [ $https_proxy ]; then |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 223 | if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then |
| 224 | mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy" |
| 225 | fi |
| 226 | if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then |
| 227 | mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy" |
| 228 | fi |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 229 | fi |
| 230 | eval $mvn_docker |
| 231 | popd |
| 232 | } |