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 { |
Victor Morales | ece790c | 2017-08-08 11:11:36 -0500 | [diff] [blame^] | 76 | if is_package_installed openjdk-8-jdk; then |
| 77 | return |
| 78 | fi |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 79 | install_package software-properties-common |
| 80 | add-apt-repository -y ppa:openjdk-r/ppa |
| 81 | install_package openjdk-8-jdk |
Victor Morales | 455bece | 2017-07-31 18:40:39 -0500 | [diff] [blame] | 82 | # ca-certificates-java is not a dependency in the Oracle JDK/JRE so this must be explicitly installed. |
| 83 | /var/lib/dpkg/info/ca-certificates-java.postinst configure |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 84 | } |
| 85 | |
| 86 | # install_maven() - Install maven binaries |
| 87 | function install_maven { |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 88 | if is_package_installed maven3; then |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 89 | return |
| 90 | fi |
Victor Morales | ece790c | 2017-08-08 11:11:36 -0500 | [diff] [blame^] | 91 | install_java |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 92 | install_package software-properties-common |
| 93 | add-apt-repository -y ppa:andrei-pozolotin/maven3 |
| 94 | install_package maven3 |
| 95 | |
| 96 | # Force Maven3 to use jdk8 |
| 97 | apt-get purge openjdk-7-jdk -y |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 98 | |
| 99 | _configure_maven |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 100 | } |
| 101 | |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 102 | # _configure_docker_proxy() - Configures proxy in Docker from ENV |
| 103 | function _configure_docker_proxy { |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 104 | if [ $http_proxy ]; then |
| 105 | echo "export http_proxy=$http_proxy" >> /etc/default/docker |
| 106 | fi |
| 107 | if [ $https_proxy ]; then |
| 108 | echo "export https_proxy=$https_proxy" >> /etc/default/docker |
| 109 | fi |
| 110 | } |
| 111 | |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 112 | # install_nodejs() - Download and install NodeJS |
| 113 | function install_nodejs { |
| 114 | if is_package_installed nodejs; then |
| 115 | return |
| 116 | fi |
| 117 | curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash - |
| 118 | install_package nodejs |
| 119 | |
| 120 | # Update NPM to latest version |
| 121 | npm install npm -g |
| 122 | } |
| 123 | |
| 124 | # install_python() - Install Python 2.7 and other tools necessary for development. |
| 125 | function install_python { |
| 126 | install_package python2.7 |
| 127 | install_package python-dev |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 128 | } |
| 129 | |
Victor Morales | 970ec19 | 2017-07-31 09:10:11 -0500 | [diff] [blame] | 130 | # _install_pip() - Install Python Package Manager |
| 131 | function _install_pip { |
| 132 | install_python |
Victor Morales | 158c18c | 2017-08-06 11:23:15 -0500 | [diff] [blame] | 133 | if [ ! -f /usr/local/bin/pip ]; then |
| 134 | curl -sL https://bootstrap.pypa.io/get-pip.py | python |
| 135 | fi |
Victor Morales | 970ec19 | 2017-07-31 09:10:11 -0500 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | # install_python_package() - Install a python module |
| 139 | function install_python_package { |
| 140 | local python_package=$1 |
| 141 | |
| 142 | _install_pip |
| 143 | pip install $python_package |
| 144 | } |
| 145 | |
| 146 | # install_docker() - Download and install docker-engine |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 147 | function install_docker { |
| 148 | if is_package_installed docker-ce; then |
| 149 | return |
| 150 | fi |
| 151 | install_package software-properties-common |
| 152 | curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add - |
| 153 | add-apt-repository \ |
| 154 | "deb [arch=amd64] https://download.docker.com/linux/ubuntu \ |
| 155 | $(lsb_release -cs) \ |
| 156 | stable" |
| 157 | install_package docker-ce |
| 158 | _configure_docker_proxy |
| 159 | service docker restart |
| 160 | sleep 10 |
| 161 | } |
| 162 | |
| 163 | # pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub |
| 164 | function pull_docker_image { |
| 165 | install_docker |
| 166 | local image=$1 |
| 167 | local tag=$2 |
| 168 | docker pull ${image} |
| 169 | if [ ${tag} ]; then |
| 170 | docker tag ${image} $tag |
| 171 | fi |
| 172 | } |
| 173 | |
| 174 | # install_docker_compose() - Download and install docker-engine |
| 175 | function install_docker_compose { |
| 176 | local docker_compose_version=${1:-1.12.0} |
| 177 | if [ ! -d /opt/docker ]; then |
| 178 | mkdir /opt/docker |
| 179 | curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose |
| 180 | chmod +x /opt/docker/docker-compose |
| 181 | fi |
| 182 | } |
| 183 | |
| 184 | # _install_ODL() - Download and Install OpenDayLight SDN controller |
| 185 | function _install_ODL { |
| 186 | if [ ! -d /opt/opendaylight/current ]; then |
| 187 | mkdir -p /opt/opendaylight/ |
| 188 | wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/ |
| 189 | tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /opt/ |
| 190 | mv "/opt/distribution-karaf-"$odl_version /opt/opendaylight/current |
| 191 | rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz" |
| 192 | fi |
| 193 | } |
| 194 | |
| 195 | # start_ODL() - Start OpenDayLight SDN controller |
| 196 | function start_ODL { |
| 197 | _install_ODL |
| 198 | if [ -d /opt/opendaylight ]; then |
| 199 | export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre |
| 200 | /opt/opendaylight/current/bin/start |
| 201 | sleep 180 |
| 202 | /opt/opendaylight/current/bin/client feature:install odl-dlux-all |
| 203 | fi |
| 204 | } |
| 205 | |
| 206 | # compile_src() - Function that compiles the java source code thru maven |
| 207 | function compile_src { |
| 208 | local src_folder=$1 |
| 209 | pushd $src_folder |
| 210 | if [ -f pom.xml ]; then |
| 211 | install_maven |
Victor Morales | 970ec19 | 2017-07-31 09:10:11 -0500 | [diff] [blame] | 212 | 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] | 213 | fi |
| 214 | popd |
| 215 | } |
| 216 | |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 217 | # build_docker_image() - Build Docker container image from source code |
| 218 | function build_docker_image { |
| 219 | local src_folder=$1 |
| 220 | local profile=$2 |
| 221 | install_maven |
| 222 | install_docker |
| 223 | pushd $src_folder |
| 224 | |
| 225 | # Cleanup external repo |
| 226 | sed -i 's|${docker.push.registry}/||g' pom.xml |
| 227 | local mvn_docker="mvn clean package docker:build" |
| 228 | if [ $profile ]; then |
| 229 | mvn_docker+=" -P $profile" |
| 230 | fi |
| 231 | if [ $http_proxy ]; then |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 232 | if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then |
| 233 | mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy" |
| 234 | fi |
| 235 | if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then |
| 236 | mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy" |
| 237 | fi |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 238 | fi |
| 239 | if [ $https_proxy ]; then |
Victor Morales | dd07480 | 2017-07-26 16:06:35 -0500 | [diff] [blame] | 240 | if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then |
| 241 | mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy" |
| 242 | fi |
| 243 | if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then |
| 244 | mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy" |
| 245 | fi |
Victor Morales | 89ce321 | 2017-06-16 18:32:48 -0500 | [diff] [blame] | 246 | fi |
| 247 | eval $mvn_docker |
| 248 | popd |
| 249 | } |