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