blob: 458b34c61dde05ae159ab2d7f9bc82cfd4ebf74b [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)
Victor Morales2a2d04d8b2017-08-10 10:27:38 -050015 export IP_ADDRESS=$(ifconfig eth0 | grep "inet addr" | tr -s ' ' | cut -d' ' -f3 | cut -d':' -f2)
Victor Moralesdd074802017-07-26 16:06:35 -050016}
Victor Morales89ce3212017-06-16 18:32:48 -050017
18# configure_dns() - DNS/GW IP address configuration
19function configure_dns {
20 echo "nameserver 10.0.0.1" >> /etc/resolvconf/resolv.conf.d/head
21 resolvconf -u
22}
23
Victor Moralesdd074802017-07-26 16:06:35 -050024# _git_timed() - git can sometimes get itself infinitely stuck with transient network
Victor Morales89ce3212017-06-16 18:32:48 -050025# 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 Moralesdd074802017-07-26 16:06:35 -050028function _git_timed {
Victor Morales89ce3212017-06-16 18:32:48 -050029 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
49function clone_repo {
50 local repo_url=https://git.onap.org/
51 local repo=$1
Victor Moralesdd074802017-07-26 16:06:35 -050052 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 Morales89ce3212017-06-16 18:32:48 -050055 else
56 pushd $dest_folder
Victor Moralesdd074802017-07-26 16:06:35 -050057 _git_timed pull
Victor Morales89ce3212017-06-16 18:32:48 -050058 popd
59 fi
60}
61
62# install_dev_tools() - Install basic dependencies
63function install_dev_tools {
64 install_package apt-transport-https
65 install_package ca-certificates
66 install_package curl
67}
68
Victor Moralesdd074802017-07-26 16:06:35 -050069# _install_bind() - Install bind utils
70function _install_bind {
Victor Morales89ce3212017-06-16 18:32:48 -050071 install_package bind9
72 install_package bind9utils
73}
74
Victor Morales89ce3212017-06-16 18:32:48 -050075# install_java() - Install java binaries
76function install_java {
Victor Moralesece790c2017-08-08 11:11:36 -050077 if is_package_installed openjdk-8-jdk; then
78 return
79 fi
Victor Morales89ce3212017-06-16 18:32:48 -050080 install_package software-properties-common
81 add-apt-repository -y ppa:openjdk-r/ppa
82 install_package openjdk-8-jdk
Victor Morales455bece2017-07-31 18:40:39 -050083 # 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 Morales89ce3212017-06-16 18:32:48 -050085}
86
87# install_maven() - Install maven binaries
88function install_maven {
Victor Moralesdd074802017-07-26 16:06:35 -050089 if is_package_installed maven3; then
Victor Morales89ce3212017-06-16 18:32:48 -050090 return
91 fi
Victor Moralesece790c2017-08-08 11:11:36 -050092 install_java
Victor Morales89ce3212017-06-16 18:32:48 -050093 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 Moralesdd074802017-07-26 16:06:35 -050099
100 _configure_maven
Victor Morales89ce3212017-06-16 18:32:48 -0500101}
102
Idan Amite17b5ec2017-08-16 14:17:59 +0300103# _configure_docker_settings() - Configures Docker settings
104function _configure_docker_settings {
Victor Morales89ce3212017-06-16 18:32:48 -0500105 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
Idan Amite17b5ec2017-08-16 14:17:59 +0300111
112 echo "DOCKER_OPTS=\"-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock\"" >> /etc/default/docker
113 usermod -a -G docker vagrant
Victor Morales89ce3212017-06-16 18:32:48 -0500114}
115
Victor Moralesdd074802017-07-26 16:06:35 -0500116# install_nodejs() - Download and install NodeJS
117function install_nodejs {
118 if is_package_installed nodejs; then
119 return
120 fi
121 curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
122 install_package nodejs
123
124 # Update NPM to latest version
125 npm install npm -g
126}
127
128# install_python() - Install Python 2.7 and other tools necessary for development.
129function install_python {
130 install_package python2.7
131 install_package python-dev
Victor Moralesdd074802017-07-26 16:06:35 -0500132}
133
Victor Morales970ec192017-07-31 09:10:11 -0500134# _install_pip() - Install Python Package Manager
135function _install_pip {
136 install_python
Victor Morales158c18c2017-08-06 11:23:15 -0500137 if [ ! -f /usr/local/bin/pip ]; then
138 curl -sL https://bootstrap.pypa.io/get-pip.py | python
139 fi
Victor Morales970ec192017-07-31 09:10:11 -0500140}
141
142# install_python_package() - Install a python module
143function install_python_package {
144 local python_package=$1
145
146 _install_pip
147 pip install $python_package
148}
149
150# install_docker() - Download and install docker-engine
Victor Moralesdd074802017-07-26 16:06:35 -0500151function install_docker {
152 if is_package_installed docker-ce; then
153 return
154 fi
155 install_package software-properties-common
156 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
157 add-apt-repository \
158 "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
159 $(lsb_release -cs) \
160 stable"
161 install_package docker-ce
Idan Amite17b5ec2017-08-16 14:17:59 +0300162 _configure_docker_settings
Victor Moralesdd074802017-07-26 16:06:35 -0500163 service docker restart
164 sleep 10
165}
166
167# pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub
168function pull_docker_image {
169 install_docker
170 local image=$1
171 local tag=$2
172 docker pull ${image}
173 if [ ${tag} ]; then
174 docker tag ${image} $tag
175 fi
176}
177
178# install_docker_compose() - Download and install docker-engine
179function install_docker_compose {
180 local docker_compose_version=${1:-1.12.0}
181 if [ ! -d /opt/docker ]; then
182 mkdir /opt/docker
183 curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose
184 chmod +x /opt/docker/docker-compose
185 fi
186}
187
188# _install_ODL() - Download and Install OpenDayLight SDN controller
189function _install_ODL {
190 if [ ! -d /opt/opendaylight/current ]; then
191 mkdir -p /opt/opendaylight/
192 wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/
193 tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /opt/
194 mv "/opt/distribution-karaf-"$odl_version /opt/opendaylight/current
195 rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz"
196 fi
197}
198
199# start_ODL() - Start OpenDayLight SDN controller
200function start_ODL {
201 _install_ODL
202 if [ -d /opt/opendaylight ]; then
203 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
204 /opt/opendaylight/current/bin/start
205 sleep 180
206 /opt/opendaylight/current/bin/client feature:install odl-dlux-all
207 fi
208}
209
210# compile_src() - Function that compiles the java source code thru maven
211function compile_src {
212 local src_folder=$1
213 pushd $src_folder
214 if [ -f pom.xml ]; then
215 install_maven
Victor Morales970ec192017-07-31 09:10:11 -0500216 mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none
Victor Moralesdd074802017-07-26 16:06:35 -0500217 fi
218 popd
219}
220
Victor Morales89ce3212017-06-16 18:32:48 -0500221# build_docker_image() - Build Docker container image from source code
222function build_docker_image {
223 local src_folder=$1
224 local profile=$2
225 install_maven
226 install_docker
227 pushd $src_folder
228
229 # Cleanup external repo
230 sed -i 's|${docker.push.registry}/||g' pom.xml
231 local mvn_docker="mvn clean package docker:build"
232 if [ $profile ]; then
233 mvn_docker+=" -P $profile"
234 fi
235 if [ $http_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500236 if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then
237 mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy"
238 fi
239 if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then
240 mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy"
241 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500242 fi
243 if [ $https_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500244 if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then
245 mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy"
246 fi
247 if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then
248 mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy"
249 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500250 fi
251 eval $mvn_docker
252 popd
253}