blob: 8d906dc684098a8c72601dd08fd3ff0fdbbd5ade [file] [log] [blame]
Victor Morales89ce3212017-06-16 18:32:48 -05001#!/bin/bash
2
Victor Morales89ce3212017-06-16 18:32:48 -05003source /var/onap/commons
Victor Moralesdd074802017-07-26 16:06:35 -05004source /var/onap/_composed_functions
5source /var/onap/_onap_functions
6
Victor Morales96512652017-08-16 13:44:28 -05007export MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' |sort -n | head -1)
Victor Morales4ab71c12017-11-08 07:28:28 -08008export NIC=$(ip route get 8.8.8.8 | awk '{ print $5; exit }')
9export IP_ADDRESS=$(ifconfig $NIC | grep "inet addr" | tr -s ' ' | cut -d' ' -f3 | cut -d':' -f2)
Victor Morales96512652017-08-16 13:44:28 -050010
Victor Moralesdd074802017-07-26 16:06:35 -050011mvn_conf_file=/root/.m2/settings.xml
12git_src_folder=/opt
13
Victor Morales89ce3212017-06-16 18:32:48 -050014# configure_dns() - DNS/GW IP address configuration
15function configure_dns {
16 echo "nameserver 10.0.0.1" >> /etc/resolvconf/resolv.conf.d/head
17 resolvconf -u
18}
19
Victor Morales4ab71c12017-11-08 07:28:28 -080020# get_next_ip() - Function that provides the next ip
21function get_next_ip {
22 local ip=${1:-$IP_ADDRESS}
23 ip_hex=$(printf '%.2X%.2X%.2X%.2X\n' `echo $ip | sed -e 's/\./ /g'`)
24 next_ip_hex=$(printf %.8X `echo $(( 0x$ip_hex + 1 ))`)
25 echo $(printf '%d.%d.%d.%d\n' `echo $next_ip_hex | sed -r 's/(..)/0x\1 /g'`)
26}
27
Victor Moralesdd074802017-07-26 16:06:35 -050028# _git_timed() - git can sometimes get itself infinitely stuck with transient network
Victor Morales89ce3212017-06-16 18:32:48 -050029# errors or other issues with the remote end. This wraps git in a
30# timeout/retry loop and is intended to watch over non-local git
31# processes that might hang.
Victor Moralesdd074802017-07-26 16:06:35 -050032function _git_timed {
Victor Morales89ce3212017-06-16 18:32:48 -050033 local count=0
34 local timeout=0
35
36 install_package git
37 until timeout -s SIGINT ${timeout} git "$@"; do
38 # 124 is timeout(1)'s special return code when it reached the
39 # timeout; otherwise assume fatal failure
40 if [[ $? -ne 124 ]]; then
41 exit 1
42 fi
43
44 count=$(($count + 1))
45 if [ $count -eq 3 ]; then
46 exit 1
47 fi
48 sleep 5
49 done
50}
51
52# clone_repo() - Clone Git repository into specific folder
53function clone_repo {
Victor Morales9f1c94b2017-11-07 07:56:04 -080054 local repo_url=${3:-"https://git.onap.org/"}
Victor Morales89ce3212017-06-16 18:32:48 -050055 local repo=$1
Victor Moralesdd074802017-07-26 16:06:35 -050056 local dest_folder=${2:-$git_src_folder/$repo}
57 if [ ! -d $dest_folder ]; then
Victor Morales7abf1a82017-11-06 09:20:27 -080058 if [[ "$debug" == "False" ]]; then
59 _git_timed clone --quiet ${repo_url}${repo} $dest_folder
60 else
61 _git_timed clone ${repo_url}${repo} $dest_folder
62 fi
Victor Morales89ce3212017-06-16 18:32:48 -050063 else
64 pushd $dest_folder
Victor Moralesdd074802017-07-26 16:06:35 -050065 _git_timed pull
Victor Morales89ce3212017-06-16 18:32:48 -050066 popd
67 fi
68}
69
Victor Moralesf1f1ba52017-11-20 16:38:28 -080070# clone_repos() - Function that clones source repositories for a given project
71function clone_repos {
72 local project=$1
Victor Morales5d21f642017-12-07 11:54:59 -080073 local repo_name=${2:-$project}
Victor Moralesf1f1ba52017-11-20 16:38:28 -080074 local name=${project}_repos[@]
75 local src_name=${project}_src_folder
76 local repos_list=("${!name}")
Victor Morales5d21f642017-12-07 11:54:59 -080077 local dest_folder=${!src_name}
Victor Moralesf1f1ba52017-11-20 16:38:28 -080078
79 for repo in ${repos_list[@]}; do
Victor Morales5d21f642017-12-07 11:54:59 -080080 clone_repo $repo $dest_folder${repo#*$repo_name}
Victor Moralesf1f1ba52017-11-20 16:38:28 -080081 done
82}
83
Victor Moralesdd074802017-07-26 16:06:35 -050084# _install_bind() - Install bind utils
85function _install_bind {
Victor Morales7ae05a42017-08-22 14:39:00 -050086 install_packages bind9 bind9utils
Victor Morales89ce3212017-06-16 18:32:48 -050087}
88
Victor Morales89ce3212017-06-16 18:32:48 -050089# install_java() - Install java binaries
90function install_java {
Victor Moralesece790c2017-08-08 11:11:36 -050091 if is_package_installed openjdk-8-jdk; then
92 return
93 fi
Victor Morales9f1c94b2017-11-07 07:56:04 -080094 source /etc/os-release || source /usr/lib/os-release
95 case ${ID,,} in
96 *suse)
97 ;;
98 ubuntu|debian)
99 install_package software-properties-common
100 add-apt-repository -y ppa:openjdk-r/ppa
101 ;;
102 rhel|centos|fedora)
103 ;;
104 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800105 update_repos
Victor Morales7ae05a42017-08-22 14:39:00 -0500106
107 # Remove Java 7
108 uninstall_packages default-jre openjdk-7-jdk openjdk-7-jre openjdk-7-jre-headless
109
Victor Morales89ce3212017-06-16 18:32:48 -0500110 install_package openjdk-8-jdk
Victor Morales455bece2017-07-31 18:40:39 -0500111 # ca-certificates-java is not a dependency in the Oracle JDK/JRE so this must be explicitly installed.
112 /var/lib/dpkg/info/ca-certificates-java.postinst configure
Victor Morales89ce3212017-06-16 18:32:48 -0500113}
114
115# install_maven() - Install maven binaries
116function install_maven {
Victor Moralesdd074802017-07-26 16:06:35 -0500117 if is_package_installed maven3; then
Victor Morales89ce3212017-06-16 18:32:48 -0500118 return
119 fi
Victor Moralesece790c2017-08-08 11:11:36 -0500120 install_java
Victor Morales9f1c94b2017-11-07 07:56:04 -0800121 source /etc/os-release || source /usr/lib/os-release
122 case ${ID,,} in
123 *suse)
124 ;;
125 ubuntu|debian)
126 install_package software-properties-common
127 add-apt-repository -y ppa:andrei-pozolotin/maven3
128 ;;
129 rhel|centos|fedora)
130 ;;
131 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800132 update_repos
Victor Morales89ce3212017-06-16 18:32:48 -0500133 install_package maven3
134
Victor Morales7ae05a42017-08-22 14:39:00 -0500135 # Remove Java 7
136 uninstall_package openjdk-7-jdk
Victor Moralesdd074802017-07-26 16:06:35 -0500137
138 _configure_maven
Victor Morales89ce3212017-06-16 18:32:48 -0500139}
140
Idan Amite17b5ec2017-08-16 14:17:59 +0300141# _configure_docker_settings() - Configures Docker settings
142function _configure_docker_settings {
Victor Moralescf269992017-10-18 09:29:55 -0700143 local docker_conf_backup=/tmp/docker.backup
144 local docker_conf=/etc/default/docker
145 local chameleonsocks_filename=chameleonsocks.sh
146
147 cp $docker_conf $docker_conf_backup
Victor Morales89ce3212017-06-16 18:32:48 -0500148 if [ $http_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700149 echo "export http_proxy=$http_proxy" >> $docker_conf
Victor Morales89ce3212017-06-16 18:32:48 -0500150 fi
151 if [ $https_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700152 echo "export https_proxy=$https_proxy" >> $docker_conf
Victor Morales5ce04212017-09-24 09:03:32 -0700153 #If you have a socks proxy, then use that to connect to the nexus repo
Kiranf2824c22017-09-22 18:05:29 -0700154 #via a redsocks container
155 if [ $socks_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700156 wget https://raw.githubusercontent.com/crops/chameleonsocks/master/$chameleonsocks_filename
157 chmod 755 $chameleonsocks_filename
Victor Morales5ce04212017-09-24 09:03:32 -0700158 socks=$(echo $socks_proxy | sed -e "s/^.*\///" | sed -e "s/:.*$//")
159 port=$(echo $socks_proxy | sed -e "s/^.*://")
Victor Moralescf269992017-10-18 09:29:55 -0700160 PROXY=$socks PORT=$port ./$chameleonsocks_filename --install
161 rm $chameleonsocks_filename
162 cp $docker_conf_backup $docker_conf
Kiranf2824c22017-09-22 18:05:29 -0700163 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500164 fi
Victor Moralescf269992017-10-18 09:29:55 -0700165 rm $docker_conf_backup
Idan Amite17b5ec2017-08-16 14:17:59 +0300166
Victor Moralescf269992017-10-18 09:29:55 -0700167 echo "DOCKER_OPTS=\"-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock\"" >> $docker_conf
Victor Morales89ce3212017-06-16 18:32:48 -0500168}
169
Victor Moralesdd074802017-07-26 16:06:35 -0500170# install_nodejs() - Download and install NodeJS
171function install_nodejs {
172 if is_package_installed nodejs; then
173 return
174 fi
175 curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
176 install_package nodejs
177
178 # Update NPM to latest version
179 npm install npm -g
180}
181
182# install_python() - Install Python 2.7 and other tools necessary for development.
183function install_python {
Victor Morales7ae05a42017-08-22 14:39:00 -0500184 install_packages python2.7 python-dev
Victor Moralesdd074802017-07-26 16:06:35 -0500185}
186
Victor Morales970ec192017-07-31 09:10:11 -0500187# _install_pip() - Install Python Package Manager
188function _install_pip {
189 install_python
Victor Morales4ab71c12017-11-08 07:28:28 -0800190 if ! which pip; then
Victor Morales158c18c2017-08-06 11:23:15 -0500191 curl -sL https://bootstrap.pypa.io/get-pip.py | python
192 fi
Victor Morales970ec192017-07-31 09:10:11 -0500193}
194
Victor Morales88d193e2017-11-16 10:50:28 -0800195# install_python_package() - Install python modules
Victor Morales970ec192017-07-31 09:10:11 -0500196function install_python_package {
Victor Morales4ab71c12017-11-08 07:28:28 -0800197 local python_packages=$@
Victor Morales970ec192017-07-31 09:10:11 -0500198
199 _install_pip
Victor Morales4ab71c12017-11-08 07:28:28 -0800200 pip install $python_packages
Victor Morales970ec192017-07-31 09:10:11 -0500201}
202
Victor Morales88d193e2017-11-16 10:50:28 -0800203# install_python_requirements() - Install a list of python modules defined in requirement.txt file
204function install_python_requirements {
205 local python_project_path=$1
206
207 _install_pip
208 pushd $python_project_path
209 pip install -r requirements.txt
210 popd
211}
212
Victor Morales970ec192017-07-31 09:10:11 -0500213# install_docker() - Download and install docker-engine
Victor Moralesdd074802017-07-26 16:06:35 -0500214function install_docker {
215 if is_package_installed docker-ce; then
216 return
217 fi
Victor Morales9f1c94b2017-11-07 07:56:04 -0800218 source /etc/os-release || source /usr/lib/os-release
219 case ${ID,,} in
220 *suse)
221 ;;
222 ubuntu|debian)
Victor Morales071b81c2017-11-07 14:13:07 -0800223 install_packages software-properties-common linux-image-extra-$(uname -r) linux-image-extra-virtual apt-transport-https ca-certificates curl
Victor Morales9f1c94b2017-11-07 07:56:04 -0800224 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
225 add-apt-repository \
226 "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
227 $(lsb_release -cs) stable"
228 ;;
229 rhel|centos|fedora)
230 ;;
231 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800232 update_repos
Victor Morales7abf1a82017-11-06 09:20:27 -0800233
Victor Moralesdd074802017-07-26 16:06:35 -0500234 install_package docker-ce
Idan Amite17b5ec2017-08-16 14:17:59 +0300235 _configure_docker_settings
Victor Moralesdd074802017-07-26 16:06:35 -0500236 service docker restart
237 sleep 10
238}
239
240# pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub
241function pull_docker_image {
242 install_docker
243 local image=$1
244 local tag=$2
245 docker pull ${image}
246 if [ ${tag} ]; then
247 docker tag ${image} $tag
248 fi
249}
250
Victor Morales9d205bc2017-12-01 17:52:07 -0800251# run_docker_image() - Starts a Docker instance
252function run_docker_image {
253 install_docker
254 docker run $@
255}
256
Victor Moralesdd074802017-07-26 16:06:35 -0500257# install_docker_compose() - Download and install docker-engine
258function install_docker_compose {
259 local docker_compose_version=${1:-1.12.0}
260 if [ ! -d /opt/docker ]; then
261 mkdir /opt/docker
262 curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose
263 chmod +x /opt/docker/docker-compose
264 fi
265}
266
267# _install_ODL() - Download and Install OpenDayLight SDN controller
268function _install_ODL {
269 if [ ! -d /opt/opendaylight/current ]; then
270 mkdir -p /opt/opendaylight/
271 wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/
272 tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /opt/
273 mv "/opt/distribution-karaf-"$odl_version /opt/opendaylight/current
274 rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz"
275 fi
276}
277
278# start_ODL() - Start OpenDayLight SDN controller
279function start_ODL {
280 _install_ODL
281 if [ -d /opt/opendaylight ]; then
282 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
283 /opt/opendaylight/current/bin/start
284 sleep 180
285 /opt/opendaylight/current/bin/client feature:install odl-dlux-all
286 fi
287}
288
289# compile_src() - Function that compiles the java source code thru maven
290function compile_src {
291 local src_folder=$1
292 pushd $src_folder
Victor Morales7abf1a82017-11-06 09:20:27 -0800293 local mvn_build='mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none'
294 if [[ "$debug" == "False" ]]; then
295 mvn_build+=" -q"
296 fi
Victor Moralesdd074802017-07-26 16:06:35 -0500297 if [ -f pom.xml ]; then
298 install_maven
Victor Morales7abf1a82017-11-06 09:20:27 -0800299 echo "Compiling $src_folder folder..."
300 eval $mvn_build
Victor Moralesdd074802017-07-26 16:06:35 -0500301 fi
302 popd
303}
304
Victor Moralesf1f1ba52017-11-20 16:38:28 -0800305# compile_repos() - Function that compiles source repositories for a given project
306function compile_repos {
307 local project=$1
308 local name=${project}_repos[@]
309 local repos_list=("${!name}")
310 local dest_folder=${3:-$git_src_folder/$project}
311
312 install_package unzip
313 for repo in ${repos_list[@]}; do
314 compile_src $dest_folder${repo#*$project}
315 done
316}
317
Victor Morales89ce3212017-06-16 18:32:48 -0500318# build_docker_image() - Build Docker container image from source code
319function build_docker_image {
320 local src_folder=$1
321 local profile=$2
Victor Morales89ce3212017-06-16 18:32:48 -0500322 install_docker
323 pushd $src_folder
324
Victor Morales9d205bc2017-12-01 17:52:07 -0800325 if [ -f pom.xml ]; then
326 install_maven
327 # Cleanup external repo
328 sed -i 's|${docker.push.registry}/||g' pom.xml
329 local docker_build="mvn clean package docker:build"
330 if [ $profile ]; then
331 docker_build+=" -P $profile"
Victor Moralesdd074802017-07-26 16:06:35 -0500332 fi
Victor Morales9d205bc2017-12-01 17:52:07 -0800333 if [ $http_proxy ]; then
334 if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then
335 docker_build+=" -Ddocker.buildArg.http_proxy=$http_proxy"
336 fi
Victor Moralesdd074802017-07-26 16:06:35 -0500337 if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then
Victor Morales9d205bc2017-12-01 17:52:07 -0800338 docker_build+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy"
339 fi
340 fi
341 if [ $https_proxy ]; then
342 if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then
343 docker_build+=" -Ddocker.buildArg.https_proxy=$https_proxy"
344 fi
345 if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then
346 docker_build+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy"
347 fi
348 fi
349 elif [ -f Dockerfile ]; then
350 # NOTE: Workaround for dmmapbc images
351 sed -i '/LocalKey/d' Dockerfile
352 sed -i "s/nexus3.onap.org\:10003\///g" Dockerfile
353 local docker_build="docker build -t $profile -f ./Dockerfile ."
354 if [ $http_proxy ]; then
355 docker_build+=" --build-arg http_proxy=$http_proxy"
356 docker_build+=" --build-arg HTTP_PROXY=$http_proxy"
357 fi
358 if [ $https_proxy ]; then
359 docker_build+=" --build-arg https_proxy=$https_proxy"
360 docker_build+=" --build-arg HTTPS_PROXY=$https_proxy"
Victor Moralesdd074802017-07-26 16:06:35 -0500361 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500362 fi
Victor Morales9d205bc2017-12-01 17:52:07 -0800363 echo $docker_build
364 eval $docker_build
Victor Morales89ce3212017-06-16 18:32:48 -0500365 popd
366}