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