blob: d0a7d978718fb33b042741a4730e65bf5356e353 [file] [log] [blame]
Victor Morales89ce3212017-06-16 18:32:48 -05001#!/bin/bash
2
3set -o xtrace
4
5source /var/onap/commons
Victor Moralesdd074802017-07-26 16:06:35 -05006source /var/onap/_composed_functions
7source /var/onap/_onap_functions
8
9mvn_conf_file=/root/.m2/settings.xml
10git_src_folder=/opt
11
12# export_env_vars() - Export environment variables
13function export_env_vars {
14 export MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' |sort -n | head -1)
15}
Victor Morales89ce3212017-06-16 18:32:48 -050016
17# configure_dns() - DNS/GW IP address configuration
18function configure_dns {
19 echo "nameserver 10.0.0.1" >> /etc/resolvconf/resolv.conf.d/head
20 resolvconf -u
21}
22
Victor Moralesdd074802017-07-26 16:06:35 -050023# _git_timed() - git can sometimes get itself infinitely stuck with transient network
Victor Morales89ce3212017-06-16 18:32:48 -050024# 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 Moralesdd074802017-07-26 16:06:35 -050027function _git_timed {
Victor Morales89ce3212017-06-16 18:32:48 -050028 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
48function clone_repo {
49 local repo_url=https://git.onap.org/
50 local repo=$1
Victor Moralesdd074802017-07-26 16:06:35 -050051 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 Morales89ce3212017-06-16 18:32:48 -050054 else
55 pushd $dest_folder
Victor Moralesdd074802017-07-26 16:06:35 -050056 _git_timed pull
Victor Morales89ce3212017-06-16 18:32:48 -050057 popd
58 fi
59}
60
61# install_dev_tools() - Install basic dependencies
62function install_dev_tools {
63 install_package apt-transport-https
64 install_package ca-certificates
65 install_package curl
66}
67
Victor Moralesdd074802017-07-26 16:06:35 -050068# _install_bind() - Install bind utils
69function _install_bind {
Victor Morales89ce3212017-06-16 18:32:48 -050070 install_package bind9
71 install_package bind9utils
72}
73
Victor Morales89ce3212017-06-16 18:32:48 -050074# install_java() - Install java binaries
75function install_java {
Victor Moralesece790c2017-08-08 11:11:36 -050076 if is_package_installed openjdk-8-jdk; then
77 return
78 fi
Victor Morales89ce3212017-06-16 18:32:48 -050079 install_package software-properties-common
80 add-apt-repository -y ppa:openjdk-r/ppa
81 install_package openjdk-8-jdk
Victor Morales455bece2017-07-31 18:40:39 -050082 # 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 Morales89ce3212017-06-16 18:32:48 -050084}
85
86# install_maven() - Install maven binaries
87function install_maven {
Victor Moralesdd074802017-07-26 16:06:35 -050088 if is_package_installed maven3; then
Victor Morales89ce3212017-06-16 18:32:48 -050089 return
90 fi
Victor Moralesece790c2017-08-08 11:11:36 -050091 install_java
Victor Morales89ce3212017-06-16 18:32:48 -050092 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 Moralesdd074802017-07-26 16:06:35 -050098
99 _configure_maven
Victor Morales89ce3212017-06-16 18:32:48 -0500100}
101
Victor Moralesdd074802017-07-26 16:06:35 -0500102# _configure_docker_proxy() - Configures proxy in Docker from ENV
103function _configure_docker_proxy {
Victor Morales89ce3212017-06-16 18:32:48 -0500104 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 Moralesdd074802017-07-26 16:06:35 -0500112# install_nodejs() - Download and install NodeJS
113function 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.
125function install_python {
126 install_package python2.7
127 install_package python-dev
Victor Moralesdd074802017-07-26 16:06:35 -0500128}
129
Victor Morales970ec192017-07-31 09:10:11 -0500130# _install_pip() - Install Python Package Manager
131function _install_pip {
132 install_python
Victor Morales158c18c2017-08-06 11:23:15 -0500133 if [ ! -f /usr/local/bin/pip ]; then
134 curl -sL https://bootstrap.pypa.io/get-pip.py | python
135 fi
Victor Morales970ec192017-07-31 09:10:11 -0500136}
137
138# install_python_package() - Install a python module
139function 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 Moralesdd074802017-07-26 16:06:35 -0500147function 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
164function 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
175function 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
185function _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
196function 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
207function compile_src {
208 local src_folder=$1
209 pushd $src_folder
210 if [ -f pom.xml ]; then
211 install_maven
Victor Morales970ec192017-07-31 09:10:11 -0500212 mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none
Victor Moralesdd074802017-07-26 16:06:35 -0500213 fi
214 popd
215}
216
Victor Morales89ce3212017-06-16 18:32:48 -0500217# build_docker_image() - Build Docker container image from source code
218function 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 Moralesdd074802017-07-26 16:06:35 -0500232 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 Morales89ce3212017-06-16 18:32:48 -0500238 fi
239 if [ $https_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500240 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 Morales89ce3212017-06-16 18:32:48 -0500246 fi
247 eval $mvn_docker
248 popd
249}