blob: 157c910de1b003fd5247596a6f6be2eb964c3ff3 [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
73 local name=${project}_repos[@]
74 local src_name=${project}_src_folder
75 local repos_list=("${!name}")
76 local dest_folder=${3:-${!src_name}}
77
78 for repo in ${repos_list[@]}; do
79 clone_repo $repo $dest_folder${repo#*$project}
80 done
81}
82
Victor Moralesdd074802017-07-26 16:06:35 -050083# _install_bind() - Install bind utils
84function _install_bind {
Victor Morales7ae05a42017-08-22 14:39:00 -050085 install_packages bind9 bind9utils
Victor Morales89ce3212017-06-16 18:32:48 -050086}
87
Victor Morales89ce3212017-06-16 18:32:48 -050088# install_java() - Install java binaries
89function install_java {
Victor Moralesece790c2017-08-08 11:11:36 -050090 if is_package_installed openjdk-8-jdk; then
91 return
92 fi
Victor Morales9f1c94b2017-11-07 07:56:04 -080093 source /etc/os-release || source /usr/lib/os-release
94 case ${ID,,} in
95 *suse)
96 ;;
97 ubuntu|debian)
98 install_package software-properties-common
99 add-apt-repository -y ppa:openjdk-r/ppa
100 ;;
101 rhel|centos|fedora)
102 ;;
103 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800104 update_repos
Victor Morales7ae05a42017-08-22 14:39:00 -0500105
106 # Remove Java 7
107 uninstall_packages default-jre openjdk-7-jdk openjdk-7-jre openjdk-7-jre-headless
108
Victor Morales89ce3212017-06-16 18:32:48 -0500109 install_package openjdk-8-jdk
Victor Morales455bece2017-07-31 18:40:39 -0500110 # ca-certificates-java is not a dependency in the Oracle JDK/JRE so this must be explicitly installed.
111 /var/lib/dpkg/info/ca-certificates-java.postinst configure
Victor Morales89ce3212017-06-16 18:32:48 -0500112}
113
114# install_maven() - Install maven binaries
115function install_maven {
Victor Moralesdd074802017-07-26 16:06:35 -0500116 if is_package_installed maven3; then
Victor Morales89ce3212017-06-16 18:32:48 -0500117 return
118 fi
Victor Moralesece790c2017-08-08 11:11:36 -0500119 install_java
Victor Morales9f1c94b2017-11-07 07:56:04 -0800120 source /etc/os-release || source /usr/lib/os-release
121 case ${ID,,} in
122 *suse)
123 ;;
124 ubuntu|debian)
125 install_package software-properties-common
126 add-apt-repository -y ppa:andrei-pozolotin/maven3
127 ;;
128 rhel|centos|fedora)
129 ;;
130 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800131 update_repos
Victor Morales89ce3212017-06-16 18:32:48 -0500132 install_package maven3
133
Victor Morales7ae05a42017-08-22 14:39:00 -0500134 # Remove Java 7
135 uninstall_package openjdk-7-jdk
Victor Moralesdd074802017-07-26 16:06:35 -0500136
137 _configure_maven
Victor Morales89ce3212017-06-16 18:32:48 -0500138}
139
Idan Amite17b5ec2017-08-16 14:17:59 +0300140# _configure_docker_settings() - Configures Docker settings
141function _configure_docker_settings {
Victor Moralescf269992017-10-18 09:29:55 -0700142 local docker_conf_backup=/tmp/docker.backup
143 local docker_conf=/etc/default/docker
144 local chameleonsocks_filename=chameleonsocks.sh
145
146 cp $docker_conf $docker_conf_backup
Victor Morales89ce3212017-06-16 18:32:48 -0500147 if [ $http_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700148 echo "export http_proxy=$http_proxy" >> $docker_conf
Victor Morales89ce3212017-06-16 18:32:48 -0500149 fi
150 if [ $https_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700151 echo "export https_proxy=$https_proxy" >> $docker_conf
Victor Morales5ce04212017-09-24 09:03:32 -0700152 #If you have a socks proxy, then use that to connect to the nexus repo
Kiranf2824c22017-09-22 18:05:29 -0700153 #via a redsocks container
154 if [ $socks_proxy ]; then
Victor Moralescf269992017-10-18 09:29:55 -0700155 wget https://raw.githubusercontent.com/crops/chameleonsocks/master/$chameleonsocks_filename
156 chmod 755 $chameleonsocks_filename
Victor Morales5ce04212017-09-24 09:03:32 -0700157 socks=$(echo $socks_proxy | sed -e "s/^.*\///" | sed -e "s/:.*$//")
158 port=$(echo $socks_proxy | sed -e "s/^.*://")
Victor Moralescf269992017-10-18 09:29:55 -0700159 PROXY=$socks PORT=$port ./$chameleonsocks_filename --install
160 rm $chameleonsocks_filename
161 cp $docker_conf_backup $docker_conf
Kiranf2824c22017-09-22 18:05:29 -0700162 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500163 fi
Victor Moralescf269992017-10-18 09:29:55 -0700164 rm $docker_conf_backup
Idan Amite17b5ec2017-08-16 14:17:59 +0300165
Victor Moralescf269992017-10-18 09:29:55 -0700166 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 -0500167}
168
Victor Moralesdd074802017-07-26 16:06:35 -0500169# install_nodejs() - Download and install NodeJS
170function install_nodejs {
171 if is_package_installed nodejs; then
172 return
173 fi
174 curl -sL https://deb.nodesource.com/setup_6.x | sudo -E bash -
175 install_package nodejs
176
177 # Update NPM to latest version
178 npm install npm -g
179}
180
181# install_python() - Install Python 2.7 and other tools necessary for development.
182function install_python {
Victor Morales7ae05a42017-08-22 14:39:00 -0500183 install_packages python2.7 python-dev
Victor Moralesdd074802017-07-26 16:06:35 -0500184}
185
Victor Morales970ec192017-07-31 09:10:11 -0500186# _install_pip() - Install Python Package Manager
187function _install_pip {
188 install_python
Victor Morales4ab71c12017-11-08 07:28:28 -0800189 if ! which pip; then
Victor Morales158c18c2017-08-06 11:23:15 -0500190 curl -sL https://bootstrap.pypa.io/get-pip.py | python
191 fi
Victor Morales970ec192017-07-31 09:10:11 -0500192}
193
Victor Morales88d193e2017-11-16 10:50:28 -0800194# install_python_package() - Install python modules
Victor Morales970ec192017-07-31 09:10:11 -0500195function install_python_package {
Victor Morales4ab71c12017-11-08 07:28:28 -0800196 local python_packages=$@
Victor Morales970ec192017-07-31 09:10:11 -0500197
198 _install_pip
Victor Morales4ab71c12017-11-08 07:28:28 -0800199 pip install $python_packages
Victor Morales970ec192017-07-31 09:10:11 -0500200}
201
Victor Morales88d193e2017-11-16 10:50:28 -0800202# install_python_requirements() - Install a list of python modules defined in requirement.txt file
203function install_python_requirements {
204 local python_project_path=$1
205
206 _install_pip
207 pushd $python_project_path
208 pip install -r requirements.txt
209 popd
210}
211
Victor Morales970ec192017-07-31 09:10:11 -0500212# install_docker() - Download and install docker-engine
Victor Moralesdd074802017-07-26 16:06:35 -0500213function install_docker {
214 if is_package_installed docker-ce; then
215 return
216 fi
Victor Morales9f1c94b2017-11-07 07:56:04 -0800217 source /etc/os-release || source /usr/lib/os-release
218 case ${ID,,} in
219 *suse)
220 ;;
221 ubuntu|debian)
Victor Morales071b81c2017-11-07 14:13:07 -0800222 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 -0800223 curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
224 add-apt-repository \
225 "deb [arch=amd64] https://download.docker.com/linux/ubuntu \
226 $(lsb_release -cs) stable"
227 ;;
228 rhel|centos|fedora)
229 ;;
230 esac
Victor Morales332d7d82017-11-06 11:51:37 -0800231 update_repos
Victor Morales7abf1a82017-11-06 09:20:27 -0800232
Victor Moralesdd074802017-07-26 16:06:35 -0500233 install_package docker-ce
Idan Amite17b5ec2017-08-16 14:17:59 +0300234 _configure_docker_settings
Victor Moralesdd074802017-07-26 16:06:35 -0500235 service docker restart
236 sleep 10
237}
238
239# pull_docker_image() - Pull Docker container image from the Public Docker Registry Hub
240function pull_docker_image {
241 install_docker
242 local image=$1
243 local tag=$2
244 docker pull ${image}
245 if [ ${tag} ]; then
246 docker tag ${image} $tag
247 fi
248}
249
250# install_docker_compose() - Download and install docker-engine
251function install_docker_compose {
252 local docker_compose_version=${1:-1.12.0}
253 if [ ! -d /opt/docker ]; then
254 mkdir /opt/docker
255 curl -L https://github.com/docker/compose/releases/download/$docker_compose_version/docker-compose-`uname -s`-`uname -m` > /opt/docker/docker-compose
256 chmod +x /opt/docker/docker-compose
257 fi
258}
259
260# _install_ODL() - Download and Install OpenDayLight SDN controller
261function _install_ODL {
262 if [ ! -d /opt/opendaylight/current ]; then
263 mkdir -p /opt/opendaylight/
264 wget "https://nexus.opendaylight.org/content/repositories/public/org/opendaylight/integration/distribution-karaf/"$odl_version"/distribution-karaf-"$odl_version".tar.gz" -P /opt/
265 tar xvf "/opt/distribution-karaf-"$odl_version".tar.gz" -C /opt/
266 mv "/opt/distribution-karaf-"$odl_version /opt/opendaylight/current
267 rm -rf "/opt/distribution-karaf-"$odl_version".tar.gz"
268 fi
269}
270
271# start_ODL() - Start OpenDayLight SDN controller
272function start_ODL {
273 _install_ODL
274 if [ -d /opt/opendaylight ]; then
275 export JAVA_HOME=/usr/lib/jvm/java-8-openjdk-amd64/jre
276 /opt/opendaylight/current/bin/start
277 sleep 180
278 /opt/opendaylight/current/bin/client feature:install odl-dlux-all
279 fi
280}
281
282# compile_src() - Function that compiles the java source code thru maven
283function compile_src {
284 local src_folder=$1
285 pushd $src_folder
Victor Morales7abf1a82017-11-06 09:20:27 -0800286 local mvn_build='mvn clean install -DskipTests=true -Dmaven.test.skip=true -Dmaven.javadoc.skip=true -Dadditionalparam=-Xdoclint:none'
287 if [[ "$debug" == "False" ]]; then
288 mvn_build+=" -q"
289 fi
Victor Moralesdd074802017-07-26 16:06:35 -0500290 if [ -f pom.xml ]; then
291 install_maven
Victor Morales7abf1a82017-11-06 09:20:27 -0800292 echo "Compiling $src_folder folder..."
293 eval $mvn_build
Victor Moralesdd074802017-07-26 16:06:35 -0500294 fi
295 popd
296}
297
Victor Moralesf1f1ba52017-11-20 16:38:28 -0800298# compile_repos() - Function that compiles source repositories for a given project
299function compile_repos {
300 local project=$1
301 local name=${project}_repos[@]
302 local repos_list=("${!name}")
303 local dest_folder=${3:-$git_src_folder/$project}
304
305 install_package unzip
306 for repo in ${repos_list[@]}; do
307 compile_src $dest_folder${repo#*$project}
308 done
309}
310
Victor Morales89ce3212017-06-16 18:32:48 -0500311# build_docker_image() - Build Docker container image from source code
312function build_docker_image {
313 local src_folder=$1
314 local profile=$2
315 install_maven
316 install_docker
317 pushd $src_folder
318
319 # Cleanup external repo
320 sed -i 's|${docker.push.registry}/||g' pom.xml
321 local mvn_docker="mvn clean package docker:build"
322 if [ $profile ]; then
323 mvn_docker+=" -P $profile"
324 fi
325 if [ $http_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500326 if ! grep -ql "docker.buildArg.http_proxy" pom.xml ; then
327 mvn_docker+=" -Ddocker.buildArg.http_proxy=$http_proxy"
328 fi
329 if ! grep -ql "docker.buildArg.HTTP_PROXY" pom.xml ; then
330 mvn_docker+=" -Ddocker.buildArg.HTTP_PROXY=$http_proxy"
331 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500332 fi
333 if [ $https_proxy ]; then
Victor Moralesdd074802017-07-26 16:06:35 -0500334 if ! grep -ql "docker.buildArg.https_proxy" pom.xml ; then
335 mvn_docker+=" -Ddocker.buildArg.https_proxy=$https_proxy"
336 fi
337 if ! grep -ql "docker.buildArg.HTTPS_PROXY" pom.xml ; then
338 mvn_docker+=" -Ddocker.buildArg.HTTPS_PROXY=$https_proxy"
339 fi
Victor Morales89ce3212017-06-16 18:32:48 -0500340 fi
341 eval $mvn_docker
342 popd
343}