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