blob: 435c621776a2a28fdf4ba501e79711b1d74b6fa6 [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 Morales54646642017-12-08 11:57:42 -08004source /var/onap/config/env-vars
Victor Moralesdd074802017-07-26 16:06:35 -05005source /var/onap/_composed_functions
6source /var/onap/_onap_functions
7
Victor Morales96512652017-08-16 13:44:28 -05008export MTU=$(/sbin/ifconfig | grep MTU | sed 's/.*MTU://' | sed 's/ .*//' |sort -n | head -1)
Victor Morales4ab71c12017-11-08 07:28:28 -08009export NIC=$(ip route get 8.8.8.8 | awk '{ print $5; exit }')
10export IP_ADDRESS=$(ifconfig $NIC | grep "inet addr" | tr -s ' ' | cut -d' ' -f3 | cut -d':' -f2)
Victor Morales96512652017-08-16 13:44:28 -050011
Victor Moralesdd074802017-07-26 16:06:35 -050012mvn_conf_file=/root/.m2/settings.xml
Victor Moralesdd074802017-07-26 16:06:35 -050013
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
Victor Morales54646642017-12-08 11:57:42 -080075 for repo in ${repos[$project]}; do
76 clone_repo $repo ${src_folders[$project]}${repo#*$repo_name}
Victor Moralesf1f1ba52017-11-20 16:38:28 -080077 done
78}
79
Victor Moralesdd074802017-07-26 16:06:35 -050080# _install_bind() - Install bind utils
81function _install_bind {
Victor Morales7ae05a42017-08-22 14:39:00 -050082 install_packages bind9 bind9utils
Victor Morales89ce3212017-06-16 18:32:48 -050083}
84
Victor Morales89ce3212017-06-16 18:32:48 -050085# install_java() - Install java binaries
86function install_java {
Victor Moralesece790c2017-08-08 11:11:36 -050087 if is_package_installed openjdk-8-jdk; then
88 return
89 fi
Victor Morales9f1c94b2017-11-07 07:56:04 -080090 source /etc/os-release || source /usr/lib/os-release
91 case ${ID,,} in
92 *suse)
93 ;;
94 ubuntu|debian)
95 install_package software-properties-common
96 add-apt-repository -y ppa:openjdk-r/ppa
97 ;;
98 rhel|centos|fedora)
99 ;;
100 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800101 update_repos
Victor Morales7ae05a42017-08-22 14:39:00 -0500102
103 # Remove Java 7
104 uninstall_packages default-jre openjdk-7-jdk openjdk-7-jre openjdk-7-jre-headless
105
Victor Morales89ce3212017-06-16 18:32:48 -0500106 install_package openjdk-8-jdk
Victor Morales455bece2017-07-31 18:40:39 -0500107 # ca-certificates-java is not a dependency in the Oracle JDK/JRE so this must be explicitly installed.
108 /var/lib/dpkg/info/ca-certificates-java.postinst configure
Victor Morales89ce3212017-06-16 18:32:48 -0500109}
110
111# install_maven() - Install maven binaries
112function install_maven {
Victor Moralesdd074802017-07-26 16:06:35 -0500113 if is_package_installed maven3; then
Victor Morales89ce3212017-06-16 18:32:48 -0500114 return
115 fi
Victor Moralesece790c2017-08-08 11:11:36 -0500116 install_java
Victor Morales9f1c94b2017-11-07 07:56:04 -0800117 source /etc/os-release || source /usr/lib/os-release
118 case ${ID,,} in
119 *suse)
120 ;;
121 ubuntu|debian)
122 install_package software-properties-common
123 add-apt-repository -y ppa:andrei-pozolotin/maven3
124 ;;
125 rhel|centos|fedora)
126 ;;
127 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800128 update_repos
Victor Morales89ce3212017-06-16 18:32:48 -0500129 install_package maven3
130
Victor Morales7ae05a42017-08-22 14:39:00 -0500131 # Remove Java 7
132 uninstall_package openjdk-7-jdk
Victor Moralesdd074802017-07-26 16:06:35 -0500133
134 _configure_maven
Victor Morales89ce3212017-06-16 18:32:48 -0500135}
136
Idan Amite17b5ec2017-08-16 14:17:59 +0300137# _configure_docker_settings() - Configures Docker settings
138function _configure_docker_settings {
Victor Moralescf269992017-10-18 09:29:55 -0700139 local docker_conf_backup=/tmp/docker.backup
140 local docker_conf=/etc/default/docker
141 local chameleonsocks_filename=chameleonsocks.sh
Victor Moralesfa9eb9c2017-12-18 09:56:13 -0800142 local max_concurrent_downloads=${1:-3}
Victor Moralescf269992017-10-18 09:29:55 -0700143
Victor Morales80f144b2018-01-10 07:20:54 -0800144 cp ${docker_conf} ${docker_conf_backup}
Victor Morales89ce3212017-06-16 18:32:48 -0500145 if [ $http_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700146 echo "export http_proxy=$http_proxy" >> $docker_conf
Victor Morales89ce3212017-06-16 18:32:48 -0500147 fi
148 if [ $https_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700149 echo "export https_proxy=$https_proxy" >> $docker_conf
Victor Morales5ce04212017-09-24 09:03:32 -0700150 #If you have a socks proxy, then use that to connect to the nexus repo
Kiranf2824c22017-09-22 18:05:29 -0700151 #via a redsocks container
152 if [ $socks_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700153 wget https://raw.githubusercontent.com/crops/chameleonsocks/master/$chameleonsocks_filename
154 chmod 755 $chameleonsocks_filename
Victor Morales5ce04212017-09-24 09:03:32 -0700155 socks=$(echo $socks_proxy | sed -e "s/^.*\///" | sed -e "s/:.*$//")
156 port=$(echo $socks_proxy | sed -e "s/^.*://")
Victor Moralescf269992017-10-18 09:29:55 -0700157 PROXY=$socks PORT=$port ./$chameleonsocks_filename --install
158 rm $chameleonsocks_filename
Victor Morales80f144b2018-01-10 07:20:54 -0800159 cp ${docker_conf_backup} ${docker_conf}
Kiranf2824c22017-09-22 18:05:29 -0700160 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500161 fi
Victor Morales80f144b2018-01-10 07:20:54 -0800162 rm ${docker_conf_backup}
Idan Amite17b5ec2017-08-16 14:17:59 +0300163
Victor Moralesfa9eb9c2017-12-18 09:56:13 -0800164 echo "DOCKER_OPTS=\"-H tcp://0.0.0.0:2375 -H unix:///var/run/docker.sock --max-concurrent-downloads $max_concurrent_downloads \"" >> $docker_conf
165 usermod -aG docker $USER
166
167 source /etc/os-release || source /usr/lib/os-release
168 case ${ID,,} in
169 *suse)
170 ;;
171 ubuntu|debian)
172 service docker restart
173 sleep 10
174 ;;
175 rhel|centos|fedora)
176 ;;
177 esac
Victor Morales89ce3212017-06-16 18:32:48 -0500178}
179
Victor Moralesdd074802017-07-26 16:06:35 -0500180# install_nodejs() - Download and install NodeJS
181function install_nodejs {
182 if is_package_installed nodejs; then
183 return
184 fi
185 curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
186 install_package nodejs
187
188 # Update NPM to latest version
189 npm install npm -g
190}
191
192# install_python() - Install Python 2.7 and other tools necessary for development.
193function install_python {
Victor Morales7ae05a42017-08-22 14:39:00 -0500194 install_packages python2.7 python-dev
Victor Moralesdd074802017-07-26 16:06:35 -0500195}
196
Victor Morales970ec192017-07-31 09:10:11 -0500197# _install_pip() - Install Python Package Manager
198function _install_pip {
199 install_python
Victor Morales4ab71c12017-11-08 07:28:28 -0800200 if ! which pip; then
Victor Morales158c18c2017-08-06 11:23:15 -0500201 curl -sL https://bootstrap.pypa.io/get-pip.py | python
202 fi
Victor Morales970ec192017-07-31 09:10:11 -0500203}
204
Victor Morales88d193e2017-11-16 10:50:28 -0800205# install_python_package() - Install python modules
Victor Morales970ec192017-07-31 09:10:11 -0500206function install_python_package {
Victor Morales4ab71c12017-11-08 07:28:28 -0800207 local python_packages=$@
Victor Morales970ec192017-07-31 09:10:11 -0500208
209 _install_pip
Victor Morales4ab71c12017-11-08 07:28:28 -0800210 pip install $python_packages
Victor Morales970ec192017-07-31 09:10:11 -0500211}
212
Victor Morales88d193e2017-11-16 10:50:28 -0800213# install_python_requirements() - Install a list of python modules defined in requirement.txt file
214function install_python_requirements {
215 local python_project_path=$1
216
217 _install_pip
218 pushd $python_project_path
219 pip install -r requirements.txt
220 popd
221}
222
Victor Morales970ec192017-07-31 09:10:11 -0500223# install_docker() - Download and install docker-engine
Victor Moralesdd074802017-07-26 16:06:35 -0500224function install_docker {
Victor Moralesfa9eb9c2017-12-18 09:56:13 -0800225 if $(docker version &>/dev/null); then
Victor Moralesdd074802017-07-26 16:06:35 -0500226 return
227 fi
Victor Morales9f1c94b2017-11-07 07:56:04 -0800228 source /etc/os-release || source /usr/lib/os-release
229 case ${ID,,} in
230 *suse)
231 ;;
232 ubuntu|debian)
Victor Morales071b81c2017-11-07 14:13:07 -0800233 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 -0800234 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
235 add-apt-repository \
236 "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
237 $(lsb_release -cs) stable"
238 ;;
239 rhel|centos|fedora)
240 ;;
241 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800242 update_repos
Victor Morales7abf1a82017-11-06 09:20:27 -0800243
Victor Moralesdd074802017-07-26 16:06:35 -0500244 install_package docker-ce
Idan Amite17b5ec2017-08-16 14:17:59 +0300245 _configure_docker_settings
Victor Moralesdd074802017-07-26 16:06:35 -0500246}
247
248# pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub
249function pull_docker_image {
250 install_docker
251 local image=$1
252 local tag=$2
253 docker pull ${image}
254 if [ ${tag} ]; then
255 docker tag ${image} $tag
256 fi
257}
258
Victor Morales472a3062018-02-02 08:26:50 -0800259# wait_docker_pull() - Function that waits for all docker pull processes
260function wait_docker_pull {
261 local counter=60
262 local delay=${1:-60}
263
264 sleep $delay
265 while [ $(ps -ef | grep "docker pull" | wc -l) -gt 1 ]; do
266 sleep $delay
267 counter=$((counter - 1))
268 if [ "$counter" -eq 0 ]; then
269 break
270 fi
271 done
272}
273
Victor Morales9d205bc2017-12-01 17:52:07 -0800274# run_docker_image() - Starts a Docker instance
275function run_docker_image {
276 install_docker
277 docker run $@
278}
279
Victor Moralesdd074802017-07-26 16:06:35 -0500280# install_docker_compose() - Download and install docker-engine
281function install_docker_compose {
282 local docker_compose_version=${1:-1.12.0}
283 if [ ! -d /opt/docker ]; then
284 mkdir /opt/docker
285 curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose
286 chmod +x /opt/docker/docker-compose
287 fi
288}
289
290# _install_ODL() - Download and Install OpenDayLight SDN controller
291function _install_ODL {
292 if [ ! -d /opt/opendaylight/current ]; then
293 mkdir -p /opt/opendaylight/
294 wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/
Victor Moralesfa9eb9c2017-12-18 09:56:13 -0800295 tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /tmp/
296 mv "/tmp/distribution-karaf-"$odl_version /opt/opendaylight/current
Victor Moralesdd074802017-07-26 16:06:35 -0500297 rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz"
298 fi
299}
300
301# start_ODL() - Start OpenDayLight SDN controller
302function start_ODL {
303 _install_ODL
304 if [ -d /opt/opendaylight ]; then
305 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
306 /opt/opendaylight/current/bin/start
307 sleep 180
308 /opt/opendaylight/current/bin/client feature:install odl-dlux-all
309 fi
310}
311
312# compile_src() - Function that compiles the java source code thru maven
313function compile_src {
314 local src_folder=$1
315 pushd $src_folder
Victor Morales7abf1a82017-11-06 09:20:27 -0800316 local mvn_build='mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none'
317 if [[ "$debug" == "False" ]]; then
318 mvn_build+=" -q"
319 fi
Victor Moralesdd074802017-07-26 16:06:35 -0500320 if [ -f pom.xml ]; then
321 install_maven
Victor Morales7abf1a82017-11-06 09:20:27 -0800322 echo "Compiling $src_folder folder..."
323 eval $mvn_build
Victor Moralesdd074802017-07-26 16:06:35 -0500324 fi
325 popd
326}
327
Victor Moralesf1f1ba52017-11-20 16:38:28 -0800328# compile_repos() - Function that compiles source repositories for a given project
329function compile_repos {
330 local project=$1
Victor Moralesf1f1ba52017-11-20 16:38:28 -0800331
Victor Morales54646642017-12-08 11:57:42 -0800332 for repo in ${repos[$project]}; do
333 compile_src ${src_folders[$project]}${repo#*$project}
Victor Moralesf1f1ba52017-11-20 16:38:28 -0800334 done
335}
336
Victor Morales89ce3212017-06-16 18:32:48 -0500337# build_docker_image() - Build Docker container image from source code
338function build_docker_image {
339 local src_folder=$1
340 local profile=$2
Victor Morales89ce3212017-06-16 18:32:48 -0500341 install_docker
342 pushd $src_folder
343
Victor Morales9d205bc2017-12-01 17:52:07 -0800344 if [ -f pom.xml ]; then
345 install_maven
346 # Cleanup external repo
347 sed -i 's|${docker.push.registry}/||g' pom.xml
Victor Moralesbe844712018-01-22 10:07:28 -0800348 local docker_build="mvn clean package docker:build -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true"
Victor Morales9d205bc2017-12-01 17:52:07 -0800349 if [ $profile ]; then
350 docker_build+=" -P $profile"
Victor Moralesdd074802017-07-26 16:06:35 -0500351 fi
Victor Morales472a3062018-02-02 08:26:50 -0800352 if [[ "$debug" == "False" ]]; then
353 docker_build+=" -q"
354 fi
Victor Morales9d205bc2017-12-01 17:52:07 -0800355 if [ $http_proxy ]; then
356 if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then
357 docker_build+=" -Ddocker.buildArg.http_proxy=$http_proxy"
358 fi
Victor Moralesdd074802017-07-26 16:06:35 -0500359 if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then
Victor Morales9d205bc2017-12-01 17:52:07 -0800360 docker_build+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy"
361 fi
362 fi
363 if [ $https_proxy ]; then
364 if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then
365 docker_build+=" -Ddocker.buildArg.https_proxy=$https_proxy"
366 fi
367 if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then
368 docker_build+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy"
369 fi
370 fi
371 elif [ -f Dockerfile ]; then
372 # NOTE: Workaround for dmmapbc images
373 sed -i '/LocalKey/d' Dockerfile
374 sed -i "s/nexus3.onap.org\:10003\///g" Dockerfile
375 local docker_build="docker build -t $profile -f ./Dockerfile ."
376 if [ $http_proxy ]; then
377 docker_build+=" --build-arg http_proxy=$http_proxy"
378 docker_build+=" --build-arg HTTP_PROXY=$http_proxy"
379 fi
380 if [ $https_proxy ]; then
381 docker_build+=" --build-arg https_proxy=$https_proxy"
382 docker_build+=" --build-arg HTTPS_PROXY=$https_proxy"
Victor Moralesdd074802017-07-26 16:06:35 -0500383 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500384 fi
Victor Morales9d205bc2017-12-01 17:52:07 -0800385 echo $docker_build
386 eval $docker_build
Victor Morales89ce3212017-06-16 18:32:48 -0500387 popd
388}
Victor Moralesfa9eb9c2017-12-18 09:56:13 -0800389
390# mount_external_partition() - Create partition and mount the external volume
391function mount_external_partition {
392 local dev_name="/dev/$1"
393 local mount_dir=$2
394
395 sfdisk $dev_name << EOF
396;
397EOF
398 mkfs -t ext4 ${dev_name}1
399 mkdir -p $mount_dir
400 mount ${dev_name}1 $mount_dir
401 echo "${dev_name}1 $mount_dir ext4 errors=remount-ro,noatime,barrier=0 0 1" >> /etc/fstab
402}
Nate Potterfdf88f32018-01-17 09:29:35 -0800403
404# add no_proxy values to environment, used for internal IPs generated at deploy time
405function add_no_proxy_value {
406 if [[ `grep "no_proxy" /etc/environment` ]]; then
407 sed -i.bak "s/^no_proxy.*$/&,$1/" /etc/environment
408 else
409 echo "no_proxy=$1" >> /etc/environment
410 fi
411 if [[ `grep "NO_PROXY" /etc/environment` ]]; then
412 sed -i.bak "s/^NO_PROXY.*$/&,$1/" /etc/environment
413 else
414 echo "NO_PROXY=$1" >> /etc/environment
415 fi
416}